001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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: * Benjamin Muskalla - bug 77710
011: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
012:
013: import java.util.HashMap;
014: import java.util.Iterator;
015:
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.jface.viewers.IColorProvider;
018: import org.eclipse.jface.viewers.LabelProvider;
019: import org.eclipse.swt.graphics.Color;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.ui.ISharedImages;
022: import org.eclipse.ui.IWorkbenchPage;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.internal.WorkbenchImages;
025: import org.eclipse.ui.internal.WorkbenchMessages;
026: import org.eclipse.ui.views.IViewCategory;
027: import org.eclipse.ui.views.IViewDescriptor;
028:
029: /**
030: * Provides labels for view children.
031: */
032: public class ViewLabelProvider extends LabelProvider implements
033: IColorProvider {
034: private HashMap images;
035: private final IWorkbenchWindow window;
036: private final Color dimmedForeground;
037:
038: /**
039: * @param window the workbench window
040: * @param dimmedForeground the dimmed foreground color to use for views that are already open
041: */
042: public ViewLabelProvider(IWorkbenchWindow window,
043: Color dimmedForeground) {
044: this .window = window;
045: this .dimmedForeground = dimmedForeground;
046: }
047:
048: Image cacheImage(ImageDescriptor desc) {
049: if (images == null) {
050: images = new HashMap(21);
051: }
052: Image image = (Image) images.get(desc);
053: if (image == null) {
054: image = desc.createImage();
055: images.put(desc, image);
056: }
057: return image;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
062: */
063: public void dispose() {
064: if (images != null) {
065: for (Iterator i = images.values().iterator(); i.hasNext();) {
066: ((Image) i.next()).dispose();
067: }
068: images = null;
069: }
070: super .dispose();
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
075: */
076: public Image getImage(Object element) {
077: if (element instanceof IViewDescriptor) {
078: ImageDescriptor desc = ((IViewDescriptor) element)
079: .getImageDescriptor();
080: if (desc != null) {
081: return cacheImage(desc);
082: }
083: } else if (element instanceof IViewCategory) {
084: ImageDescriptor desc = WorkbenchImages
085: .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
086: return cacheImage(desc);
087: }
088: return null;
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
093: */
094: public String getText(Object element) {
095: String label = WorkbenchMessages.ViewLabel_unknown;
096: if (element instanceof IViewCategory) {
097: label = ((IViewCategory) element).getLabel();
098: } else if (element instanceof IViewDescriptor) {
099: label = ((IViewDescriptor) element).getLabel();
100: }
101: return DialogUtil.removeAccel(label);
102: }
103:
104: public Color getBackground(Object element) {
105: return null;
106: }
107:
108: public Color getForeground(Object element) {
109: if (element instanceof IViewDescriptor) {
110: IWorkbenchPage activePage = window.getActivePage();
111: if (activePage != null) {
112: if (activePage.findView(((IViewDescriptor) element)
113: .getId()) != null) {
114: return dimmedForeground;
115: }
116: }
117: }
118: return null;
119: }
120: }
|