001: package jimm.datavision.field;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.Section;
005: import jimm.datavision.ErrorHandler;
006: import jimm.datavision.gui.FieldWidget;
007: import jimm.datavision.gui.ImageFieldWidget;
008: import jimm.datavision.gui.SectionWidget;
009: import java.net.URL;
010: import java.net.MalformedURLException;
011: import java.awt.MediaTracker;
012: import javax.swing.ImageIcon;
013: import javax.swing.GrayFilter;
014:
015: /**
016: * Represents an external image. The <code>value</code> instance value
017: * stores the images file's path.
018: *
019: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
020: */
021: public class ImageField extends Field {
022:
023: public static final String TYPE_STRING = "image";
024:
025: protected URL imageURL;
026: protected ImageIcon imageIcon;
027: protected ImageIcon hiddenImageIcon;
028:
029: /**
030: * Constructor. We make sure the value (a file path) is an absolute file
031: * path.
032: *
033: * @param id the unique identifier for the new field
034: * @param report the report containing this line
035: * @param section the section containing this line
036: * @param value the value; a file path string
037: * @param visible show/hide flag
038: */
039: public ImageField(Long id, Report report, Section section,
040: Object value, boolean visible) {
041: super (id, report, section, null, visible);
042: setValue(value);
043: }
044:
045: /**
046: * Always returns the bounds height.
047: */
048: public double getOutputHeight() {
049: return bounds.height;
050: }
051:
052: /**
053: * Returns the image URL.
054: *
055: * @return the image URL
056: */
057: public URL getImageURL() {
058: return imageURL;
059: }
060:
061: /**
062: * Returns the image icon, visually dimmed if the field is hidden.
063: *
064: * @return the image icon, dimmed if the field is not visible
065: */
066: public ImageIcon getImageIcon() {
067: if (isVisible())
068: return getVisibleImageIcon();
069: else
070: return getHiddenImageIcon();
071: }
072:
073: /**
074: * Returns the image icon.
075: *
076: * @return the image icon
077: */
078: public ImageIcon getVisibleImageIcon() {
079: if (imageIcon == null && value != null)
080: imageIcon = new ImageIcon(getImageURL());
081: return imageIcon;
082: }
083:
084: /**
085: * Returns a dimmed version of the the image icon.
086: *
087: * @return a dimmed version of the the image icon
088: */
089: public ImageIcon getHiddenImageIcon() {
090: if (hiddenImageIcon == null && value != null && canLoad()) {
091: ImageIcon ii = getVisibleImageIcon();
092: if (ii != null) {
093: hiddenImageIcon = new ImageIcon(GrayFilter
094: .createDisabledImage(ii.getImage()));
095: }
096: }
097: return hiddenImageIcon;
098: }
099:
100: /**
101: * Sets our value and image URL.
102: * <p>
103: *
104: */
105: public void setValue(Object newValue) {
106: imageIcon = null;
107: String str = newValue.toString();
108:
109: // If the string is not a URL, add "file:" to the beginning of the
110: // string.
111: if (str.indexOf(":/") == -1 && !str.startsWith("file:"))
112: str = "file:" + str;
113:
114: try {
115: imageURL = (str == null || str.length() == 0) ? null
116: : new URL(str);
117: super .setValue(newValue); // Notify observers
118: } catch (MalformedURLException e) {
119: ErrorHandler.error(e);
120: }
121: }
122:
123: public boolean canLoad() {
124: return getVisibleImageIcon() != null
125: && getVisibleImageIcon().getImageLoadStatus() == MediaTracker.COMPLETE;
126: }
127:
128: public FieldWidget makeWidget(SectionWidget sw) {
129: return new ImageFieldWidget(sw, this );
130: }
131:
132: public String dragString() {
133: return typeString() + ":" + value;
134: }
135:
136: public String typeString() {
137: return TYPE_STRING;
138: }
139:
140: public String formulaString() {
141: return "{" + value + "}";
142: }
143:
144: public String toString() {
145: if (!visible)
146: return null;
147:
148: Object v = getValue();
149: return v == null ? "" : v.toString();
150: }
151:
152: }
|