From 34d77dbc53d92c0518c7f7024c4736b0027aaca9 Mon Sep 17 00:00:00 2001 From: axelcl Date: Fri, 8 Jul 2005 21:30:33 +0000 Subject: [PATCH] Changed the UI Image handling. Avoid java.lang.NullPointerException and java.lang.NoClassDefFoundError --- .../sourceforge/phpdt/internal/ui/PHPUiImages.java | 76 +++++++++++++++---- .../phpeclipse/wizards/html/EditElementWizard.java | 4 +- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUiImages.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUiImages.java index d4836ab..395ee76 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUiImages.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUiImages.java @@ -2,6 +2,8 @@ package net.sourceforge.phpdt.internal.ui; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; import net.sourceforge.phpeclipse.PHPeclipsePlugin; @@ -16,7 +18,7 @@ public class PHPUiImages { "net.sourceforge.phpdt.internal.ui."; protected static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length(); - protected static URL fgIconBaseURL; + protected static URL fgIconBaseURL=null; // static { // String pathSuffix = "icons/"; @@ -38,8 +40,9 @@ public class PHPUiImages { static { fgIconBaseURL= PHPeclipsePlugin.getDefault().getBundle().getEntry("/icons/"); //$NON-NLS-1$ } - protected static final ImageRegistry IMAGE_REGISTRY = new ImageRegistry(); - + private static ImageRegistry fgImageRegistry= null; + private static HashMap fgAvoidSWTErrorMap= null; + protected static final String OBJ_PREFIX = "obj16"; protected static final String OVR_PREFIX = "ovr16"; protected static final String CTOOL_PREFIX = "ctool16"; @@ -380,14 +383,28 @@ public class PHPUiImages { createManaged(T_OBJ, IMG_CORRECTION_REMOVE); createManaged(T_OBJ, IMG_CORRECTION_ADD); } + /** * Returns the image managed under the given key in this registry. * * @param key the image's key * @return the image managed under the given key - */ + */ public static Image get(String key) { - return IMAGE_REGISTRY.get(key); + return getImageRegistry().get(key); + } + + /** + * Returns the image descriptor for the given key in this registry. Might be called in a non-UI thread. + * + * @param key the image's key + * @return the image descriptor for the given key + */ + public static ImageDescriptor getDescriptor(String key) { + if (fgImageRegistry == null) { + return (ImageDescriptor) fgAvoidSWTErrorMap.get(key); + } + return getImageRegistry().getDescriptor(key); } /** @@ -410,8 +427,19 @@ public class PHPUiImages { setImageDescriptors(action, "lcl16", iconName); } - public static ImageRegistry getImageRegistry() { - return IMAGE_REGISTRY; + /* + * Helper method to access the image registry from the JavaPlugin class. + */ + /* package */ static ImageRegistry getImageRegistry() { + if (fgImageRegistry == null) { + fgImageRegistry= new ImageRegistry(); + for (Iterator iter= fgAvoidSWTErrorMap.keySet().iterator(); iter.hasNext();) { + String key= (String) iter.next(); + fgImageRegistry.put(key, (ImageDescriptor) fgAvoidSWTErrorMap.get(key)); + } + fgAvoidSWTErrorMap= null; + } + return fgImageRegistry; } //---- Helper methods to access icons on the file system -------------------------------------- @@ -442,16 +470,32 @@ public class PHPUiImages { action.setImageDescriptor(create("e" + type, relPath)); } - protected static ImageDescriptor createManaged( - String prefix, - String name) { + private static ImageDescriptor createManaged(String prefix, String name) { try { - ImageDescriptor result = - ImageDescriptor.createFromURL( - makeIconFileURL( - prefix, - name.substring(NAME_PREFIX_LENGTH))); - IMAGE_REGISTRY.put(name, result); + ImageDescriptor result= ImageDescriptor.createFromURL(makeIconFileURL(prefix, name.substring(NAME_PREFIX_LENGTH))); + if (fgAvoidSWTErrorMap == null) { + fgAvoidSWTErrorMap= new HashMap(); + } + fgAvoidSWTErrorMap.put(name, result); + if (fgImageRegistry != null) { + PHPeclipsePlugin.logErrorMessage("Image registry already defined"); //$NON-NLS-1$ + } + return result; + } catch (MalformedURLException e) { + return ImageDescriptor.getMissingImageDescriptor(); + } + } + + private static ImageDescriptor createManaged(String prefix, String name, String key) { + try { + ImageDescriptor result= ImageDescriptor.createFromURL(makeIconFileURL(prefix, name.substring(NAME_PREFIX_LENGTH))); + if (fgAvoidSWTErrorMap == null) { + fgAvoidSWTErrorMap= new HashMap(); + } + fgAvoidSWTErrorMap.put(key, result); + if (fgImageRegistry != null) { + PHPeclipsePlugin.logErrorMessage("Image registry already defined"); //$NON-NLS-1$ + } return result; } catch (MalformedURLException e) { return ImageDescriptor.getMissingImageDescriptor(); diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/EditElementWizard.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/EditElementWizard.java index 3535053..1e965a9 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/EditElementWizard.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/EditElementWizard.java @@ -1,5 +1,5 @@ /* - * $Id: EditElementWizard.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $ + * $Id: EditElementWizard.java,v 1.2 2005-07-08 21:30:33 axelcl Exp $ * Copyright Narushima Hironori. All rights reserved. */ package net.sourceforge.phpeclipse.wizards.html; @@ -43,7 +43,7 @@ public class EditElementWizard extends Wizard { this.targetElemName = targetElemName; setWindowTitle("Edit HTML Element"); - setDefaultPageImageDescriptor(PHPUiImages.getImageRegistry().getDescriptor("wizban/editelem_wiz.gif")); + setDefaultPageImageDescriptor(PHPUiImages.getDescriptor("wizban/editelem_wiz.gif")); setForcePreviousAndNextButtons(true); } -- 1.7.1