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 90363 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]
uddated patch for JFace
colored-labels-jface-2.txt (text/plain), 74.88 KB, created by
Martin Aeschlimann
on 2008-02-21 10:53:36 EST
(
hide
)
Description:
uddated patch for JFace
Filename:
MIME Type:
Creator:
Martin Aeschlimann
Created:
2008-02-21 10:53:36 EST
Size:
74.88 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.ui.workbench >Index: Eclipse UI/org/eclipse/ui/dialogs/FilteredItemsSelectionDialog.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/dialogs/FilteredItemsSelectionDialog.java,v >retrieving revision 1.58 >diff -u -r1.58 FilteredItemsSelectionDialog.java >--- Eclipse UI/org/eclipse/ui/dialogs/FilteredItemsSelectionDialog.java 11 Jan 2008 15:10:00 -0000 1.58 >+++ Eclipse UI/org/eclipse/ui/dialogs/FilteredItemsSelectionDialog.java 21 Feb 2008 15:47:14 -0000 >@@ -17,17 +17,12 @@ > import java.io.IOException; > import java.io.StringReader; > import java.io.StringWriter; >-import java.util.ArrayList; >-import java.util.Arrays; >-import java.util.Collections; >-import java.util.Comparator; >-import java.util.HashMap; >-import java.util.HashSet; >-import java.util.Iterator; >-import java.util.LinkedList; >+import java.util.*; > import java.util.List; >-import java.util.Set; > >+import org.eclipse.core.commands.AbstractHandler; >+import org.eclipse.core.commands.ExecutionEvent; >+import org.eclipse.core.commands.IHandler; > import org.eclipse.core.runtime.Assert; > import org.eclipse.core.runtime.CoreException; > import org.eclipse.core.runtime.IProgressMonitor; >@@ -37,26 +32,23 @@ > import org.eclipse.core.runtime.Status; > import org.eclipse.core.runtime.SubProgressMonitor; > import org.eclipse.core.runtime.jobs.Job; >- >-import org.eclipse.core.commands.AbstractHandler; >-import org.eclipse.core.commands.ExecutionEvent; >-import org.eclipse.core.commands.IHandler; >- >+import org.eclipse.jface.action.Action; >+import org.eclipse.jface.action.ActionContributionItem; >+import org.eclipse.jface.action.IAction; >+import org.eclipse.jface.action.IMenuListener; >+import org.eclipse.jface.action.IMenuManager; >+import org.eclipse.jface.action.LegacyActionTools; >+import org.eclipse.jface.action.MenuManager; >+import org.eclipse.jface.dialogs.IDialogSettings; >+import org.eclipse.jface.viewers.*; >+import org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider.IStyledLabelProvider; >+import org.eclipse.osgi.util.NLS; > import org.eclipse.swt.SWT; > import org.eclipse.swt.accessibility.AccessibleAdapter; > import org.eclipse.swt.accessibility.AccessibleEvent; > import org.eclipse.swt.custom.CLabel; > import org.eclipse.swt.custom.ViewForm; >-import org.eclipse.swt.events.KeyAdapter; >-import org.eclipse.swt.events.KeyEvent; >-import org.eclipse.swt.events.ModifyEvent; >-import org.eclipse.swt.events.ModifyListener; >-import org.eclipse.swt.events.MouseAdapter; >-import org.eclipse.swt.events.MouseEvent; >-import org.eclipse.swt.events.SelectionAdapter; >-import org.eclipse.swt.events.SelectionEvent; >-import org.eclipse.swt.events.TraverseEvent; >-import org.eclipse.swt.events.TraverseListener; >+import org.eclipse.swt.events.*; > import org.eclipse.swt.graphics.Color; > import org.eclipse.swt.graphics.Font; > import org.eclipse.swt.graphics.GC; >@@ -65,46 +57,7 @@ > import org.eclipse.swt.graphics.Rectangle; > 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.Event; >-import org.eclipse.swt.widgets.Label; >-import org.eclipse.swt.widgets.Menu; >-import org.eclipse.swt.widgets.Shell; >-import org.eclipse.swt.widgets.Text; >-import org.eclipse.swt.widgets.ToolBar; >-import org.eclipse.swt.widgets.ToolItem; >- >-import org.eclipse.jface.action.Action; >-import org.eclipse.jface.action.ActionContributionItem; >-import org.eclipse.jface.action.IAction; >-import org.eclipse.jface.action.IMenuListener; >-import org.eclipse.jface.action.IMenuManager; >-import org.eclipse.jface.action.LegacyActionTools; >-import org.eclipse.jface.action.MenuManager; >-import org.eclipse.jface.dialogs.IDialogSettings; >-import org.eclipse.jface.viewers.ContentViewer; >-import org.eclipse.jface.viewers.DoubleClickEvent; >-import org.eclipse.jface.viewers.IColorProvider; >-import org.eclipse.jface.viewers.IContentProvider; >-import org.eclipse.jface.viewers.IDoubleClickListener; >-import org.eclipse.jface.viewers.IFontProvider; >-import org.eclipse.jface.viewers.ILabelDecorator; >-import org.eclipse.jface.viewers.ILabelProvider; >-import org.eclipse.jface.viewers.ILabelProviderListener; >-import org.eclipse.jface.viewers.ILazyContentProvider; >-import org.eclipse.jface.viewers.ISelection; >-import org.eclipse.jface.viewers.ISelectionChangedListener; >-import org.eclipse.jface.viewers.IStructuredContentProvider; >-import org.eclipse.jface.viewers.LabelProvider; >-import org.eclipse.jface.viewers.LabelProviderChangedEvent; >-import org.eclipse.jface.viewers.SelectionChangedEvent; >-import org.eclipse.jface.viewers.StructuredSelection; >-import org.eclipse.jface.viewers.TableViewer; >-import org.eclipse.jface.viewers.Viewer; >-import org.eclipse.jface.viewers.ViewerFilter; >- >+import org.eclipse.swt.widgets.*; > import org.eclipse.ui.ActiveShellExpression; > import org.eclipse.ui.IMemento; > import org.eclipse.ui.PlatformUI; >@@ -119,8 +72,6 @@ > import org.eclipse.ui.progress.UIJob; > import org.eclipse.ui.statushandlers.StatusManager; > >-import org.eclipse.osgi.util.NLS; >- > /** > * Shows a list of items to the user with a text entry field for a string > * pattern used to filter the list of items. >@@ -1520,8 +1471,7 @@ > } > } > >- private class ItemsListLabelProvider extends LabelProvider implements >- IColorProvider, IFontProvider, ILabelProviderListener { >+ private class ItemsListLabelProvider extends StyledCellLabelProvider implements ILabelProviderListener { > private ILabelProvider provider; > > private ILabelDecorator selectionDecorator; >@@ -1606,12 +1556,7 @@ > return provider; > } > >- /* >- * (non-Javadoc) >- * >- * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object) >- */ >- public Image getImage(Object element) { >+ private Image getImage(Object element) { > if (element instanceof ItemsListSeparator) { > return WorkbenchImages > .getImage(IWorkbenchGraphicConstants.IMG_OBJ_SEPARATOR); >@@ -1620,12 +1565,52 @@ > return provider.getImage(element); > } > >- /* >- * (non-Javadoc) >- * >- * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object) >- */ >- public String getText(Object element) { >+ private StyledString getStyledString(Object element, IStyledLabelProvider provider) { >+ StyledString string= provider.getStyledText(element); >+ >+ if (selectionDecorator != null && isSelected(element)) { >+ String decorated= selectionDecorator.decorateText(string.getString(), element); >+ return new StyledString(decorated); // no need to add colors when element is selected >+ } >+ return string; >+ } >+ >+ public void update(ViewerCell cell) { >+ Object element= cell.getElement(); >+ >+ if (!(element instanceof ItemsListSeparator) && provider instanceof IStyledLabelProvider) { >+ IStyledLabelProvider styledLabelProvider= (IStyledLabelProvider) provider; >+ StyledString styledString= getStyledString(element, styledLabelProvider); >+ >+ cell.setText(styledString.getString()); >+ cell.setStyleRanges(styledString.getStyleRanges()); >+ cell.setImage(styledLabelProvider.getImage(element)); >+ } else { >+ cell.setText(getText(element)); >+ cell.setImage(getImage(element)); >+ } >+ cell.setFont(getFont(element)); >+ cell.setForeground(getForeground(element)); >+ cell.setBackground(getBackground(element)); >+ >+ super.update(cell); >+ } >+ >+ private boolean isSelected(Object element) { >+ int[] selectionIndices = list.getTable().getSelectionIndices(); >+ List elements = Arrays >+ .asList(contentProvider.getElements(null)); >+ for (int i = 0; i < selectionIndices.length; i++) { >+ if (elements.size() > selectionIndices[i] >+ && element >+ .equals(elements.get(selectionIndices[i]))) { >+ return true; >+ } >+ } >+ return false; >+ } >+ >+ private String getText(Object element) { > if (element instanceof ItemsListSeparator) { > return getSeparatorLabel(((ItemsListSeparator) element) > .getName()); >@@ -1633,23 +1618,8 @@ > > String str = provider.getText(element); > >- if (selectionDecorator != null && element != null) { >- >- // ((StructuredSelection)list.getSelection()).toList().contains(element)) >- // cannot be used - virtual tables produce cycles in >- // update item - get selection invocation scenarios >- >- int[] selectionIndices = list.getTable().getSelectionIndices(); >- List elements = Arrays >- .asList(contentProvider.getElements(null)); >- for (int i = 0; i < selectionIndices.length; i++) { >- if (elements.size() > selectionIndices[i] >- && element >- .equals(elements.get(selectionIndices[i]))) { >- str = selectionDecorator.decorateText(str, element); >- break; >- } >- } >+ if (selectionDecorator != null && element != null && isSelected(element)) { >+ str = selectionDecorator.decorateText(str, element); > } > return str; > } >@@ -1794,6 +1764,8 @@ > ((ILabelProviderListener) l[i]).labelProviderChanged(event); > } > } >+ >+ > } > > /** >#P org.eclipse.jface >Index: src/org/eclipse/jface/viewers/ViewerRow.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerRow.java,v >retrieving revision 1.20 >diff -u -r1.20 ViewerRow.java >--- src/org/eclipse/jface/viewers/ViewerRow.java 1 Feb 2008 15:18:01 -0000 1.20 >+++ src/org/eclipse/jface/viewers/ViewerRow.java 21 Feb 2008 15:47:17 -0000 >@@ -322,4 +322,19 @@ > public Rectangle getTextBounds(int index) { > return null; > } >+ >+ >+ /** >+ * Returns the location and bounds of the area where the image is drawn. >+ * >+ * @param index >+ * the column index >+ * @return the bounds of the of the image area. May return <code>null</code> >+ * if the underlying widget implementation doesn't provide this >+ * information >+ * @since 3.4 >+ */ >+ public Rectangle getImageBounds(int index) { >+ return null; >+ } > } >Index: src/org/eclipse/jface/viewers/SimpleStyledCellLabelProvider.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/viewers/SimpleStyledCellLabelProvider.java,v >retrieving revision 1.5 >diff -u -r1.5 SimpleStyledCellLabelProvider.java >--- src/org/eclipse/jface/viewers/SimpleStyledCellLabelProvider.java 3 Feb 2008 20:09:59 -0000 1.5 >+++ src/org/eclipse/jface/viewers/SimpleStyledCellLabelProvider.java 21 Feb 2008 15:47:17 -0000 >@@ -50,6 +50,9 @@ > * <p><strong>NOTE:</strong> This API is experimental and may be deleted or > * changed before 3.4 is released.</p> > * >+ * >+ * @deprecated Will be removed before 3.4 M6. Use {@link StyledCellLabelProvider} instead. >+ * > * @since 3.4 > */ > public abstract class SimpleStyledCellLabelProvider extends >Index: src/org/eclipse/jface/viewers/TableViewerRow.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/viewers/TableViewerRow.java,v >retrieving revision 1.13 >diff -u -r1.13 TableViewerRow.java >--- src/org/eclipse/jface/viewers/TableViewerRow.java 1 Feb 2008 15:18:01 -0000 1.13 >+++ src/org/eclipse/jface/viewers/TableViewerRow.java 21 Feb 2008 15:47:17 -0000 >@@ -221,6 +221,13 @@ > public Rectangle getTextBounds(int index) { > return item.getTextBounds(index); > } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.ViewerRow#getImageBounds(int) >+ */ >+ public Rectangle getImageBounds(int index) { >+ return item.getImageBounds(index); >+ } > > private boolean hasColumns() { > return this.item.getParent().getColumnCount() != 0; >Index: src/org/eclipse/jface/viewers/ViewerCell.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerCell.java,v >retrieving revision 1.17 >diff -u -r1.17 ViewerCell.java >--- src/org/eclipse/jface/viewers/ViewerCell.java 20 Feb 2008 20:48:37 -0000 1.17 >+++ src/org/eclipse/jface/viewers/ViewerCell.java 21 Feb 2008 15:47:17 -0000 >@@ -13,6 +13,7 @@ > > package org.eclipse.jface.viewers; > >+import org.eclipse.swt.custom.StyleRange; > import org.eclipse.swt.graphics.Color; > import org.eclipse.swt.graphics.Font; > import org.eclipse.swt.graphics.Image; >@@ -54,6 +55,9 @@ > */ > public static int RIGHT = 1 << 3; > >+ >+ private static final String KEY_TEXT_LAYOUT = "styled_label_key_"; //$NON-NLS-1$ >+ > /** > * Create a new instance of the receiver on the row. > * >@@ -168,6 +172,31 @@ > row.setImage(columnIndex, image); > > } >+ >+ /** >+ * Set the style ranges to be applied on the text label >+ * Note: Requires {@link StyledCellLabelProvider} with owner draw enabled. >+ * >+ * @param styleRanges the styled ranges >+ * >+ * @since 3.4 >+ */ >+ public void setStyleRanges(StyleRange[] styleRanges) { >+ getItem().setData(KEY_TEXT_LAYOUT + columnIndex, styleRanges); >+ } >+ >+ >+ /** >+ * Returns the style ranges to be applied on the text label or <code>null</code> if no >+ * style ranges have been set. >+ * >+ * @return styleRanges the styled ranges >+ * >+ * @since 3.4 >+ */ >+ public StyleRange[] getStyleRanges() { >+ return (StyleRange[]) getItem().getData(KEY_TEXT_LAYOUT + columnIndex); >+ } > > /** > * Set the columnIndex. >@@ -302,7 +331,52 @@ > public Rectangle getTextBounds() { > return row.getTextBounds(columnIndex); > } >+ >+ /** >+ * Returns the location and bounds of the area where the image is drawn >+ * >+ * @return The bounds of the of the image area. May return <code>null</code> >+ * if the underlying widget implementation doesn't provide this >+ * information >+ * @since 3.4 >+ */ >+ public Rectangle getImageBounds() { >+ return row.getImageBounds(columnIndex); >+ } > >+ /** >+ * Gets the foreground color of the cell. >+ * >+ * @return the foreground of the cell or <code>null</code> for the default foreground >+ * >+ * @since 3.4 >+ */ >+ public Color getForeground() { >+ return row.getForeground(columnIndex); >+ } >+ >+ /** >+ * Gets the background color of the cell. >+ * >+ * @return the background of the cell or <code>null</code> for the default background >+ * >+ * @since 3.4 >+ */ >+ public Color getBackground() { >+ return row.getBackground(columnIndex); >+ } >+ >+ /** >+ * Gets the font of the cell. >+ * >+ * @return the font of the cell or <code>null</code> for the default font >+ * >+ * @since 3.4 >+ */ >+ public Font getFont() { >+ return row.getFont(columnIndex); >+ } >+ > /* > * (non-Javadoc) > * >Index: src/org/eclipse/jface/viewers/TreeViewerRow.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/viewers/TreeViewerRow.java,v >retrieving revision 1.15 >diff -u -r1.15 TreeViewerRow.java >--- src/org/eclipse/jface/viewers/TreeViewerRow.java 1 Feb 2008 15:18:01 -0000 1.15 >+++ src/org/eclipse/jface/viewers/TreeViewerRow.java 21 Feb 2008 15:47:17 -0000 >@@ -375,6 +375,13 @@ > return item.getTextBounds(index); > } > >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.ViewerRow#getImageBounds(int) >+ */ >+ public Rectangle getImageBounds(int index) { >+ return item.getImageBounds(index); >+ } >+ > private boolean hasColumns() { > return this.item.getParent().getColumnCount() != 0; > } >Index: src/org/eclipse/jface/preference/JFacePreferences.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/preference/JFacePreferences.java,v >retrieving revision 1.12 >diff -u -r1.12 JFacePreferences.java >--- src/org/eclipse/jface/preference/JFacePreferences.java 16 Mar 2007 18:01:01 -0000 1.12 >+++ src/org/eclipse/jface/preference/JFacePreferences.java 21 Feb 2008 15:47:17 -0000 >@@ -31,6 +31,30 @@ > * Identifier for the Active Hyperlink Colour > */ > public static final String ACTIVE_HYPERLINK_COLOR = "ACTIVE_HYPERLINK_COLOR"; //$NON-NLS-1$ >+ >+ /** >+ * Identifier for the color used to show extra informations in labels, as a qualified name. >+ * For example in 'Foo.txt - myproject/bar', the qualifier is '- myproject/bar'. >+ * >+ * @since 3.4 >+ */ >+ public static final String QUALIFIER_COLOR= "QUALIFIER_COLOR"; //$NON-NLS-1$ >+ >+ /** >+ * Identifier for the color used to show label decorations >+ * For example in 'Foo.txt [1.16]', the decoration is '[1.16]'. >+ * >+ * @since 3.4 >+ */ >+ public static final String DECORATIONS_COLOR= "DECORATIONS_COLOR"; //$NON-NLS-1$ >+ >+ /** >+ * Identifier for the color used to counter informations >+ * For example in 'Foo.txt (2 matches)', the counter information is '(2 matches)'. >+ * >+ * @since 3.4 >+ */ >+ public static final String COUNTER_COLOR= "COUNTER_COLOR"; //$NON-NLS-1$ > > private static IPreferenceStore preferenceStore; > >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,462 @@ >+/******************************************************************************* >+ * 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.StyledString.Style; >+import org.eclipse.swt.graphics.Color; >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.Image; >+ >+ >+/** >+ * A {@link DecoratingStyledCellLabelProvider} is a {@link StyledCellLabelProvider} >+ * that uses a nested {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider} >+ * to compute styled text label and image. >+ * Optionally, it can also take a {@link ILabelDecorator} to decorate the label. >+ * >+ * <p>Use this label provider to add styled ranges to existing label providers: >+ * Existing label providers can be enhanced by implementing >+ * {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider}, and are then able of being >+ * used in viewer with styled labels.</p> >+ * >+ * <p>The {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider} can >+ * optionally implement {@link IColorProvider} and {@link IFontProvider} to provide >+ * foreground and background color and a default font. >+ * </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 StyledCellLabelProvider { >+ >+ /** >+ * Interface marking a label provider that provides styled text labels and images. >+ * <p>The {@link DecoratingStyledCellLabelProvider.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 StyledString 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; >+ >+ 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 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) { >+ if (labelProvider == null) >+ throw new IllegalArgumentException("Label provider must not be null"); //$NON-NLS-1$ >+ >+ this.styledLabelProvider= labelProvider; >+ this.decorator= decorator; >+ this.decorationContext= decorationContext; >+ if (decorator != null && decorationContext == null) { >+ decorationContext= createDefaultDecorationContext(); >+ } >+ labelProviderListener= new ILabelProviderListener() { >+ public void labelProviderChanged(LabelProviderChangedEvent event) { >+ fireLabelProviderChanged(event); >+ } >+ }; >+ labelProvider.addListener(labelProviderListener); >+ if (decorator != null) >+ decorator.addListener(labelProviderListener); >+ } >+ >+ /** >+ * Creates a {@link DecoratingStyledCellLabelProvider} that delegates the requests for >+ * styled labels and for images to a {@link IStyledLabelProvider}. >+ * >+ * @param labelProvider the styled label provider >+ */ >+ public DecoratingStyledCellLabelProvider(IStyledLabelProvider labelProvider) { >+ this(labelProvider, null, null); >+ } >+ >+ 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 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 (decorator == null) >+ return false; >+ >+ Object element= cell.getElement(); >+ String oldText= cell.getText(); >+ >+ boolean isDecorationPending= false; >+ if (decorator instanceof LabelDecorator) { >+ isDecorationPending= !((LabelDecorator) decorator).prepareDecoration(element, oldText, getDecorationContext()); >+ } else if (decorator instanceof IDelayedLabelDecorator) { >+ isDecorationPending= !((IDelayedLabelDecorator) 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 >+ } >+ >+ Object element= cell.getElement(); >+ >+ StyledString styledString= getStyledText(element); >+ cell.setText(styledString.getString()); >+ >+ if (isOwnerDrawEnabled()) { >+ cell.setStyleRanges(styledString.getStyleRanges()); >+ } 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 (decorator instanceof IColorDecorator) { >+ Color foreground= ((IColorDecorator) decorator).decorateForeground(element); >+ if (foreground != null) >+ return foreground; >+ } >+ if (styledLabelProvider instanceof IColorProvider) { >+ return ((IColorProvider) 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 (decorator instanceof IColorDecorator) { >+ Color color= ((IColorDecorator) decorator).decorateBackground(element); >+ if (color != null) >+ return color; >+ } >+ if (styledLabelProvider instanceof IColorProvider) { >+ return ((IColorProvider) 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 (decorator instanceof IFontDecorator) { >+ Font font= ((IFontDecorator) decorator).decorateFont(element); >+ if (font != null) >+ return font; >+ } >+ if (styledLabelProvider instanceof IFontProvider) { >+ return ((IFontProvider) 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) { >+ Image image= styledLabelProvider.getImage(element); >+ if (decorator == null) { >+ return image; >+ } >+ >+ Image decorated= null; >+ if (decorator instanceof LabelDecorator) { >+ decorated= ((LabelDecorator) decorator).decorateImage(image, element, getDecorationContext()); >+ } else { >+ decorated= 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 StyledString getStyledText(Object element) { >+ StyledString string= styledLabelProvider.getStyledText(element); >+ if (decorator == null) { >+ return string; >+ } >+ >+ String label= string.getString(); >+ String decorated; >+ if (decorator instanceof LabelDecorator) { >+ decorated= ((LabelDecorator) decorator).decorateText(label, element, getDecorationContext()); >+ } else { >+ decorated= decorator.decorateText(label, element); >+ } >+ if (decorated == null) >+ return string; >+ >+ int originalStart= decorated.indexOf(label); >+ if (originalStart == -1) { >+ return new StyledString(decorated); // the decorator did something wild >+ } >+ >+ if (decorated.length() == label.length()) >+ return string; >+ >+ Style style= getDecorationStyle(element); >+ if (originalStart > 0) { >+ StyledString newString= new StyledString(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 StyledString.Style} to be used for string decorations. By default >+ * the {@link StyledString#DECORATIONS_STYLE 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 Style getDecorationStyle(Object element) { >+ return StyledString.DECORATIONS_STYLE; >+ } >+ >+ /** >+ * Returns the styled string provider. >+ * >+ * @return the wrapped label provider >+ */ >+ public IStyledLabelProvider getStyledStringProvider() { >+ return styledLabelProvider; >+ } >+ >+ /** >+ * Sets the styled label provider. Removes all known listeners from the old >+ * provider, and adds all known listeners to the new provider. The old >+ * provider 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 newLabelProvider the new styled label provider >+ */ >+ public void setStyledLabelProvider(IStyledLabelProvider newLabelProvider) { >+ if (newLabelProvider == null) >+ throw new IllegalArgumentException(); >+ >+ IStyledLabelProvider oldProvider= this.styledLabelProvider; >+ if (newLabelProvider != oldProvider) { >+ oldProvider.removeListener(labelProviderListener); >+ this.styledLabelProvider= newLabelProvider; >+ newLabelProvider.addListener(labelProviderListener); >+ >+ } >+ fireLabelProviderChanged(new LabelProviderChangedEvent(this)); >+ } >+ >+ /** >+ * 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 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(labelProviderListener); >+ this.decorator= newDecorator; >+ if (newDecorator != null) { >+ newDecorator.addListener(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); >+ styledLabelProvider.addListener(listener); >+ if (decorator != null) { >+ decorator.addListener(listener); >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) >+ */ >+ public void removeListener(ILabelProviderListener listener) { >+ super.removeListener(listener); >+ styledLabelProvider.removeListener(listener); >+ if (decorator != null) { >+ decorator.removeListener(listener); >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String) >+ */ >+ public boolean isLabelProperty(Object element, String property) { >+ if (styledLabelProvider.isLabelProperty(element, property)) { >+ return true; >+ } >+ return decorator != null && decorator.isLabelProperty(element, property); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.StyledCellLabelProvider#dispose() >+ */ >+ public void dispose() { >+ super.dispose(); >+ if (decorationContext != null) { >+ Object manager = decorationContext.getProperty(DecorationContext.RESOURCE_MANAGER_KEY); >+ if (manager instanceof ResourceManager) >+ ((ResourceManager) manager).dispose(); >+ } >+ styledLabelProvider.removeListener(labelProviderListener); >+ styledLabelProvider.dispose(); >+ if (decorator != null) { >+ decorator.removeListener(labelProviderListener); >+ decorator.dispose(); >+ } >+ } >+ >+} >Index: src/org/eclipse/jface/viewers/StyledString.java >=================================================================== >RCS file: src/org/eclipse/jface/viewers/StyledString.java >diff -N src/org/eclipse/jface/viewers/StyledString.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/viewers/StyledString.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,423 @@ >+/******************************************************************************* >+ * 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 java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.jface.preference.JFacePreferences; >+import org.eclipse.jface.resource.ColorRegistry; >+import org.eclipse.jface.resource.JFaceResources; >+import org.eclipse.swt.custom.StyleRange; >+import org.eclipse.swt.graphics.TextStyle; >+ >+/** >+ * Represents a string with styled ranges. All ranges mark substrings of the string and do not overlap. >+ * Styles are represented by {@link Style}. >+ * >+ * The styled string can be modified: >+ * <ul> >+ * <li>new strings with styles can be appended</li> >+ * <li>styles can by applied to to the existing string</li> >+ * </ul> >+ * >+ * <p> >+ * This class may be instantiated; it is not intended to be subclassed. >+ * </p> >+ * @since 3.4 >+ */ >+public class StyledString { >+ >+ /** >+ * Represents a style that can be associated to one ore more ranges in the {@link StyledString} >+ * >+ */ >+ public static abstract class Style { >+ >+ /** >+ * Applies the styles represented by this object to the given textStyle. >+ * >+ * @param textStyle the {@link TextStyle} to modify >+ */ >+ public abstract void applyStyles(TextStyle textStyle); >+ } >+ >+ >+ /** >+ * A built-in style using the {@link JFacePreferences#QUALIFIER_COLOR} >+ * managed in the JFace color registry (See {@link JFaceResources#getColorRegistry()}). >+ */ >+ public static final Style QUALIFIER_STYLE= createColorRegistryStyle(JFacePreferences.QUALIFIER_COLOR, null); >+ >+ /** >+ * A built-in style using the {@link JFacePreferences#COUNTER_COLOR} >+ * managed in the JFace color registry (See {@link JFaceResources#getColorRegistry()}). >+ */ >+ public static final Style COUNTER_STYLE= createColorRegistryStyle(JFacePreferences.COUNTER_COLOR, null); >+ >+ /** >+ * A built-in style using the {@link JFacePreferences#DECORATIONS_COLOR} >+ * managed in the JFace color registry (See {@link JFaceResources#getColorRegistry()}). >+ */ >+ public static final Style DECORATIONS_STYLE= createColorRegistryStyle(JFacePreferences.DECORATIONS_COLOR, null); >+ >+ /** >+ * Creates a style that takes the given foreground and background colors from the JFace color registry. >+ * >+ * @param foregroundColorName the color name for the foreground color >+ * @param backgroundColorName the color name for the background color >+ * >+ * @return the created style >+ */ >+ public static Style createColorRegistryStyle(String foregroundColorName, String backgroundColorName) { >+ return new DefaultStyle(foregroundColorName, backgroundColorName); >+ } >+ >+ private static final StyleRange[] EMPTY= new StyleRange[0]; >+ private StringBuffer fBuffer; >+ private StyleRunList fStyleRuns; >+ >+ /** >+ * Creates an empty {@link StyledString}. >+ */ >+ public StyledString() { >+ fBuffer= new StringBuffer(); >+ fStyleRuns= null; >+ } >+ >+ /** >+ * Creates an {@link StyledString} initialized with a string without a style associated. >+ * >+ * @param string the string >+ */ >+ public StyledString(String string) { >+ this(string, null); >+ } >+ >+ /** >+ * Creates an {@link StyledString} initialized with a string and a style. >+ * >+ * @param string the string >+ * @param style the style of the text or <code>null</code> to not associated a style. >+ */ >+ public StyledString(String string, Style style) { >+ this(); >+ append(string, style); >+ } >+ >+ /** >+ * Returns the string of this {@link StyledString}. >+ * >+ * @return the current string of this {@link StyledString}. >+ */ >+ public String getString() { >+ return fBuffer.toString(); >+ } >+ >+ /** >+ * Returns the length of the string of this {@link StyledString}. >+ * >+ * @return the length of the current string >+ */ >+ public int length() { >+ return fBuffer.length(); >+ } >+ >+ /** >+ * Appends a string to the {@link StyledString}. The appended string will have no style associated. >+ * >+ * @param string the string to append. >+ * @return returns a reference to this object. >+ */ >+ public StyledString append(String string) { >+ return append(string, null); >+ } >+ >+ /** >+ * Appends a character to the {@link StyledString}. The appended character will have no style associated. >+ * >+ * @param ch the character to append. >+ * @return returns a reference to this object. >+ */ >+ public StyledString append(char ch) { >+ return append(String.valueOf(ch), null); >+ } >+ >+ /** >+ * Appends a string with styles to the {@link StyledString}. >+ * >+ * @param string the string to append. >+ * @return returns a reference to this object. >+ */ >+ public StyledString append(StyledString string) { >+ if (string.length() == 0) { >+ return this; >+ } >+ >+ int offset= fBuffer.length(); >+ fBuffer.append(string.getString()); >+ >+ List otherRuns= string.fStyleRuns; >+ if (otherRuns != null && !otherRuns.isEmpty()) { >+ for (int i= 0; i < otherRuns.size(); i++) { >+ StyleRun curr= (StyleRun) otherRuns.get(i); >+ if (i == 0 && curr.offset != 0) { >+ appendStyleRun(null, offset); // appended string will start with the default color >+ } >+ appendStyleRun(curr.style, offset + curr.offset); >+ } >+ } else { >+ appendStyleRun(null, offset); // appended string will start with the default color >+ } >+ return this; >+ } >+ >+ /** >+ * Appends a character with a style to the {@link StyledString}. The appended character will >+ * have the given style associated. >+ * >+ * @param ch the character to append. >+ * @param style the style to of the character to append or <code>null</code> if no style should be >+ * associated to the string. >+ * @return returns a reference to this object. >+ */ >+ public StyledString append(char ch, Style style) { >+ return append(String.valueOf(ch), style); >+ } >+ >+ /** >+ * Appends a string with a style to the {@link StyledString}. The appended string will >+ * have the given style associated. >+ * >+ * @param string the string to append. >+ * @param style the style to of the string to append or <code>null</code> if no style should be >+ * associated to the string. >+ * @return returns a reference to this object. >+ */ >+ public StyledString append(String string, Style style) { >+ if (string.length() == 0) >+ return this; >+ >+ int offset= fBuffer.length(); // the length before appending >+ fBuffer.append(string); >+ appendStyleRun(style, offset); >+ return this; >+ } >+ >+ /** >+ * Sets a style to the given source range. The range must be subrange of actual string of this {@link StyledString}. >+ * Styles previously set for that range will be overwritten. >+ * >+ * @param offset the start offset of the range >+ * @param length the length of the range >+ * @param style the style to set >+ * >+ * @throws StringIndexOutOfBoundsException if <code>start</code> is >+ * less than zero, or if offset plus length is greater than the length of this object. >+ */ >+ public void setStyle(int offset, int length, Style style) { >+ if (offset < 0 || offset + length > fBuffer.length()) { >+ throw new StringIndexOutOfBoundsException("Invalid offset (" + offset + ") or length (" + length + ")"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ >+ } >+ if (length == 0) { >+ return; >+ } >+ if (!hasRuns() || getLastRun().offset <= offset) { >+ appendStyleRun(style, offset); >+ if (offset + length != fBuffer.length()) { >+ appendStyleRun(null, offset + length); >+ } >+ return; >+ } >+ >+ int endRun= findRun(offset + length); >+ if (endRun >= 0) { >+ // run with the same end index, nothing to change >+ } else { >+ endRun= -(endRun + 1); >+ if (offset + length < fBuffer.length()) { >+ Style prevStyle= endRun > 0 ? fStyleRuns.getRun(endRun - 1).style : null; >+ fStyleRuns.add(endRun, new StyleRun(offset + length, prevStyle)); >+ } >+ } >+ >+ int startRun= findRun(offset); >+ if (startRun >= 0) { >+ // run with the same start index >+ StyleRun styleRun= fStyleRuns.getRun(startRun); >+ styleRun.style= style; >+ } else { >+ startRun= -(startRun + 1); >+ >+ Style prevStyle= startRun > 0 ? fStyleRuns.getRun(startRun - 1).style : null; >+ if (isDifferentStyle(prevStyle, style) || (startRun == 0 && style != null)) { >+ fStyleRuns.add(startRun, new StyleRun(offset, style)); >+ endRun++; // endrun is moved one back >+ } else { >+ startRun--; // we use the previous >+ } >+ } >+ if (startRun + 1 < endRun) { >+ fStyleRuns.removeRange(startRun + 1, endRun); >+ } >+ } >+ >+ /** >+ * Returns {@link StyleRange} for all applied styles to this string >+ * >+ * @return an array of all {@link StyleRange} applied to this string. >+ */ >+ public StyleRange[] getStyleRanges() { >+ if (hasRuns()) { >+ ArrayList res= new ArrayList(); >+ >+ List styleRuns= getStyleRuns(); >+ int offset= 0; >+ Style style= null; >+ for (int i= 0; i < styleRuns.size(); i++) { >+ StyleRun curr= (StyleRun) styleRuns.get(i); >+ if (isDifferentStyle(curr.style, style)) { >+ if (curr.offset > offset && style != null) { >+ res.add(createStyleRange(offset, curr.offset, style)); >+ } >+ offset= curr.offset; >+ style= curr.style; >+ } >+ } >+ if (fBuffer.length() > offset && style != null) { >+ res.add(createStyleRange(offset, fBuffer.length(), style)); >+ } >+ return (StyleRange[]) res.toArray(new StyleRange[res.size()]); >+ } >+ return EMPTY; >+ } >+ >+ private int findRun(int offset) { >+ // method assumes that fStyleRuns is not null >+ int low= 0; >+ int high= fStyleRuns.size() - 1; >+ while (low <= high) { >+ int mid = (low + high) / 2; >+ StyleRun styleRun= fStyleRuns.getRun(mid); >+ if (styleRun.offset < offset) { >+ low = mid + 1; >+ } else if (styleRun.offset > offset) { >+ high = mid - 1; >+ } else { >+ return mid; // key found >+ } >+ } >+ return -(low + 1); // key not found. >+ } >+ >+ private StyleRange createStyleRange(int start, int end, Style style) { >+ StyleRange styleRange= new StyleRange(); >+ styleRange.start= start; >+ styleRange.length= end - start; >+ style.applyStyles(styleRange); >+ return styleRange; >+ } >+ >+ >+ /* (non-Javadoc) >+ * @see java.lang.Object#toString() >+ */ >+ public String toString() { >+ return fBuffer.toString(); >+ } >+ >+ private boolean hasRuns() { >+ return fStyleRuns != null && !fStyleRuns.isEmpty(); >+ } >+ >+ private void appendStyleRun(Style style, int offset) { >+ StyleRun lastRun= getLastRun(); >+ if (lastRun != null && lastRun.offset == offset) { >+ lastRun.style= style; >+ return; >+ } >+ >+ if (lastRun == null && style != null || lastRun != null && isDifferentStyle(style, lastRun.style)) { >+ getStyleRuns().add(new StyleRun(offset, style)); >+ } >+ } >+ >+ private boolean isDifferentStyle(Style style1, Style style2) { >+ if (style1 == null) { >+ return style2 != null; >+ } >+ return !style1.equals(style2); >+ } >+ >+ private StyleRun getLastRun() { >+ if (fStyleRuns == null || fStyleRuns.isEmpty()) { >+ return null; >+ } >+ return fStyleRuns.getRun(fStyleRuns.size() - 1); >+ } >+ >+ private List getStyleRuns() { >+ if (fStyleRuns == null) >+ fStyleRuns= new StyleRunList(); >+ return fStyleRuns; >+ } >+ >+ private static class StyleRun { >+ public int offset; >+ public Style style; >+ >+ public StyleRun(int offset, Style style) { >+ this.offset= offset; >+ this.style= style; >+ } >+ >+ public String toString() { >+ return "Offset " + offset + ", style: " + style.toString(); //$NON-NLS-1$//$NON-NLS-2$ >+ } >+ } >+ >+ private static class StyleRunList extends ArrayList { >+ private static final long serialVersionUID= 123L; >+ >+ public StyleRunList() { >+ super(3); >+ } >+ >+ public StyleRun getRun(int index) { >+ return (StyleRun) get(index); >+ } >+ >+ public void removeRange(int fromIndex, int toIndex) { >+ super.removeRange(fromIndex, toIndex); >+ } >+ } >+ >+ private static class DefaultStyle extends Style { >+ private final String fForegroundColorName; >+ private final String fBackgroundColorName; >+ >+ public DefaultStyle(String foregroundColorName, String backgroundColorName) { >+ fForegroundColorName= foregroundColorName; >+ fBackgroundColorName= backgroundColorName; >+ } >+ >+ public void applyStyles(TextStyle textStyle) { >+ ColorRegistry colorRegistry= JFaceResources.getColorRegistry(); >+ if (fForegroundColorName != null) { >+ textStyle.foreground= colorRegistry.get(fForegroundColorName); >+ } >+ if (fBackgroundColorName != null) { >+ textStyle.background= colorRegistry.get(fBackgroundColorName); >+ } >+ } >+ } >+ >+} >Index: src/org/eclipse/jface/viewers/StyledCellLabelProvider.java >=================================================================== >RCS file: src/org/eclipse/jface/viewers/StyledCellLabelProvider.java >diff -N src/org/eclipse/jface/viewers/StyledCellLabelProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/viewers/StyledCellLabelProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,377 @@ >+/******************************************************************************* >+ * 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.viewers; >+ >+import org.eclipse.core.runtime.Assert; >+import org.eclipse.jface.window.Window; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.custom.StyleRange; >+import org.eclipse.swt.graphics.Color; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Rectangle; >+import org.eclipse.swt.graphics.TextLayout; >+import org.eclipse.swt.widgets.Display; >+import org.eclipse.swt.widgets.Event; >+ >+/** >+ * A {@link StyledCellLabelProvider} supports styled labels by using owner >+ * draw. >+ * Besides the styles in labels, the label provider preserves native viewer behavior: >+ * <ul> >+ * <li>similar image and label positioning</li> >+ * <li>native drawing of focus and selection</li> >+ * </ul> >+ * <p> >+ * For providing the label's styles, create a subclass and overwrite >+ * {@link StyledCellLabelProvider#update(ViewerCell)} to >+ * return set all information needed to render a element. Use >+ * {@link ViewerCell#setStyleRanges(StyleRange[])} to set style ranges >+ * on the label. >+ * </p> >+ * <p> >+ * The current version of the {@link StyledCellLabelProvider} will ignore all font settings on >+ * {@link StyleRange}. Different fonts would make labels wider, and the native >+ * selection drawing could not be reused. >+ * </p> >+ * >+ * <p><strong>NOTE:</strong> This API is experimental and may be deleted or >+ * changed before 3.4 is released.</p> >+ * >+ * @since 3.4 >+ */ >+public abstract class StyledCellLabelProvider extends OwnerDrawLabelProvider { >+ >+ /** >+ * Style constant for indicating that the styled colors are to be applied >+ * even it the viewer's item is selected. Default is not to apply colors. >+ */ >+ public static final int COLORS_ON_SELECTION = 1 << 0; >+ >+ /** >+ * Style constant for indicating to draw the focus if requested by the owner >+ * draw event. Default is to draw the focus. >+ */ >+ public static final int NO_FOCUS = 1 << 1; >+ >+ /** >+ * Private constant to indicate if owner draw is enabled for the >+ * label provider's column. >+ */ >+ private static final int OWNER_DRAW_ENABLED = 1 << 4; >+ >+ private int style; >+ >+ // reused text layout >+ private TextLayout cachedTextLayout; >+ >+ private ColumnViewer viewer; >+ private ViewerColumn column; >+ >+ /** >+ * Creates a new StyledCellLabelProvider. By default, owner draw is enabled, focus is drawn and no >+ * colors are painted on selected elements. >+ */ >+ public StyledCellLabelProvider() { >+ this(0); >+ } >+ >+ /** >+ * Creates a new StyledCellLabelProvider. By default, owner draw is enabled. >+ * >+ * @param style >+ * the style bits >+ * @see StyledCellLabelProvider#COLORS_ON_SELECTION >+ * @see StyledCellLabelProvider#NO_FOCUS >+ */ >+ public StyledCellLabelProvider(int style) { >+ this.style = style & (COLORS_ON_SELECTION | NO_FOCUS) >+ | OWNER_DRAW_ENABLED; >+ } >+ >+ /** >+ * Returns <code>true</code> is the owner draw rendering is enabled for this label provider. >+ * By default owner draw rendering is enabled. If owner draw rendering is disabled, rending is >+ * done by the viewer and no styled ranges (see {@link ViewerCell#getStyleRanges()}) >+ * are drawn. >+ * >+ * @return <code>true</code> is the rendering of styles is enabled. >+ */ >+ public boolean isOwnerDrawEnabled() { >+ return (this.style & OWNER_DRAW_ENABLED) != 0; >+ } >+ >+ /** >+ * Specifies whether owner draw rendering is enabled for this label >+ * provider. By default owner draw rendering is enabled. If owner draw >+ * rendering is disabled, rendering is done by the viewer and no styled >+ * ranges (see {@link ViewerCell#getStyleRanges()}) are drawn. >+ * It is the caller's responsibility to also call >+ * {@link StructuredViewer#refresh()} or similar methods to update the >+ * underlying widget. >+ * >+ * @param enabled >+ * specifies if owner draw rendering is enabled >+ */ >+ public void setOwnerDrawEnabled(boolean enabled) { >+ boolean isEnabled= isOwnerDrawEnabled(); >+ if (isEnabled != enabled) { >+ if (enabled) { >+ this.style |= OWNER_DRAW_ENABLED; >+ } else { >+ this.style &= ~OWNER_DRAW_ENABLED; >+ } >+ if (this.viewer != null) { >+ setOwnerDrawEnabled(this.viewer, this.column, enabled); >+ } >+ } >+ } >+ >+ /** >+ * Returns the viewer on which this label provider is installed on or <code>null</code> if the >+ * label provider is not installed. >+ * >+ * @return the viewer on which this label provider is installed on or <code>null</code> if the >+ * label provider is not installed. >+ */ >+ protected final ColumnViewer getViewer() { >+ return this.viewer; >+ } >+ >+ /** >+ * Returns the column on which this label provider is installed on or <code>null</code> if the >+ * label provider is not installed. >+ * >+ * @return the column on which this label provider is installed on or <code>null</code> if the >+ * label provider is not installed. >+ */ >+ protected final ViewerColumn getColumn() { >+ return this.column; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#initialize(org.eclipse.jface.viewers.ColumnViewer, org.eclipse.jface.viewers.ViewerColumn) >+ */ >+ public void initialize(ColumnViewer viewer, ViewerColumn column) { >+ Assert.isTrue(this.viewer == null && this.column == null, "Label provider instance already in use"); //$NON-NLS-1$ >+ >+ this.viewer= viewer; >+ this.column= column; >+ super.initialize(viewer, column, isOwnerDrawEnabled()); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.BaseLabelProvider#dispose() >+ */ >+ public void dispose() { >+ if (this.cachedTextLayout != null) { >+ cachedTextLayout.dispose(); >+ cachedTextLayout = null; >+ } >+ >+ this.viewer= null; >+ this.column= null; >+ >+ super.dispose(); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#update(org.eclipse.jface.viewers.ViewerCell) >+ */ >+ public void update(ViewerCell cell) { >+ // clients must override and configure the cell and call super >+ super.update(cell); // calls 'repaint' to trigger the paint listener >+ } >+ >+ private TextLayout getSharedTextLayout(Display display) { >+ if (cachedTextLayout == null) { >+ cachedTextLayout = new TextLayout(display); >+ cachedTextLayout.setOrientation(Window.getDefaultOrientation()); >+ } else { >+ cachedTextLayout.setText(""); // make sure no previous ranges are cleared //$NON-NLS-1$ >+ } >+ return cachedTextLayout; >+ } >+ >+ private boolean useColors(Event event) { >+ return (event.detail & SWT.SELECTED) == 0 >+ || (this.style & COLORS_ON_SELECTION) != 0; >+ } >+ >+ private boolean drawFocus(Event event) { >+ return (event.detail & SWT.FOCUSED) != 0 >+ && (this.style & NO_FOCUS) == 0; >+ } >+ >+ /** >+ * Returns a {@link TextLayout} instance for the given cell >+ * configured with the style ranges. The text layout instance is managed by >+ * the label provider. Caller of the method must not dispose the text >+ * layout. >+ * >+ * @param diplay >+ * the current display >+ * @param applyColors >+ * if set, create colors in the result >+ * @param cell >+ * the viewer cell >+ * @return a TextLayout instance >+ */ >+ private TextLayout getTextLayoutForInfo(Display display, ViewerCell cell, boolean applyColors) { >+ TextLayout layout = getSharedTextLayout(display); >+ >+ layout.setText(cell.getText()); >+ layout.setFont(cell.getFont()); // set also if null to clear previous usages >+ >+ StyleRange[] styleRanges = cell.getStyleRanges(); >+ if (styleRanges != null) { // user didn't fill styled ranges >+ for (int i = 0; i < styleRanges.length; i++) { >+ StyleRange curr = prepareStyleRange(styleRanges[i], applyColors); >+ layout.setStyle(curr, curr.start, curr.start + curr.length - 1); >+ } >+ } >+ >+ return layout; >+ } >+ >+ /** >+ * Prepares the given style range before it is applied to the label. This method makes sure that >+ * no colors are drawn when the element is selected. >+ * The current version of the {@link StyledCellLabelProvider} will also ignore all font settings on the >+ * style range. Clients can override. >+ * >+ * @param styleRange >+ * the style range to prepare. the style range element must not be modified >+ * @param applyColors >+ * specifies if colors should be applied. >+ * @return >+ * returns the style range to use on the label >+ */ >+ protected StyleRange prepareStyleRange(StyleRange styleRange, boolean applyColors) { >+ // if no colors apply or font is set, create a clone and clear the >+ // colors and font >+ if (styleRange.font != null || !applyColors >+ && (styleRange.foreground != null || styleRange.background != null)) { >+ styleRange = (StyleRange) styleRange.clone(); >+ styleRange.font = null; // ignore font settings until bug 168807 is resolved >+ if (!applyColors) { >+ styleRange.foreground = null; >+ styleRange.background = null; >+ } >+ } >+ return styleRange; >+ } >+ >+ private ViewerCell getViewerCell(Event event, Object element) { >+ ViewerRow row= viewer.getViewerRowFromItem(event.item); >+ return new ViewerCell(row, event.index, element); >+ } >+ >+ /** >+ * Handle the erase event. The default implementation does nothing to ensure >+ * keep native selection highlighting working. >+ * >+ * @param event >+ * the erase event >+ * @param element >+ * the model object >+ * @see SWT#EraseItem >+ */ >+ protected void erase(Event event, Object element) { >+ // use native erase >+ if (isOwnerDrawEnabled()) { >+ // info has been set by 'update': announce that we paint ourselves >+ event.detail &= ~SWT.FOREGROUND; >+ } >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#measure(org.eclipse.swt.widgets.Event, >+ * java.lang.Object) >+ */ >+ protected void measure(Event event, Object element) { >+ // use native measuring >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#paint(org.eclipse.swt.widgets.Event, >+ * java.lang.Object) >+ */ >+ protected void paint(Event event, Object element) { >+ if (!isOwnerDrawEnabled()) >+ return; >+ >+ ViewerCell cell= getViewerCell(event, element); >+ >+ boolean applyColors = useColors(event); >+ GC gc = event.gc; >+ // remember colors to restore the GC later >+ Color oldForeground = gc.getForeground(); >+ Color oldBackground = gc.getBackground(); >+ >+ if (applyColors) { >+ Color foreground= cell.getForeground(); >+ if (foreground != null) { >+ gc.setForeground(foreground); >+ } >+ >+ Color background= cell.getBackground(); >+ if (background != null) { >+ gc.setBackground(background); >+ } >+ } >+ >+ Image image = cell.getImage(); >+ if (image != null) { >+ Rectangle imageBounds = cell.getImageBounds(); >+ Rectangle bounds = image.getBounds(); >+ >+ // center the image in the given space >+ int x = imageBounds.x >+ + Math.max(0, (imageBounds.width - bounds.width) / 2); >+ int y = imageBounds.y >+ + Math.max(0, (imageBounds.height - bounds.height) / 2); >+ gc.drawImage(image, x, y); >+ } >+ >+ TextLayout textLayout = getTextLayoutForInfo(event.display, cell, applyColors); >+ >+ Rectangle textBounds = cell.getTextBounds(); >+ Rectangle layoutBounds = textLayout.getBounds(); >+ >+ int x = textBounds.x; >+ int y = textBounds.y >+ + Math.max(0, (textBounds.height - layoutBounds.height) / 2); >+ >+ textLayout.draw(gc, x, y); >+ >+ if (drawFocus(event)) { >+ Rectangle focusBounds = cell.getViewerRow().getBounds(); >+ gc.drawFocus(focusBounds.x, focusBounds.y, focusBounds.width, >+ focusBounds.height); >+ } >+ >+ if (applyColors) { >+ gc.setForeground(oldForeground); >+ gc.setBackground(oldBackground); >+ } >+ } >+ >+} >#P org.eclipse.jface.snippets >Index: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java,v >retrieving revision 1.2 >diff -u -r1.2 Snippet049SimpleStyledCellLabelProvider.java >--- Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java 3 Feb 2008 20:09:58 -0000 1.2 >+++ Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java 21 Feb 2008 15:47:19 -0000 >@@ -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.DecoratingStyledCellLabelProvider.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,7 +35,7 @@ > > > /** >- * Using a {@link SimpleStyledCellLabelProvider} on tree viewer. Compare the result with a native tree viewer. >+ * Using a {@link StyledCellLabelProvider} on tree viewer. Compare the result with a native tree viewer. > */ > public class Snippet049SimpleStyledCellLabelProvider { > >@@ -50,6 +45,10 @@ > > > 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); >@@ -79,7 +78,7 @@ > 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 ownerDrawViewer= createViewer("Owner draw viewer:", composite, new DecoratingStyledCellLabelProvider(labelProvider), new DecoratingStyledCellLabelProvider(dateLabelProvider)); //$NON-NLS-1$ > > final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, labelProvider, dateLabelProvider); //$NON-NLS-1$ > >@@ -105,7 +104,7 @@ > > public void widgetSelected(SelectionEvent e) { > boolean newState= button2.getSelection(); >- ((DecoratingLabelProvider) ownerDrawViewer.getLabelProvider(0)).setOwnerDrawEnabled(newState); >+ ((DecoratingStyledCellLabelProvider) ownerDrawViewer.getLabelProvider(0)).setOwnerDrawEnabled(newState); > ownerDrawViewer.refresh(); > } > }); >@@ -117,7 +116,7 @@ > > public void widgetSelected(SelectionEvent e) { > boolean newState= button3.getSelection(); >- ((DecoratingDateLabelProvider) ownerDrawViewer.getLabelProvider(1)).setOwnerDrawEnabled(newState); >+ ((DecoratingStyledCellLabelProvider) ownerDrawViewer.getLabelProvider(1)).setOwnerDrawEnabled(newState); > ownerDrawViewer.refresh(); > } > }); >@@ -145,12 +144,12 @@ > 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 @@ > > 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 ExampleLabelProvider 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)); >@@ -277,16 +193,41 @@ > return "null"; //$NON-NLS-1$ > } > >+ public StyledString getStyledText(Object element) { >+ String text= getText(element); >+ StyledString styledString= new StyledString(text); >+ 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$ >+ styledString.append(decoration, StyledString.COUNTER_STYLE); >+ } >+ } >+ return styledString; >+ } > } > >- private static class ModifiedDateLabelProvider extends ColumnLabelProvider { >+ private static class ModifiedDateLabelProvider extends ColumnLabelProvider implements IStyledLabelProvider { > public String getText(Object element) { > if (element instanceof File) { > File file= (File) element; >- return new Date(file.lastModified()).toLocaleString(); >+ return DateFormat.getDateInstance().format(new Date(file.lastModified())); > } > return "-"; //$NON-NLS-1$ > } >+ >+ public StyledString getStyledText(Object element) { >+ String text= getText(element); >+ >+ StyledString styledString= new StyledString(text); >+ if (element instanceof File) { >+ File file= (File) element; >+ String decoration = DateFormat.getTimeInstance(3).format(new Date(file.lastModified())); >+ styledString.append(' '); >+ styledString.append(decoration, StyledString.COUNTER_STYLE); >+ } >+ return styledString; >+ } > } > > private static class FileSystemContentProvider implements ITreeContentProvider { >Index: Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java,v >retrieving revision 1.2 >diff -u -r1.2 Snippet050SimpleStyledCellLabelProvider.java >--- Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java 3 Feb 2008 20:09:58 -0000 1.2 >+++ Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java 21 Feb 2008 15:47:19 -0000 >@@ -14,18 +14,12 @@ > 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.jface.preference.JFacePreferences; >+import org.eclipse.jface.resource.JFaceResources; >+import org.eclipse.jface.viewers.*; > 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.graphics.RGB; > import org.eclipse.swt.layout.GridData; > import org.eclipse.swt.layout.GridLayout; > import org.eclipse.swt.widgets.Button; >@@ -37,7 +31,7 @@ > import org.eclipse.swt.widgets.Shell; > > /** >- * Using a {@link SimpleStyledCellLabelProvider} on table viewer. Compare the result with a native table viewer. >+ * Using a {@link StyledCellLabelProvider} on table viewer. Compare the result with a native table viewer. > */ > > public class Snippet050SimpleStyledCellLabelProvider { >@@ -49,6 +43,8 @@ > > 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)); >@@ -118,7 +114,7 @@ > } > } > if (root == null) { >- throw new RuntimeException("couldn't get a non-empty root file"); >+ throw new RuntimeException("couldn't get a non-empty root file"); //$NON-NLS-1$ > } > tableViewer.setInput(root); > >@@ -126,43 +122,46 @@ > } > > /** >- * Implements a {@link SimpleStyledCellLabelProvider} that wraps a normal label >+ * Implements a {@link StyledCellLabelProvider} that wraps a normal label > * provider and adds some decorations in color > */ >- private static class DecoratingLabelProvider extends SimpleStyledCellLabelProvider { >+ private static class DecoratingLabelProvider extends StyledCellLabelProvider { > >- private static final StyleRange[] NO_RANGES= new StyleRange[0]; > private final ILabelProvider fWrappedLabelProvider; > > public DecoratingLabelProvider(ILabelProvider labelProvider) { > fWrappedLabelProvider= labelProvider; > } >- >- protected LabelPresentationInfo getLabelPresentationInfo(Object element) { >+ >+ public void update(ViewerCell cell) { >+ Object element= cell.getElement(); >+ > String text= fWrappedLabelProvider.getText(element); > Image image= fWrappedLabelProvider.getImage(element); >- > >- StyleRange[] ranges= NO_RANGES; >+ StyledString styledString= new StyledString(text); > 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 }; >- } >+ String decoration = MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$ >+ styledString.append(decoration, StyledString.COUNTER_STYLE); > } >- return new LabelPresentationInfo(text, ranges, image, null, null, null); >+ >+ cell.setText(styledString.getString()); >+ cell.setStyleRanges(styledString.getStyleRanges()); >+ cell.setImage(image); >+ super.update(cell); >+ } >+ >+ public void addListener(ILabelProviderListener listener) { >+ super.addListener(listener); >+ fWrappedLabelProvider.addListener(listener); > } > >+ public void removeListener(ILabelProviderListener listener) { >+ super.removeListener(listener); >+ fWrappedLabelProvider.removeListener(listener); >+ } >+ > public void dispose() { > super.dispose(); > fWrappedLabelProvider.dispose();
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