001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010: import java.net.*;
011: import java.awt.event.*;
012:
013: import com.javelin.swinglets.event.*;
014:
015: /**
016: * SIcon defines an icon that can be loaded from a URL or in memory
017: * image. This can have areas added to it to make it an ImageMap.
018: *
019: * @author Robin Sharp
020: */
021:
022: public class SIcon extends SComponent {
023:
024: /**
025: * Construct an empty SIcon.
026: * <p>
027: * setImage() OR setUrl() will set the image.
028: */
029: public SIcon() {
030: }
031:
032: /**
033: * Creates a <code>SIcon</code> instance with the instance of the specified image.
034: * This will create a gif encoder in the ImageManager for the image.
035: */
036: public SIcon(SImage image) {
037: this .url = ImageManager.getManager().getUrl().toExternalForm()
038: + "/?" + SConstants.IMAGE_NAME + "=" + image.getName();
039: setSize(image.getWidth(), image.getHeight());
040:
041: ImageManager.getManager().put(image.getName(), image);
042: imageName = image.getName();
043: }
044:
045: /**
046: * Creates a <code>SIcon</code> instance with the instance of the specified image.
047: */
048: public SIcon(SImage image, int width, int height) {
049: this .url = ImageManager.getManager().getUrl().toExternalForm()
050: + "/?" + SConstants.IMAGE_NAME + "=" + getName();
051:
052: setSize(width, height);
053:
054: ImageManager.getManager().put(getName(), image);
055: imageName = image.getName();
056: }
057:
058: /**
059: * Creates a <code>SIcon</code> instance with the instance of the specified image.
060: * This will add SConstants.getRealPath() to the file to get the URL.
061: */
062: public SIcon(String file) {
063: this (file, null);
064: }
065:
066: /**
067: * Creates a <code>SIcon</code> instance with the instance of the specified image
068: * and hypelink.<br>
069: * This will add SConstants.getRealPath() to the file to get the URL..
070: */
071: public SIcon(String file, SLink link) {
072: setFile(file);
073: setLink(link);
074: }
075:
076: /**
077: * Creates a <code>SIcon</code> instance with the instance of the specified image,
078: * and size. <br>
079: * This will add SConstants.getRealPath() to the file to get the URL.
080: */
081: public SIcon(String file, int width, int height) {
082: this (file, width, height, null);
083: }
084:
085: /**
086: * Creates a <code>SIcon</code> instance with the instance of the specified image,
087: * size and hyperlink.<br>
088: * This will add SConstants.getRealPath() to the file to get the URL.
089: */
090: public SIcon(String file, int width, int height, SLink link) {
091: setFile(file);
092:
093: setSize(width, height);
094: this .link = link;
095: }
096:
097: /**
098: * Returns the name of the L&F class that renders this component.
099: */
100: public Class getUIClass() {
101: return SIcon.class;
102: }
103:
104: /**
105: * Get the image's URL.<br>
106: */
107: public String getUrl() {
108: if (file != null && url == null) {
109: url = getSwingletManager().getImagePath() + file;
110: }
111:
112: return url;
113: }
114:
115: /**
116: * Get the url directly.<br>
117: */
118: public SIcon setUrl(String url) {
119: imageName = null;
120:
121: firePropertyChange("url", this .url, this .url = url);
122:
123: return this ;
124: }
125:
126: /**
127: * Set the image's File. The SConstants.getRealPath() is added to the File
128: * to creat the URL.
129: */
130: public String getFile() {
131: return file;
132: }
133:
134: /**
135: * Set the image's File. The SConstants.getRealPath() is added to the File to
136: * create the URL.
137: */
138: public SIcon setFile(String file) {
139: url = null;
140: imageName = null;
141:
142: firePropertyChange("file", this .file, this .file = file);
143: return this ;
144: }
145:
146: /**
147: * Get the file name from th url, minus the path and the extension.
148: * This can be used for display purposes.
149: */
150: public String getFileName() {
151: if (url != null) {
152: int slashIndex = url.lastIndexOf('/');
153: if (slashIndex < 0)
154: slashIndex = 0;
155:
156: int dotIndex = url.lastIndexOf('.');
157: if (dotIndex < 0) {
158: return url.substring(slashIndex + 1);
159: } else {
160: return url.substring(slashIndex + 1, dotIndex);
161: }
162: }
163:
164: return null;
165: }
166:
167: /**
168: * Set the URL (and size) though the image.
169: */
170: public SIcon setImage(SImage image) {
171: setSize(image.getWidth(), image.getHeight());
172:
173: if (imageName != null) {
174: ImageManager.getManager().put(imageName, null);
175: }
176:
177: ImageManager.getManager().put(image.getName(), image);
178: imageName = image.getName();
179:
180: firePropertyChange("url", this .url, this .url = ImageManager
181: .getManager().getUrl().toExternalForm()
182: + "/?" + SConstants.IMAGE_NAME + "=" + image.getName());
183:
184: return this ;
185: }
186:
187: /**
188: * Set the Hyper Link.
189: */
190: public SIcon setLink(SLink link) {
191: firePropertyChange("link", this .link, this .link = link);
192: return this ;
193: }
194:
195: /**
196: * Get the Hyper Link.
197: */
198: public SLink getLink() {
199: return link;
200: }
201:
202: /**
203: * Returns the icon that this button displays when rolled over.
204: */
205: public SIcon getRolloverIcon() {
206: return rolloverIcon;
207: }
208:
209: /**
210: * Defines the icon this button will display when rolled over.
211: */
212: public SIcon setRolloverIcon(SIcon rolloverIcon) {
213: firePropertyChange("rolloverIcon", this .rolloverIcon,
214: this .rolloverIcon = rolloverIcon);
215: return this ;
216: }
217:
218: public Vector getRolloverTargetPairs() {
219: return targetPairs;
220: }
221:
222: /**
223: * Makes another icon change when we rollover
224: */
225: public SIcon addRolloverTarget(SIcon targeticon, SIcon rolloverIcon) {
226: if (targetPairs == null) {
227: targetPairs = new Vector();
228: }
229:
230: targetPairs
231: .addElement(new Object[] { targeticon, rolloverIcon });
232:
233: return this ;
234: }
235:
236: /**
237: * Add an SArea to the image to make it an image map
238: */
239: public void addArea(SArea area) {
240: if (areas == null) {
241: areas = new Vector();
242: }
243:
244: areas.addElement(area);
245: }
246:
247: /**
248: * Remove an Area from an Icon
249: */
250: public void removeArea(SArea area) {
251: if (areas == null)
252: return;
253: areas.removeElement(area);
254: }
255:
256: /**
257: * Get an Area count
258: */
259: public int getAreaCount() {
260: if (areas == null)
261: return 0;
262: return areas.size();
263: }
264:
265: /**
266: * Get the SAreas from an Icon.
267: */
268: public Enumeration getAreas() {
269: if (areas == null) {
270: areas = new Vector();
271: }
272:
273: return areas.elements();
274: }
275:
276: /**
277: * Remove all the SAreas from the SIcon
278: */
279: public void removeAllAreas() {
280: if (areas == null)
281: return;
282: areas.removeAllElements();
283: }
284:
285: /**
286: * Get the SImage associated with this Icon, if this is a
287: * url load the image from the url.
288: */
289: public SImage getImage() {
290: SImage image = ImageManager.getManager().get(getName());
291:
292: if (image == null) {
293: try {
294: ImageManager.getManager().put(
295: getName(),
296: new SImage(getSwingletManager().getImagePath()
297: + url));
298: } catch (Exception e) {
299: e.printStackTrace();
300: }
301: }
302:
303: return image;
304: }
305:
306: /**
307: * Processes events occurring on this component.
308: */
309: protected void processEvent(AWTEvent event) {
310: if (event instanceof FormEvent) {
311: processActionEvent(new ActionEvent(this ,
312: ActionEvent.ACTION_PERFORMED, null));
313: } else {
314: super .processEvent(event);
315: }
316: }
317:
318: // PROTECTED ////////////////////////////////////////////////////////
319:
320: protected String imageName;
321:
322: protected String file;
323: protected String url;
324: protected SLink link;
325: protected Vector areas;
326: protected SIcon rolloverIcon;
327: protected Vector targetPairs;
328:
329: }
|