001: /*
002: * $Id: JGraphpadImageIcon.java,v 1.2 2005/08/17 21:20:29 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * This file is licensed under the JGraph software license, a copy of which
008: * will have been provided to you in the file LICENSE at the root of your
009: * installation directory. If you are unable to locate this file please
010: * contact JGraph sales for another copy.
011: */
012: package com.jgraph.pad.util;
013:
014: import java.io.File;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017:
018: import javax.swing.ImageIcon;
019:
020: import com.jgraph.editor.JGraphEditorResources;
021:
022: /**
023: * This class transform a <CODE>ImageIcon</CODE> into a bean, allowing for
024: * encoding and decoding in XML using <CODE>XMLEncoder</CODE> and <CODE>
025: * XMLDecoder</CODE>.
026: */
027: public class JGraphpadImageIcon extends ImageIcon {
028:
029: private String filename = null;
030:
031: /**
032: * Creates an uninitialized image icon.
033: */
034: public JGraphpadImageIcon() {
035: super ();
036: }
037:
038: /**
039: * Creates an image icon from the specified file. The image will be
040: * preloaded by using <CODE>MediaTracker</CODE> to monitor the loading
041: * answer of the image. The specified <CODE>String</CODE> can be a file
042: * name or a file path. When specifying a path, use the Internet-standard
043: * forward-slash ("/") as a separator. (The string is converted to an <CODE>
044: * URL</CODE>, so the forward-slash works on all systems.) For example,
045: * specify:
046: *
047: * <PRE>
048: *
049: * new JGraphpadImageIcon("images/myImage.gif")
050: *
051: * </PRE>
052: *
053: * The <CODE>description</CODE> is initialized to the filename string.
054: *
055: * @param filename
056: * A <CODE>String</CODE> specifying a filename or path.
057: */
058: public JGraphpadImageIcon(String filename) {
059: setFileName(filename);
060: }
061:
062: /**
063: * Creates an image icon from the specified file. The image will be
064: * preloaded by using <CODE>MediaTracker</CODE> to monitor the loading
065: * answer of the image.
066: *
067: * @param filename
068: * The name of the file containing the image.
069: * @param description
070: * A brief textual description of the image.
071: */
072: public JGraphpadImageIcon(String filename, String description) {
073: setDescription(description);
074: setFileName(filename);
075: }
076:
077: /**
078: * Creates an image icon from the specified <CODE>URL</CODE>. The image
079: * will be preloaded by using <CODE>MediaTracker</CODE> to monitor the
080: * loaded answer of the image. The icon's <CODE>description</CODE> is
081: * initialized to be a string representation of the URL.
082: *
083: * @param location
084: * The <CODE>URL</CODE> for the image.
085: */
086: public JGraphpadImageIcon(URL location) {
087: setFileName(location.toExternalForm());
088: }
089:
090: /**
091: * Creates an image icon from the specified <CODE>URL</CODE>. The image
092: * will be preloaded by using <CODE>MediaTracker</CODE> to monitor the
093: * loaded answer of the image.
094: *
095: * @param location
096: * The URL for the image.
097: * @param description
098: * A brief textual description of the image.
099: */
100: public JGraphpadImageIcon(URL location, String description) {
101: setDescription(description);
102: setFileName(location.toExternalForm());
103: }
104:
105: /**
106: * Returns the file name used to initialize the image.
107: */
108: public String getFileName() {
109: return filename;
110: }
111:
112: /**
113: * Initializes this image icon from the specified file. The image will be
114: * preloaded by using <CODE>MediaTracker</CODE> to monitor the loading
115: * answer of the image. The specified <CODE>String</CODE> can be a file
116: * name or a file path. When specifying a path, use the Internet-standard
117: * forward-slash ("/") as a separator. (The string is converted to an <CODE>
118: * URL</CODE>, so the forward-slash works on all systems.) For example,
119: * specify:
120: *
121: * <PRE>
122: *
123: * new BeanifiedIcon().setFileName("images/myImage.gif")
124: *
125: * </PRE>
126: *
127: * @param filename
128: * A <CODE>String</CODE> specifying a filename or path.
129: */
130: public void setFileName(String filename) {
131:
132: // Try classpath first
133: ImageIcon icon = JGraphEditorResources.getImage(filename);
134: if (icon != null) {
135: this .filename = filename;
136: setImage(icon.getImage());
137: } else {
138:
139: URL loadableName;
140:
141: try {
142: try {
143: loadableName = new URL(filename);
144: } catch (MalformedURLException ex) {
145: loadableName = new File(filename).toURL();
146: }
147: this .filename = loadableName.toExternalForm();
148: } catch (Exception ex) {
149: return;
150: }
151:
152: if (getDescription() == null)
153: setDescription(filename);
154:
155: icon = JGraphEditorResources.getImage(this.filename);
156: if (icon != null)
157: setImage(icon.getImage());
158:
159: }
160:
161: }
162:
163: }
|