SWT(JFace)小制作 BugTracker

代码如下:
实现1:
BugTracker.java


代码如下:

package swt_jface.demo6;
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.Arrays;
import java.util.Comparator;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
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.Text;
import org.eclipse.swt.widgets.ToolBar;
public class BugTracker {

Display display = new Display();
Shell shell = new Shell(display);
Table table;
Image bugIcon = new Image(shell.getDisplay(), "C:/icons/bug.gif");
public BugTracker() {
GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
shell.setText("Bug Tracking System");
Action actionAddNew = new Action("New bug") {
public void run() {
TableItem item = new TableItem(table, SWT.NULL);
item.setImage(bugIcon);
table.select(table.getItemCount() - 1);
}
};
Action actionDelete = new Action("Delete selected") {
public void run() {
int index = table.getSelectionIndex();
if (index < 0) {
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 #" + table.getItem(index).getText(0));
if (messageBox.open() == SWT.YES) {
table.remove(index);
}
}
};
Action actionSave = new Action("Save") {
public void run() {
saveBugs();
}
};
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.update(true);
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(GridData.FILL_BOTH));
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn tcID = new TableColumn(table, SWT.LEFT);
tcID.setText("ID");
TableColumn tcSummary = new TableColumn(table, SWT.NULL);
tcSummary.setText("Summary");
TableColumn tcAssignedTo = new TableColumn(table, SWT.NULL);
tcAssignedTo.setText("Assigned to");
TableColumn tcSolved = new TableColumn(table, SWT.NULL);
tcSolved.setText("Solved");
tcID.setWidth(60);
tcSummary.setWidth(200);
tcAssignedTo.setWidth(80);
tcSolved.setWidth(50);
table.pack();
final TableEditor editor = new TableEditor(table);
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Point point = new Point(event.x, event.y);
final TableItem item = table.getItem(point);
if (item == null)
return;
int column = -1;
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(point))
column = i;
}
if (column != 3)
return;
final Button button = new Button(table, SWT.CHECK);
button.setSelection(item.getText(column).equalsIgnoreCase("YES"));
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.setEditor(button, item, column);
final int selectedColumn = column;
Listener buttonListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut :
item.setText(
selectedColumn,
button.getSelection() ? "YES" : "NO");
button.dispose();
break;
case SWT.Traverse :
switch (e.detail) {
case SWT.TRAVERSE_RETURN :
item.setText(
selectedColumn,
button.getSelection()
? "YES"
: "NO");
case SWT.TRAVERSE_ESCAPE :
button.dispose();
e.doit = false;
}
break;
}
}
};
button.addListener(SWT.FocusOut, buttonListener);
button.addListener(SWT.Traverse, buttonListener);
button.setFocus();
}
});
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Point point = new Point(event.x, event.y);
final TableItem item = table.getItem(point);
if (item == null)
return;
int column = -1;
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(point))
column = i;
}
if (column < 0 || column >= 3)
return;
final Text text = new Text(table, SWT.NONE);
text.setText(item.getText(column));
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.setEditor(text, item, column);
final int selectedColumn = column;
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut :
item.setText(selectedColumn, text.getText());
text.dispose();
break;
case SWT.Traverse :
switch (e.detail) {
case SWT.TRAVERSE_RETURN :
item.setText(
selectedColumn,
text.getText());
//FALL THROUGH
case SWT.TRAVERSE_ESCAPE :
text.dispose();
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);
text.setFocus();
}
});
loadBugs();
table.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("Selected: " + table.getSelection()[0]);
}
});
Listener sortListener = new Listener() {
public void handleEvent(Event event) {
if(! (event.widget instanceof TableColumn))
return;
TableColumn tc = (TableColumn)event.widget;
sortTable(table, table.indexOf(tc));
System.out.println("The table is sorted by column #" + table.indexOf(tc));
}
};
for(int i=0; i<table.getColumnCount(); i++)
((TableColumn)table.getColumn(i)).addListener(SWT.Selection, sortListener);
shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void sortTable(Table table, int columnIndex) {
if(table == null || table.getColumnCount() <= 1)
return;
if(columnIndex < 0 || columnIndex >= table.getColumnCount())
throw new IllegalArgumentException("The specified column does not exist. ");
final int colIndex = columnIndex;
Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
return ((TableItem)o1).getText(colIndex).compareTo(((TableItem)o2).getText(colIndex));
}
public boolean equals(Object obj) {
return false;
}
};
TableItem[] tableItems = table.getItems();
Arrays.sort(tableItems, comparator);
for(int i=0; i<tableItems.length; i++) {
TableItem item = new TableItem(table, SWT.NULL);
for(int j=0; j<table.getColumnCount(); j++) {
item.setText(j, tableItems[i].getText(j));
item.setImage(j, tableItems[i].getImage(j));
}
tableItems[i].dispose();
}
}
private void saveBugs() {
DataOutputStream out = null;
try {
File file = new File("bugs.dat");
out = new DataOutputStream(new FileOutputStream(file));
for (int i = 0; i < table.getItemCount(); i++) {
TableItem item = table.getItem(i);
out.writeUTF(item.getText(0));
out.writeUTF(item.getText(1));
out.writeUTF(item.getText(2));
out.writeBoolean(item.getText(3).equalsIgnoreCase("YES"));
}
} catch (IOException ioe) {
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void loadBugs() {
DataInputStream in = null;
try {
File file = new File("bugs.dat");
if (!file.exists())
return;
in = new DataInputStream(new FileInputStream(file));
while (true) {
String id = in.readUTF();
String summary = in.readUTF();
String assignedTo = in.readUTF();
boolean solved = in.readBoolean();
TableItem item = new TableItem(table, SWT.NULL);
item.setImage(bugIcon);
item.setText(
new String[] {
id,
summary,
assignedTo,
solved ? "Yes" : "No" });
}
} catch (IOException ioe) {
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new BugTracker();
}
}

实现2:
BugTrackerJFace.java


代码如下:

package swt_jface.demo6;
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 {
public static class Bug {

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();
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) {
} 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(), "C:/icons/bug.gif");
String[] colNames = new String[]{"ID", "Summary", "Assigned to", "Solved"};

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);
}
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 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));

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);
}
});
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) {
}
});

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) {
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);
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);
}
});

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();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
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) {
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new BugTrackerJFace();
}
}

(0)

相关推荐

  • SWT(JFace)小制作 BugTracker

    代码如下: 实现1: BugTracker.java 复制代码 代码如下: package swt_jface.demo6; 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 j

  • SWT JFace 小制作 文本阅读器

    代码如下: 复制代码 代码如下: package swt_jface.demo11; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import org.eclipse.core.runtime.IProgressMonit

  • SWT(JFace)小制作 FileBrowser文件浏览

    代码如下: 复制代码 代码如下: package swt_jface.demo6; import java.io.File; import java.util.Date; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import o

  • SWT JFace Bookmark 制作

    代码如下: BookmarkOrganizer.java 复制代码 代码如下: package swt_jface.demo11; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import ja

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • SWT(JFace) 简易浏览器 制作实现代码第1/2页

    代码如下: BrowserExample.java 复制代码 代码如下: package swt_jface.demo5; import java.text.MessageFormat; import java.util.MissingResourceException; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; import org.eclipse.swt.browser.Browser; import org.e

  • SWT(JFace) FTP客户端实现

    Jar包一览: org.eclipse.jface_3.4.2.M20090107-0800.jar org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar org.eclipse.core.commands_3.4.0.I20080509-2000.jar org.eclipse.core.runtime_3.4.0.v20080512.jar org.eclipse.equinox.common_3.4.0.v20080421-2006.jar or

  • SWT(JFace) Wizard(Eclipse插件编程必备)

    演示代码如下: HotelReservation.java 复制代码 代码如下: package swt_jface.demo12; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.SWT; import org.eclips

  • SWT(JFace) 打印功能

    演示代码如下: 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.printing.PrintDialog; import org.eclipse.swt.printing.Printer; import org.eclipse.swt.printing.PrinterData; import org.eclipse.swt.widgets.Display

  • SWT(JFace) 文本编辑器 实现代码

    代码如下: 实现1: BasicEditor.java 复制代码 代码如下: package swt_jface.demo5; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.eclipse.jface.resource.ImageDescriptor; i

随机推荐