包含文件的保存和读取,工具条,按钮,表格数据的初始化,增加删除,表格数据的编辑,排序等. package com.laozizhu.search.client.test; 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; /** * SWT综合联系,BUG跟踪工具。<br> * 包含文件的保存和读取,工具条,按钮,<br> * 表格数据的初始化,增加删除,表格数据的编辑,排序等。 * * @author 老紫竹(laozizhu.com) */ public class BugTrackerJFace { /** * BUG记录 * * @author 老紫竹(laozizhu.com) */ 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; } } Display display = new Display(); Shell shell = new Shell(display); Table table; TableViewer tableViewer; Vector<Bug> bugs; Image bugIcon = new Image(shell.getDisplay(), "laozizhu.png"); String[] colNames = new String[] { "编号", "概述", "委派给", "已解决" }; /** * BUG存储 */ class BugSorter extends ViewerSorter { private int propertyIndex; public BugSorter(String sortByProperty) { for (int i = 0; i < colNames.length; i++) { if (colNames[i].equals(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() { // 各种动作 // 增加BUG的动作 Action actionAddNew = new Action("增加BUG") { public void run() { Bug bug = new Bug("", "", "", false); bugs.add(bug); // 刷新表格 tableViewer.refresh(false); } }; // 删除此项 Action actionDelete = new Action("删除选中") { public void run() { // 拿到选中的数据 IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection(); // 拿到选中的第一个数据 Bug bug = (Bug) selection.getFirstElement(); if (bug == null) { System.out.println("请先选择一个BUG."); return; } // 确认删除 MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO); messageBox.setText("确认"); messageBox.setMessage("你想删除这个编号为#" + bug.id + "的BUG吗 ?"); // 如果是 // SWT.YES 是 // SWT.NO 否 // SWT.CANCEL 取消 // SWT.RETRY 重试 // SWT.ABORT 放弃 // SWT.IGNORE 忽略 if (messageBox.open() == SWT.YES) { bugs.remove(bug); tableViewer.refresh(false); } } }; // 保存 Action actionSave = new Action("保存") { public void run() { saveBugs(bugs); } }; // 视图过滤 // 用来显示未解决的BUG 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("只显示为解决的") { 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(160); 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() { @SuppressWarnings("unchecked") public Object[] getElements(Object inputElement) { Vector<Bug> v = (Vector<Bug>) 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 ? "是" : "否"; } 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]; // 第一个是文本编辑框 // 2,3和第一个相同 // 第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 = loadBugs(new File("bugs.dat")); tableViewer.setInput(bugs); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } /** * 从文件读取保存的BUG数据。 * * @param file * 文件名 * @return */ private static Vector<Bug> loadBugs(File file) { Vector<Bug> v = new Vector<Bug>(); 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; } /** * 保存BUG数据 * * @param v */ private void saveBugs(Vector<Bug> v) { 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 = 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(); } }