xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/type/ImageRef.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 /*
30  *        Copyright (C) 1996  Active Software, Inc.
31  *                  All rights reserved.
32  *
33  * @(#) ImageRef.java 1.17 - last change made 08/07/96
34  */
35 
36 package sunsoft.jws.visual.rt.type;
37 
38 import sunsoft.jws.visual.rt.base.Global;
39 import sunsoft.jws.visual.rt.base.VJException;
40 
41 import java.awt.Component;
42 import java.awt.Image;
43 import java.awt.MediaTracker;
44 import java.awt.image.ImageObserver;
45 import java.applet.Applet;
46 import java.net.URL;
47 import java.net.MalformedURLException;
48 
49 /**
50  * Stores an image and the URL or filename that it came from.  This
51  * class is capable of converting a filename to a URL and will do so
52  * in order to load the image using a "file:" URL.  The creation of
53  * the image is delayed until the image is requested.
54  *
55  * @see Image
56  * @version 1.17, 08/07/96
57  */
58 public class ImageRef implements ImageObserver {
59     private Image img;
60     private String filename;
61     private URL url;
62     private int imgWidth, imgHeight;
63     private boolean gotWidth, gotHeight;
64     private boolean errorFlagged = false;	// set in imageUpdate
65     /* JSTYLED */
66     private boolean disableErrorPrint = false;	// allow println in imageUpdate
67 
68     /**
69      * Constructs a new instance of ImageRef that use a
70      * URL to find the image when it is requested.
71      * The size of the image
72      * cannot be appended to the URL.
73      */
ImageRef(URL url)74     public ImageRef(URL url) {
75         init(null, url, -1, -1);
76     }
77 
78     /**
79      * Constructs a new instance of ImageRef given the name of a file
80      * for the image.  The filename may be relative to the codebase or
81      * any of the directories in the classpath.  The size of the image
82      * may be appended to the filename to help with initial layout of
83      * widgets containing images, like this: "imagefile.gif;24x48".
84      */
ImageRef(String filename)85     public ImageRef(String filename) {
86         int index = filename.indexOf(/* NOI18N */ ';');
87         if (index != -1) {
88             String dims = filename.substring(index+1);
89             filename = filename.substring(0, index);
90 
91             index = dims.indexOf(/* NOI18N */ 'x');
92 
93             try {
94                 int w, h;
95 
96                 w = Integer.parseInt(dims.substring(0, index));
97                 h = Integer.parseInt(dims.substring(index+1));
98 
99                 init(filename, null, w, h);
100             } catch (Exception e) {
101                 init(filename, null, -1, -1);
102             }
103         } else {
104             init(filename, null, -1, -1);
105         }
106     }
107 
init(String filename, URL url, int imgWidth, int imgHeight)108     private void init(String filename, URL url, int imgWidth,
109 		      int imgHeight) {
110         this.img = null;
111         this.filename = filename;
112         this.url = url;
113         this.imgWidth = imgWidth;
114         this.imgHeight = imgHeight;
115     }
116 
117     /**
118      * Gets the image stored here (or referenced by the URL).  */
getImage(Component comp, Applet applet)119     public Image getImage(Component comp, Applet applet) {
120         try {
121             cacheImage(comp, applet);
122         }
123         catch (VJException vje) {
124             return null;
125         }
126         return img;
127     }
128 
129     /**
130      * Returns the width of the image.  If the image is not yet loaded,
131      * then returns the expected width of the image.
132      */
getWidth(Component comp, Applet applet)133     public int getWidth(Component comp, Applet applet) {
134         try {
135             cacheImage(comp, applet);
136         }
137         catch (VJException vje) {
138             return 0;
139         }
140         return imgWidth;
141     }
142 
143     /**
144      * Returns the height of the image.  If the image is not
145      * yet loaded,
146      * then returns the expected height of the image.
147      */
getHeight(Component comp, Applet applet)148     public int getHeight(Component comp, Applet applet) {
149         try {
150             cacheImage(comp, applet);
151         }
152         catch (VJException vje) {
153             return 0;
154         }
155         return imgHeight;
156     }
157 
158     /**
159      * Returns the URL stored here.
160      */
getURL()161     public URL getURL() {
162         return (url);
163     }
164 
165     /**
166      * Returns the file name of the image.
167      */
getFileName()168     public String getFileName() {
169         String name;
170 
171         if (filename != null)
172             name = filename;
173         else
174             name = getURL().toExternalForm();
175 
176         return name;
177     }
178 
179     /**
180      * Returns the preferred string representation of this
181      * image reference.
182     */
toString()183     public String toString() {
184         String s = getFileName();
185 
186         if (imgWidth != -1 && imgHeight != -1)
187             s = s + /* NOI18N */";" + imgWidth + /* NOI18N */"x"
188 		+ imgHeight;
189 
190         return s;
191     }
192 
193     /**
194      * Start loading the image if we haven't already.
195      * Attempt to cache the
196      * width and height of the image.
197      */
cacheImage(Component comp, Applet applet)198     private void cacheImage(Component comp, Applet applet)
199 	throws VJException {
200         if (img != null)
201             return;
202 
203         if (url == null) {
204             url = Global.util.pathToURL(filename, applet);
205             if (url == null)
206 		/* BEGIN JSTYLED */
207 		throw new VJException(Global.fmtMsg(
208 						    "sunsoft.jws.visual.rt.type.ImageRef.FMT.32",
209 						    Global.getMsg("sunsoft.jws.visual.rt.type.ImageRef.could__not__find__file__.32"),
210 						    filename,
211 						    Global.getMsg("sunsoft.jws.visual.rt.type.ImageRef.-ba--qu-__relative__to__class.33")));
212 	    /* END JSTYLED */
213 	}
214 
215         img = comp.getToolkit().getImage(url);
216         if (img == null)
217             return;
218 
219         int w = img.getWidth(this);
220         if (w != -1)
221             imgWidth = w;
222 
223         int h = img.getHeight(this);
224         if (h != -1)
225             imgHeight = h;
226     }
227 
228     /**
229      * Verifies that the image for this image ref loaded successfully,
230      * returns true if it does.  Warning: will wait until image is
231      * loaded before returning, so you shouldn't make this call unless
232      * you are really interested in reporting an error message when
233      * images can't be loaded.
234      */
verifyImage(Component comp, Applet applet)235     public boolean verifyImage(Component comp, Applet applet) {
236         disableErrorPrint = true;
237 
238         if (img != null) {
239             // image has already been set up
240             if (errorFlagged)
241                 return false;
242             if (gotWidth && gotHeight)
243                 return true;
244         } else {
245             // set up the image
246             try {
247                 cacheImage(comp, applet);
248             }
249             catch (VJException vje) {
250                 return false;
251             }
252             return true;
253         }
254 
255         // start loading the image and wait for it to finish
256         MediaTracker tracker = new MediaTracker(comp);
257         tracker.addImage(img, 0);
258         try {
259             tracker.waitForID(0);
260         }
261         catch (InterruptedException e) {
262             return false;
263         }
264         return ((tracker.statusID(0, false)
265 		 & MediaTracker.ERRORED) == 0);
266     }
267 
268     /**
269      * Gets called when an update of the image's width
270      * and height are available.
271     */
imageUpdate(Image img, int infoflags, int x, int y, int width, int height)272     public boolean imageUpdate(Image img, int infoflags,
273 			       int x, int y, int width, int height) {
274         if (((infoflags & ERROR) != 0) && !errorFlagged) {
275             if (!disableErrorPrint)
276                 /* JSTYLED */
277 		System.out.println(Global.getMsg("sunsoft.jws.visual.rt.type.ImageRef.Error-co-__could__not__loa.34")
278 				   + getFileName() + /* NOI18N */"\"");
279             errorFlagged = true;
280         }
281 
282         if ((infoflags & WIDTH) != 0) {
283             gotWidth = true;
284             imgWidth = width;
285         }
286 
287         if ((infoflags & HEIGHT) != 0) {
288             gotHeight = true;
289             imgHeight = height;
290         }
291 
292         return (gotWidth && gotHeight);
293     }
294 }
295