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.mobility.svgcore.export;
042:
043: import java.awt.Dialog;
044: import java.awt.Dimension;
045: import javax.microedition.m2g.SVGImage;
046: import org.netbeans.modules.mobility.svgcore.SVGDataObject;
047: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
048: import org.openide.DialogDescriptor;
049: import org.openide.DialogDisplayer;
050: import org.openide.nodes.Node;
051: import org.openide.util.Exceptions;
052: import org.openide.util.HelpCtx;
053: import org.openide.util.Lookup;
054: import org.openide.util.NbBundle;
055: import org.openide.util.actions.CookieAction;
056:
057: /**
058: *
059: * @author Pavel Benes, suchys
060: */
061: public final class SaveElementAsImage extends CookieAction {// implements Presenter.Popup, Presenter.Menu {
062:
063: protected void performAction(Node[] activatedNodes) {
064: Lookup l = activatedNodes[0].getLookup();
065: SVGDataObject doj = (SVGDataObject) l
066: .lookup(SVGDataObject.class);
067: SVGObject svgObj = (SVGObject) l.lookup(SVGObject.class);
068: SVGImage image = (SVGImage) l.lookup(SVGImage.class);
069:
070: if (svgObj != null && image != null) {
071: try {
072: SVGImageRasterizerPanel panel = new SVGImageRasterizerPanel(
073: doj, svgObj.getElementId());
074: DialogDescriptor dd = new DialogDescriptor(panel,
075: NbBundle.getMessage(SaveElementAsImage.class,
076: "TITLE_ImageExport"));
077: Dialog dlg = DialogDisplayer.getDefault().createDialog(
078: dd);
079: SaveAnimationAsImageAction.setDialogMinimumSize(dlg);
080: dlg.setVisible(true);
081:
082: if (dd.getValue() == DialogDescriptor.OK_OPTION) {
083: AnimationRasterizer.export(doj, panel);
084: }
085: } catch (Exception e) {
086: Exceptions.printStackTrace(e);
087: }
088: }
089: }
090:
091: protected int mode() {
092: return CookieAction.MODE_ANY;
093: }
094:
095: public String getName() {
096: return NbBundle.getMessage(SaveElementAsImage.class,
097: "CTL_SVGExportAction"); //NOI18N
098: }
099:
100: protected Class[] cookieClasses() {
101: return new Class[] { SVGDataObject.class };
102: }
103:
104: protected void initialize() {
105: super .initialize();
106: // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
107: putValue("noIconInMenu", Boolean.TRUE); //NOI18N
108: }
109:
110: public HelpCtx getHelpCtx() {
111: return HelpCtx.DEFAULT_HELP;
112: }
113:
114: protected boolean asynchronous() {
115: return false;
116: }
117:
118: /**
119: * First method to render a single element, based on the 226 API.
120: *
121: * @param elt the SVGLocatableElement to render
122: * @param svgImage the containing SVGImage
123: * @param doc the related Document
124: * @param svg the root SVG element.
125: */
126: /*
127: private void renderElement(SVGDataObject doj, final SVGLocatableElement elt,
128: final SVGImage svgImage ) {
129: FileObject primaryFile = doj.getPrimaryFile();
130:
131: J2MEProject project = null;
132: Project p = FileOwnerQuery.getOwner (primaryFile);
133: if (p != null && p instanceof J2MEProject){
134: project = (J2MEProject) p;
135: }
136:
137: //SVGRasterizerPanel panel = new SVGRasterizerPanel(ScreenSizeHelper.getCurrentDeviceScreenSize(primaryFile, null), project != null);
138: SVGAnimationRasterizerPanel panel = new SVGAnimationRasterizerPanel(doj);
139: DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(SaveAnimationAsImageAction.class, "TITLE_ImageExport"));
140: DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
141: //int imageWidth = panel.getImageWidth();
142: //int imageHeigth = panel.getImageHeigth();
143: //AnimationRasterizer.ImageType imageType = panel.getFormat();
144: //float compressionQuality = panel.getCompressionQuality();
145: //boolean progressive = panel.isProgressive();
146: //boolean forAllConfig = panel.isForAllConfigurations();
147:
148: if (dd.getValue() == DialogDescriptor.OK_OPTION){
149: //
150: // The following adjusts the element's transform so that its user space is identical
151: // to the root element's user space.
152: //
153: int h = svgImage.getViewportHeight();
154: int w = svgImage.getViewportWidth();
155:
156: SVGSVGElement svg = (SVGSVGElement) svgImage.getDocument().getDocumentElement();
157:
158: // svg -> screen
159: SVGMatrix svgCTM = svg.getScreenCTM();
160:
161: // element -> svg -> screen
162: SVGMatrix eltCTM = elt.getScreenCTM();
163:
164: // screen -> svg
165: SVGMatrix svgICTM = svgCTM.inverse();
166:
167: // elt-> svg matrix
168: SVGMatrix eltToSvg = svgICTM.mMultiply(eltCTM);
169:
170: // The current elt transform, if any
171: SVGMatrix origTxf = elt.getMatrixTrait("transform");
172: SVGMatrix eltTxf = elt.getMatrixTrait("transform");
173:
174: SVGMatrix toSvgSpace= eltTxf.mMultiply(eltToSvg.inverse());
175:
176: // Get the current viewBox
177: SVGRect viewBox = svg.getRectTrait("viewBox");
178:
179: // Get the overall content's bounding box, including our element.
180: SVGRect allBBox = svg.getBBox();
181:
182: // Now, move our element 'away' from all content (move it to the right of the content)
183: SVGRect bbox = elt.getBBox();
184: toSvgSpace.mTranslate(-bbox.getX() + allBBox.getX() + allBBox.getWidth(), 0);
185: elt.setMatrixTrait("transform", toSvgSpace);
186:
187: // Now, set the new viewBox.
188: bbox.setX(allBBox.getX() + allBBox.getWidth());
189: svg.setRectTrait("viewBox", bbox);
190:
191: AnimationRasterizer.exportElement(primaryFile, project, svgImage, elt.getId(), panel);
192:
193: svg.setRectTrait("viewBox", viewBox);
194: elt.setMatrixTrait("transform", origTxf);
195: svgImage.setViewportWidth(w);
196: svgImage.setViewportHeight(h);
197: }
198: */
199: // /**
200: // * The overlay image.
201: // */
202: // BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
203: //
204: // Graphics g = bi.createGraphics();
205: // g.setColor(Color.black);
206: // g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
207: // ScalableGraphics sg = ScalableGraphics.createInstance();
208: // sg.bindTarget(g);
209: //
210: // // To avoid still rendering content outside the viewport, we set the viewport
211: // // to have the same aspect ratio as the viewBox.
212: // // We know the height is non-zero because we got here through hit detection.
213: // float ar = bbox.getWidth() / bbox.getHeight();
214: // int iw = svgImage.getViewportWidth();
215: // int ih = svgImage.getViewportHeight();
216: // int vw = 0;
217: // int vh = 0;
218: // if (ar > 1) {
219: // vw = bi.getWidth();
220: // vh = (int) (vw / ar);
221: // } else {
222: // vh = bi.getHeight();
223: // vw = (int) (vh * ar);
224: // }
225: // svgImage.setViewportWidth(vw);
226: // svgImage.setViewportHeight(vh);
227: // sg.render(0, 0, svgImage);
228: // sg.releaseTarget();
229: // Restore values.
230: // }
231: // public JMenuItem getPopupPresenter() {
232: // JMenuItem result = new JMenuItem("Export..."); //remember JMenu is a subclass of JMenuItem
233: // return result;
234: // }
235: }
|