001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.IPath;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.graphics.Color;
018: import org.eclipse.swt.graphics.Image;
019: import org.eclipse.swt.widgets.Display;
020:
021: import org.eclipse.jface.resource.ImageRegistry;
022: import org.eclipse.jface.viewers.DecorationOverlayIcon;
023: import org.eclipse.jface.viewers.IColorProvider;
024: import org.eclipse.jface.viewers.IDecoration;
025: import org.eclipse.jface.viewers.LabelProvider;
026:
027: import org.eclipse.ui.ISharedImages;
028: import org.eclipse.ui.PlatformUI;
029:
030: import org.eclipse.jdt.internal.corext.util.Messages;
031:
032: import org.eclipse.jdt.internal.ui.JavaPlugin;
033: import org.eclipse.jdt.internal.ui.JavaPluginImages;
034: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
035:
036: public class CPVariableElementLabelProvider extends LabelProvider
037: implements IColorProvider {
038:
039: // shared, do not dispose:
040: private Image fJARImage;
041: private Image fFolderImage;
042: private Color fResolvedBackground;
043:
044: private Image fDeprecatedJARImage;
045: private Image fDeprecatedFolderImage;
046:
047: private boolean fHighlightReadOnly;
048:
049: public CPVariableElementLabelProvider(boolean highlightReadOnly) {
050: ImageRegistry reg = JavaPlugin.getDefault().getImageRegistry();
051: fJARImage = reg.get(JavaPluginImages.IMG_OBJS_EXTJAR);
052: fFolderImage = PlatformUI.getWorkbench().getSharedImages()
053: .getImage(ISharedImages.IMG_OBJ_FOLDER);
054:
055: fDeprecatedJARImage = new DecorationOverlayIcon(fJARImage,
056: JavaPluginImages.DESC_OVR_DEPRECATED,
057: IDecoration.TOP_LEFT).createImage();
058: fDeprecatedFolderImage = new DecorationOverlayIcon(
059: fFolderImage, JavaPluginImages.DESC_OVR_DEPRECATED,
060: IDecoration.TOP_LEFT).createImage();
061:
062: fHighlightReadOnly = highlightReadOnly;
063: fResolvedBackground = null;
064: }
065:
066: /*
067: * @see LabelProvider#getImage(java.lang.Object)
068: */
069: public Image getImage(Object element) {
070: if (element instanceof CPVariableElement) {
071: CPVariableElement curr = (CPVariableElement) element;
072: IPath path = curr.getPath();
073: if (path.toFile().isFile()) {
074: return curr.isDeprecated() ? fDeprecatedJARImage
075: : fJARImage;
076: }
077: return curr.isDeprecated() ? fDeprecatedFolderImage
078: : fFolderImage;
079: }
080: return super .getImage(element);
081: }
082:
083: /*
084: * @see LabelProvider#getText(java.lang.Object)
085: */
086: public String getText(Object element) {
087: if (element instanceof CPVariableElement) {
088: CPVariableElement curr = (CPVariableElement) element;
089: String name = curr.getName();
090: IPath path = curr.getPath();
091:
092: String result = name;
093: ArrayList restrictions = new ArrayList(2);
094:
095: if (curr.isReadOnly() && fHighlightReadOnly) {
096: restrictions
097: .add(NewWizardMessages.CPVariableElementLabelProvider_read_only);
098: }
099: if (curr.isDeprecated()) {
100: restrictions
101: .add(NewWizardMessages.CPVariableElementLabelProvider_deprecated);
102: }
103: if (restrictions.size() == 1) {
104: result = Messages
105: .format(
106: NewWizardMessages.CPVariableElementLabelProvider_one_restriction,
107: new Object[] { result,
108: restrictions.get(0) });
109: } else if (restrictions.size() == 2) {
110: result = Messages
111: .format(
112: NewWizardMessages.CPVariableElementLabelProvider_two_restrictions,
113: new Object[] { result,
114: restrictions.get(0),
115: restrictions.get(1) });
116: }
117:
118: if (path != null) {
119: String appendix;
120: if (!path.isEmpty()) {
121: appendix = path.toOSString();
122: } else {
123: appendix = NewWizardMessages.CPVariableElementLabelProvider_empty;
124: }
125: result = Messages
126: .format(
127: NewWizardMessages.CPVariableElementLabelProvider_appendix,
128: new Object[] { result, appendix });
129: }
130:
131: return result;
132: }
133:
134: return super .getText(element);
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
139: */
140: public Color getForeground(Object element) {
141: return null;
142: }
143:
144: /* (non-Javadoc)
145: * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
146: */
147: public Color getBackground(Object element) {
148: if (element instanceof CPVariableElement) {
149: CPVariableElement curr = (CPVariableElement) element;
150: if (fHighlightReadOnly && curr.isReadOnly()) {
151: if (fResolvedBackground == null) {
152: Display display = Display.getCurrent();
153: fResolvedBackground = display
154: .getSystemColor(SWT.COLOR_INFO_BACKGROUND);
155: }
156: return fResolvedBackground;
157: }
158: }
159: return null;
160: }
161:
162: /* (non-Javadoc)
163: * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
164: */
165: public void dispose() {
166: super.dispose();
167: fDeprecatedFolderImage.dispose();
168: fDeprecatedJARImage.dispose();
169: }
170:
171: }
|