KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > field > ImageField


1 package jimm.datavision.field;
2 import jimm.datavision.Report;
3 import jimm.datavision.Section;
4 import jimm.datavision.ErrorHandler;
5 import jimm.datavision.gui.FieldWidget;
6 import jimm.datavision.gui.ImageFieldWidget;
7 import jimm.datavision.gui.SectionWidget;
8 import java.net.URL JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.awt.MediaTracker JavaDoc;
11 import javax.swing.ImageIcon JavaDoc;
12 import javax.swing.GrayFilter JavaDoc;
13
14 /**
15  * Represents an external image. The <code>value</code> instance value
16  * stores the images file's path.
17  *
18  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
19  */

20 public class ImageField extends Field {
21
22 public static final String JavaDoc TYPE_STRING = "image";
23
24 protected URL JavaDoc imageURL;
25 protected ImageIcon JavaDoc imageIcon;
26 protected ImageIcon JavaDoc hiddenImageIcon;
27
28 /**
29  * Constructor. We make sure the value (a file path) is an absolute file
30  * path.
31  *
32  * @param id the unique identifier for the new field
33  * @param report the report containing this line
34  * @param section the section containing this line
35  * @param value the value; a file path string
36  * @param visible show/hide flag
37  */

38 public ImageField(Long JavaDoc id, Report report, Section section, Object JavaDoc value,
39           boolean visible)
40 {
41     super(id, report, section, null, visible);
42     setValue(value);
43 }
44
45 /**
46  * Always returns the bounds height.
47  */

48 public double getOutputHeight() {
49     return bounds.height;
50 }
51
52 /**
53  * Returns the image URL.
54  *
55  * @return the image URL
56  */

57 public URL JavaDoc getImageURL() { return imageURL; }
58
59 /**
60  * Returns the image icon, visually dimmed if the field is hidden.
61  *
62  * @return the image icon, dimmed if the field is not visible
63  */

64 public ImageIcon JavaDoc getImageIcon() {
65     if (isVisible())
66     return getVisibleImageIcon();
67     else
68     return getHiddenImageIcon();
69 }
70
71 /**
72  * Returns the image icon.
73  *
74  * @return the image icon
75  */

76 public ImageIcon JavaDoc getVisibleImageIcon() {
77     if (imageIcon == null && value != null)
78     imageIcon = new ImageIcon JavaDoc(getImageURL());
79     return imageIcon;
80 }
81
82 /**
83  * Returns a dimmed version of the the image icon.
84  *
85  * @return a dimmed version of the the image icon
86  */

87 public ImageIcon JavaDoc getHiddenImageIcon() {
88     if (hiddenImageIcon == null && value != null && canLoad()) {
89     ImageIcon JavaDoc ii = getVisibleImageIcon();
90     if (ii != null) {
91         hiddenImageIcon =
92         new ImageIcon JavaDoc(GrayFilter.createDisabledImage(ii.getImage()));
93     }
94     }
95     return hiddenImageIcon;
96 }
97
98 /**
99  * Sets our value and image URL.
100  * <p>
101  *
102  */

103 public void setValue(Object JavaDoc newValue) {
104     imageIcon = null;
105     String JavaDoc str = newValue.toString();
106
107     // If the string is not a URL, add "file:" to the beginning of the
108
// string.
109
if (str.indexOf(":/") == -1 && !str.startsWith("file:"))
110     str = "file:" + str;
111
112     try {
113     imageURL = (str == null || str.length() == 0) ? null : new URL JavaDoc(str);
114     super.setValue(newValue); // Notify observers
115
}
116     catch (MalformedURLException JavaDoc e) {
117     ErrorHandler.error(e);
118     }
119 }
120
121 public boolean canLoad() {
122     return getVisibleImageIcon() != null
123     && getVisibleImageIcon().getImageLoadStatus() == MediaTracker.COMPLETE;
124 }
125
126 public FieldWidget makeWidget(SectionWidget sw) {
127     return new ImageFieldWidget(sw, this);
128 }
129
130 public String JavaDoc dragString() {
131     return typeString() + ":" + value;
132 }
133
134 public String JavaDoc typeString() { return TYPE_STRING; }
135
136 public String JavaDoc formulaString() { return "{" + value + "}"; }
137
138 public String JavaDoc toString() {
139     if (!visible) return null;
140
141     Object JavaDoc v = getValue();
142     return v == null ? "" : v.toString();
143 }
144
145 }
146
Popular Tags