應朋友要求,寫了一個小工具,主要就是實現下面的要求:

??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(631,?414);
?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(0,?0);
?63?????????fd_text.top?=?new?FormAttachment(0,?0);
?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(0,?286);
?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(0,?180);
?97?????????fd_formatsqlresult10Label.bottom?=?new?FormAttachment(100,?-4);
?98?????????fd_formatsqlresult10Label.left?=?new?FormAttachment(0,?5);
?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
交口稱贊 閱讀(1737)
評論(2) 編輯 收藏 所屬分類:
Eclipse RCP SWT 、
java相關