001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.image;
043:
044: import java.awt.Graphics;
045: import java.awt.Image;
046: import java.awt.Rectangle;
047: import java.beans.PropertyEditor;
048: import java.beans.PropertyEditorSupport;
049: import java.io.BufferedInputStream;
050: import java.io.IOException;
051: import java.lang.reflect.InvocationTargetException;
052: import java.net.URL;
053: import javax.swing.Icon;
054: import javax.swing.ImageIcon;
055:
056: import org.openide.actions.OpenAction;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileStateInvalidException;
059: import org.openide.loaders.*;
060: import org.openide.nodes.*;
061: import org.openide.ErrorManager;
062: import org.openide.util.actions.SystemAction;
063: import org.openide.util.HelpCtx;
064: import org.openide.util.NbBundle;
065:
066: /**
067: * Object that represents one file containing an image.
068: * @author Petr Hamernik, Jaroslav Tulach, Ian Formanek, Michael Wever
069: * @author Marian Petras
070: */
071: public class ImageDataObject extends MultiDataObject implements
072: CookieSet.Factory {
073:
074: /** Generated serialized version UID. */
075: static final long serialVersionUID = -6035788991669336965L;
076:
077: /** Base for image resource. */
078: private static final String IMAGE_ICON_BASE = "org/netbeans/modules/image/imageObject.png"; // NOI18N
079:
080: /** Open support for this image data object. */
081: private transient ImageOpenSupport openSupport;
082: /** Print support for this image data object **/
083: private transient ImagePrintSupport printSupport;
084:
085: /** Constructor.
086: * @param pf primary file object for this data object
087: * @param loader the data loader creating it
088: * @exception DataObjectExistsException if there was already a data object for it
089: */
090: public ImageDataObject(FileObject pf, MultiFileLoader loader)
091: throws DataObjectExistsException {
092: super (pf, loader);
093:
094: getCookieSet().add(ImageOpenSupport.class, this );
095: getCookieSet().add(ImagePrintSupport.class, this );
096: }
097:
098: /** Implements <code>CookieSet.Factory</code> interface. */
099: public Node.Cookie createCookie(Class clazz) {
100: if (clazz.isAssignableFrom(ImageOpenSupport.class))
101: return getOpenSupport();
102: else if (clazz.isAssignableFrom(ImagePrintSupport.class))
103: return getPrintSupport();
104: else
105: return null;
106: }
107:
108: /** Gets image open support. */
109: private ImageOpenSupport getOpenSupport() {
110: if (openSupport == null) {
111: synchronized (this ) {
112: if (openSupport == null)
113: openSupport = new ImageOpenSupport(
114: getPrimaryEntry());
115: }
116: }
117:
118: return openSupport;
119: }
120:
121: protected ImagePrintSupport getPrintSupport() {
122: if (printSupport == null) {
123: synchronized (this ) {
124: if (printSupport == null)
125: printSupport = new ImagePrintSupport(this );
126: }
127: }
128: return printSupport;
129: }
130:
131: /** Help context for this object.
132: * @return the help context
133: */
134: public HelpCtx getHelpCtx() {
135: return HelpCtx.DEFAULT_HELP;
136: }
137:
138: /** Get a URL for the image.
139: * @return the image url
140: */
141: URL getImageURL() {
142: try {
143: return getPrimaryFile().getURL();
144: } catch (FileStateInvalidException ex) {
145: return null;
146: }
147: }
148:
149: /** Gets image data for the image object.
150: * @return the image data
151: * @deprecated use getImage() instead
152: */
153: private byte[] getImageData() {
154: try {
155: FileObject fo = getPrimaryFile();
156: byte[] imageData = new byte[(int) fo.getSize()];
157: BufferedInputStream in = new BufferedInputStream(fo
158: .getInputStream());
159: in.read(imageData, 0, (int) fo.getSize());
160: in.close();
161: return imageData;
162: } catch (IOException ioe) {
163: return new byte[0];
164: }
165: }
166:
167: // Michael Wever 26/09/2001
168: /** Gets image for the image data
169: * @return the image or <code>null</code> if image could not be created
170: * @return java.io.IOException if an error occurs during reading
171: */
172: public Image getImage() throws IOException {
173: return javax.imageio.ImageIO.read(getPrimaryFile()
174: .getInputStream());
175: }
176:
177: /** Create a node to represent the image. Overrides superclass method.
178: * @return node delegate */
179: protected Node createNodeDelegate() {
180: return new ImageNode(this );
181: }
182:
183: /** Node representing <code>ImageDataObject</code>. */
184: private static final class ImageNode extends DataNode {
185: /** Constructs image node. */
186: public ImageNode(ImageDataObject obj) {
187: super (obj, Children.LEAF);
188: //setIconBase(IMAGE_ICON_BASE);
189: setIconBaseWithExtension(IMAGE_ICON_BASE);
190: setDefaultAction(SystemAction.get(OpenAction.class));
191: }
192:
193: /** Creates property sheet. Ovrrides superclass method. */
194: protected Sheet createSheet() {
195: Sheet s = super .createSheet();
196: Sheet.Set ss = s.get(Sheet.PROPERTIES);
197: if (ss == null) {
198: ss = Sheet.createPropertiesSet();
199: s.put(ss);
200: }
201: ss.put(new ThumbnailProperty(getDataObject()));
202: return s;
203: }
204:
205: /** Property representing for thumbanil property in the sheet. */
206: private static final class ThumbnailProperty extends
207: PropertySupport.ReadOnly {
208: /** (Image) data object associated with. */
209: private final DataObject obj;
210:
211: /** Constructs property. */
212: public ThumbnailProperty(DataObject obj) {
213: super ("thumbnail", Icon.class, // NOI18N
214: NbBundle.getMessage(ImageDataObject.class,
215: "PROP_Thumbnail"), NbBundle
216: .getMessage(ImageDataObject.class,
217: "HINT_Thumbnail"));
218: this .obj = obj;
219: }
220:
221: /** Gets value of property. Overrides superclass method. */
222: public Object getValue() throws InvocationTargetException {
223: try {
224: return new ImageIcon(obj.getPrimaryFile().getURL());
225: } catch (FileStateInvalidException fsie) {
226: throw new InvocationTargetException(fsie);
227: }
228: }
229:
230: /** Gets property editor. */
231: public PropertyEditor getPropertyEditor() {
232: return new ThumbnailPropertyEditor();
233: }
234:
235: /** Property editor for thumbnail property. */
236: private final class ThumbnailPropertyEditor extends
237: PropertyEditorSupport {
238: /** Overrides superclass method.
239: * @return <code>true</code> */
240: public boolean isPaintable() {
241: return true;
242: }
243:
244: /** Patins thumbanil of the image. Overrides superclass method. */
245: public void paintValue(Graphics g, Rectangle r) {
246: ImageIcon icon = null;
247:
248: try {
249: icon = (ImageIcon) ThumbnailProperty.this
250: .getValue();
251: } catch (InvocationTargetException ioe) {
252: if (Boolean
253: .getBoolean("netbeans.debug.exceptions")) { // NOI18N
254: ErrorManager.getDefault().notify(ioe);
255: }
256: }
257:
258: if (icon != null) {
259: int iconWidth = icon.getIconWidth();
260: int iconHeight = icon.getIconHeight();
261:
262: // Shrink image if necessary.
263: double scale = (double) iconWidth / iconHeight;
264:
265: if (iconWidth > r.width) {
266: iconWidth = r.width;
267: iconHeight = (int) (iconWidth / scale);
268: }
269:
270: if (iconHeight > r.height) {
271: iconHeight = r.height;
272: iconWidth = (int) (iconHeight * scale);
273: }
274:
275: // Try to center it if it fits, else paint as much as possible.
276: int x;
277: if (iconWidth < r.x) {
278: x = (r.x - iconWidth) / 2;
279: } else {
280: x = 5; // XXX Indent.
281: }
282:
283: int y;
284: if (iconHeight < r.y) {
285: y = (r.y - iconHeight) / 2;
286: } else {
287: y = 0;
288: }
289:
290: Graphics g2 = g.create(r.x, r.y, r.width,
291: r.height);
292: g.drawImage(icon.getImage(), x, y, iconWidth,
293: iconHeight, null);
294: }
295: }
296:
297: /** Overrides superclass method.
298: * @return <code>null</code> */
299: public String getAsText() {
300: return null;
301: }
302: } // End of class ThumbnailPropertyEditor.
303: } // End of class ThumbnailProperty.
304: } // End of class ImageNode.
305:
306: }
|