01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package net.refractions.udig.internal.ui.operations;
11:
12: import java.util.HashMap;
13: import java.util.Iterator;
14:
15: import net.refractions.udig.ui.internal.Messages;
16: import net.refractions.udig.ui.operations.OpAction;
17:
18: import org.eclipse.jface.resource.ImageDescriptor;
19: import org.eclipse.jface.viewers.LabelProvider;
20: import org.eclipse.swt.graphics.Image;
21: import org.eclipse.ui.ISharedImages;
22: import org.eclipse.ui.internal.WorkbenchImages;
23: import org.eclipse.ui.internal.dialogs.DialogUtil;
24:
25: /**
26: * Provides labels for view children.
27: */
28: public class OperationLabelProvider extends LabelProvider {
29: private HashMap images;
30:
31: Image cacheImage(ImageDescriptor desc) {
32: if (images == null)
33: images = new HashMap(21);
34: Image image = (Image) images.get(desc);
35: if (image == null) {
36: image = desc.createImage();
37: images.put(desc, image);
38: }
39: return image;
40: }
41:
42: /* (non-Javadoc)
43: * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
44: */
45: public void dispose() {
46: if (images != null) {
47: for (Iterator i = images.values().iterator(); i.hasNext();) {
48: ((Image) i.next()).dispose();
49: }
50: images = null;
51: }
52: super .dispose();
53: }
54:
55: public Image getImage(Object element) {
56: if (element instanceof OpAction) {
57: ImageDescriptor desc = ((OpAction) element)
58: .getImageDescriptor();
59: if (desc != null)
60: return cacheImage(desc);
61: } else if (element instanceof OperationCategory) {
62: ImageDescriptor desc = WorkbenchImages
63: .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
64: return cacheImage(desc);
65: }
66: return null;
67: }
68:
69: public String getText(Object element) {
70: String label = Messages.OperationLabelProvider_unknown;
71: if (element instanceof OpAction) {
72: label = ((OpAction) element).getText();
73: } else if (element instanceof OperationCategory) {
74: label = ((OperationCategory) element).getMenuText();
75: }
76: return DialogUtil.removeAccel(label);
77: }
78: }
|