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 ; 9 import java.net.MalformedURLException ; 10 import java.awt.MediaTracker ; 11 import javax.swing.ImageIcon ; 12 import javax.swing.GrayFilter ; 13 14 20 public class ImageField extends Field { 21 22 public static final String TYPE_STRING = "image"; 23 24 protected URL imageURL; 25 protected ImageIcon imageIcon; 26 protected ImageIcon hiddenImageIcon; 27 28 38 public ImageField(Long id, Report report, Section section, Object value, 39 boolean visible) 40 { 41 super(id, report, section, null, visible); 42 setValue(value); 43 } 44 45 48 public double getOutputHeight() { 49 return bounds.height; 50 } 51 52 57 public URL getImageURL() { return imageURL; } 58 59 64 public ImageIcon getImageIcon() { 65 if (isVisible()) 66 return getVisibleImageIcon(); 67 else 68 return getHiddenImageIcon(); 69 } 70 71 76 public ImageIcon getVisibleImageIcon() { 77 if (imageIcon == null && value != null) 78 imageIcon = new ImageIcon (getImageURL()); 79 return imageIcon; 80 } 81 82 87 public ImageIcon getHiddenImageIcon() { 88 if (hiddenImageIcon == null && value != null && canLoad()) { 89 ImageIcon ii = getVisibleImageIcon(); 90 if (ii != null) { 91 hiddenImageIcon = 92 new ImageIcon (GrayFilter.createDisabledImage(ii.getImage())); 93 } 94 } 95 return hiddenImageIcon; 96 } 97 98 103 public void setValue(Object newValue) { 104 imageIcon = null; 105 String str = newValue.toString(); 106 107 if (str.indexOf(":/") == -1 && !str.startsWith("file:")) 110 str = "file:" + str; 111 112 try { 113 imageURL = (str == null || str.length() == 0) ? null : new URL (str); 114 super.setValue(newValue); } 116 catch (MalformedURLException 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 dragString() { 131 return typeString() + ":" + value; 132 } 133 134 public String typeString() { return TYPE_STRING; } 135 136 public String formulaString() { return "{" + value + "}"; } 137 138 public String toString() { 139 if (!visible) return null; 140 141 Object v = getValue(); 142 return v == null ? "" : v.toString(); 143 } 144 145 } 146 | Popular Tags |