5.1.5. StructuredViewerStructuredViewer æ¯å表æ¥çå¨ï¼è¡¨æ ¼æ¥çå¨ï¼æ æ¥çå¨çæ½è±¡è¶ ç±»ã(åè§Figure 5-7) Figure 5-7. StructuredViewer å±æ¬¡å¾ä¸å为é常使ç¨çAPIï¼
|
5.1.6. ListViewerListViewerç±» å°è£ äºList æ§ä»¶ï¼å¯ä»¥ç¨æ¥æ¾ç¤ºå¯¹è±¡éåèä¸æ¯å符串éåãå表æ¥çå¨éè¦è®¾ç½®æ ç¾ä¾ç»å¨åå 容ä¾ç»å¨ã常ç¨APIå æ¬:ä¸é¢çå对象å°ç¨å¨ä»¥åçç¨åºä¾åä¸ã public class Person { public String firstName = "John"; public String lastName = "Doe"; public int age = 37; public Person[] children = new Person[0]; public Person parent = null; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public Person(String firstName, String lastName, int age, Person[] children) { this(firstName, lastName, age); this.children = children; for (int i = 0; i < children.length; i++) { children[i].parent = this; } } public static Person[] example() { return new Person[] { new Person("Dan", "Rubel", 41, new Person[] { new Person("Beth", "Rubel", 11), new Person("David", "Rubel", 6)}), new Person("Eric", "Clayberg", 42, new Person[] { new Person("Lauren", "Clayberg", 9), new Person("Lee", "Clayberg", 7)}), new Person("Mike", "Taylor", 55) }; } public String toString() { return firstName + " " + lastName; } } ä¸é¢çä¾åå建äºä¸ä¸ªå表æ¥çå¨ï¼å¹¶è®¾ç½®äºæ ç¾ä¾ç»å¨ï¼å 容ä¾ç»å¨åæåºå¨ã (åè§ Figure 5-8) 注æ: è¦ç¬ç«è¿è¡æ¤ç¨åº, ä½ å¿ é¡»æ·»å å¦ä¸å项å°ä½ çJava Build Path (æ件çæ¬å·è¦ç¬¦åå®è£ çEclipse)ã ECLIPSE_HOME/plugins/org.eclipse.core.runtime_3.1.2.jar ECLIPSE_HOME/plugins/org.eclipse.jface_3.1.1.jar ECLIPSE_HOME/plugins/org.eclipse.jface.text_3.1.2.jar ECLIPSE_HOME/plugins/org.eclipse.text_3.1.1.jar import org.eclipse.jface.viewers.*; import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class ListViewerExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("List Viewer Example"); shell.setBounds(100, 100, 200, 100); shell.setLayout(new FillLayout()); final ListViewer listViewer = new ListViewer(shell, SWT.SINGLE); listViewer.setLabelProvider( new PersonListLabelProvider()); listViewer.setContentProvider( new ArrayContentProvider()); listViewer.setInput(Person.example()); listViewer.setSorter(new ViewerSorter() { public int compare( Viewer viewer, Object p1, Object p2) { return ((Person) p1).lastName .compareToIgnoreCase(((Person) p2).lastName); } }); listViewer.addSelectionChangedListener( new ISelectionChangedListener() { public void selectionChanged( SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); System.out.println("Selected: " + selection.getFirstElement()); } }); listViewer.addDoubleClickListener( new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); System.out.println("Double Clicked: " + selection.getFirstElement()); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } Figure 5-8. ListViewer example.å表æ¥çå¨å建åï¼ä½¿ç¨ setLabelProvider() æ¹æ³è®¾ç½®æ ç¾ä¾ç»å¨ï¼ä½¿ç¨setContentProvider() 设置å 容ä¾ç»å¨ã PersonListLabelProviderä½ä¸ºå 容ä¾ç»å¨, è¿åç±ååç»æçæåæ ç¾ï¼æ²¡æè¿åå¾æ ã该类ç¨åºå¦ä¸: public class PersonListLabelProvider extends LabelProvider { public Image getImage(Object element) { return null; } public String getText(Object element) { Person person = (Person) element; return person.firstName + " " + person.lastName; } } 使ç¨å 建ç ArrayContentProvideræ为å 容ä¾ç»å¨ï¼å®å°è¾å ¥çéåæ å°ä¸ºæ°ç»ãä½¿ç¨ setInput() æ¹æ³è®¾ç½®è¾å ¥æ°æ®ãæåºå¨å®ä¹äºcompare()æ¹æ³ï¼å®åºäºååè¿è¡æåºãæåï¼ä¸ºå表æ¥çå¨æ·»å äºselectionChanged çå¬å¨ådoubleClickçå¬å¨ã |
5.1.7. TableViewerTableViewer ç±»å°è£ äºTableæ§ä»¶ãè¡¨æ ¼æ¥çå¨æä¾äºå¯ç¼è¾çï¼åç´çï¼å¤åçæ¡ç®å表ãå¨å表ä¸ï¼æ¯ä¸ä¸ªæ¡ç®æ¾ç¤ºä¸ºä¸è¡åå æ ¼ãæ¯ä¸ä¸ªåå æ ¼æ¾ç¤ºäºæ¡ç®çç¸åºå±æ§ãè¡¨æ ¼æ¥çå¨éè¦é ç½®æ ç¾ä¾ç»å¨ï¼å 容ä¾ç»å¨ååä¿¡æ¯ã CheckboxTableViewer å å ¥äºå¯¹ä¸ªå«æ¡ç®æ³ç°çæ¯æï¼è¿å¯ä»¥éä¸ååéä¸æ¡ç®ç¸å ³èçå¤éæ¡ã常ç¨çAPIå æ¬:The CheckboxTableViewer 常ç¨APIå æ¬: ä¸é¢çç¨åºæ ·ä¾å建ä¸ä¸ªæ¥æååçè¡¨æ ¼æ¥çå¨ã (åè§Figure 5-9)ã Figure 5-9. TableViewer æ ·ä¾import org.eclipse.jface.viewers.*; import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class TableViewerExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Table Viewer Example"); shell.setBounds(100, 100, 325, 200); shell.setLayout(new FillLayout()); final TableViewer tableViewer = new TableViewer( shell, SWT.SINGLE | SWT.FULL_SELECTION); final Table table = tableViewer.getTable(); table.setHeaderVisible(true); table.setLinesVisible(true); String[] columnNames = new String[] { "First Name", "Last Name", "Age", "Num Children"}; int[] columnWidths = new int[] { 100, 100, 35, 75}; int[] columnAlignments = new int[] { SWT.LEFT, SWT.LEFT, SWT.CENTER, SWT.CENTER}; for (int i = 0; i < columnNames.length; i++) { TableColumn tableColumn = new TableColumn(table, columnAlignments[i]); tableColumn.setText(columnNames[i]); tableColumn.setWidth(columnWidths[i]); } tableViewer.setLabelProvider( new PersonTableLabelProvider()); tableViewer.setContentProvider( new ArrayContentProvider()); tableViewer.setInput(Person.example()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } åå»ºè¡¨æ ¼æ¥çå¨åï¼è°ç¨setHeaderVisible() å setLinesVisible() æ¹æ³ä½¿å表头å¯è§ï¼è¡¨æ ¼çº¿å¯è§ãç¶åå å ¥ååçä¿¡æ¯ãéè¿setText() å setWidth() æ¹æ³è®¾ç½®å表头çæåå宽度ã(åè§ 7.8 è, èªå¨è°æ´è¡¨æ ¼å)ã ä½¿ç¨ setLabelProvider()æ¹æ³è®¾ç½®æ ç¾ä¾ç»å¨ï¼ 使ç¨setContentProvider() æ¹æ³è®¾ç½®å 容ä¾ç»å¨ãæ ç¾ä¾ç»å¨PersonTableLabelProvider, ç¨æ¥è¿åè¡¨æ ¼æ¯ä¸åçæåæ ç¾ï¼æ²¡æè¿åå¾æ ã ç¨åºå¦ä¸: import org.eclipse.jface.viewers.*; import org.eclipse.swt.graphics.*; public class PersonTableLabelProvider extends LabelProvider implements ITableLabelProvider { public Image getColumnImage( Object element, int) { return null; } public String getColumnText(Object element, int index) { Person person = (Person) element; switch (index) { case 0 : return person.firstName; case 1 : return person.lastName; case 2 : return Integer.toString(person.age); case 3 : return Integer.toString(person.children.length); default : return "unknown " + index; } } } |