SWT comprehensive exercises and bug tracking tools

Source: Internet
Author: User
Including file storage and reading, tool bar, buttons, table data initialization, addition and deletion, table data editing, sorting, etc.

 

 

Package COM. laozizhu. search. client. test; <br/> Import Java. io. datainputstream; <br/> Import Java. io. dataoutputstream; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. util. vector; <br/> Import Org. eclipse. jface. action. action; <br/> Import Org. eclipse. jface. action. separator; <br/> Import Org. eclipse. jfac E. action. toolbarmanager; <br/> Import Org. eclipse. jface. viewers. celleditor; <br/> Import Org. eclipse. jface. viewers. checkboxcelleditor; <br/> Import Org. eclipse. jface. viewers. icellmodifier; <br/> Import Org. eclipse. jface. viewers. ilabelproviderlistener; <br/> Import Org. eclipse. jface. viewers. istructuredcontentprovider; <br/> Import Org. eclipse. jface. viewers. istructuredselection; <br/> Import Org. eclipse. jf Ace. viewers. itablelabelprovider; <br/> Import Org. eclipse. jface. viewers. tableviewer; <br/> Import Org. eclipse. jface. viewers. textcelleditor; <br/> Import Org. eclipse. jface. viewers. viewer; <br/> Import Org. eclipse. jface. viewers. viewerfilter; <br/> Import Org. eclipse. jface. viewers. viewersorter; <br/> Import Org. eclipse. SWT. SWT; <br/> Import Org. eclipse. SWT. graphics. image; <br/> Import Org. eclipse. SWT. layout. Griddata; <br/> Import Org. eclipse. SWT. layout. gridlayout; <br/> Import Org. eclipse. SWT. widgets. display; <br/> Import Org. eclipse. SWT. widgets. event; <br/> Import Org. eclipse. SWT. widgets. item; <br/> Import Org. eclipse. SWT. widgets. listener; <br/> Import Org. eclipse. SWT. widgets. messageBox; <br/> Import Org. eclipse. SWT. widgets. shell; <br/> Import Org. eclipse. SWT. widgets. table; <br/> Import Org. eclipse. SWT. widge Ts. tablecolumn; <br/> Import Org. eclipse. SWT. widgets. tableitem; <br/> Import Org. eclipse. SWT. widgets. toolbar; <br/>/** <br/> * SWT comprehensive contact and bug tracking tool. <Br> <br/> * storage and reading of files, toolbar, buttons, <br> <br/> * initialization, deletion, and editing of table data, sort. <Br/> * @ author laozizhu.com) <br/> */<br/> public class bugtrackerjface {<br/>/** <br/> * bug record <br/> * @ author laozizhu.com) <br/> */<br/> Public static class bug {<br/> Public String ID; <br/> Public String summary; <br/> Public String assignedto; <br/> Public Boolean issolved; <br/> Public bug (string ID, string summary, string assignedto, Boolean issolved) {<br/> T His. id = ID; <br/> This. summary = Summary; <br/> This. assignedto = assignedto; <br/> This. issolved = issolved; <br/>}< br/> display = New Display (); <br/> shell = new shell (Display ); <br/> table; <br/> tableviewer; <br/> vector <bug> bugs; <br/> image bugicon = new image (shell. getdisplay (), "laozizhu.png"); <br/> string [] colnames = new string [] {"no.", "Overview", "Delegate to", "resolved "}; <br/>/** <br/> * bug storage <br/> */<br/> class bugsorter extends viewersorter {<br/> private int propertyindex; <br/> Public bugsorter (string sortbyproperty) {<br/> for (INT I = 0; I <colnames. length; I ++) {<br/> If (colnames [I]. equals (sortbyproperty) {<br/> This. propertyindex = I; <br/> return; <br/>}< br/> throw new illegalargumentexception ("Unrecognized property: "+ Sortbyproperty); <br/>}< br/>/* <br/> * (non-javadoc) <br/> * @ See <br/> * Org. eclipse. jface. viewers. viewersorter # Compare (Org. eclipse. jface. viewers <br/> *. viewer, Java. lang. object, Java. lang. object) <br/> */<br/> Public int compare (viewer, object E1, object E2) {<br/> bug bug1 = (Bug) E1; <br/> bug bug2 = (Bug) E2; <br/> switch (propertyindex) {<br/> case 0: <br/> return Bug1.id. compareto (bug2.id); <br/> case 1: <br/> return bug1.summary. compareto (bug2.summary); <br/> case 2: <br/> return bug1.assignedto. compareto (bug2.assignedto); <br/> case 3: <br/> If (bug1.issolved = bug2.issolved) <br/> return 0; <br/> If (bug1.issolved) <br/> return 1; <br/> else <br/> return-1; <br/> default: <br/> return 0; <br/>}< br/> Public bugtrackerjface () {<br/>/ /Various actions <br/> // Add bug actions <br/> action actionaddnew = new action ("add bug") {<br/> Public void run () {<br/> bug = new bug ("", false); <br/> bugs. add (Bug); <br/> // refresh the table <br/> tableviewer. refresh (false); <br/>}< br/>}; <br/> // delete this item <br/> action actiondelete = new action ("delete selected ") {<br/> Public void run () {<br/> // obtain the selected data <br/> istructuredselection selection = (istructuredselection) T Ableviewer. getselection (); <br/> // get the first selected data <br/> bug = (Bug) selection. getfirstelement (); <br/> If (bug = NULL) {<br/> system. out. println ("select a bug first. "); <br/> return; <br/>}< br/> // confirm deletion <br/> MessageBox = new MessageBox (shell, SWT. yes | SWT. no); <br/> MessageBox. settext ("OK"); <br/> MessageBox. setmessage ("you want to delete this number as #" + bug. ID + "bug? "); <Br/> // If yes <br/> // SWT. yes <br/> // SWT. no no <br/> // SWT. cancel cancel <br/> // SWT. retry again <br/> // SWT. abort abandon <br/> // SWT. ignore ignore <br/> If (MessageBox. open () = SWT. yes) {<br/> bugs. remove (Bug); <br/> tableviewer. refresh (false); <br/>}< br/> }; <br/> // save <br/> action actionsave = new action ("save") {<br/> Public void run () {<br/> savebugs (bugs); <br/>}< br/>}; <br/> // View Figure filtering <br/> // used to display unsolved bugs <br/> final viewerfilter filter = new viewerfilter () {<br/> Public Boolean select (viewer, object parentelement, object element) {<br/> If (! (Bug) element ). issolved) <br/> return true; <br/> return false; <br/>}< br/> }; <br/> action actionshowunsolvedonly = new action ("only displayed as resolved") {<br/> Public void run () {<br/> If (! Ischecked () {<br/> // Delete filter <br/> tableviewer. removefilter (filter); <br/>}else {<br/> // if selected, filter <br/> tableviewer. addfilter (filter); <br/>}< br/>}; <br/> // actionshowunsolvedonly is not selected by default. <br/>. setchecked (false); <br/> // toolbar <br/> toolbar = new toolbar (shell, SWT. right | SWT. flat); <br/> // toolbar manager <br/> toolbarmanager manager = new toolbarmanager (toolbar); <br/> // Add button <br /> Manager. add (actionaddnew); <br/> manager. add (actiondelete); <br/> manager. add (new separator (); <br/> manager. add (actionsave); <br/> manager. add (new separator (); <br/> manager. add (actionshowunsolvedonly); <br/> manager. update (true); <br/> // start layout <br/> shell. setlayout (New gridlayout (); <br/> // create a table <br/> table = new table (shell, SWT. border | SWT. full_selection); <br/> tablecolumn TCID = new Tablecolumn (table, SWT. left); <br/> TCID. settext (colnames [0]); <br/> tablecolumn tcsummary = new tablecolumn (table, SWT. null); <br/> tcsummary. settext (colnames [1]); <br/> tablecolumn tcassignedto = new tablecolumn (table, SWT. null); <br/> tcassignedto. settext (colnames [2]); <br/> tablecolumn tcsolved = new tablecolumn (table, SWT. null); <br/> tcsolved. settext (colnames [3]); <br/> TCID. setwidth (160); <B R/> tcsummary. setwidth (200); <br/> tcassignedto. setwidth (80); <br/> tcsolved. setwidth (50); <br/> // table view <br/> tableviewer = new tableviewer (table); <br/> tableviewer. gettable (). setlinesvisible (true); <br/> tableviewer. gettable (). setheadervisible (true); <br/> // you can specify the padding parameter. <br/> tableviewer. gettable (). setlayoutdata (New griddata (griddata. fill_both); <br/> // sets the content provider attempted by the table <br/> tableviewer. setcontentpr Ovider (New istructuredcontentprovider () {<br/> @ suppresswarnings ("unchecked") <br/> Public object [] getelements (Object inputelement) {<br/> vector <bug> V = (vector <bug>) inputelement; <br/> return v. toarray (); <br/>}< br/> Public void dispose () {<br/> system. out. println ("disposing... "); <br/>}< br/> Public void inputchanged (viewer, object oldinput, object newinput) {<br/> system. out. Println ("input changed: Old =" + oldinput + ", new =" + newinput); <br/>}< br/> }); <br/> // set the title provider <br/> tableviewer. setlabelprovider (New itablelabelprovider () {<br/> // graphic column <br/> public image getcolumnimage (object element, int columnindex) {<br/> If (columnindex = 0) <br/> return bugicon; <br/> return NULL; <br/>}< br/> // text column <br/> Public String getcolumntext (object element, int columnin Dex) {<br/> bug = (Bug) element; <br/> switch (columnindex) {<br/> case 0: <br/> return bug. ID; <br/> case 1: <br/> return bug. summary; <br/> case 2: <br/> return bug. assignedto; <br/> case 3: <br/> return bug. issolved? "Yes": "no"; <br/>}< br/> return NULL; <br/>}< br/> Public void addlistener (ilabelproviderlistener listener) {<br/>}< br/> Public void dispose () {<br/>}< br/> Public Boolean islabelproperty (object element, string Property) {<br/> return false; <br/>}< br/> Public void removelistener (ilabelproviderlistener listener) {<br/>}< br/> }); <br/> // set column attributes. <br/> tableviewer. setcolumnproperties (Co Lnames); <br/> // Unit Editor <br/> celleditor [] celleditors = new celleditor [4]; <br/> // The first one is the text editing box <br/> // check box <br/> and the first one is the same <br/> // check box <br/> celleditors [0] = new textcelleditor (table ); <br/> celleditors [1] = celleditors [0]; <br/> celleditors [2] = celleditors [0]; <br/> celleditors [3] = new checkboxcelleditor (table); <br/> tableviewer. setcelleditors (celleditors); <br/> // sets the unit modifier <br/> tableviewer. SETC Ellmodifier (New icellmodifier () {<br/> // whether to allow changes <br/> Public Boolean canmodify (object element, string property) {<br/> return true; <br/>}< br/> // obtain the value <br/> Public object getvalue (object element, string Property) {<br/> // obtain the index <br/> int Index =-1; <br/> for (INT I = 0; I <colnames. length; I ++) {<br/> If (colnames [I]. equals (property) {<br/> Index = I; <br/> break; <br/>}< br/> // Return the corresponding data <br/> bug = (Bug) element; <br/> switch (INDEX) {<br/> case 0: <br/> return bug. ID; <br/> case 1: <br/> return bug. summary; <br/> case 2: <br/> return bug. assignedto; <br/> case 3: <br/> return new Boolean (bug. issolved); <br/>}< br/> return NULL; <br/>}< br/> // modify <br/> Public void modify (object element, string property, object Value) {<br/> system. out. println ("Modify:" + E LEMENT + "," + property + "," + value); <br/> // obtain the index <br/> int Index =-1; <br/> for (INT I = 0; I <colnames. length; I ++) {<br/> If (colnames [I]. equals (property) {<br/> Index = I; <br/> break; <br/>}< br/> bug = NULL; <br/> If (element instanceof item) {<br/> tableitem item = (tableitem) element; <br/> bug = (Bug) item. getdata (); <br/>}else {<br/> bug = (Bug) element; <br/>} <Br/> switch (INDEX) {<br/> case 0: <br/> bug. id = (string) value; <br/> break; <br/> case 1: <br/> bug. summary = (string) value; <br/> break; <br/> case 2: <br/> bug. assignedto = (string) value; <br/> break; <br/> case 3: <br/> bug. issolved = (Boolean) value ). booleanvalue (); <br/> break; <br/>}< br/> tableviewer. update (Bug, null); <br/>}< br/>}); <br/> // sort <br/> TCID. addlistener (SWT. sel Ection, new listener () {<br/> Public void handleevent (event) {<br/> tableviewer. setsorter (New bugsorter (colnames [0]); <br/>}< br/>}); <br/> tcsummary. addlistener (SWT. selection, new listener () {<br/> Public void handleevent (event) {<br/> tableviewer. setsorter (New bugsorter (colnames [1]); <br/>}< br/>}); <br/> tcassignedto. addlistener (SWT. selection, new listener () {<br/> Public Void handleevent (event) {<br/> tableviewer. setsorter (New bugsorter (colnames [2]); <br/>}< br/>}); <br/> tcsolved. addlistener (SWT. selection, new listener () {<br/> Public void handleevent (event) {<br/> tableviewer. setsorter (New bugsorter (colnames [3]); <br/>}< br/> }); <br/> // load data from a file <br/> bugs = loadbugs (new file ("bugs. dat "); <br/> tableviewer. setinput (bugs); <br/> shell. pack (); <Br/> shell. open (); <br/> while (! Shell. isdisposed () {<br/> If (! Display. readanddispatch () {<br/> display. sleep (); <br/>}< br/> display. dispose (); <br/>}< br/>/** <br/> * read the stored bug data from the file. <Br/> * @ Param file <br/> * file name <br/> * @ return <br/> */<br/> Private Static vector <bug> loadbugs (File file) {<br/> vector <bug> V = new vector <bug> (); <br/> datainputstream in = NULL; <br/> try {<br/> If (! File. exists () {<br/> return V; <br/>}< br/> In = new datainputstream (New fileinputstream (File )); <br/> while (true) {<br/> string id = in. readutf (); <br/> string summary = in. readutf (); <br/> string assignedto = in. readutf (); <br/> Boolean solved = in. readboolean (); <br/> v. add (new bug (ID, summary, assignedto, solved); <br/>}< br/>} catch (ioexception IOE) {<br/>}finally {<br/> try {<br /> If (in! = NULL) <br/> in. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> return V; <br/>}< br/>/** <br/> * save Bug data <br/> * @ Param v <br/> */< br/> private void savebugs (vector <bug> V) {<br/> dataoutputstream out = NULL; <br/> try {<br/> file = new file ("bugs. dat "); <br/> out = new dataoutputstream (New fileoutputstream (File); <br/> for (INT I = 0; I <v. size (); I ++) {<br/> bug = v. elementat (I); <br/> out. writeutf (bug. ID); <br/> out. writeutf (bug. summary); <br/> out. writeutf (bug. assignedto); <br/> out. writeboolean (bug. issolved); <br/>}< br/>}catch (ioexception IOE) {<br/>}finally {<br/> try {<br/> If (OUT! = NULL) <br/> out. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> New bugtrackerjface (); <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.