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: package org.netbeans.modules.vmd.midp.actions;
042:
043: import org.netbeans.api.visual.widget.Scene;
044: import org.netbeans.modules.vmd.api.flow.visual.FlowScene;
045: import org.netbeans.modules.vmd.api.flow.FlowSupport;
046: import org.netbeans.modules.vmd.api.io.ActiveViewSupport;
047: import org.netbeans.modules.vmd.api.io.DataObjectContext;
048: import org.netbeans.modules.vmd.api.io.DesignDocumentAwareness;
049: import org.netbeans.modules.vmd.api.model.Debug;
050: import org.netbeans.modules.vmd.api.model.DesignDocument;
051: import org.openide.DialogDescriptor;
052: import org.openide.DialogDisplayer;
053: import org.openide.util.HelpCtx;
054: import org.openide.util.NbBundle;
055: import org.openide.util.actions.SystemAction;
056:
057: import javax.imageio.ImageIO;
058: import javax.swing.*;
059: import javax.swing.filechooser.FileFilter;
060: import java.awt.*;
061: import java.awt.event.ActionEvent;
062: import java.awt.image.BufferedImage;
063: import java.io.File;
064: import java.io.IOException;
065:
066: /**
067: * @author David Kaspar
068: */
069: public class ExportFlowAsImageAction extends SystemAction implements
070: DesignDocumentAwareness {
071:
072: private DesignDocument document;
073:
074: public String getName() {
075: return NbBundle.getMessage(ExportFlowAsImageAction.class,
076: "NAME_ExportFlowAsImage"); // NOI18N
077: }
078:
079: public HelpCtx getHelpCtx() {
080: return new HelpCtx(ExportFlowAsImageAction.class);
081: }
082:
083: public boolean isEnabled() {
084: updateDesignDocumentReference();
085: FlowScene scene = FlowSupport.getFlowSceneForDocument(document);
086:
087: if (scene == null)
088: return false;
089:
090: JComponent view = scene.getView();
091: if (view == null || !view.isShowing())
092: return false;
093: Rectangle rectangle = scene.getBounds();
094: return rectangle.width > 0 && rectangle.height > 0;
095: }
096:
097: private void updateDesignDocumentReference() {
098: DataObjectContext context = ActiveViewSupport.getDefault()
099: .getActiveView().getContext();
100: context.addDesignDocumentAwareness(this );
101: context.removeDesignDocumentAwareness(this );
102: }
103:
104: public void actionPerformed(ActionEvent e) {
105: updateDesignDocumentReference();
106: FlowScene scene = FlowSupport.getFlowSceneForDocument(document);
107: if (scene == null)
108: return;
109: saveAsImage(scene);
110: }
111:
112: private void saveAsImage(Scene scene) {
113: Rectangle rectangle = scene.getBounds();
114: BufferedImage bi = new BufferedImage(rectangle.width,
115: rectangle.height, BufferedImage.TYPE_4BYTE_ABGR);
116: Graphics2D graphics = bi.createGraphics();
117: scene.paint(graphics);
118: graphics.dispose();
119:
120: JFileChooser chooser = new JFileChooser();
121: chooser.setDialogTitle(NbBundle.getMessage(
122: ExportFlowAsImageAction.class,
123: "TITLE_ExportFlowAsImage")); // NOI18N
124: chooser.setDialogType(JFileChooser.SAVE_DIALOG);
125: chooser.setMultiSelectionEnabled(false);
126: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
127: chooser.setFileFilter(new FileFilter() {
128: public boolean accept(File file) {
129: return file.isDirectory()
130: || file.getName().toLowerCase()
131: .endsWith(".png"); // NOI18N
132: }
133:
134: public String getDescription() {
135: return NbBundle.getMessage(
136: ExportFlowAsImageAction.class,
137: "LBL_FileFilterPNG"); // NOI18N
138: }
139: });
140: if (chooser.showSaveDialog(scene.getView()) != JFileChooser.APPROVE_OPTION)
141: return;
142:
143: File file = chooser.getSelectedFile();
144: if (!file.getName().toLowerCase().endsWith(".png")) // NOI18N
145: file = new File(file.getParentFile(), file.getName()
146: + ".png"); // NOI18N
147: if (file.exists()) {
148: DialogDescriptor descriptor = new DialogDescriptor(
149: NbBundle
150: .getMessage(ExportFlowAsImageAction.class,
151: "LBL_AlreadyExists", file
152: .getAbsolutePath()), // NOI18N
153: NbBundle.getMessage(ExportFlowAsImageAction.class,
154: "TITLE_AlreadyExists"), // NOI18N
155: true, DialogDescriptor.YES_NO_OPTION,
156: DialogDescriptor.NO_OPTION, null);
157: DialogDisplayer.getDefault().createDialog(descriptor)
158: .setVisible(true);
159: if (descriptor.getValue() != DialogDescriptor.YES_OPTION)
160: return;
161: }
162:
163: try {
164: ImageIO.write(bi, "png", file); // NOI18N
165: } catch (IOException e) {
166: throw Debug.error(e);
167: }
168:
169: }
170:
171: public void setDesignDocument(DesignDocument designDocument) {
172: document = designDocument;
173: }
174:
175: }
|