1 19 20 21 package org.netbeans.modules.image; 22 23 24 import java.awt.Graphics ; 25 import java.awt.Image ; 26 import java.awt.Rectangle ; 27 import java.beans.PropertyEditor ; 28 import java.beans.PropertyEditorSupport ; 29 import java.io.BufferedInputStream ; 30 import java.io.IOException ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.net.URL ; 33 import javax.swing.Icon ; 34 import javax.swing.ImageIcon ; 35 36 import org.openide.actions.OpenAction; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileStateInvalidException; 39 import org.openide.loaders.*; 40 import org.openide.nodes.*; 41 import org.openide.ErrorManager; 42 import org.openide.util.actions.SystemAction; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.NbBundle; 45 46 47 52 public class ImageDataObject extends MultiDataObject implements CookieSet.Factory { 53 54 55 static final long serialVersionUID = -6035788991669336965L; 56 57 58 private static final String IMAGE_ICON_BASE = "org/netbeans/modules/image/imageObject.png"; 60 61 private transient ImageOpenSupport openSupport; 62 63 private transient ImagePrintSupport printSupport; 64 65 70 public ImageDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException { 71 super(pf, loader); 72 73 getCookieSet().add(ImageOpenSupport.class, this); 74 getCookieSet().add(ImagePrintSupport.class, this); 75 } 76 77 78 79 public Node.Cookie createCookie(Class clazz) { 80 if(clazz.isAssignableFrom(ImageOpenSupport.class)) 81 return getOpenSupport(); 82 else if( clazz.isAssignableFrom(ImagePrintSupport.class)) 83 return getPrintSupport(); 84 else 85 return null; 86 } 87 88 89 private ImageOpenSupport getOpenSupport() { 90 if(openSupport == null) { 91 synchronized(this) { 92 if(openSupport == null) 93 openSupport = new ImageOpenSupport(getPrimaryEntry()); 94 } 95 } 96 97 return openSupport; 98 } 99 100 protected ImagePrintSupport getPrintSupport(){ 101 if(printSupport == null) { 102 synchronized(this) { 103 if(printSupport == null) 104 printSupport = new ImagePrintSupport( this ); 105 } 106 } 107 return printSupport; 108 } 109 110 113 public HelpCtx getHelpCtx() { 114 return HelpCtx.DEFAULT_HELP; 115 } 116 117 120 URL getImageURL() { 121 try { 122 return getPrimaryFile().getURL(); 123 } catch (FileStateInvalidException ex) { 124 return null; 125 } 126 } 127 128 132 private byte[] getImageData() { 133 try { 134 FileObject fo = getPrimaryFile(); 135 byte[] imageData = new byte[(int)fo.getSize()]; 136 BufferedInputStream in = new BufferedInputStream (fo.getInputStream()); 137 in.read(imageData, 0, (int)fo.getSize()); 138 in.close(); 139 return imageData; 140 } catch(IOException ioe) { 141 return new byte[0]; 142 } 143 } 144 145 150 public Image getImage() throws IOException { 151 return javax.imageio.ImageIO.read(getPrimaryFile().getInputStream()); 152 } 153 154 155 157 protected Node createNodeDelegate () { 158 return new ImageNode(this); 159 } 160 161 162 163 private static final class ImageNode extends DataNode { 164 165 public ImageNode(ImageDataObject obj) { 166 super(obj, Children.LEAF); 167 setIconBaseWithExtension(IMAGE_ICON_BASE); 169 setDefaultAction (SystemAction.get (OpenAction.class)); 170 } 171 172 173 protected Sheet createSheet() { 174 Sheet s = super.createSheet(); 175 Sheet.Set ss = s.get(Sheet.PROPERTIES); 176 if (ss == null) { 177 ss = Sheet.createPropertiesSet(); 178 s.put(ss); 179 } 180 ss.put(new ThumbnailProperty(getDataObject())); 181 return s; 182 } 183 184 185 186 private static final class ThumbnailProperty extends PropertySupport.ReadOnly { 187 188 private final DataObject obj; 189 190 191 public ThumbnailProperty(DataObject obj) { 192 super("thumbnail", Icon .class, NbBundle.getMessage(ImageDataObject.class, "PROP_Thumbnail"), 194 NbBundle.getMessage(ImageDataObject.class, "HINT_Thumbnail")); 195 this.obj = obj; 196 } 197 198 199 public Object getValue() throws InvocationTargetException { 200 try { 201 return new ImageIcon (obj.getPrimaryFile().getURL()); 202 } catch (FileStateInvalidException fsie) { 203 throw new InvocationTargetException (fsie); 204 } 205 } 206 207 208 public PropertyEditor getPropertyEditor() { 209 return new ThumbnailPropertyEditor(); 210 } 211 212 213 214 private final class ThumbnailPropertyEditor extends PropertyEditorSupport { 215 217 public boolean isPaintable() { 218 return true; 219 } 220 221 222 public void paintValue(Graphics g, Rectangle r) { 223 ImageIcon icon = null; 224 225 try { 226 icon = (ImageIcon )ThumbnailProperty.this.getValue(); 227 } catch(InvocationTargetException ioe) { 228 if(Boolean.getBoolean("netbeans.debug.exceptions")) { ErrorManager.getDefault().notify(ioe); 230 } 231 } 232 233 if(icon != null) { 234 int iconWidth = icon.getIconWidth(); 235 int iconHeight = icon.getIconHeight(); 236 237 238 double scale = (double)iconWidth / iconHeight; 240 241 if(iconWidth > r.width) { 242 iconWidth = r.width; 243 iconHeight = (int) (iconWidth / scale); 244 } 245 246 if(iconHeight > r.height) { 247 iconHeight = r.height; 248 iconWidth = (int) (iconHeight * scale); 249 } 250 251 int x; 253 if(iconWidth < r.x) { 254 x = (r.x - iconWidth) / 2; 255 } else { 256 x = 5; } 258 259 int y; 260 if(iconHeight < r.y) { 261 y = (r.y - iconHeight) / 2; 262 } else { 263 y = 0; 264 } 265 266 Graphics g2 = g.create(r.x, r.y, r.width, r.height); 267 g.drawImage(icon.getImage(), x, y, iconWidth, iconHeight, null); 268 } 269 } 270 271 273 public String getAsText() { 274 return null; 275 } 276 } } } 280 } 281 | Popular Tags |