package org.eclipse.jface.snippets.viewers; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableColorProvider; import org.eclipse.jface.viewers.ITableFontProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TableColumn; /** * Example usage of none mandatory interfaces of ITableFontProvider and * ITableColorProvider * * @author Tom Schindl <tom.schindl@bestsolution.at> * */ public class Snippet013TableViewerNoneMandatoryLabelProvider { private class MyContentProvider implements IStructuredContentProvider { /* * (non-Javadoc) * * @see * org.eclipse.jface.viewers.IStructuredContentProvider#getElements( * java.lang.Object) */ public Object[] getElements(Object inputElement) { return (MyModel[]) inputElement; } /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.IContentProvider#dispose() */ public void dispose() { } /* * (non-Javadoc) * * @see * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse * .jface.viewers.Viewer, java.lang.Object, java.lang.Object) */ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } } public Snippet013TableViewerNoneMandatoryLabelProvider(Shell shell) { final TableViewer v = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION); v.setLabelProvider(new MyLabelProvider()); v.setContentProvider(new MyContentProvider()); TableColumn column = new TableColumn(v.getTable(), SWT.NONE); column.setWidth(200); column.setText("Column 1"); column = new TableColumn(v.getTable(), SWT.NONE); column.setWidth(200); column.setText("Column 2"); MyModel[] model = createModel(); v.setInput(model); v.getTable().setLinesVisible(true); v.getTable().setHeaderVisible(true); } private MyModel[] createModel() { MyModel[] elements = new MyModel[10]; for (int i = 0; i < 10; i++) { elements[i] = new MyModel(i); } return elements; } /** * @param args */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); new Snippet013TableViewerNoneMandatoryLabelProvider(shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
package org.eclipse.jface.snippets.viewers; public class MyModel { public int counter; public MyModel(int counter) { this.counter = counter; } public String toString() { return "Item " + this.counter; } }
package org.eclipse.jface.snippets.viewers; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.jface.viewers.ITableColorProvider; import org.eclipse.jface.viewers.ITableFontProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; public class MyLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider, ITableColorProvider { FontRegistry registry = new FontRegistry(); public Image getColumnImage(Object element, int columnIndex) { return null; } public String getColumnText(Object element, int columnIndex) { return "Column " + columnIndex + " => " + element.toString(); } public Font getFont(Object element, int columnIndex) { if (((MyModel) element).counter % 2 == 0) { return registry.getBold(Display.getCurrent().getSystemFont() .getFontData()[0].getName()); } return null; } public Color getBackground(Object element, int columnIndex) { if (((MyModel) element).counter % 2 == 0) { return Display.getCurrent().getSystemColor(SWT.COLOR_RED); } return null; } public Color getForeground(Object element, int columnIndex) { if (((MyModel) element).counter % 2 == 1) { return Display.getCurrent().getSystemColor(SWT.COLOR_RED); } return null; } }