
  1 import java.util.LinkedList;
  2Â
  3 import org.eclipse.swt.SWT;
  4 import org.eclipse.swt.events.KeyAdapter;
  5 import org.eclipse.swt.events.KeyEvent;
  6 import org.eclipse.swt.events.SelectionAdapter;
  7 import org.eclipse.swt.events.SelectionEvent;
  8 import org.eclipse.swt.layout.FormAttachment;
  9 import org.eclipse.swt.layout.FormData;
 10 import org.eclipse.swt.layout.FormLayout;
 11 import org.eclipse.swt.widgets.Button;
 12 import org.eclipse.swt.widgets.Display;
 13 import org.eclipse.swt.widgets.Label;
 14 import org.eclipse.swt.widgets.Shell;
 15 import org.eclipse.swt.widgets.Text;
 16Â
 17 public class FormatSqlResult {
 18Â
 19     private Text text;
 20Â
 21     protected Shell shell;
 22Â
 23     /**
 24      * Launch the application
 25      *Â
 26      * @param args
 27      */
 28     public static void main(String[] args) {
 29         try {
 30             FormatSqlResult window = new FormatSqlResult();
 31             window.open();
 32         } catch (Exception e) {
 33             e.printStackTrace();
 34         }
 35     }
 36Â
 37     /**
 38      * Open the window
 39      */
 40     public void open() {
 41         final Display display = Display.getDefault();
 42         createContents();
 43         shell.open();
 44         shell.layout();
 45         while (!shell.isDisposed()) {
 46             if (!display.readAndDispatch())
 47                 display.sleep();
 48         }
 49     }
 50Â
 51     /**
 52      * Create contents of the window
 53      */
 54     protected void createContents() {
 55         shell = new Shell();
 56         shell.setLayout(new FormLayout());
 57         shell.setSize(631, 414);
 58         shell.setText("FormatSqlResult");
 59Â
 60         text = new Text(shell, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
 61         text.addKeyListener(new KeyAdapter() {
 62             public void keyPressed(KeyEvent e) {
 63                 System.out.println(e.keyCode);
 64             }
 65         });
 66         final FormData fd_text = new FormData();
 67         fd_text.bottom = new FormAttachment(100, -34);
 68         fd_text.right = new FormAttachment(100, -5);
 69         fd_text.left = new FormAttachment(0, 0);
 70         fd_text.top = new FormAttachment(0, 0);
 71         text.setLayoutData(fd_text);
 72         final Button formatButton = new Button(shell, SWT.NONE);
 73         formatButton.addSelectionListener(new SelectionAdapter() {
 74             public void widgetSelected(SelectionEvent e) {
 75                 String str = text.getText();
 76                 if (str != null && str.length() > 0) {
 77                     text.setText(getSpaceText(str.replaceAll("\r", "")));
 78                     text.selectAll();
 79                 }
 80Â
 81             }
 82         });
 83         final FormData fd_formatButton = new FormData();
 84         fd_formatButton.left = new FormAttachment(0, 286);
 85         fd_formatButton.right = new FormAttachment(100, -287);
 86         fd_formatButton.top = new FormAttachment(100, -26);
 87         fd_formatButton.bottom = new FormAttachment(100, -4);
 88         formatButton.setLayoutData(fd_formatButton);
 89         formatButton.setText("Format");
 90Â
 91         final Label label = new Label(shell, SWT.NONE);
 92         final FormData fd_label = new FormData();
 93         fd_label.top = new FormAttachment(100, -19);
 94         fd_label.left = new FormAttachment(100, -130);
 95         fd_label.bottom = new FormAttachment(100, -4);
 96         fd_label.right = new FormAttachment(100, -5);
 97         label.setLayoutData(fd_label);
 98         label.setText("ç‰ˆæƒæ‰€æœ‰ï¼š(x¨¬)交壿U°èµž");
 99Â
100         final Label formatsqlresult10Label = new Label(shell, SWT.NONE);
101         final FormData fd_formatsqlresult10Label = new FormData();
102         fd_formatsqlresult10Label.top = new FormAttachment(100, -19);
103         fd_formatsqlresult10Label.right = new FormAttachment(0, 180);
104         fd_formatsqlresult10Label.bottom = new FormAttachment(100, -4);
105         fd_formatsqlresult10Label.left = new FormAttachment(0, 5);
106Â Â Â Â Â Â Â Â Â formatsqlresult10Label.setLayoutData(fd_formatsqlresult10Label);
107         formatsqlresult10Label.setText("FormatSqlResult version 2.0");
108Â
109         final Button button = new Button(shell, SWT.NONE);
110         button.addSelectionListener(new SelectionAdapter() {
111             public void widgetSelected(SelectionEvent e) {
112Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â text.selectAll();
113Â Â Â Â Â Â Â Â Â Â Â Â Â }
114Â Â Â Â Â Â Â Â Â });
115         final FormData fd_button = new FormData();
116         fd_button.top = new FormAttachment(formatButton, -22, SWT.BOTTOM);
117         fd_button.bottom = new FormAttachment(formatButton, 0, SWT.BOTTOM);
118         fd_button.left = new FormAttachment(0, 239);
119         fd_button.right = new FormAttachment(0, 275);
120Â Â Â Â Â Â Â Â Â button.setLayoutData(fd_button);
121         button.setText("全�/span>");
122Â
123         final Button button_1 = new Button(shell, SWT.NONE);
124         button_1.addSelectionListener(new SelectionAdapter() {
125             public void widgetSelected(SelectionEvent e) {
126Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â text.setText("");
127Â Â Â Â Â Â Â Â Â Â Â Â Â }
128Â Â Â Â Â Â Â Â Â });
129         final FormData fd_button_1 = new FormData();
130         fd_button_1.left = new FormAttachment(formatButton, 9, SWT.DEFAULT);
131         fd_button_1.right = new FormAttachment(100, -242);
132         fd_button_1.bottom = new FormAttachment(100, -4);
133         fd_button_1.top = new FormAttachment(100, -26);
134Â Â Â Â Â Â Â Â Â button_1.setLayoutData(fd_button_1);
135         button_1.setText("清空");
136Â Â Â Â Â }
137Â
138     public String getSpaceText(String allStr) {
139         String[] strs = allStr.split("\n");
140         String lineStr;
141         int row = -1;
142         if (strs != null && strs.length > 0) {
143             lineStr = strs[0];
144             String[] lineStrs = lineStr.split("\t");
145             row = lineStrs.length;
146Â Â Â Â Â Â Â Â Â }
147         int[] max = new int[row];
148         for (int i = 0; i < max.length; i++) {
149Â Â Â Â Â Â Â Â Â Â Â Â Â max[i]Â =Â -1;
150Â Â Â Â Â Â Â Â Â }
151         LinkedList all = new LinkedList();
152         for (int i = 0; i < row; i++) {
153             LinkedList list = new LinkedList();
154Â Â Â Â Â Â Â Â Â Â Â Â Â all.add(list);
155Â Â Â Â Â Â Â Â Â }
156         for (int i = 0; i < strs.length; i++) {
157             lineStr = strs[i];
158             String[] lineStrs = lineStr.split("\t");
159             int length = -1;
160             for (int j = 0; j < row; j++) {
161                 if (j < lineStrs.length) {
162                     length = lineStrs[j].getBytes().length;
163                     if (length > max[j]) {
164Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â max[j]Â =Â length;
165Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
166Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((LinkedList)Â all.get(j)).add(lineStrs[j]);
167                 } else {
168Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((LinkedList)Â all.get(j)).add("");
169Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
170Â
171Â Â Â Â Â Â Â Â Â Â Â Â Â }
172Â Â Â Â Â Â Â Â Â }
173Â
174         StringBuffer sb = new StringBuffer();
175         int line = ((LinkedList) all.get(0)).size();
176         for (int i = 0; i < line; i++) {
177             for (int j = 0; j < all.size(); j++) {
178                 String str = (String) ((LinkedList) all.get(j)).get(i);
179                 int length = max[j] - str.getBytes().length + 1;
180Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sb.append(str);
181                 for (int k = 0; k < length; k++) {
182Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sb.append("Â ");
183Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
184Â Â Â Â Â Â Â Â Â Â Â Â Â }
185Â Â Â Â Â Â Â Â Â Â Â Â Â sb.append("\n");
186Â Â Â Â Â Â Â Â Â }
187         return sb.toString();
188Â Â Â Â Â }
189Â
190Â }
191Â
已制作æˆexeåQŒå¯ä»¥åŒå‡?y¨¢n)L‰§è¡Œï¼Œåªæ”¯æŒWindowsåQŒäØ“(f¨´)äº?ji¨£n)兼容jdk版本åQŒæ²¡æ•¢ç”¨æ³›åž‹ã€?
下è²åQ?a href="/Files/vip01/FormatSqlResult2.rar">FormatSqlResult2.rar

]]>