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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.vmd.midpnb.actions;
040:
041: import java.awt.event.ActionEvent;
042: import java.util.Collection;
043: import java.util.Iterator;
044: import java.util.Map;
045: import org.netbeans.modules.vmd.api.model.DesignComponent;
046: import org.netbeans.modules.vmd.api.model.PropertyValue;
047: import org.netbeans.modules.vmd.api.model.common.ActiveDocumentSupport;
048: import org.netbeans.modules.vmd.midp.components.MidpProjectSupport;
049: import org.netbeans.modules.vmd.midp.components.MidpTypes;
050: import org.netbeans.modules.vmd.midpnb.components.svg.SVGImageCD;
051: import org.netbeans.modules.vmd.midpnb.components.svg.SVGPlayerCD;
052: import org.openide.cookies.OpenCookie;
053: import org.openide.filesystems.FileObject;
054: import org.openide.loaders.DataObject;
055: import org.openide.loaders.DataObjectNotFoundException;
056: import org.openide.util.Exceptions;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.NbBundle;
059: import org.openide.util.actions.SystemAction;
060:
061: /**
062: *
063: * @author Karol Harezlak
064: */
065: public class EditSVGFileAction extends SystemAction {
066:
067: @Override
068: public String getName() {
069: return NbBundle.getMessage(EditSVGFileAction.class,
070: "NAME_EditSVGFile"); // NOI18N
071: }
072:
073: @Override
074: public HelpCtx getHelpCtx() {
075: return HelpCtx.DEFAULT_HELP;
076: }
077:
078: @Override
079: public void actionPerformed(ActionEvent ev) {
080: FileObject svgFileObject = getSVGFileObject();
081: if (svgFileObject == null) {
082: return;
083: }
084: try {
085: OpenCookie oc = DataObject.find(svgFileObject).getCookie(
086: OpenCookie.class);
087: if (oc == null) {
088: return;
089: }
090: oc.open();
091: } catch (DataObjectNotFoundException ex) {
092: Exceptions.printStackTrace(ex);
093: }
094:
095: }
096:
097: @Override
098: public boolean isEnabled() {
099: FileObject svgFileObject = getSVGFileObject();
100: return svgFileObject != null;
101: }
102:
103: private DesignComponent getActiveComponent() {
104: Collection<DesignComponent> components = ActiveDocumentSupport
105: .getDefault().getActiveComponents();
106: if (components.size() == 1) {
107: Iterator<DesignComponent> iterator = components.iterator();
108: if (iterator.hasNext()) {
109: return iterator.next();
110: }
111: }
112: return null;
113: }
114:
115: private FileObject getSVGFileObject() {
116: final DesignComponent animatorComponent = getActiveComponent();
117: if (animatorComponent == null)
118: return null;
119: final FileObject[] svgFileWrapper = new FileObject[1];
120: animatorComponent.getDocument().getTransactionManager()
121: .readAccess(new Runnable() {
122: public void run() {
123: PropertyValue value = animatorComponent
124: .readProperty(SVGPlayerCD.PROP_SVG_IMAGE);
125: if (!PropertyValue.Kind.USERCODE.equals(value
126: .getKind())) {
127: DesignComponent svgImageComponent = value
128: .getComponent();
129: FileObject svgFileObject;
130: if (svgImageComponent != null) {
131: PropertyValue propertyValue = svgImageComponent
132: .readProperty(SVGImageCD.PROP_RESOURCE_PATH);
133: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
134: Map<FileObject, FileObject> images = MidpProjectSupport
135: .getFileObjectsForRelativeResourcePath(
136: animatorComponent
137: .getDocument(),
138: MidpTypes
139: .getString(propertyValue));
140: Iterator<FileObject> iterator = images
141: .keySet().iterator();
142: svgFileObject = iterator.hasNext() ? iterator
143: .next()
144: : null;
145: svgFileWrapper[0] = svgFileObject;
146: }
147: }
148: }
149: }
150: });
151: return svgFileWrapper[0];
152: }
153: }
|