1 11 package org.eclipse.ui.internal.util; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.jface.resource.ColorDescriptor; 20 import org.eclipse.jface.resource.DeviceResourceDescriptor; 21 import org.eclipse.jface.resource.DeviceResourceException; 22 import org.eclipse.jface.resource.FontDescriptor; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.jface.resource.JFaceResources; 25 import org.eclipse.jface.resource.ResourceManager; 26 import org.eclipse.swt.events.DisposeEvent; 27 import org.eclipse.swt.events.DisposeListener; 28 import org.eclipse.swt.graphics.Color; 29 import org.eclipse.swt.graphics.Font; 30 import org.eclipse.swt.graphics.Image; 31 import org.eclipse.swt.widgets.Button; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Item; 34 import org.eclipse.swt.widgets.Label; 35 import org.eclipse.swt.widgets.TableItem; 36 import org.eclipse.swt.widgets.ToolItem; 37 import org.eclipse.swt.widgets.Widget; 38 import org.eclipse.ui.internal.WorkbenchPlugin; 39 40 90 public final class Descriptors { 91 private static final String DISPOSE_LIST = "Descriptors.disposeList"; 93 private Descriptors() { 94 } 95 96 private static final class ResourceMethod { 97 ResourceMethod(Method m, String id) { 98 method = m; 99 this.id = id; 100 } 101 102 Method method; 103 DeviceResourceDescriptor oldDescriptor; 104 String id; 105 106 public void invoke(Widget toCall, DeviceResourceDescriptor newDescriptor) { 107 if (newDescriptor == oldDescriptor) { 108 return; 109 } 110 111 ResourceManager mgr = JFaceResources.getResources(toCall.getDisplay()); 112 113 Object newResource; 114 try { 115 newResource = newDescriptor == null? null : mgr.create(newDescriptor); 116 } catch (DeviceResourceException e1) { 117 WorkbenchPlugin.log(e1); 118 return; 119 } 120 121 try { 122 method.invoke(toCall, new Object [] {newResource}); 123 } catch (IllegalArgumentException e) { 124 throw e; 125 } catch (IllegalAccessException e) { 126 WorkbenchPlugin.log(e); 127 return; 128 } catch (InvocationTargetException e) { 129 if (e.getTargetException() instanceof RuntimeException ) { 130 throw (RuntimeException )e.getTargetException(); 131 } 132 WorkbenchPlugin.log(e); 133 return; 134 } 135 136 if (oldDescriptor != null) { 138 mgr.destroy(oldDescriptor); 140 } 141 142 144 oldDescriptor = newDescriptor; 145 } 146 147 public void dispose() { 148 if (oldDescriptor != null) { 150 ResourceManager mgr = JFaceResources.getResources(); 151 mgr.destroy(oldDescriptor); 153 oldDescriptor = null; 154 } 155 156 } 157 } 158 159 private static DisposeListener disposeListener = new DisposeListener() { 160 public void widgetDisposed(DisposeEvent e) { 161 doDispose(e.widget); 162 } 163 }; 164 165 167 176 public static void setImage(Item item, ImageDescriptor descriptor) { 177 callMethod(item, "setImage", descriptor, Image.class); } 179 180 182 public static void setHotImage(ToolItem item, ImageDescriptor descriptor) { 183 callMethod(item, "setHotImage", descriptor, Image.class); } 185 186 public static void setDisabledImage(ToolItem item, ImageDescriptor descriptor) { 187 callMethod(item, "setDisabledImage", descriptor, Image.class); } 189 190 192 public static void setFont(TableItem item, FontDescriptor descriptor) { 193 callMethod(item, "setFont", descriptor, Font.class); } 195 196 public static void setBackground(TableItem item, ColorDescriptor descriptor) { 197 callMethod(item, "setBackground", descriptor, Color.class); } 199 200 public static void setForeground(TableItem item, ColorDescriptor descriptor) { 201 callMethod(item, "setForeground", descriptor, Color.class); } 203 204 206 public static void setBackground(Control control, ColorDescriptor descriptor) { 207 callMethod(control, "setBackground", descriptor, Color.class); } 209 210 public static void setForeground(Control control, ColorDescriptor descriptor) { 211 callMethod(control, "setForeground", descriptor, Color.class); } 213 214 216 public static void setImage(Button button, ImageDescriptor descriptor) { 217 callMethod(button, "setImage", descriptor, Image.class); } 219 220 public static void setImage(Label label, ImageDescriptor descriptor) { 221 callMethod(label, "setImage", descriptor, Image.class); } 223 224 private static ResourceMethod getResourceMethod(Widget toCall, String methodName, Class resourceType) throws NoSuchMethodException { 225 Object oldData = toCall.getData(DISPOSE_LIST); 226 227 if (oldData instanceof List ) { 228 for (Iterator iter = ((List )oldData).iterator(); iter.hasNext();) { 230 ResourceMethod method = (ResourceMethod) iter.next(); 231 232 if (method.id == methodName) { 233 return method; 234 } 235 } 236 } if (oldData instanceof ResourceMethod) { 237 if (((ResourceMethod)oldData).id == methodName) { 238 return ((ResourceMethod)oldData); 239 } 240 241 List newList = new ArrayList (); 242 newList.add(oldData); 243 oldData = newList; 244 toCall.setData(DISPOSE_LIST, oldData); 245 } 246 247 249 Class clazz = toCall.getClass(); 250 251 Method method; 252 try { 253 method = clazz.getMethod(methodName, new Class [] {resourceType}); 254 } catch (SecurityException e) { 255 throw e; 256 } 257 258 ResourceMethod result = new ResourceMethod(method, methodName); 259 260 if (oldData == null) { 261 toCall.setData(DISPOSE_LIST, result); 262 toCall.addDisposeListener(disposeListener); 263 } else { 264 ((List )oldData).add(result); 265 } 266 267 return result; 268 } 269 270 private static void callMethod(Widget toCall, String methodName, DeviceResourceDescriptor descriptor, Class resourceType) { 271 ResourceMethod method; 272 try { 273 method = getResourceMethod(toCall, methodName, resourceType); 274 } catch (NoSuchMethodException e) { 275 WorkbenchPlugin.log(e); 276 return; 277 } 278 279 method.invoke(toCall, descriptor); 280 } 281 282 private static void doDispose(Widget widget) { 283 Object oldData = widget.getData(DISPOSE_LIST); 284 285 if (oldData instanceof ArrayList ) { 286 ArrayList list = ((ArrayList )oldData); 287 ResourceMethod[] data = (ResourceMethod[]) list.toArray(new ResourceMethod[list.size()]); 288 289 for (int i = 0; i < data.length; i++) { 291 ResourceMethod method = data[i]; 292 293 method.dispose(); 294 } 295 } 296 297 if (oldData instanceof ResourceMethod) { 298 ((ResourceMethod)oldData).dispose(); 299 } 300 } 301 302 } 303 | Popular Tags |