Bug Tracker JFace : Table « SWT JFace Eclipse « Java






Bug Tracker JFace

Bug Tracker JFace

/*******************************************************************************
 * 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.viewers.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 {
        if (!file.exists())
          return v;
        in = new DataInputStream(new FileInputStream(file));

        while (true) {
          String id = in.readUTF();
          String summary = in.readUTF();
          String assignedTo = in.readUTF();
          boolean solved = in.readBoolean();
          v.add(new Bug(id, summary, assignedTo, solved));
        }
        
      } catch (IOException ioe) {
        // Ignore.
      } finally {
        try {
          if (in != null)
            in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      
      return v;
    }
  }
  
  Display display = new Display();
  Shell shell = new Shell(display);
  
  Table table;
  TableViewer tableViewer;
  
  Vector bugs;
  
  Image bugIcon = new Image(shell.getDisplay(), "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<colNames.length; i++) {
        if(colNames[i].equals(sortByProperty)) {
          this.property = sortByProperty;
          this.propertyIndex = i;
          return;
        }
      }
      
      throw new IllegalArgumentException("Unrecognized property: " + sortByProperty);
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
     */
    public int compare(Viewer viewer, Object e1, Object e2) {
      Bug bug1 = (Bug)e1;
      Bug bug2 = (Bug)e2;
      
      switch(propertyIndex) {
        case 0:
          return bug1.id.compareTo(bug2.id);
        case 1:
          return bug1.summary.compareTo(bug2.summary);
        case 2:
          return bug1.assignedTo.compareTo(bug2.assignedTo);
        case 3:
          if(bug1.isSolved == bug2.isSolved)
            return 0;
          
          if(bug1.isSolved)
            return 1;
          else
            return -1;
        default:
          return 0;
      }
      
    }

  }  
  
  
  public BugTrackerJFace() {
    // Action.
    Action actionAddNew = new Action("New bug") {
      public void run() {
          // Append.
        Bug bug = new Bug("", "", "", false);
        bugs.add(bug);
        tableViewer.refresh(false);
      }
    };

    Action actionDelete = new Action("Delete selected") {
      public void run() {
        IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
        Bug bug = (Bug)selection.getFirstElement();
        if (bug == null) {
          System.out.println("Please select an item first. ");
          return;
        }
        MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO);
        messageBox.setText("Confirmation");
        messageBox.setMessage(
          "Are you sure to 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(! isChecked())
          tableViewer.removeFilter(filter);
        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 inputChanged(
        Viewer viewer,
        Object oldInput,
        Object newInput) {
          System.out.println("Input changed: old=" + 
            oldInput + ", new=" + newInput);
      }
    });
    
    // Sets the label provider. 
    tableViewer.setLabelProvider(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) {
          case 0:
            return bug.id;
          case 1:
            return bug.summary;
          case 2:
            return bug.assignedTo;
          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<colNames.length; i++) {
          if(colNames[i].equals(property)) {
            index = i;
            break;
          }
        }
        Bug bug = (Bug)element;
        
        switch(index) {
          case 0:
            return bug.id;
          case 1:
            return bug.summary;
          case 2:
            return bug.assignedTo;
          case 3:
            return new Boolean(bug.isSolved);
        }
        
        return null;
      }

      public void modify(Object element, String property, Object value) {
        System.out.println("Modify: " + element + ", " + property + ", " + value);
        // Get the index first. 
        int index = -1;
        for(int i=0; i<colNames.length; i++) {
          if(colNames[i].equals(property)) {
            index = i;
            break;
          }
        }
        
        Bug bug = null;
        if(element instanceof Item) {
          TableItem item = (TableItem)element;
          bug = (Bug)item.getData();
        }else{
          bug = (Bug)element;
        }
        
        switch(index) {
          case 0:
            bug.id = (String)value;
            break;
          case 1:
            bug.summary = (String)value;
            break;
          case 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 event) {
        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(Event event) {
        tableViewer.setSorter(new BugSorter(colNames[3]));
      }
    });
    
    bugs = Bug.loadBugs(new File("bugs.dat"));
    
    tableViewer.setInput(bugs);

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }

  
  
  private void saveBugs(Vector v) {
    // Save bugs to a file.
    DataOutputStream out = null;
    try {
      File file = new File("bugs.dat");

      out = new DataOutputStream(new FileOutputStream(file));

      for (int i = 0; i < v.size(); i++) {
        Bug bug = (Bug)v.elementAt(i);
        out.writeUTF(bug.id);
        out.writeUTF(bug.summary);
        out.writeUTF(bug.assignedTo);
        out.writeBoolean(bug.isSolved);
      }
    } catch (IOException ioe) {
      // Ignore.
    } finally {
      try {
        if (out != null)
          out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
    new BugTrackerJFace();
  }
}


           
       








Related examples in the same category

1.How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener FactoryHow to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory
2.Print KTable (SWT Table)Example Print KTable (SWT Table)Example
3.The source of a custom table class for Java SWT applicationsThe source of a custom table class for Java SWT applications
4.SWT Table Editor
5.Simple File Browser in SWT TableSimple File Browser in SWT Table
6.Bug TrackerBug Tracker
7.File Browser DemoFile Browser Demo
8.TableEditor exampleTableEditor example
9.File Browser SampleFile Browser Sample
10.File Browser JFace
11.Bug ReportBug Report
12.Displays ASCII CodesDisplays ASCII Codes
13.Demonstrates the SWT.VIRTUAL styleDemonstrates the SWT.VIRTUAL style
14.Displays a tableDisplays a table
15.A table of baseball players and allows sortingA table of baseball players and allows sorting
16.Demonstrates TableCursorDemonstrates TableCursor
17.Demonstrates TableTreeDemonstrates TableTree
18.Demonstrates TableEditorDemonstrates TableEditor
19.Shows the extensions on the system and their associated programsShows the extensions on the system and their associated programs
20.Table Example 3Table Example 3
21.Table Example 2Table Example 2
22.Table ExampleTable Example
23.Demonstrates TableViewersDemonstrates TableViewers
24.Demonstrates CheckboxTableViewerDemonstrates CheckboxTableViewer
25.Demonstrates CellEditorsDemonstrates CellEditors
26.SWT Table Simple DemoSWT Table Simple Demo
27.Create fake tooltips for items in a SWT tableCreate fake tooltips for items in a SWT table
28.Place a progress bar in a SWT tablePlace a progress bar in a SWT table
29.edit a cell in a SWT table (in place, fancy)edit a cell in a SWT table (in place, fancy)
30.edit the text of a SWT table item (in place)edit the text of a SWT table item (in place)
31.navigate a SWT table cells with arrow keysnavigate a SWT table cells with arrow keys
32.Update SWT table item textUpdate SWT table item text
33.Sort a SWT table by columnSort a SWT table by column
34.Select an index (select and scroll) in a SWT tableSelect an index (select and scroll) in a SWT table
35.Scroll a SWT table (set the top index)Scroll a SWT table (set the top index)
36.Resize columns as SWT table resizesResize columns as SWT table resizes
37.Remove selected items in a SWT tableRemove selected items in a SWT table
38.Print selected items in a SWT tablePrint selected items in a SWT table
39.Place arbitrary controls in a SWT tablePlace arbitrary controls in a SWT table
40.Reorder columns and reorder columns programmaticallyReorder columns and reorder columns programmatically
41.Insert a SWT table column (at an index)Insert a SWT table column (at an index)
42.Insert a SWT table item (at an index)Insert a SWT table item (at an index)
43.Find a SWT table cell from mouse down (works for any table style)Find a SWT table cell from mouse down (works for any table style)
44.Find a table cell from mouse down (SWT.FULL_SELECTION)Find a table cell from mouse down (SWT.FULL_SELECTION)
45.Detect a selection or check event in a table (SWT.CHECK)Detect a selection or check event in a table (SWT.CHECK)
46.Create a SWT table with 1,000,000 itemsCreate a SWT table with 1,000,000 items
47.Create a SWT table (columns, headers, lines)Create a SWT table (columns, headers, lines)
48.Create a SWT table (no columns, no headers)Create a SWT table (no columns, no headers)
49.Color cells and rows in SWT tableColor cells and rows in SWT table
50.Create a virtual SWT table and add 1000 entries to it every 500 msCreate a virtual SWT table and add 1000 entries to it every 500 ms
51.Dropped data type depends on target item in tableDropped data type depends on target item in table
52.Create a table (lazy)Create a table (lazy)