1 11 package org.eclipse.ui.externaltools.internal.model; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 16 import org.eclipse.jface.action.IAction; 17 import org.eclipse.jface.resource.CompositeImageDescriptor; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.jface.resource.ImageRegistry; 20 import org.eclipse.swt.graphics.Image; 21 22 25 public class ExternalToolsImages { 26 27 30 private static ImageRegistry imageRegistry; 31 32 35 private static ImageDescriptorRegistry imageDescriptorRegistry; 36 37 38 private static URL ICON_BASE_URL= null; 39 40 static { 41 String pathSuffix = "icons/full/"; ICON_BASE_URL= ExternalToolsPlugin.getDefault().getBundle().getEntry(pathSuffix); 43 } 44 45 private final static String OBJECT= "obj16/"; 48 51 private static void declareImages() { 52 declareRegistryImage(IExternalToolConstants.IMG_TAB_MAIN, OBJECT + "main_tab.gif"); } 55 56 62 private final static void declareRegistryImage(String key, String path) { 63 ImageDescriptor desc= ImageDescriptor.getMissingImageDescriptor(); 64 try { 65 desc= ImageDescriptor.createFromURL(makeIconFileURL(path)); 66 } catch (MalformedURLException me) { 67 } 68 imageRegistry.put(key, desc); 69 } 70 71 74 public static ImageRegistry getImageRegistry() { 75 if (imageRegistry == null) { 76 initializeImageRegistry(); 77 } 78 return imageRegistry; 79 } 80 81 109 public static ImageRegistry initializeImageRegistry() { 110 imageRegistry= new ImageRegistry(ExternalToolsPlugin.getStandardDisplay()); 111 declareImages(); 112 return imageRegistry; 113 } 114 115 119 public static Image getImage(String key) { 120 return getImageRegistry().get(key); 121 } 122 123 127 public static ImageDescriptor getImageDescriptor(String key) { 128 return getImageRegistry().getDescriptor(key); 129 } 130 131 private static URL makeIconFileURL(String iconPath) throws MalformedURLException { 132 if (ICON_BASE_URL == null) { 133 throw new MalformedURLException (); 134 } 135 136 return new URL (ICON_BASE_URL, iconPath); 137 } 138 139 143 public static void setLocalImageDescriptors(IAction action, String iconName) { 144 setImageDescriptors(action, "lcl16", iconName); } 146 147 private static void setImageDescriptors(IAction action, String type, String relPath) { 148 149 try { 150 ImageDescriptor id= ImageDescriptor.createFromURL(makeIconFileURL("d" + type, relPath)); if (id != null) 152 action.setDisabledImageDescriptor(id); 153 } catch (MalformedURLException e) { 154 ExternalToolsPlugin.getDefault().log(e); 155 } 156 157 try { 158 ImageDescriptor id= ImageDescriptor.createFromURL(makeIconFileURL("c" + type, relPath)); if (id != null) 160 action.setHoverImageDescriptor(id); 161 } catch (MalformedURLException e) { 162 ExternalToolsPlugin.getDefault().log(e); 163 } 164 165 action.setImageDescriptor(create("e" + type, relPath)); } 167 168 private static URL makeIconFileURL(String prefix, String name) throws MalformedURLException { 169 if (ICON_BASE_URL == null) { 170 throw new MalformedURLException (); 171 } 172 173 StringBuffer buffer= new StringBuffer (prefix); 174 buffer.append('/'); 175 buffer.append(name); 176 return new URL (ICON_BASE_URL, buffer.toString()); 177 } 178 179 private static ImageDescriptor create(String prefix, String name) { 180 try { 181 return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name)); 182 } catch (MalformedURLException e) { 183 ExternalToolsPlugin.getDefault().log(e); 184 return ImageDescriptor.getMissingImageDescriptor(); 185 } 186 } 187 188 191 public static Image getImage(CompositeImageDescriptor imageDescriptor) { 192 if (imageDescriptorRegistry == null) { 193 imageDescriptorRegistry = new ImageDescriptorRegistry(); 194 } 195 return imageDescriptorRegistry.get(imageDescriptor); 196 } 197 198 public static void disposeImageDescriptorRegistry() { 199 if (imageDescriptorRegistry != null) { 200 imageDescriptorRegistry.dispose(); 201 } 202 } 203 } 204 | Popular Tags |