1 11 12 package org.eclipse.ui.internal.cheatsheets.composite.explorer; 13 14 import java.io.IOException ; 15 import java.net.URL ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 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; 39 42 private class ImageSet { 43 Map images = new HashMap (); 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 iter = images.values().iterator(); iter.hasNext(); ) { 56 Image nextImage = (Image)iter.next(); 57 nextImage.dispose(); 58 } 59 } 60 } 61 62 private Map imageMap = null; 64 public TreeLabelProvider() { 65 imageMap = new HashMap (); 66 } 67 68 public String getText(Object obj) { 69 String result; 70 if (obj instanceof ICompositeCheatSheetTask) { 71 result = ((ICompositeCheatSheetTask) obj).getName(); 72 } else { 73 result = obj.toString(); 74 } 75 if (result == null) { 76 result = ""; } 78 return result; 79 } 80 81 public Image getImage(Object 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 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 106 private ImageSet createImages(String 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", images, 120 desc); 121 createImageWithOverlay(ICompositeCheatSheetTask.SKIPPED, 122 "$nl$/icons/ovr16/task_skipped.gif", images, 124 desc); 125 createDisabledImage(kind, BLOCKED, 126 images, 127 baseImage); 128 createImageWithOverlay(ICompositeCheatSheetTask.COMPLETED, 129 "$nl$/icons/ovr16/task_complete.gif", images, 131 desc); 132 133 } 134 return images; 135 } 136 137 private ImageDescriptor getPredefinedImageDescriptor(String kind, boolean isEnabled) { 138 String filename; 139 if (ICompositeCheatsheetTags.CHEATSHEET_TASK_KIND.equals(kind)) { 140 filename = "cheatsheet_task.gif"; } else if (ITaskGroup.SET.equals(kind)) { 142 filename = "task_set.gif"; } else if (ITaskGroup.CHOICE.equals(kind)) { 144 filename = "task_choice.gif"; } else if (ITaskGroup.SEQUENCE.equals(kind)) { 146 filename = "task_sequence.gif"; } else { 148 return null; 149 } 150 String iconPath = "$nl$/icons/"; 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 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 kind, int state, ImageSet images, Image baseImage) { 168 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 relativePath) { 188 Bundle bundle = CheatSheetPlugin.getPlugin().getBundle(); 189 URL 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 e) { 195 return null; 196 } 197 } 198 199 public void dispose() { 200 if (imageMap != null) { 201 for (Iterator 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 |