KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > explorer > TreeLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.cheatsheets.composite.explorer;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.FileLocator;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
27 import org.eclipse.ui.internal.cheatsheets.composite.model.TaskStateUtilities;
28 import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
29 import org.eclipse.ui.internal.cheatsheets.composite.views.TaskEditorManager;
30 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
31 import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup;
32 import org.osgi.framework.Bundle;
33
34 public class TreeLabelProvider extends LabelProvider {
35
36     private static int BLOCKED = -1;
37     private Image defaultImage = null; // Image for tasks with null kind
38

39     /*
40      * A set of related images
41      */

42     private class ImageSet {
43         // Use a map rather than array so the nuber of icons is not hard coded
44
Map JavaDoc images = new HashMap JavaDoc();
45         
46         public void put(int index, Image image) {
47             images.put(Integer.toString(index), image);
48         }
49         
50         public Image getImage(int index) {
51             return (Image)images.get(Integer.toString(index));
52         }
53         
54         void dispose() {
55             for (Iterator JavaDoc iter = images.values().iterator(); iter.hasNext(); ) {
56                 Image nextImage = (Image)iter.next();
57                 nextImage.dispose();
58             }
59         }
60     }
61     
62     private Map JavaDoc imageMap = null; // each entry is an ImageSet
63

64     public TreeLabelProvider() {
65         imageMap = new HashMap JavaDoc();
66     }
67
68     public String JavaDoc getText(Object JavaDoc obj) {
69         String JavaDoc result;
70         if (obj instanceof ICompositeCheatSheetTask) {
71             result = ((ICompositeCheatSheetTask) obj).getName();
72         } else {
73             result = obj.toString();
74         }
75         if (result == null) {
76             result = ""; //$NON-NLS-1$
77
}
78         return result;
79     }
80
81     public Image getImage(Object JavaDoc obj) {
82         if (obj instanceof ICompositeCheatSheetTask) {
83             ICompositeCheatSheetTask task = (ICompositeCheatSheetTask) obj;
84             return lookupImage(task.getKind(), task.getState(), TaskStateUtilities.isBlocked(task));
85         }
86         return super.getImage(obj);
87     }
88
89     public Image lookupImage(String JavaDoc kind, int state, boolean isBlocked) {
90         ImageSet images = (ImageSet) imageMap.get(kind);
91         if (images == null) {
92             images = createImages(kind);
93             imageMap.put(kind, images);
94         }
95         if (isBlocked) {
96             return images.getImage(BLOCKED);
97         }
98         return images.getImage(state);
99     }
100
101     /**
102      * Create a set of images for a task which may be [redefined.
103      * @param kind
104      * @return
105      */

106     private ImageSet createImages(String JavaDoc kind) {
107         ImageSet images = new ImageSet();
108         ImageDescriptor desc;
109         desc = getPredefinedImageDescriptor(kind, true);
110         if (desc == null) {
111             desc = TaskEditorManager.getInstance().getImageDescriptor(kind);
112         }
113         if (desc != null) {
114             Image baseImage = desc.createImage();
115             images.put(ICompositeCheatSheetTask.NOT_STARTED, baseImage);
116             
117             createImageWithOverlay(ICompositeCheatSheetTask.IN_PROGRESS,
118                        "$nl$/icons/ovr16/task_in_progress.gif", //$NON-NLS-1$
119
images,
120                        desc);
121             createImageWithOverlay(ICompositeCheatSheetTask.SKIPPED,
122                        "$nl$/icons/ovr16/task_skipped.gif", //$NON-NLS-1$
123
images,
124                        desc);
125             createDisabledImage(kind, BLOCKED,
126                        images,
127                        baseImage);
128             createImageWithOverlay(ICompositeCheatSheetTask.COMPLETED,
129                        "$nl$/icons/ovr16/task_complete.gif", //$NON-NLS-1$
130
images,
131                        desc);
132             
133         }
134         return images;
135     }
136
137     private ImageDescriptor getPredefinedImageDescriptor(String JavaDoc kind, boolean isEnabled) {
138         String JavaDoc filename;
139         if (ICompositeCheatsheetTags.CHEATSHEET_TASK_KIND.equals(kind)) {
140             filename = "cheatsheet_task.gif"; //$NON-NLS-1$
141
} else if (ITaskGroup.SET.equals(kind)) {
142             filename = "task_set.gif"; //$NON-NLS-1$
143
} else if (ITaskGroup.CHOICE.equals(kind)) {
144             filename = "task_choice.gif"; //$NON-NLS-1$
145
} else if (ITaskGroup.SEQUENCE.equals(kind)) {
146             filename = "task_sequence.gif"; //$NON-NLS-1$
147
} else {
148             return null;
149         }
150         String JavaDoc iconPath = "$nl$/icons/"; //$NON-NLS-1$
151
if (isEnabled) {
152             iconPath += CheatSheetPlugin.T_OBJ;
153         } else {
154             iconPath += CheatSheetPlugin.T_DLCL;
155         }
156         iconPath += filename;
157         return createImageDescriptor(iconPath);
158     }
159
160     private void createImageWithOverlay(int state, String JavaDoc imagePath, ImageSet images, ImageDescriptor baseDescriptor) {
161         ImageDescriptor descriptor = createImageDescriptor(imagePath);
162         OverlayIcon icon = new OverlayIcon(baseDescriptor, new ImageDescriptor[][] {
163                 {}, { descriptor } });
164         images.put(state, icon.createImage());
165     }
166     
167     private void createDisabledImage(String JavaDoc kind, int state, ImageSet images, Image baseImage) {
168         // The four images for task_set, task_sequence, task_choice and cheatsheet_task can be found
169
// in icons/dlcl16.
170
// TODO extend the extension point to allow disabled images to be specified.
171
//if
172

173         ImageDescriptor desc = getPredefinedImageDescriptor(kind, false);
174         Image disabledImage;
175         if (desc != null) {
176             disabledImage = desc.createImage();
177         } else {
178             disabledImage = createGrayedImage(baseImage);
179         }
180         images.put(state, disabledImage);
181     }
182
183     private Image createGrayedImage(Image image) {
184         return new Image(image.getDevice(), image, SWT.IMAGE_DISABLE);
185     }
186
187     private ImageDescriptor createImageDescriptor(String JavaDoc relativePath) {
188         Bundle bundle = CheatSheetPlugin.getPlugin().getBundle();
189         URL JavaDoc url = FileLocator.find(bundle, new Path(relativePath),null);
190         if (url == null) return null;
191         try {
192             url = FileLocator.resolve(url);
193             return ImageDescriptor.createFromURL(url);
194         } catch (IOException JavaDoc e) {
195             return null;
196         }
197     }
198
199     public void dispose() {
200         if (imageMap != null) {
201             for (Iterator JavaDoc iter = imageMap.values().iterator(); iter.hasNext(); ) {
202                 ImageSet nextImages = (ImageSet)iter.next();
203                 nextImages.dispose();
204             }
205             imageMap = null;
206         }
207         if (defaultImage != null) {
208             defaultImage.dispose();
209             defaultImage = null;
210         }
211     }
212     
213 }
214
Popular Tags