Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBoris Bokowski2008-02-26 15:22:00 +0000
committerBoris Bokowski2008-02-26 15:22:00 +0000
commita25c1500008ba6cdfa996db406af06503c4185f6 (patch)
treefe6dbb70f2aa795115ad57bf0293c66ca9d0d116 /examples/org.eclipse.jface.snippets
parentc2e266bd3a094d7156c5694caedb761e5587f427 (diff)
downloadeclipse.platform.ui-a25c1500008ba6cdfa996db406af06503c4185f6.tar.gz
eclipse.platform.ui-a25c1500008ba6cdfa996db406af06503c4185f6.tar.xz
eclipse.platform.ui-a25c1500008ba6cdfa996db406af06503c4185f6.zip
Releasing last part for Bug 219393 [Viewers] StyledCellLabelProvider update
Diffstat (limited to 'examples/org.eclipse.jface.snippets')
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java151
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java (renamed from examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java)179
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java245
3 files changed, 207 insertions, 368 deletions
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java
new file mode 100644
index 00000000000..e70bcc968c7
--- /dev/null
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java
@@ -0,0 +1,151 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ * Michael Krkoska - initial API and implementation (bug 188333)
+ *******************************************************************************/
+package org.eclipse.jface.snippets.viewers;
+
+import java.io.File;
+import java.text.MessageFormat;
+
+import org.eclipse.jface.preference.JFacePreferences;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.StyledCellLabelProvider;
+import org.eclipse.jface.viewers.StyledStringBuilder;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerCell;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * Using a {@link StyledCellLabelProvider} on table viewer.
+ */
+
+public class Snippet049StyledCellLabelProvider {
+
+
+ private static final int SHELL_WIDTH= 400;
+ private static final Display DISPLAY= Display.getDefault();
+
+
+ public static void main(String[] args) {
+
+ JFaceResources.getColorRegistry().put(JFacePreferences.COUNTER_COLOR, new RGB(0,127,174));
+
+ Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
+ shell.setSize(SHELL_WIDTH, 400);
+ shell.setLayout(new GridLayout(1, false));
+
+ Snippet049StyledCellLabelProvider example= new Snippet049StyledCellLabelProvider();
+ Control composite= example.createPartControl(shell);
+ composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+
+ shell.open();
+
+ while (!shell.isDisposed()) {
+ if (!DISPLAY.readAndDispatch()) {
+ DISPLAY.sleep();
+ }
+ }
+ DISPLAY.dispose();
+ }
+
+ public Snippet049StyledCellLabelProvider() {
+ }
+
+ public Composite createPartControl(Composite parent) {
+ Composite composite= new Composite(parent, SWT.NONE);
+
+ composite.setLayout(new GridLayout(1, true));
+
+ Label label= new Label(composite, SWT.NONE);
+ label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
+ label.setText("Viewer with a StyledCellLabelProvider:"); //$NON-NLS-1$
+
+ ExampleLabelProvider labelProvider= new ExampleLabelProvider();
+ FileSystemContentProvider contentProvider= new FileSystemContentProvider();
+
+ final TableViewer tableViewer= new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
+
+ tableViewer.setContentProvider(contentProvider);
+ tableViewer.setLabelProvider(labelProvider);
+
+ GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
+ tableViewer.getControl().setLayoutData(data);
+ tableViewer.setInput(new Object());
+
+ return composite;
+ }
+
+ private static class ExampleLabelProvider extends StyledCellLabelProvider {
+
+ private static int IMAGE_SIZE= 16;
+ private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
+ private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
+
+
+ public ExampleLabelProvider() {
+ }
+
+ public void update(ViewerCell cell) {
+ Object element= cell.getElement();
+
+ if (element instanceof File) {
+ File file= (File) element;
+
+ StyledStringBuilder styledString= new StyledStringBuilder(file.getName());
+ String decoration = MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
+ styledString.append(decoration, StyledStringBuilder.COUNTER_STYLER);
+
+ cell.setText(styledString.toString());
+ cell.setStyleRanges(styledString.toStyleRanges());
+
+ if (file.isDirectory()) {
+ cell.setImage(IMAGE1);
+ } else {
+ cell.setImage(IMAGE2);
+ }
+ } else {
+ cell.setText("Unknown element"); //$NON-NLS-1$
+ }
+
+ super.update(cell);
+ }
+ }
+
+ private static class FileSystemContentProvider implements IStructuredContentProvider {
+
+ public Object[] getElements(Object element) {
+ File[] roots = File.listRoots();
+ for (int i = 0; i < roots.length; i++) {
+ File[] list = roots[i].listFiles();
+ if (list != null && list.length > 0) {
+ return list;
+ }
+ }
+ return roots;
+ }
+
+ public void dispose() {
+ }
+
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ }
+ }
+}
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java
index ed0fffcf3ef..3220eb9b185 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007 IBM Corporation and others.
+ * Copyright (c) 2007, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -12,24 +12,19 @@
package org.eclipse.jface.snippets.viewers;
import java.io.File;
+import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
-import org.eclipse.jface.viewers.CellLabelProvider;
-import org.eclipse.jface.viewers.ColumnLabelProvider;
-import org.eclipse.jface.viewers.ColumnViewer;
-import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.jface.viewers.SimpleStyledCellLabelProvider;
-import org.eclipse.jface.viewers.TreeViewer;
-import org.eclipse.jface.viewers.TreeViewerColumn;
-import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.preference.JFacePreferences;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.*;
+import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
@@ -40,9 +35,9 @@ import org.eclipse.swt.widgets.Shell;
/**
- * Using a {@link SimpleStyledCellLabelProvider} on tree viewer. Compare the result with a native tree viewer.
+ * Using a {@link DelegatingStyledCellLabelProvider} on tree viewer with multiple columns. Compare the result with a native tree viewer.
*/
-public class Snippet049SimpleStyledCellLabelProvider {
+public class Snippet050DelegatingStyledCellLabelProvider {
private static final int SHELL_WIDTH= 640;
@@ -50,12 +45,16 @@ public class Snippet049SimpleStyledCellLabelProvider {
public static void main(String[] args) {
+
+ JFaceResources.getColorRegistry().put(JFacePreferences.COUNTER_COLOR, new RGB(0,127,174));
+
+
Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
shell.setSize(SHELL_WIDTH, 300);
shell.setLayout(new GridLayout(1, false));
- Snippet049SimpleStyledCellLabelProvider example= new Snippet049SimpleStyledCellLabelProvider();
+ Snippet050DelegatingStyledCellLabelProvider example= new Snippet050DelegatingStyledCellLabelProvider();
example.createPartControl(shell);
shell.open();
@@ -68,20 +67,21 @@ public class Snippet049SimpleStyledCellLabelProvider {
DISPLAY.dispose();
}
- public Snippet049SimpleStyledCellLabelProvider() {
+ public Snippet050DelegatingStyledCellLabelProvider() {
}
public void createPartControl(Composite parent) {
Composite composite= new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(2, true));
+
+ final DelegatingStyledCellLabelProvider styledCellLP1= new DelegatingStyledCellLabelProvider(new NameAndSizeLabelProvider());
+ final DelegatingStyledCellLabelProvider styledCellLP2= new DelegatingStyledCellLabelProvider(new ModifiedDateLabelProvider());
+ final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, styledCellLP1, styledCellLP2); //$NON-NLS-1$
- ExampleLabelProvider labelProvider= new ExampleLabelProvider();
- ModifiedDateLabelProvider dateLabelProvider= new ModifiedDateLabelProvider();
-
- final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, new DecoratingLabelProvider(labelProvider), new DecoratingDateLabelProvider(dateLabelProvider)); //$NON-NLS-1$
-
- final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, labelProvider, dateLabelProvider); //$NON-NLS-1$
+ CellLabelProvider normalLP1= new NameAndSizeLabelProvider();
+ CellLabelProvider normalLP2= new ModifiedDateLabelProvider();
+ final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, normalLP1, normalLP2); //$NON-NLS-1$
Composite buttons= new Composite(parent, SWT.NONE);
buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
@@ -105,7 +105,7 @@ public class Snippet049SimpleStyledCellLabelProvider {
public void widgetSelected(SelectionEvent e) {
boolean newState= button2.getSelection();
- ((DecoratingLabelProvider) ownerDrawViewer.getLabelProvider(0)).setOwnerDrawEnabled(newState);
+ styledCellLP1.setOwnerDrawEnabled(newState);
ownerDrawViewer.refresh();
}
});
@@ -117,11 +117,10 @@ public class Snippet049SimpleStyledCellLabelProvider {
public void widgetSelected(SelectionEvent e) {
boolean newState= button3.getSelection();
- ((DecoratingDateLabelProvider) ownerDrawViewer.getLabelProvider(1)).setOwnerDrawEnabled(newState);
+ styledCellLP2.setOwnerDrawEnabled(newState);
ownerDrawViewer.refresh();
}
});
-
}
private static class FileSystemRoot {
@@ -145,12 +144,12 @@ public class Snippet049SimpleStyledCellLabelProvider {
treeViewer.setContentProvider(new FileSystemContentProvider());
TreeViewerColumn tvc1 = new TreeViewerColumn(treeViewer, SWT.NONE);
- tvc1.getColumn().setText("Name");
+ tvc1.getColumn().setText("Name"); //$NON-NLS-1$
tvc1.getColumn().setWidth(200);
tvc1.setLabelProvider(labelProvider1);
TreeViewerColumn tvc2 = new TreeViewerColumn(treeViewer, SWT.NONE);
- tvc2.getColumn().setText("Date Modified");
+ tvc2.getColumn().setText("Date Modified"); //$NON-NLS-1$
tvc2.getColumn().setWidth(200);
tvc2.setLabelProvider(labelProvider2);
@@ -161,94 +160,11 @@ public class Snippet049SimpleStyledCellLabelProvider {
return treeViewer;
}
-
- /**
- * Implements a {@link SimpleStyledCellLabelProvider} that wraps a normal label
- * provider and adds some decorations in color
- */
- private static class DecoratingLabelProvider extends SimpleStyledCellLabelProvider {
-
- private static final StyleRange[] NO_RANGES= new StyleRange[0];
- private final ILabelProvider fWrappedLabelProvider;
-
- public DecoratingLabelProvider(ILabelProvider labelProvider) {
- fWrappedLabelProvider= labelProvider;
- }
-
- protected LabelPresentationInfo getLabelPresentationInfo(Object element) {
- String text= fWrappedLabelProvider.getText(element);
- Image image= fWrappedLabelProvider.getImage(element);
-
-
- StyleRange[] ranges= NO_RANGES;
- if (element instanceof File) {
- File file= (File) element;
- if (file.isFile()) {
- String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
-
- int decorationStart= text.length();
- int decorationLength= decoration.length();
-
- text+= decoration;
-
- Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE);
-
- StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null);
- ranges= new StyleRange[] { styleRange };
- }
- }
- return new LabelPresentationInfo(text, ranges, image, null, null, null);
- }
-
- public void dispose() {
- super.dispose();
- fWrappedLabelProvider.dispose();
- }
- }
-
- private static class DecoratingDateLabelProvider extends SimpleStyledCellLabelProvider {
-
- private static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- private static final StyleRange[] NO_RANGES= new StyleRange[0];
- private final ILabelProvider fWrappedLabelProvider;
-
- public DecoratingDateLabelProvider(ILabelProvider labelProvider) {
- fWrappedLabelProvider= labelProvider;
- }
-
- protected LabelPresentationInfo getLabelPresentationInfo(Object element) {
- String text= fWrappedLabelProvider.getText(element);
- Image image= fWrappedLabelProvider.getImage(element);
-
- StyleRange[] ranges= NO_RANGES;
- if (element instanceof File) {
- File file= (File) element;
- String decoration= " " + DAYS[new Date(file.lastModified()).getDay()];
-
- int decorationStart= text.length();
- int decorationLength= decoration.length();
-
- text+= decoration;
-
- Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
-
- StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null);
- ranges= new StyleRange[] { styleRange };
- }
- return new LabelPresentationInfo(text, ranges, image, null, null, null);
- }
-
- public void dispose() {
- super.dispose();
- fWrappedLabelProvider.dispose();
- }
- }
-
/**
* A simple label provider
*/
- private static class ExampleLabelProvider extends ColumnLabelProvider {
+ private static class NameAndSizeLabelProvider extends ColumnLabelProvider implements IStyledLabelProvider {
private static int IMAGE_SIZE= 16;
private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
@@ -267,25 +183,46 @@ public class Snippet049SimpleStyledCellLabelProvider {
}
public String getText(Object element) {
+ return getStyledText(element).toString();
+ }
+
+ public StyledStringBuilder getStyledText(Object element) {
+ StyledStringBuilder styledString= new StyledStringBuilder();
if (element instanceof File) {
File file= (File) element;
if (file.getName().length() == 0) {
- return file.getAbsolutePath();
+ styledString.append(file.getAbsolutePath());
+ } else {
+ styledString.append(file.getName());
}
- return file.getName();
- }
- return "null"; //$NON-NLS-1$
+ if (file.isFile()) {
+ String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
+ styledString.append(decoration, StyledStringBuilder.COUNTER_STYLER);
+ }
+ }
+ return styledString;
}
-
}
- private static class ModifiedDateLabelProvider extends ColumnLabelProvider {
+ private static class ModifiedDateLabelProvider extends ColumnLabelProvider implements IStyledLabelProvider {
public String getText(Object element) {
+ return getStyledText(element).toString();
+ }
+
+ public StyledStringBuilder getStyledText(Object element) {
+ StyledStringBuilder styledString= new StyledStringBuilder();
if (element instanceof File) {
File file= (File) element;
- return new Date(file.lastModified()).toLocaleString();
+
+ String date= DateFormat.getDateInstance().format(new Date(file.lastModified()));
+ styledString.append(date);
+
+ styledString.append(' ');
+
+ String time = DateFormat.getTimeInstance(3).format(new Date(file.lastModified()));
+ styledString.append(time, StyledStringBuilder.COUNTER_STYLER);
}
- return "-"; //$NON-NLS-1$
+ return styledString;
}
}
@@ -328,8 +265,4 @@ public class Snippet049SimpleStyledCellLabelProvider {
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
-
-
-
-
}
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java
deleted file mode 100644
index 3a6552f9adb..00000000000
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java
+++ /dev/null
@@ -1,245 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2007 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- * Michael Krkoska - initial API and implementation (bug 188333)
- *******************************************************************************/
-package org.eclipse.jface.snippets.viewers;
-
-import java.io.File;
-import java.text.MessageFormat;
-
-import org.eclipse.jface.viewers.ColumnViewer;
-import org.eclipse.jface.viewers.IBaseLabelProvider;
-import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.jface.viewers.LabelProvider;
-import org.eclipse.jface.viewers.SimpleStyledCellLabelProvider;
-import org.eclipse.jface.viewers.TableViewer;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.StyleRange;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Shell;
-
-/**
- * Using a {@link SimpleStyledCellLabelProvider} on table viewer. Compare the result with a native table viewer.
- */
-
-public class Snippet050SimpleStyledCellLabelProvider {
-
-
- private static final int SHELL_WIDTH= 640;
- private static final Display DISPLAY= Display.getDefault();
-
-
- public static void main(String[] args) {
-
- Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
- shell.setSize(SHELL_WIDTH, 300);
- shell.setLayout(new GridLayout(1, false));
-
- Snippet050SimpleStyledCellLabelProvider example= new Snippet050SimpleStyledCellLabelProvider();
- example.createPartControl(shell);
-
- shell.open();
-
- while (!shell.isDisposed()) {
- if (!DISPLAY.readAndDispatch()) {
- DISPLAY.sleep();
- }
- }
- DISPLAY.dispose();
- }
-
- public Snippet050SimpleStyledCellLabelProvider() {
- }
-
- public void createPartControl(Composite parent) {
- Composite composite= new Composite(parent, SWT.NONE);
- composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
- composite.setLayout(new GridLayout(2, true));
-
- ExampleLabelProvider labelProvider= new ExampleLabelProvider();
-
- final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, new DecoratingLabelProvider(labelProvider)); //$NON-NLS-1$
-
- final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, labelProvider); //$NON-NLS-1$
-
- Button button= new Button(parent, SWT.NONE);
- button.setText("Refresh Viewers"); //$NON-NLS-1$
- button.addListener(SWT.Modify, new Listener() {
-
- public void handleEvent(Event event) {
- ownerDrawViewer.refresh();
- normalViewer.refresh();
- }
- });
-
- }
-
- private ColumnViewer createViewer(String description, Composite parent, IBaseLabelProvider labelProviders) {
-
- Composite composite= new Composite(parent, SWT.NONE);
- composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
- composite.setLayout(new GridLayout(1, true));
-
- Label label= new Label(composite, SWT.NONE);
- label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
- label.setText(description);
-
- TableViewer tableViewer= new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
- tableViewer.setContentProvider(new FileSystemContentProvider());
- tableViewer.setLabelProvider(labelProviders);
-
- GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
- tableViewer.getControl().setLayoutData(data);
- File[] roots = File.listRoots();
- File root = null;
- for (int i = 0; i < roots.length; i++) {
- String[] list = roots[i].list();
- if (list != null && list.length > 0) {
- root = roots[i];
- break;
- }
- }
- if (root == null) {
- throw new RuntimeException("couldn't get a non-empty root file");
- }
- tableViewer.setInput(root);
-
- return tableViewer;
- }
-
- /**
- * Implements a {@link SimpleStyledCellLabelProvider} that wraps a normal label
- * provider and adds some decorations in color
- */
- private static class DecoratingLabelProvider extends SimpleStyledCellLabelProvider {
-
- private static final StyleRange[] NO_RANGES= new StyleRange[0];
- private final ILabelProvider fWrappedLabelProvider;
-
- public DecoratingLabelProvider(ILabelProvider labelProvider) {
- fWrappedLabelProvider= labelProvider;
- }
-
- protected LabelPresentationInfo getLabelPresentationInfo(Object element) {
- String text= fWrappedLabelProvider.getText(element);
- Image image= fWrappedLabelProvider.getImage(element);
-
-
- StyleRange[] ranges= NO_RANGES;
- if (element instanceof File) {
- File file= (File) element;
- if (file.isFile()) {
- String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
-
- int decorationStart= text.length();
- int decorationLength= decoration.length();
-
- text+= decoration;
-
- Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE);
-
- StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null);
- ranges= new StyleRange[] { styleRange };
- }
- }
- return new LabelPresentationInfo(text, ranges, image, null, null, null);
- }
-
- public void dispose() {
- super.dispose();
- fWrappedLabelProvider.dispose();
- }
- }
-
-
- /**
- * A simple label provider
- */
- private static class ExampleLabelProvider extends LabelProvider {
-
- private static int IMAGE_SIZE= 16;
- private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
- private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
-
- public Image getImage(Object element) {
- if (element instanceof File) {
- File file= (File) element;
- if (file.isDirectory()) {
- return IMAGE1;
- } else {
- return IMAGE2;
- }
- }
- return null;
- }
-
- public String getText(Object element) {
- if (element instanceof File) {
- File file= (File) element;
- return file.getName();
- }
- return "null"; //$NON-NLS-1$
- }
-
- }
-
- private static class FileSystemContentProvider implements ITreeContentProvider {
-
- public Object[] getChildren(Object element) {
- if (element instanceof File) {
- File file= (File) element;
- if (file.isDirectory()) {
- File[] listFiles= file.listFiles();
- if (listFiles != null) {
- return listFiles;
- }
- }
- }
- return new Object[0];
- }
-
- public Object getParent(Object element) {
- if (element instanceof File) {
- File file= (File) element;
- return file.getParentFile();
- }
- return null;
- }
-
- public boolean hasChildren(Object element) {
- return getChildren(element).length > 0;
- }
-
- public Object[] getElements(Object inputElement) {
- return getChildren(inputElement);
- }
-
- public void dispose() {
- }
-
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
- }
- }
-
-
-
-
-}

Back to the top