Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 90747 Details for
Bug 219393
[Viewers] StyledCellLabelProvider update
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Delegating and Decorating label provider & snippet update
219393.patch (text/plain), 56.29 KB, created by
Martin Aeschlimann
on 2008-02-26 10:01:51 EST
(
hide
)
Description:
Delegating and Decorating label provider & snippet update
Filename:
MIME Type:
Creator:
Martin Aeschlimann
Created:
2008-02-26 10:01:51 EST
Size:
56.29 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jface >Index: src/org/eclipse/jface/viewers/DelegatingStyledCellLabelProvider.java >=================================================================== >RCS file: src/org/eclipse/jface/viewers/DelegatingStyledCellLabelProvider.java >diff -N src/org/eclipse/jface/viewers/DelegatingStyledCellLabelProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/viewers/DelegatingStyledCellLabelProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,210 @@ >+/******************************************************************************* >+ * Copyright (c) 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 >+ *******************************************************************************/ >+package org.eclipse.jface.viewers; >+ >+import org.eclipse.swt.graphics.Color; >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.Image; >+ >+ >+/** >+ * A {@link DelegatingStyledCellLabelProvider} is a {@link StyledCellLabelProvider} >+ * that delegates requests for the styled string and the image >+ * to a {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}. >+ * >+ * <p>Existing label providers can be enhanced by implementing >+ * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} so they can be >+ * used in viewers with styled labels.</p> >+ * >+ * <p>The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can >+ * optionally implement {@link IColorProvider} and {@link IFontProvider} to provide >+ * foreground and background color and a default font. >+ * </p> >+ * >+ * @since 3.4 >+ */ >+public class DelegatingStyledCellLabelProvider extends StyledCellLabelProvider { >+ >+ /** >+ * Interface marking a label provider that provides styled text labels and images. >+ * <p>The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can >+ * optionally implement {@link IColorProvider} and {@link IFontProvider} to provide >+ * foreground and background color and a default font. >+ * </p> >+ */ >+ public static interface IStyledLabelProvider extends IBaseLabelProvider { >+ >+ /** >+ * Returns the styled text label for the given element >+ * >+ * @param element the element to evaluate the styled string for >+ * >+ * @return the styled string. >+ */ >+ public StyledStringBuilder getStyledText(Object element); >+ >+ /** >+ * Returns the image for the label of the given element. The image >+ * is owned by the label provider and must not be disposed directly. >+ * Instead, dispose the label provider when no longer needed. >+ * >+ * @param element the element for which to provide the label image >+ * @return the image used to label the element, or <code>null</code> >+ * if there is no image for the given object >+ */ >+ public Image getImage(Object element); >+ } >+ >+ >+ private IStyledLabelProvider styledLabelProvider; >+ >+ /** >+ * Creates a {@link DelegatingStyledCellLabelProvider} that delegates the requests for the >+ * styled labels and the images to a {@link IStyledLabelProvider}. >+ * >+ * @param labelProvider the label provider that provides the styled labels and the images >+ */ >+ public DelegatingStyledCellLabelProvider(IStyledLabelProvider labelProvider) { >+ if (labelProvider == null) >+ throw new IllegalArgumentException("Label provider must not be null"); //$NON-NLS-1$ >+ >+ this.styledLabelProvider= labelProvider; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell) >+ */ >+ public void update(ViewerCell cell) { >+ Object element= cell.getElement(); >+ >+ StyledStringBuilder styledString= getStyledText(element); >+ cell.setText(styledString.toString()); >+ if (isOwnerDrawEnabled()) { >+ cell.setStyleRanges(styledString.toStyleRanges()); >+ } else { >+ cell.setStyleRanges(null); >+ } >+ >+ cell.setImage(getImage(element)); >+ cell.setFont(getFont(element)); >+ cell.setForeground(getForeground(element)); >+ cell.setBackground(getBackground(element)); >+ >+ super.update(cell); >+ } >+ >+ /** >+ * Provides a foreground color for the given element. >+ * >+ * @param element the element >+ * @return the foreground color for the element, or <code>null</code> >+ * to use the default foreground color >+ */ >+ public Color getForeground(Object element) { >+ if (this.styledLabelProvider instanceof IColorProvider) { >+ return ((IColorProvider) this.styledLabelProvider).getForeground(element); >+ } >+ return null; >+ } >+ >+ /** >+ * Provides a background color for the given element. >+ * >+ * @param element the element >+ * @return the background color for the element, or <code>null</code> >+ * to use the default background color >+ */ >+ public Color getBackground(Object element) { >+ if (this.styledLabelProvider instanceof IColorProvider) { >+ return ((IColorProvider) this.styledLabelProvider).getBackground(element); >+ } >+ return null; >+ } >+ >+ /** >+ * Provides a font for the given element. >+ * >+ * @param element the element >+ * @return the font for the element, or <code>null</code> >+ * to use the default font >+ */ >+ public Font getFont(Object element) { >+ if (this.styledLabelProvider instanceof IFontProvider) { >+ return ((IFontProvider) this.styledLabelProvider).getFont(element); >+ } >+ return null; >+ } >+ >+ /** >+ * Returns the image for the label of the given element. The image >+ * is owned by the label provider and must not be disposed directly. >+ * Instead, dispose the label provider when no longer needed. >+ * >+ * @param element the element for which to provide the label image >+ * @return the image used to label the element, or <code>null</code> >+ * if there is no image for the given object >+ */ >+ public Image getImage(Object element) { >+ return this.styledLabelProvider.getImage(element); >+ } >+ >+ /** >+ * Returns the styled text for the label of the given element. >+ * >+ * @param element the element for which to provide the styled label text >+ * @return the styled text string used to label the element >+ */ >+ public StyledStringBuilder getStyledText(Object element) { >+ return this.styledLabelProvider.getStyledText(element); >+ } >+ >+ /** >+ * Returns the styled string provider. >+ * >+ * @return the wrapped label provider >+ */ >+ public IStyledLabelProvider getStyledStringProvider() { >+ return this.styledLabelProvider; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) >+ */ >+ public void addListener(ILabelProviderListener listener) { >+ super.addListener(listener); >+ this.styledLabelProvider.addListener(listener); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) >+ */ >+ public void removeListener(ILabelProviderListener listener) { >+ super.removeListener(listener); >+ this.styledLabelProvider.removeListener(listener); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String) >+ */ >+ public boolean isLabelProperty(Object element, String property) { >+ return this.styledLabelProvider.isLabelProperty(element, property); >+ } >+ >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.StyledCellLabelProvider#dispose() >+ */ >+ public void dispose() { >+ super.dispose(); >+ this.styledLabelProvider.dispose(); >+ } >+ >+} >Index: src/org/eclipse/jface/viewers/DecoratingStyledCellLabelProvider.java >=================================================================== >RCS file: src/org/eclipse/jface/viewers/DecoratingStyledCellLabelProvider.java >diff -N src/org/eclipse/jface/viewers/DecoratingStyledCellLabelProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/viewers/DecoratingStyledCellLabelProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,326 @@ >+/******************************************************************************* >+ * Copyright (c) 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 >+ *******************************************************************************/ >+package org.eclipse.jface.viewers; >+ >+import org.eclipse.core.runtime.Assert; >+import org.eclipse.jface.resource.JFaceResources; >+import org.eclipse.jface.resource.LocalResourceManager; >+import org.eclipse.jface.resource.ResourceManager; >+import org.eclipse.jface.viewers.StyledStringBuilder.Styler; >+import org.eclipse.swt.graphics.Color; >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.Image; >+ >+ >+/** >+ * A {@link DecoratingStyledCellLabelProvider} is a {@link DelegatingStyledCellLabelProvider} >+ * that uses a nested {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider} >+ * to compute styled text label and image and takes a {@link ILabelDecorator} to decorate the label. >+ * >+ * <p>Use this label provider as a replacement for the {@link DecoratingLabelProvider} when >+ * decorating styled text labels.</p> >+ * >+ * <p>The {@link DecoratingStyledCellLabelProvider} will try to evaluate the text decoration added >+ * by the {@link ILabelDecorator} and will apply the style returned by {@link #getDecorationStyle(Object)} >+ * </p> >+ * <p>The {@link ILabelDecorator} can >+ * optionally implement {@link IColorDecorator} and {@link IFontDecorator} to provide >+ * foreground and background color and font decoration. >+ * </p> >+ * >+ * @since 3.4 >+ */ >+public class DecoratingStyledCellLabelProvider extends DelegatingStyledCellLabelProvider { >+ >+ private ILabelDecorator decorator; >+ private IDecorationContext decorationContext; >+ private ILabelProviderListener labelProviderListener; >+ >+ /** >+ * Creates a {@link DecoratingStyledCellLabelProvider} that delegates the requests for >+ * styled labels and for images to a {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}. >+ * >+ * @param labelProvider the styled label provider >+ * @param decorator a label decorator or <code>null</code> to not decorate the label >+ * @param decorationContext a decoration context or <code>null</code> if the no decorator >+ * is configured or the default decorator should be used >+ */ >+ public DecoratingStyledCellLabelProvider(IStyledLabelProvider labelProvider, ILabelDecorator decorator, IDecorationContext decorationContext) { >+ super(labelProvider); >+ >+ this.decorator= decorator; >+ this.decorationContext= decorationContext; >+ if (decorator != null && decorationContext == null) { >+ decorationContext= createDefaultDecorationContext(); >+ } >+ this.labelProviderListener= new ILabelProviderListener() { >+ public void labelProviderChanged(LabelProviderChangedEvent event) { >+ fireLabelProviderChanged(event); >+ } >+ }; >+ labelProvider.addListener(this.labelProviderListener); >+ if (decorator != null) >+ decorator.addListener(this.labelProviderListener); >+ } >+ >+ private IDecorationContext createDefaultDecorationContext() { >+ DecorationContext newContext = new DecorationContext(); >+ newContext.putProperty(DecorationContext.RESOURCE_MANAGER_KEY, new LocalResourceManager(JFaceResources.getResources())); >+ return newContext; >+ } >+ >+ /** >+ * Returns the decoration context associated with this label provider. It >+ * will be passed to the decorator if the decorator is an instance of >+ * {@link LabelDecorator}. >+ * >+ * @return the decoration context associated with this label provider >+ */ >+ public IDecorationContext getDecorationContext() { >+ return this.decorationContext; >+ } >+ >+ /** >+ * Set the decoration context that will be based to the decorator for this >+ * label provider if that decorator implements {@link LabelDecorator}. >+ * >+ * If this decorationContext has a {@link ResourceManager} stored for the >+ * {@link DecorationContext#RESOURCE_MANAGER_KEY} property it will be >+ * disposed when the label provider is disposed. >+ * >+ * @param decorationContext >+ * the decoration context. >+ */ >+ public void setDecorationContext(IDecorationContext decorationContext) { >+ Assert.isNotNull(decorationContext); >+ this.decorationContext = decorationContext; >+ } >+ >+ private boolean waitForPendingDecoration(ViewerCell cell) { >+ if (this.decorator == null) >+ return false; >+ >+ Object element= cell.getElement(); >+ String oldText= cell.getText(); >+ >+ boolean isDecorationPending= false; >+ if (this.decorator instanceof LabelDecorator) { >+ isDecorationPending= !((LabelDecorator) this.decorator).prepareDecoration(element, oldText, getDecorationContext()); >+ } else if (this.decorator instanceof IDelayedLabelDecorator) { >+ isDecorationPending= !((IDelayedLabelDecorator) this.decorator).prepareDecoration(element, oldText); >+ } >+ if (isDecorationPending && oldText.length() == 0) { >+ // item is empty: is shown for the first time: don't wait >+ return false; >+ } >+ return isDecorationPending; >+ } >+ >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell) >+ */ >+ public void update(ViewerCell cell) { >+ if (waitForPendingDecoration(cell)) { >+ return; // wait until the decoration is ready >+ } >+ super.update(cell); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getForeground(java.lang.Object) >+ */ >+ public Color getForeground(Object element) { >+ if (this.decorator instanceof IColorDecorator) { >+ Color foreground= ((IColorDecorator) this.decorator).decorateForeground(element); >+ if (foreground != null) >+ return foreground; >+ } >+ return super.getForeground(element); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getBackground(java.lang.Object) >+ */ >+ public Color getBackground(Object element) { >+ if (this.decorator instanceof IColorDecorator) { >+ Color color= ((IColorDecorator) this.decorator).decorateBackground(element); >+ if (color != null) >+ return color; >+ } >+ return super.getBackground(element); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getFont(java.lang.Object) >+ */ >+ public Font getFont(Object element) { >+ if (this.decorator instanceof IFontDecorator) { >+ Font font= ((IFontDecorator) this.decorator).decorateFont(element); >+ if (font != null) >+ return font; >+ } >+ return super.getFont(element); >+ } >+ >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getImage(java.lang.Object) >+ */ >+ public Image getImage(Object element) { >+ Image image= super.getImage(element); >+ Image decorated= null; >+ if (this.decorator instanceof LabelDecorator) { >+ decorated= ((LabelDecorator) this.decorator).decorateImage(image, element, getDecorationContext()); >+ } else { >+ decorated= this.decorator.decorateImage(image, element); >+ } >+ if (decorated != null) >+ return decorated; >+ >+ return image; >+ } >+ >+ /** >+ * Returns the styled text for the label of the given element. >+ * >+ * @param element the element for which to provide the styled label text >+ * @return the styled text string used to label the element >+ */ >+ public StyledStringBuilder getStyledText(Object element) { >+ StyledStringBuilder string= super.getStyledText(element); >+ >+ String label= string.toString(); >+ String decorated; >+ if (this.decorator instanceof LabelDecorator) { >+ decorated= ((LabelDecorator) this.decorator).decorateText(label, element, getDecorationContext()); >+ } else { >+ decorated= this.decorator.decorateText(label, element); >+ } >+ if (decorated == null) >+ return string; >+ >+ int originalStart= decorated.indexOf(label); >+ if (originalStart == -1) { >+ return new StyledStringBuilder(decorated); // the decorator did something wild >+ } >+ >+ if (decorated.length() == label.length()) >+ return string; >+ >+ Styler style= getDecorationStyle(element); >+ if (originalStart > 0) { >+ StyledStringBuilder newString= new StyledStringBuilder(decorated.substring(0, originalStart), style); >+ newString.append(string); >+ string= newString; >+ } >+ if (decorated.length() > originalStart + label.length()) { // decorator appended something >+ return string.append(decorated.substring(originalStart + label.length()), style); >+ } >+ return string; >+ } >+ >+ /** >+ * Sets the {@link StyledStringBuilder.Styler} to be used for string decorations. By default >+ * the {@link StyledStringBuilder#DECORATIONS_STYLER decoration style}. Clients can override. >+ * >+ * Note that it is the client's responsibility to react on color changes of the decoration color by >+ * refreshing the view >+ * >+ * @param element the element that has been decorated >+ * >+ * @return return the decoration style >+ */ >+ protected Styler getDecorationStyle(Object element) { >+ return StyledStringBuilder.DECORATIONS_STYLER; >+ } >+ >+ /** >+ * Returns the decorator or <code>null</code> if no decorator is installed >+ * >+ * @return the decorator or <code>null</code> if no decorator is installed >+ */ >+ public ILabelDecorator getLabelDecorator() { >+ return this.decorator; >+ } >+ >+ /** >+ * Sets the label decorator. Removes all known listeners from the old >+ * decorator, and adds all known listeners to the new decorator. The old >+ * decorator is not disposed. Fires a label provider changed event >+ * indicating that all labels should be updated. Has no effect if the given >+ * decorator is identical to the current one. >+ * >+ * @param newDecorator >+ * the label decorator, or <code>null</code> if no decorations >+ * are to be applied >+ */ >+ public void setLabelDecorator(ILabelDecorator newDecorator) { >+ ILabelDecorator oldDecorator= this.decorator; >+ if (oldDecorator != newDecorator) { >+ if (oldDecorator != null) >+ oldDecorator.removeListener(this.labelProviderListener); >+ this.decorator= newDecorator; >+ if (newDecorator != null) { >+ newDecorator.addListener(this.labelProviderListener); >+ } >+ } >+ fireLabelProviderChanged(new LabelProviderChangedEvent(this)); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) >+ */ >+ public void addListener(ILabelProviderListener listener) { >+ super.addListener(listener); >+ if (this.decorator != null) { >+ this.decorator.addListener(this.labelProviderListener); >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) >+ */ >+ public void removeListener(ILabelProviderListener listener) { >+ super.removeListener(listener); >+ if (this.decorator != null) { >+ this.decorator.removeListener(this.labelProviderListener); >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String) >+ */ >+ public boolean isLabelProperty(Object element, String property) { >+ if (super.isLabelProperty(element, property)) { >+ return true; >+ } >+ return this.decorator != null && this.decorator.isLabelProperty(element, property); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.StyledCellLabelProvider#dispose() >+ */ >+ public void dispose() { >+ super.dispose(); >+ if (this.decorationContext != null) { >+ Object manager = this.decorationContext.getProperty(DecorationContext.RESOURCE_MANAGER_KEY); >+ if (manager instanceof ResourceManager) >+ ((ResourceManager) manager).dispose(); >+ } >+ if (this.decorator != null) { >+ this.decorator.removeListener(this.labelProviderListener); >+ this.decorator.dispose(); >+ this.decorator= null; >+ } >+ } >+ >+} >#P org.eclipse.jface.snippets >Index: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java >=================================================================== >RCS file: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java >diff -N Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java >--- Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java 3 Feb 2008 20:09:58 -0000 1.2 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,335 +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 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.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.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.Label; >-import org.eclipse.swt.widgets.Shell; >- >- >-/** >- * Using a {@link SimpleStyledCellLabelProvider} on tree viewer. Compare the result with a native tree viewer. >- */ >-public class Snippet049SimpleStyledCellLabelProvider { >- >- >- 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)); >- >- Snippet049SimpleStyledCellLabelProvider example= new Snippet049SimpleStyledCellLabelProvider(); >- example.createPartControl(shell); >- >- shell.open(); >- >- while (!shell.isDisposed()) { >- if (!DISPLAY.readAndDispatch()) { >- DISPLAY.sleep(); >- } >- } >- DISPLAY.dispose(); >- } >- >- public Snippet049SimpleStyledCellLabelProvider() { >- } >- >- 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)); >- >- 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$ >- >- Composite buttons= new Composite(parent, SWT.NONE); >- buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); >- buttons.setLayout(new GridLayout(3, false)); >- >- >- Button button1= new Button(buttons, SWT.PUSH); >- button1.setText("Refresh Viewers"); //$NON-NLS-1$ >- button1.addSelectionListener(new SelectionAdapter() { >- >- public void widgetSelected(SelectionEvent e) { >- ownerDrawViewer.refresh(); >- normalViewer.refresh(); >- } >- }); >- >- final Button button2= new Button(buttons, SWT.CHECK); >- button2.setText("Owner draw on column 1"); //$NON-NLS-1$ >- button2.setSelection(true); >- button2.addSelectionListener(new SelectionAdapter() { >- >- public void widgetSelected(SelectionEvent e) { >- boolean newState= button2.getSelection(); >- ((DecoratingLabelProvider) ownerDrawViewer.getLabelProvider(0)).setOwnerDrawEnabled(newState); >- ownerDrawViewer.refresh(); >- } >- }); >- >- final Button button3= new Button(buttons, SWT.CHECK); >- button3.setText("Owner draw on column 2"); //$NON-NLS-1$ >- button3.setSelection(true); >- button3.addSelectionListener(new SelectionAdapter() { >- >- public void widgetSelected(SelectionEvent e) { >- boolean newState= button3.getSelection(); >- ((DecoratingDateLabelProvider) ownerDrawViewer.getLabelProvider(1)).setOwnerDrawEnabled(newState); >- ownerDrawViewer.refresh(); >- } >- }); >- >- } >- >- private static class FileSystemRoot { >- public File[] getRoots() { >- return File.listRoots(); >- } >- } >- >- private ColumnViewer createViewer(String description, Composite parent, CellLabelProvider labelProvider1, CellLabelProvider labelProvider2) { >- >- 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); >- >- TreeViewer treeViewer= new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); >- treeViewer.getTree().setHeaderVisible(true); >- treeViewer.setContentProvider(new FileSystemContentProvider()); >- >- TreeViewerColumn tvc1 = new TreeViewerColumn(treeViewer, SWT.NONE); >- tvc1.getColumn().setText("Name"); >- tvc1.getColumn().setWidth(200); >- tvc1.setLabelProvider(labelProvider1); >- >- TreeViewerColumn tvc2 = new TreeViewerColumn(treeViewer, SWT.NONE); >- tvc2.getColumn().setText("Date Modified"); >- tvc2.getColumn().setWidth(200); >- tvc2.setLabelProvider(labelProvider2); >- >- GridData data= new GridData(GridData.FILL, GridData.FILL, true, true); >- treeViewer.getControl().setLayoutData(data); >- >- treeViewer.setInput(new FileSystemRoot()); >- >- 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 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; >- if (file.getName().length() == 0) { >- return file.getAbsolutePath(); >- } >- return file.getName(); >- } >- return "null"; //$NON-NLS-1$ >- } >- >- } >- >- private static class ModifiedDateLabelProvider extends ColumnLabelProvider { >- public String getText(Object element) { >- if (element instanceof File) { >- File file= (File) element; >- return new Date(file.lastModified()).toLocaleString(); >- } >- return "-"; //$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; >- } >- } >- } else if (element instanceof FileSystemRoot) { >- return ((FileSystemRoot) element).getRoots(); >- } >- 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) { >- } >- } >- >- >- >- >-} >Index: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java >=================================================================== >RCS file: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java >diff -N Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java >--- Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java 3 Feb 2008 20:09:58 -0000 1.2 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -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) { >- } >- } >- >- >- >- >-} >Index: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java >=================================================================== >RCS file: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java >diff -N Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050DelegatingStyledCellLabelProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,268 @@ >+/******************************************************************************* >+ * 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.DateFormat; >+import java.text.MessageFormat; >+import java.util.Date; >+ >+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.events.SelectionAdapter; >+import org.eclipse.swt.events.SelectionEvent; >+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; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Display; >+import org.eclipse.swt.widgets.Label; >+import org.eclipse.swt.widgets.Shell; >+ >+ >+/** >+ * Using a {@link DelegatingStyledCellLabelProvider} on tree viewer with multiple columns. Compare the result with a native tree viewer. >+ */ >+public class Snippet050DelegatingStyledCellLabelProvider { >+ >+ >+ private static final int SHELL_WIDTH= 640; >+ 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, 300); >+ shell.setLayout(new GridLayout(1, false)); >+ >+ Snippet050DelegatingStyledCellLabelProvider example= new Snippet050DelegatingStyledCellLabelProvider(); >+ example.createPartControl(shell); >+ >+ shell.open(); >+ >+ while (!shell.isDisposed()) { >+ if (!DISPLAY.readAndDispatch()) { >+ DISPLAY.sleep(); >+ } >+ } >+ DISPLAY.dispose(); >+ } >+ >+ 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$ >+ >+ 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)); >+ buttons.setLayout(new GridLayout(3, false)); >+ >+ >+ Button button1= new Button(buttons, SWT.PUSH); >+ button1.setText("Refresh Viewers"); //$NON-NLS-1$ >+ button1.addSelectionListener(new SelectionAdapter() { >+ >+ public void widgetSelected(SelectionEvent e) { >+ ownerDrawViewer.refresh(); >+ normalViewer.refresh(); >+ } >+ }); >+ >+ final Button button2= new Button(buttons, SWT.CHECK); >+ button2.setText("Owner draw on column 1"); //$NON-NLS-1$ >+ button2.setSelection(true); >+ button2.addSelectionListener(new SelectionAdapter() { >+ >+ public void widgetSelected(SelectionEvent e) { >+ boolean newState= button2.getSelection(); >+ styledCellLP1.setOwnerDrawEnabled(newState); >+ ownerDrawViewer.refresh(); >+ } >+ }); >+ >+ final Button button3= new Button(buttons, SWT.CHECK); >+ button3.setText("Owner draw on column 2"); //$NON-NLS-1$ >+ button3.setSelection(true); >+ button3.addSelectionListener(new SelectionAdapter() { >+ >+ public void widgetSelected(SelectionEvent e) { >+ boolean newState= button3.getSelection(); >+ styledCellLP2.setOwnerDrawEnabled(newState); >+ ownerDrawViewer.refresh(); >+ } >+ }); >+ } >+ >+ private static class FileSystemRoot { >+ public File[] getRoots() { >+ return File.listRoots(); >+ } >+ } >+ >+ private ColumnViewer createViewer(String description, Composite parent, CellLabelProvider labelProvider1, CellLabelProvider labelProvider2) { >+ >+ 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); >+ >+ TreeViewer treeViewer= new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); >+ treeViewer.getTree().setHeaderVisible(true); >+ treeViewer.setContentProvider(new FileSystemContentProvider()); >+ >+ TreeViewerColumn tvc1 = new TreeViewerColumn(treeViewer, SWT.NONE); >+ 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"); //$NON-NLS-1$ >+ tvc2.getColumn().setWidth(200); >+ tvc2.setLabelProvider(labelProvider2); >+ >+ GridData data= new GridData(GridData.FILL, GridData.FILL, true, true); >+ treeViewer.getControl().setLayoutData(data); >+ >+ treeViewer.setInput(new FileSystemRoot()); >+ >+ return treeViewer; >+ } >+ >+ /** >+ * A simple label provider >+ */ >+ 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)); >+ 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) { >+ 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) { >+ styledString.append(file.getAbsolutePath()); >+ } else { >+ styledString.append(file.getName()); >+ } >+ 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 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; >+ >+ 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 styledString; >+ } >+ } >+ >+ 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; >+ } >+ } >+ } else if (element instanceof FileSystemRoot) { >+ return ((FileSystemRoot) element).getRoots(); >+ } >+ 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) { >+ } >+ } >+} >Index: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java >=================================================================== >RCS file: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java >diff -N Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049StyledCellLabelProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -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) { >+ } >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 219393
:
90042
|
90043
|
90363
|
90364
| 90747