private static ImageIcon getIconFromData(final ComponentDescriptor<?> componentDescriptor, final int width) { final String cacheKey = "remote: " + componentDescriptor.getDisplayName() + ",width=" + width; final Image image = _imageManager.getImageFromCache(cacheKey); ImageIcon imageIcon; if (image == null) { final HasIcon descriptorWithIcon = (HasIcon) componentDescriptor; if (descriptorWithIcon.getIconData() == null || descriptorWithIcon.getIconData().length == 0) { // If no data from server, use generic icon. (Then it will be possible to overlay it) final ClassLoader classLoader = componentDescriptor.getComponentClass().getClassLoader(); final String imagePath = getDescriptorImagePath(componentDescriptor, classLoader, true); if (imagePath == null) { return null; } imageIcon = _imageManager.getImageIcon(imagePath, width, classLoader); } else { imageIcon = new ImageIcon(descriptorWithIcon.getIconData()); } final BufferedImage bufferedImage = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB); bufferedImage.getGraphics().drawImage(imageIcon.getImage(), 0, 0, width, width, null); imageIcon = new ImageIcon(bufferedImage); _imageManager.storeImageIntoCache(cacheKey, imageIcon.getImage()); } else { imageIcon = new ImageIcon(image); } return imageIcon; }
/** * Gets the icon of a component based on it's {@link ComponentDescriptor}. * * @param descriptor * @param newWidth * @param allowTransparentForUnspecific whether or not to use a transparent icon in case only a generic icon could * be found. This is useful for menu items where the icon is not a requirement. * @return */ public static ImageIcon getDescriptorIcon(final ComponentDescriptor<?> descriptor, final int newWidth, final boolean allowTransparentForUnspecific) { if (descriptor instanceof HasIcon) { final ImageIcon imageIcon = getIconFromData(descriptor, newWidth); if (imageIcon != null) { return imageIcon; } } final ClassLoader classLoader = descriptor.getComponentClass().getClassLoader(); final boolean allowGeneric = !allowTransparentForUnspecific; final String imagePath = getDescriptorImagePath(descriptor, classLoader, allowGeneric); if (imagePath == null) { return getTransparentIcon(newWidth); } return _imageManager.getImageIcon(imagePath, newWidth, classLoader); }