1 / 34

Eclipse Plug-in Development

Eclipse Plug-in Development. SWT/JFace Development Part 4 Viewers. Contents. Introducing the JFace Viewers Viewers in JFace ComboViewer ListViewer TableViewer Filter Cell Editor TreeViewer Filter Cell Editor. Viewers. Content provider IStructuredContentProvider

flaviar
Download Presentation

Eclipse Plug-in Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Eclipse Plug-in Development SWT/JFace Development Part 4 Viewers Soyatec (http://www.soyatec.com)

  2. Contents • Introducing the JFace Viewers • Viewers in JFace • ComboViewer • ListViewer • TableViewer • Filter • Cell Editor • TreeViewer • Filter • Cell Editor Soyatec (http://www.soyatec.com)

  3. Viewers • Content provider • IStructuredContentProvider • List elements of domain • Controls • List • Combo • Table • ArrayContentProvider • Array • Collection • ITreeContentProvider • List children • Determine parent • Tree Soyatec (http://www.soyatec.com)

  4. Viewers • Label provider • ILabelProvider • Used for Lists and Trees • Defines icon and label for each element • CellLabelProvider • Used for Tables • Defines a label provider per column • ITableLabelProvider • Used for Tables • Provide the text and/or image for each column of a given element Soyatec (http://www.soyatec.com)

  5. Viewers • Input • Domain model • Null to clear contents Soyatec (http://www.soyatec.com)

  6. Viewer • Creating a Viewer • Initialize • Set content provider • Set label provider • Set input • Manage Selections • ISelectionChangeListener • IStructuredSelection • Set selection and reveal Soyatec (http://www.soyatec.com)

  7. Viewers • Custom a Viewer • create SWT controls for viewer (in constructor) (optional) • initialize SWT controls from input (inputChanged) • define viewer-specific update methods • support selections (setSelection, getSelection) Soyatec (http://www.soyatec.com)

  8. ComboViewer • Initialize • Default • With Combo or CCombo control • ComboViewerviewer1 = newComboViewer(parent, SWT.READ_ONLY); • CComboccombo = newCCombo(parent, SWT.READ_ONLY); • ComboViewer viewer2 = newComboViewer(ccombo); • Combo combo = new Combo(parent, SWT.READ_ONLY); • ComboViewer viewer3 = newComboViewer(combo); Soyatec (http://www.soyatec.com)

  9. ListViewer • Initialize • Default • With List control • ListViewer viewer1 = newListViewer(parent); • ListViewer viewer2 = newListViewer(parent, SWT.MULTI | SWT.BORDER); • List list = new List(parent, SWT.MULTI | SWT.BORDER); • ListViewer viewer3 = newListViewer(list); Soyatec (http://www.soyatec.com)

  10. TableViewer • Initialize • Default • With Table control • TableViewer viewer1 = newTableViewer(parent); • TableViewer viewer2 = newTableViewer(parent, SWT.FULL_SELECTION|SWT.BORDER); • Table table = new Table(parent, SWT.FULL_SELECTION|SWT.BORDER); • TableViewer viewer3 = newTableViewer(table); Soyatec (http://www.soyatec.com)

  11. Table Viewer • Domain model • Try to list all books for a library • Library library = new Library("Center Public Library"); • library.addBook(new Book("Children Education, 3-6 years old", • new Author("Ben Jermain", 34))); • library.addBook(new Book("Children Education, 6-12 years old", • new Author("Ben Jermain", 34))); • library.addBook(new Book("Baby Names Book", new Author("QuanJia", 56))); • library.addBook(new Book("Java Development", new Author("Van Jondon", • 43))); Soyatec (http://www.soyatec.com)

  12. Table Viewer • Content Provider • privatestaticclass MyContentProvicer1 implements • IStructuredContentProvider { • publicvoid dispose() { • } • publicvoidinputChanged(Viewer viewer, Object oldInput, Object newInput) { • } • publicObject[] getElements(Object inputElement) { • if(inputElementinstanceof Library) { • return((Library) inputElement).getBooks().toArray(); • } • returnnew Object[0]; • } • } Soyatec (http://www.soyatec.com)

  13. Table Viewer • Label provider • privatestaticclass MyLabelProvider1 extendsLabelProvider { • publicString getText(Object element) { • if(elementinstanceof Model) { • return((Model) element).getName(); • } • returnsuper.getText(element); • } • } Soyatec (http://www.soyatec.com)

  14. Table Viewer • Set Input • TableViewerviewer = newTableViewer(shell); • viewer.setContentProvider(new MyContentProvicer1()); • viewer.setLabelProvider(new MyLabelProvider1()); • viewer.setInput(library); Soyatec (http://www.soyatec.com)

  15. Table Viewer • Add columns • viewer.getTable().setHeaderVisible(true); • TableViewerColumnnameColumn = newTableViewerColumn(viewer, SWT.NONE); • nameColumn.getColumn().setText("Name"); • nameColumn.getColumn().setWidth(200); • TableViewerColumnauthorColumn = newTableViewerColumn(viewer, SWT.NONE); • authorColumn.getColumn().setText("Author"); • authorColumn.getColumn().setWidth(100); Soyatec (http://www.soyatec.com)

  16. Table Viewer • Update label provider for Author column • privatestaticclass MyLabelProvider2 extendsLabelProviderimplements • ITableLabelProvider { • publicImage getColumnImage(Object element, intcolumnIndex) { • returnnull; • } • publicString getColumnText(Object element, intcolumnIndex) { • if(elementinstanceof Book) { • if(columnIndex == 0) { • return((Book) element).getName(); • } elseif (columnIndex == 1) { • Author author = ((Book) element).getAuthor(); • if(author != null) { • returnauthor.getName(); • } • } • } • returnnull; • } • } • } Soyatec (http://www.soyatec.com)

  17. Table Viewer • CellLableProvider • ColumnLabelProvider • nameColumn.setLabelProvider(newColumnLabelProvider() { • publicString getText(Object element) { • if(elementinstanceof Book) { • return((Book) element).getName(); • } • returnsuper.getText(element); • } • publicColor getForeground(Object element) { • returndisplay.getSystemColor(SWT.COLOR_BLUE); • } • }); Soyatec (http://www.soyatec.com)

  18. Table Viewer • CellLabelProvider • ColumnLabelProvider • authorColumn.setLabelProvider(newColumnLabelProvider() { • publicString getText(Object element) { • if(elementinstanceof Book) { • Author author = ((Book) element).getAuthor(); • if(author != null) { • returnauthor.getName(); • } • } • returnsuper.getText(element); • } • publicColor getBackground(Object element) { • returndisplay.getSystemColor(SWT.COLOR_CYAN); • } • }); Soyatec (http://www.soyatec.com)

  19. Table Viewer • CellLabelProvider • OwnerDrawLabelProvider • viewer.setLabelProvider(newOwnerDrawLabelProvider() { • protectedvoid measure(Event event, Object element) { • LineEntryline = (LineEntry) element; • Point size = event.gc.textExtent(line.line); • event.width= viewer.getTable().getColumn(event.index).getWidth(); • intlines = size.x / event.width + 1; • event.height= size.y * lines; • } • protectedvoid paint(Event event, Object element) { • LineEntryentry = (LineEntry) element; • event.gc.drawText(entry.line, event.x, event.y, true); • } • }); Soyatec (http://www.soyatec.com)

  20. Table Viewer • Cell Editor • viewer.setColumnProperties(COLUMN_PROPERYIES); • viewer.setCellEditors(newCellEditor[] { • newTextCellEditor(viewer.getTable()), null}); • viewer.setCellModifier(newICellModifier() { • … • }); publicvoid modify(Object element, String property, Object value) { if(COLUMN_PROPERYIES[0].equals(property)) { if(elementinstanceofTableItem) { element= ((TableItem) element).getData(); } if(elementinstanceof Book) { ((Book) element).setName(value.toString()); } } viewer.update(element, new String[] { property }); } public Object getValue(Object element, String property) { if(COLUMN_PROPERYIES[0].equals(property)) { if(elementinstanceof Book) { return((Book) element).getName(); } } returnnull; } publicbooleancanModify(Object element, String property) { returnCOLUMN_PROPERYIES[0].equals(property); } Soyatec (http://www.soyatec.com)

  21. Table Viewer • Cell Editor with EditingSupport • SWT.FULL_SELECTION • authorColumn.setEditingSupport(newEditingSupport(viewer) { • … • }); protectedbooleancanEdit(Object element) { returntrue; } protectedCellEditorgetCellEditor(Object element) { returnnewDialogCellEditor(viewer.getTable()) { protectedObject openDialogBox(Control cellEditorWindow) { InputDialogdialog = newInputDialog(cellEditorWindow .getShell(), "Author Name", "Change the name of Author", (String) getValue(), null); dialog.open(); returndialog.getValue(); } }; } protectedvoidsetValue(Object element, Object value) { if(elementinstanceof Book) { Author author = ((Book) element).getAuthor(); if(author != null) { author.setName(value.toString()); } } viewer.update(element, null); } protected Object getValue(Object element) { if(elementinstanceof Book) { Author author = ((Book) element).getAuthor(); returnauthor.getName(); } returnnull; } Soyatec (http://www.soyatec.com)

  22. Table Viewer • CellEditors • TextCellEditor • ColorCellEditor • ComboBoxCellEditor • CheckboxCellEditor • DialogCellEditor Soyatec (http://www.soyatec.com)

  23. Table Viewer • ViewerFilter • viewer.addFilter(newViewerFilter() { • publicboolean select(Viewer viewer, Object parentElement, • Object element) { • if(elementinstanceof Book) { • Author author = ((Book) element).getAuthor(); • if(author != null && author.getAge() < 40) { • returnfalse; • } • } • returntrue; • } • }); Soyatec (http://www.soyatec.com)

  24. Table Viewer • ViewerComparator • viewer.getTable().setSortColumn(nameColumn.getColumn()); • nameColumn.getColumn().addSelectionListener(newSelectionAdapter() { • publicvoidwidgetSelected(SelectionEvente) { • viewer.setComparator(newViewerComparator() { • publicint compare(Viewer viewer, Object e1, Object e2) { • String n1 = ((Book) e1).getName(); • String n2 = ((Book) e2).getName(); • intvalue = Collator.getInstance().compare(n1, n2); • intsort = ((TableViewer) viewer).getTable() • .getSortDirection(); • if((sort & SWT.DOWN) != 0) { • return-value; • } elseif ((sort & SWT.UP) != 0) { • returnvalue; • } • return0; • } • }); • intsort = viewer.getTable().getSortDirection(); • viewer.getTable().setSortDirection( • (SWT.UP & sort) != 0 ? SWT.DOWN : SWT.UP); • } • }); Soyatec (http://www.soyatec.com)

  25. Table Viewer • Virtual Table & ILazyContentProvider • TableViewer viewer = new TableViewer(shell, SWT.VIRTUAL); • viewer.setContentProvider(new LazyContentProvider()); • viewer.setLabelProvider(new LabelProvider()); • String[] inputs = new String[100000]; • for (inti = 0; i < inputs.length; i++) { • inputs[i] = "Value " + i; • } • viewer.setItemCount(inputs.length); • viewer.setUseHashlookup(true); • viewer.setInput(inputs); privatestaticclassLazyContentProviderimplementsILazyContentProvider { privateTableViewerviewer; privateString[] elements; publicvoid dispose() { } publicvoidinputChanged(Viewer viewer, Object oldInput, Object newInput) { this.viewer= (TableViewer) viewer; elements= (String[]) newInput; } publicvoidupdateElement(intindex) { viewer.replace(elements[index], index); } } Soyatec (http://www.soyatec.com)

  26. Table Viewer • TableEditor • nameColumn.setLabelProvider(newCellLabelProvider() { • publicvoid update(ViewerCellcell) { • TableItemitem = (TableItem) cell.getItem(); • if(item.getData("EDITOR") != null) { • TableEditoreditor = (TableEditor) item.getData("EDITOR"); • editor.getEditor().dispose(); • editor.dispose(); • } • TableEditoreditor = newTableEditor(item.getParent()); • Composite control = new Composite(item.getParent(), SWT.NONE); • control.setBackground(item.getParent().getBackground()); • control.setBackgroundMode(SWT.INHERIT_DEFAULT); • RowLayoutlayout = newRowLayout(SWT.HORIZONTAL); • control.setLayout(layout); • Button button = new Button(control, SWT.RADIO); • button.setText(((Book) cell.getElement()).getName()); • editor.grabHorizontal= true; • editor.setEditor(control, item, 0); • item.setData("EDITOR", editor); • } • }); Soyatec (http://www.soyatec.com)

  27. TreeViewer • Initialize • Default • With Tree control • TreeViewer viewer1 = newTreeViewer(parent); • TreeViewer viewer2 = newTreeViewer(parent, SWT.FULL_SELECTION|SWT.BORDER); • Tree tree = new Tree(parent, SWT.FULL_SELECTION|SWT.BORDER); • TreeViewer viewer3 = newTreeViewer(tree); Soyatec (http://www.soyatec.com)

  28. TreeViewer • Content Provider • ITreeContentProvider public Object[] getChildren(Object parentElement) { if(parentElementinstanceof Library) { Set<Author> authors = authorMap.get(parentElement); if(authors == null) { authors= newHashSet<Author>(); List<Book> books = ((Library) parentElement).getBooks(); for(Book book : books) { Author author = book.getAuthor(); if(author == null) { continue; } authors.add(author); } authorMap.put((Library) parentElement, authors); } returnauthors.toArray(); } elseif (parentElementinstanceof Author) { return((Author) parentElement).getBooks().toArray(); } returnnew Object[0]; } public Object[] getElements(Object inputElement) { if(inputElementinstanceof List<?>) { return((List<?>) inputElement).toArray(); } returnnew Object[0]; } public Object getParent(Object element) { if(elementinstanceof Book) { return((Book) element).getAuthor(); } returnnull; } publicbooleanhasChildren(Object element) { returngetChildren(element).length != 0; } Soyatec (http://www.soyatec.com)

  29. Tree Viewer • Expand and Collapse • setAutoExpandLevel(int level) // 0, 1 … ALL_LEVELS • setExpandedElements(Object[] elements) • setExpandedState(Object elementOrTreePath, boolean expanded) • setExpandedTreePaths(TreePath[] treePaths) • setExpandPreCheckFilters(booleancheckFilters) • expandAll() • collapseAll() Soyatec (http://www.soyatec.com)

  30. TreeViewer • TreePath • ITreePathContentProvider • ITreePathLabelProvider Soyatec (http://www.soyatec.com)

  31. TreeViewer • Virtual Tree • ILazyTreeContentProvider • ILazyTreePathContentProvider Soyatec (http://www.soyatec.com)

  32. Column Viewer • ColumnViewerEditorActivationStrategy • FocusCellHighlighter • FocusCellOwnerDrawHighlighter • ColumnViewerToolTipSupport Soyatec (http://www.soyatec.com)

  33. Any Questions? Skype: jin.liu.soyatec Email: jin.liu@soyatec.com Soyatec (http://www.soyatec.com)

  34. The end Soyatec (http://www.soyatec.com)

More Related