/ * * Create Date 2005-4-7 * * Todo To change this generated file template, go to * Window - Preference - Java - Code - Code Template * / package csviewer;
Import java.io.bufferedreader; import java.io. *; import java.io.ioException;
Import javax.swing.table.tableColumn;
Import org.eclipse.swt.swt; import org.eclipse.swt.events. *; import org.eclipse.swt.Layout. *; Import org.eclipse.swt.widgets. *;
/ ** * @Author administrator * * * / public class csviewer {/ ** * @Author yantong * * * /
PRIVATE shell shell;
Private display display;
Private text Textbox;
Private Table TABLE;
Private label statusbar;
Private menuitem mitemopen;
PRIVATE FINAL STRING [] extensions = {"* .csv", "*"};
PRIVATE STRING FILENAME;
Public shell Open (Display Display) {this.display = display;
Shell = new shell (Display); Shell.Settext ("csviewer"); shell.setLayout (New GridLayout (1, true));
Composite comp = new Composite (shell, SWT.NO_FOCUS); comp.setLayout (new GridLayout (1, true)); GridData compGrid = new GridData (); compGrid.horizontalAlignment = GridData.FILL; compGrid.verticalAlignment = GridData.FILL; Compgrid.grabexcesshorizontalspace = true; Compgrid.grabexcessVerticalSpace = true; comp.setlayoutdata (Compgrike);
textBox = new Text (comp, SWT.SINGLE | SWT.BORDER); textBox.setText ( ""); GridData textGrid = new GridData (); textGrid.horizontalAlignment = GridData.FILL; textGrid.grabExcessHorizontalSpace = true; textBox.setLayoutData ( TextGrid;
CreateTable (COMP);
statusBar = new Label (shell, SWT.LEFT); GridData gridData = new GridData (); gridData.horizontalAlignment = GridData.FILL; statusBar.setLayoutData (gridData); statusBar.setText ( "specified CSV file"); Menu menubar = new Menu (shell, swt.bar); shell.setmenubar; Menuitem mitemfile = new menuItem (MenuBar, SWT.CASCADE); mitemfile.settext ("File (& F)"); Menu Menufile = New Menu (MItemfile); mItemFile.setMenu (menuFile); mItemOpen = new MenuItem (menuFile, SWT.PUSH); mItemOpen.setText ( "open (& O)"); mItemOpen.addSelectionListener (new SelectionAdapter () {public void widgetSelected (SelectionEvent e) {getOpenFileName ( ); If (filename! = Null) {new thread (new csvloader ()). Start (); // loadbegin (); // loadFile (); // loadend ();}} }); // new MenuItem (menuFIle, SWT.SEPARATOR); MenuItem mItemExit = new MenuItem (menuFile, SWT.PUSH); mItemExit.setText ( "off (& X)"); mItemExit.addSelectionListener (new SelectionAdapter () {public Void WidgetSelected (SelectionEvent E) {close ();}};
Shell.open (); return shell;}
/ ** * @param parent * / private void createTable (Composite parent) {table = new Table (parent, SWT.FULL_SELECTION | SWT.BORDER); GridData tableGrid = new GridData (); tableGrid.horizontalAlignment = GridData.FILL; tableGrid .verticalAlignment = GridData.FILL; tableGrid.grabExcessHorizontalSpace = true; tableGrid.grabExcessVerticalSpace = true; table.setLayoutData (tableGrid); table.setLinesVisible (true); table.setHeaderVisible (true); TableColumn col = new TableColumn (SWT.LEFT) ; col.setWidth (100); table.addSelectionListener (new SelectionAdapter () {public void widgetSelected (SelectionEvent e) {setTableSelectedIndex ();}});} private void getOpenFileName () {FileDialog openDialog = new FileDialog (shell, SWT. Open); openDialog.setfilterextensions; filename = Opendialog.Open ();
Private void close () {shell.close (); shell.dispose ();}
Private void settableselectedIndex () {int index = table.getSelectionIndex (); status bar.settext ("INDEX 1) " Row ");}
/ / The following can be used to use other thread processing display procedures / * 1. Generate the Runnable class, you can make a separate file, in order to make it easy to create an internal class * / / * 2. modify the menu handler for convenience only in CSVIEWER, will call Loadbegin, LoadFile, Loadend's place to modify, * Replace with thread execution, New Thread (New CSVLoader ()). start (); / * 3. Put the loadbegin, loadfile, and loadend method in thread CSVLoader, * make it possible Other threads are executed, * / / * 4. Threading issues, org.eclipse.swt.swtexception: * Problem is to operate Swt Widget Processing Somewhere outside the UI thread * You can use Display Syncexec and AsyncEc Methods Transfer your processing to the UI thread * / / * 5. Transfer processing to the UI, add an Asyncexec method (CSVLoader append UTILITY method Checkasyncexec ()) * Modify the LoadBegin, the loadFile method, change it to the UI stream using the Checkasyncexec method * / Class CSVLoader Implements Runnable {Private Long Begintime; Public Void Run () {loadingbegin (); loadingFile (); loadingnd ();
private void loadBegin () {checkAsyncExec (new Runnable () {public void run () {mItemOpen.setEnabled (false); textBox.setText (fileName); Composite comp = table.getParent (); table.dispose (); createTable ( Comp.Layout (); status bar.settext ("Read ...");}}); begintime = system.currentTimeMillis (); // mitemopen.senabled (false); // textbox.setText Filename); // composite comp = table.getParent (); // table.dispose (); // CreateTable (); // comp.Layout (); // status bar.settext ("Read ... "); // begintime = system.currenttimemillis ();
Private void loading () {bufferedreader reader = null; try (file file file = new file (filename); if (! file.exists ()) {return;} reader = new bufferedReader (fileReader (file);
String line = null; while ((line = reader.readline ())! = Null) {final string [] DATAS = line.split (","); // In order to refer to the LOCAL variable of the loadFile method in the RUN method , Define it as Final Boolean isok = Checkasyncexec (New Runnable () {{r (Table.isdisposed ()) {Return;} for (INT i = Table.getColumnCount (); i } private void loadEnd () {checkAsyncExec (new Runnable () {public void run () {if (mItemOpen.isDisposed () || statusBar.isDisposed ()) {return;} mItemOpen.setEnabled (true); long end = System. CurrentTimeMillis (); status bar.Settext ("Reading: Processing Time" (End-Begintime) "Microsecond");}}); // mitemopen.setenabled (TRUE); // long end = system.currenttimemillis (); // status bar.settext ("Read End: Process Time" (End - Begintime) "Microsecond");} Private Boolean Checkasyncexec (Runnable R) {if (! Display.isposed ()) {Display .asyncexec (r); Return true;} else {returnaf false;}}} Public static void main (string [] args) {display display = new display (); csviewer viewer = new csviewer (); shell shell = viewer.open (! shell.isposed ()) {// pass ReadandDispatch () Method Displays the screen and time process // in SWT, the thread that generates the display item and performs the method within the loop is referred to as a UI thread. // If the UI thread call is taken too long, the application looks like stopping. / / The long-term operation of the user time trigger should be performed in a separate thread so that the event cycle thread returns, // and accesss an event from the application's queue. // (You must use explicit locking and serialization to control from other threads access small windows and platform API) if (! Display.readanddispatch ()) display.sleep (); // If there is no event available in the queue, display enters Sleep status so that other applications have the opportunity to run. } Display.dispose (); }