SWT TABLE

xiaoxiao2021-03-30  215

/ ************************************************** ****************************** * ALL Right Reserved. Copyright (C) 1998, 2004 Jackwind Li Guojie * * CREATED ON MAR 5, 2004 7:38:59 PM by Jack $ ID $ * ******************************************** **************************************************** / Import Java.io.DataInputStream ; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Vector; import org.eclipse.jface. action.Action; import org.eclipse.jface.action.Separator; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.viewers.CellEditor; import org.eclipse.jface.viewers.CheckboxCellEditor; import org .eclipse.jface.viewers.ICellModifier; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers .Itablelabelprovider; import org.eclipse.jface.vi ewers.TableViewer; import org.eclipse.jface.viewers.TextCellEditor; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerFilter; import org.eclipse.jface.viewers.ViewerSorter; import org .eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display ; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Item; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.MessageBox; import org.eclipse. swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.ToolBar;

. Public class BugTrackerJFace {/ ** * Represents a bug report * * / public static class Bug {// For the sake of simplicity, all variables are public public String id;. Public String summary; public String assignedTo; public boolean isSolved; public Bug (String id, String summary, String assignedTo, boolean isSolved) {this.id = id; this.summary = summary; this.assignedTo = assignedTo; this.isSolved = isSolved;} public static Vector loadBugs (File file) { Vector v = new vector (); // load bugs from a file. DataInputStream IN = NULL; try.exists ()) Return V; IN = New DataInputStream (New FileInputStream (file); while (True ) {String id = in.readutf (); string summary = in.readutf (); string assignedto = in.readutf (); boolean solid = in.readboolean (); v.add (new bug (ID, summary, assignedto , Solved);}} catch (IOException IO e) {// ignore.} Finally {Try {if (in! = null) in .close ();} catch (ooException e) {E.PrintStackTrace ();}}}}}}}}}}}} Return V;}} (); Shell shell = new shell (display); TableTable; TableViewer TableViewer; Vector Bugs; Image Bugs; "Java2s.gif"); String [] colnames = new string [] { "ID", "Summary", "Assigned to", "Solved"};. // Sorter class BugSorter extends ViewerSorter {private String property; private int propertyIndex; public BugSorter (String sortByProperty) {for (int i = 0;

i

New MessageBox (shell, swt.yes | swt.no); MessageBox.Settext ("confirmation"); MessageBox.SetMessage ("Are you have remove the bug with ID #" bug.id); if (MessageBox.Open () == SWT.YES) {Bugs.remove (bug); TableViewer.refresh (false);}}}}; action actionsAve = new action ("save") {public void Run () {savebugs (bugs); }; final ViewerFilter filter = new ViewerFilter () {public boolean select (Viewer viewer, Object parentElement, Object element) {if (((Bug) element) .isSolved!) return true; return false;}}; Action actionShowUnsolvedOnly = new Action ( "Show unsolved only") {public void run () {if tableViewer.removeFilter (filter) (isChecked ()!); else tableViewer.addFilter (filter);}}; actionShowUnsolvedOnly.setChecked (false); ToolBar toolBar = New Toolbar (Shell, Swt.right | SWT. FLAT); ToolBarManager manager = new ToolBarManager (toolBar); manager.add (actionAddNew); manager.add (actionDelete); manager.add (new Separator ()); manager.add (actionSave); manager.add (new Separator ( )); manager.add (actionShowUnsolvedOnly); manager.update (true); shell.setLayout (new GridLayout ()); table = new Table (shell, SWT.BORDER | SWT.FULL_SELECTION); TableColumn tcID = new TableColumn (table , SWT.LEFT); tcID.setText (colNames [0]); TableColumn tcSummary = new TableColumn (table, SWT.NULL); tcSummary.setText (colNames [1]); TableColumn tcAssignedTo = new TableColumn (table, SWT.NULL ); Tcassignedto.settext (colnames [2]);

TableColumn tcSolved = new TableColumn (table, SWT.NULL); tcSolved.setText (colNames [3]); tcID.setWidth (60); tcSummary.setWidth (200); tcAssignedTo.setWidth (80); tcSolved.setWidth (50) ; tableViewer = new TableViewer (table); tableViewer.getTable () setLinesVisible (true);. tableViewer.getTable () setHeaderVisible (true);. tableViewer.getTable () setLayoutData (new GridData (GridData.FILL_BOTH));. // sets the content provider tableViewer.setContentProvider (new IStructuredContentProvider () {public Object [] getElements (Object inputElement) {Vector v = (Vector) inputElement;. return v.toArray ();} public void dispose () {System.out. Println ("Disposing ...");} public void infutchanged (Viewer Viewer, Object OldInput, Object newinput) {system.out.println ("Input Changed: Old = OldInput ", new = " newInput); }}); // sets the label provider. TableViewer.setLabelProvid er (new ITableLabelProvider () {public Image getColumnImage (Object element, int columnIndex) {if (columnIndex == 0) return bugIcon; return null;} public String getColumnText (Object element, int columnIndex) {Bug bug = (Bug) element Switch (columnIndex) {cas 0: return bug.id; case 1: return bug.summary; case 2: return bug.issignedto; case 3: return bug.issolved? "YES": "no";} return null;

} Public void addListener (ILabelProviderListener listener) {} public void dispose () {} public boolean isLabelProperty (Object element, String property) {return false;} public void removeListener (ILabelProviderListener listener) {}}); // Sets cell editors. tableViewer.setColumnProperties (colNames); CellEditor [] cellEditors = new CellEditor [4]; cellEditors [0] = new TextCellEditor (table); cellEditors [1] = cellEditors [0]; cellEditors [2] = cellEditors [0]; cellEditors [3] = new CheckboxCellEditor (table); tableViewer.setCellEditors (cellEditors); tableViewer.setCellModifier (new ICellModifier () {public boolean canModify (Object element, String property) {return true;} public Object getValue (Object element, String property ) {// get the index first. Int index = -1; for (int i = 0; i

) {IF (color) {index = i; break;}} bug bug = null; if (element instanceof item) {TableItem item = (TableItem) Element; bug = (bug) Item. Else {bug = (bug) ELSE {bug = (bug) ELSE} switch (index) {case 0: bug.id = (string) value; break; case 1: bug.summary = (string) value; break; casp 2 : Bug.assignedto = (string) Value; Break; Case 3: bug.issolved = (Boolean) value) .BooleanValue (); break;} TableViewer.Update (bug, null);}}); // setting sorters . tcID.addListener (SWT.Selection, new Listener () {public void handleEvent (Event event) {tableViewer.setSorter (new BugSorter (colNames [0]));}}); tcSummary.addListener (SWT.Selection, new Listener () {Public void handleevent (event evenet) { tableViewer.setSorter (new BugSorter (colNames [1]));}}); tcAssignedTo.addListener (SWT.Selection, new Listener () {public void handleEvent (Event event) {tableViewer.setSorter (new BugSorter (colNames [2] ));}}); Tcsolved.addlistener (SWT.SELECTION, New Listener () {public void handleevent (entneT) {TableViewer.setsRuter (colnames [3]));}}); bugs = bug. Loadbugs (New File ("Bugs.dat"); TableViewer.setInput (BUGS); shell.Pack (); shell.Open (); //textuser.forcefocus ();

转载请注明原文地址:https://www.9cbs.com/read-130490.html

New Post(0)