/* * * Perforce/JBuilder Opentool * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package com.dafreels.vcs.util; import java.util.Hashtable; import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import javax.swing.GrayFilter; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JLabel; /** * Title: Perforce OpenTool * Description: * Copyright: Copyright (c) 2002 * Company: * @author Shannon J. Newbold * @version 1.0 */ public class ImageFactory { public static Hashtable disabledImageTable = new Hashtable(); public static MediaTracker mTracker = new MediaTracker(new JLabel()); /** * return an ImageIcon from the complete or relative path */ public static javax.swing.ImageIcon getImageIcon(String name ) { if ( name == null || name.length() == 0 ) return null; try { java.net.URL url = ClassLoader.getSystemResource(name); if ( url == null ) return null; java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit(); if ( tk == null ) return null; java.awt.Image image = tk.getImage(url); if ( image == null ) return null; return new javax.swing.ImageIcon(image); } catch (Exception e) { System.out.println("getImageIcon: Error loading " + name +" Error: " + e); e.printStackTrace(); } return null; } /** * The method creates a factory that allows the interation over a 1 or 2 dimmensional * "array" of same sized icons in an image. * @param name the name of the image to load * @param width the width of every image * @param height the height of every image. * @returns a new factory that draws the images. Returns null if there was a problem loading the image. */ public static CompositeIconFactory getCompositeIconFactory(String name, int width, int height) { javax.swing.ImageIcon img = getImageIcon(name); if(img == null) return null; return new CompositeIconFactory(img.getImage(),width,height); } /** * Creates a new image that is a disabled version of the given image. * Returns null if the input image was null or there was a problem with the * GrayFilter. */ public static synchronized Image getDisabledImage(Image image) { if (image == null) return null; Image dis_img = (Image)disabledImageTable.get(image); if(dis_img == null) { dis_img = GrayFilter.createDisabledImage(image); waitForImage(dis_img); disabledImageTable.put(image,dis_img); } return dis_img; } /** * Creates a new icon that is a disabled version of the given icon. * Returns null if the input image was null or there was a problem with the * GrayFilter. */ public static synchronized Icon getDisabledIcon(Icon icon) { if (icon == null) return null; Icon dis_icon = (Icon)disabledImageTable.get(icon); if(dis_icon == null) { Image img; if(icon instanceof ImageIcon) { img = ((ImageIcon)icon).getImage(); } else { img = new java.awt.image.BufferedImage(icon.getIconWidth(),icon.getIconHeight(),2); Graphics g = img.getGraphics(); icon.paintIcon(null,g,0,0); } img = GrayFilter.createDisabledImage(img); dis_icon = new ImageIcon(img); disabledImageTable.put(icon,dis_icon); } return dis_icon; } private static void waitForImage(Image img) { try { mTracker.addImage(img,0); mTracker.waitForID(0); mTracker.removeImage(img,0); } catch (Exception e) { } } private class q extends Component { } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 4090 | David Freels |
Fixed bug where paths with spaces would not work. Also checked in new code or JBuilderX. |