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.vmd.midpnb.screen.display;
043:
044: import org.netbeans.modules.mobility.svgcore.util.Util;
045: import org.netbeans.modules.vmd.api.model.Debug;
046: import org.netbeans.modules.vmd.api.model.DesignComponent;
047: import org.netbeans.modules.vmd.api.model.PropertyValue;
048: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
049: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
050: import org.netbeans.modules.vmd.midp.components.MidpProjectSupport;
051: import org.netbeans.modules.vmd.midp.components.MidpTypes;
052: import org.netbeans.modules.vmd.midp.screen.display.DisplayableDisplayPresenter;
053: import org.netbeans.modules.vmd.midp.screen.display.ScreenFileObjectListener;
054: import org.netbeans.modules.vmd.midp.screen.display.property.ResourcePropertyEditor;
055: import org.netbeans.modules.vmd.midpnb.components.svg.SVGImageCD;
056: import org.netbeans.modules.vmd.midpnb.components.svg.SVGPlayerCD;
057: import org.openide.filesystems.FileObject;
058: import org.openide.util.Exceptions;
059: import org.openide.util.NbBundle;
060: import javax.microedition.m2g.SVGImage;
061: import javax.swing.*;
062: import java.awt.*;
063: import java.io.IOException;
064: import java.util.ArrayList;
065: import java.util.Collection;
066: import java.util.Iterator;
067: import java.util.Map;
068:
069: /**
070: *
071: * @author Anton Chechel
072: * @version 1.0
073: */
074: public class SVGPlayerDisplayPresenter extends
075: DisplayableDisplayPresenter {
076:
077: private JLabel stringLabel;
078: private SVGImageComponent imageView = new SVGImageComponent();
079: private ScreenFileObjectListener imageFileListener;
080: private FileObject svgFileObject;
081:
082: public SVGPlayerDisplayPresenter() {
083: JPanel contentPanel = getPanel().getContentPanel();
084: contentPanel.setLayout(new BorderLayout());
085: stringLabel = new JLabel();
086: stringLabel.setHorizontalAlignment(JLabel.CENTER);
087: }
088:
089: @Override
090: public void reload(ScreenDeviceInfo deviceInfo) {
091: super .reload(deviceInfo);
092: JPanel contentPanel = getPanel().getContentPanel();
093: contentPanel.removeAll();
094:
095: final DesignComponent animatorComponent = getComponent();
096: PropertyValue value = animatorComponent
097: .readProperty(SVGPlayerCD.PROP_SVG_IMAGE);
098: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
099: DesignComponent svgImageComponent = value.getComponent();
100:
101: SVGImage svgImage = null;
102: boolean notSVGTiny = false;
103: if (svgImageComponent != null) {
104: PropertyValue propertyValue = svgImageComponent
105: .readProperty(SVGImageCD.PROP_RESOURCE_PATH);
106: if (propertyValue.getKind() == PropertyValue.Kind.VALUE) {
107: Map<FileObject, FileObject> images = MidpProjectSupport
108: .getFileObjectsForRelativeResourcePath(
109: animatorComponent.getDocument(),
110: MidpTypes.getString(propertyValue));
111: Iterator<FileObject> iterator = images.keySet()
112: .iterator();
113: svgFileObject = iterator.hasNext() ? iterator
114: .next() : null;
115: if (svgFileObject != null) {
116: try {
117: svgImage = Util.createSVGImage(
118: svgFileObject, true);
119: if (svgFileObject != null) {
120: svgFileObject
121: .removeFileChangeListener(imageFileListener);
122: imageFileListener = new ScreenFileObjectListener(
123: getRelatedComponent(),
124: svgImageComponent,
125: SVGImageCD.PROP_RESOURCE_PATH);
126: svgFileObject
127: .addFileChangeListener(imageFileListener);
128: }
129: } catch (IOException e) {
130: Debug.warning(e);
131: notSVGTiny = true;
132: }
133: }
134: }
135: }
136: imageView.setImage(svgImage);
137: if (svgImage != null) {
138: contentPanel.add(imageView, BorderLayout.CENTER);
139: } else {
140: if (notSVGTiny) {
141: stringLabel.setText(NbBundle.getMessage(
142: SVGPlayerDisplayPresenter.class,
143: "DISP_svg_image_not_svg_tiny")); // NOI18N
144: contentPanel.add(stringLabel, BorderLayout.CENTER);
145: } else {
146: stringLabel.setText(NbBundle.getMessage(
147: SVGPlayerDisplayPresenter.class,
148: "DISP_svg_image_not_specified")); // NOI18N
149: contentPanel.add(stringLabel, BorderLayout.CENTER);
150: }
151: }
152: } else {
153: stringLabel.setText(NbBundle.getMessage(
154: SVGPlayerDisplayPresenter.class,
155: "DISP_svg_image_is_usercode")); // NOI18N
156: contentPanel.add(stringLabel, BorderLayout.CENTER);
157: }
158: }
159:
160: @Override
161: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
162: ArrayList<ScreenPropertyDescriptor> descriptors = new ArrayList<ScreenPropertyDescriptor>(
163: super .getPropertyDescriptors());
164: ResourcePropertyEditor imagePropertyEditor = new ResourcePropertyEditor(
165: SVGPlayerCD.PROP_SVG_IMAGE, getComponent());
166: if (stringLabel.getParent() != null) {
167: descriptors.add(new ScreenPropertyDescriptor(
168: getComponent(), stringLabel, imagePropertyEditor));
169: } else {
170: descriptors.add(new ScreenPropertyDescriptor(
171: getComponent(), imageView, imagePropertyEditor));
172: }
173: return descriptors;
174: }
175:
176: @Override
177: protected void notifyDetached(DesignComponent component) {
178: if (svgFileObject != null && imageFileListener != null) {
179: svgFileObject.removeFileChangeListener(imageFileListener);
180: }
181: svgFileObject = null;
182: imageFileListener = null;
183: }
184: }
|