随笔 - 119  文章 - 3173  trackbacks - 0
<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

交友莫独酒,茅台西凤游。
口干古井贡,心徜洋河流。
称多情杜康,趟无量双沟。
赞中华巍巍,无此不销愁。

常用链接

留言簿(68)

随笔分类(136)

随笔档案(122)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 520597
  • 排名 - 93

最新评论

应朋友要求,写了一个小工具,主要就是实现下面的要求:
FormatSqlResult.jpg

  1 import java.util.LinkedList;
  2 
  3 import org.eclipse.swt.SWT;
  4 import org.eclipse.swt.events.SelectionAdapter;
  5 import org.eclipse.swt.events.SelectionEvent;
  6 import org.eclipse.swt.layout.FormAttachment;
  7 import org.eclipse.swt.layout.FormData;
  8 import org.eclipse.swt.layout.FormLayout;
  9 import org.eclipse.swt.widgets.Button;
 10 import org.eclipse.swt.widgets.Display;
 11 import org.eclipse.swt.widgets.Label;
 12 import org.eclipse.swt.widgets.Shell;
 13 import org.eclipse.swt.widgets.Text;
 14 
 15 public class FormatSqlResult {
 16 
 17     private Text text;
 18 
 19     protected Shell shell;
 20 
 21     /**
 22      * Launch the application
 23      * 
 24      * @param args
 25      */
 26     public static void main(String[] args) {
 27         try {
 28             FormatSqlResult window = new FormatSqlResult();
 29             window.open();
 30         } catch (Exception e) {
 31             e.printStackTrace();
 32         }
 33     }
 34 
 35     /**
 36      * Open the window
 37      */
 38     public void open() {
 39         final Display display = Display.getDefault();
 40         createContents();
 41         shell.open();
 42         shell.layout();
 43         while (!shell.isDisposed()) {
 44             if (!display.readAndDispatch())
 45                 display.sleep();
 46         }
 47     }
 48 
 49     /**
 50      * Create contents of the window
 51      */
 52     protected void createContents() {
 53         shell = new Shell();
 54         shell.setLayout(new FormLayout());
 55         shell.setSize(631414);
 56         shell.setText("FormatSqlResult");
 57 
 58         text = new Text(shell, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
 59         final FormData fd_text = new FormData();
 60         fd_text.bottom = new FormAttachment(100-34);
 61         fd_text.right = new FormAttachment(100-5);
 62         fd_text.left = new FormAttachment(00);
 63         fd_text.top = new FormAttachment(00);
 64         text.setLayoutData(fd_text);
 65         final Button formatButton = new Button(shell, SWT.NONE);
 66         formatButton.addSelectionListener(new SelectionAdapter() {
 67             public void widgetSelected(SelectionEvent e) {
 68                 String str = text.getText();
 69                 if (str != null && str.length() > 0) {
 70                     text.setText(getSpaceText(str.replaceAll("\r""")));
 71                     text.selectAll();
 72                 }
 73 
 74             }
 75         });
 76         final FormData fd_formatButton = new FormData();
 77         fd_formatButton.left = new FormAttachment(0286);
 78         fd_formatButton.right = new FormAttachment(100-287);
 79         fd_formatButton.top = new FormAttachment(100-26);
 80         fd_formatButton.bottom = new FormAttachment(100-4);
 81         formatButton.setLayoutData(fd_formatButton);
 82         formatButton.setText("Format");
 83 
 84         final Label label = new Label(shell, SWT.NONE);
 85         final FormData fd_label = new FormData();
 86         fd_label.top = new FormAttachment(100-19);
 87         fd_label.left = new FormAttachment(100-130);
 88         fd_label.bottom = new FormAttachment(100-4);
 89         fd_label.right = new FormAttachment(100-5);
 90         label.setLayoutData(fd_label);
 91         label.setText("版权所有:交口称赞");
 92 
 93         final Label formatsqlresult10Label = new Label(shell, SWT.NONE);
 94         final FormData fd_formatsqlresult10Label = new FormData();
 95         fd_formatsqlresult10Label.top = new FormAttachment(100-19);
 96         fd_formatsqlresult10Label.right = new FormAttachment(0180);
 97         fd_formatsqlresult10Label.bottom = new FormAttachment(100-4);
 98         fd_formatsqlresult10Label.left = new FormAttachment(05);
 99         formatsqlresult10Label.setLayoutData(fd_formatsqlresult10Label);
100         formatsqlresult10Label.setText("FormatSqlResult version 1.5");
101     }
102 
103     public String getSpaceText(String allStr) {
104         String[] strs = allStr.split("\n");
105         String lineStr;
106         int row = -1;
107         if (strs != null && strs.length > 0) {
108             lineStr = strs[0];
109             String[] lineStrs = lineStr.split("\t");
110             row = lineStrs.length;
111         }
112         int[] max = new int[row];
113         for (int i = 0; i < max.length; i++) {
114             max[i] = -1;
115         }
116         LinkedList all = new LinkedList();
117         for (int i = 0; i < row; i++) {
118             LinkedList list = new LinkedList();
119             all.add(list);
120         }
121         for (int i = 0; i < strs.length; i++) {
122             lineStr = strs[i];
123             String[] lineStrs = lineStr.split("\t");
124             for (int j = 0; j < lineStrs.length; j++) {
125                 int length = lineStrs[j].length();
126                 if (length > max[j]) {
127                     max[j] = length;
128                 }
129                 ((LinkedList) all.get(j)).add(lineStrs[j]);
130             }
131         }
132 
133         StringBuffer sb = new StringBuffer();
134         int line = ((LinkedList) all.get(0)).size();
135         for (int i = 0; i < line; i++) {
136             for (int j = 0; j < all.size(); j++) {
137                 String str = (String) ((LinkedList) all.get(j)).get(i);
138                 int length = max[j] - str.length() + 1;
139                 sb.append(str);
140                 for (int k = 0; k < length; k++) {
141                     sb.append(" ");
142                 }
143             }
144             sb.append("\n");
145         }
146         return sb.toString();
147     }
148 
149 }
150 


已制作成exe,可以双击执行,只支持Windows,为了兼容jdk版本,没敢用泛型。

下载:FormatSqlResult1.rar

posted on 2007-08-09 11:02 交口称赞 阅读(1726) 评论(2)  编辑  收藏 所属分类: Eclipse RCP SWTjava相关

FeedBack:
# re: FormatSqlResult--SQL查询结果,格式化工具,已作成exe,付源码 2007-08-10 16:45 good
good  回复  更多评论
  
# re: FormatSqlResult--SQL查询结果,格式化工具,已作成exe,付源码 2007-10-29 21:58 随笔
兄弟永远支持你!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: