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.components.svg;
043:
044: import org.netbeans.modules.vmd.api.codegen.*;
045: import org.netbeans.modules.vmd.api.inspector.InspectorFolderComponentPresenter;
046: import org.netbeans.modules.vmd.api.inspector.InspectorPositionPresenter;
047: import org.netbeans.modules.vmd.api.inspector.common.FolderPositionControllerFactory;
048: import org.netbeans.modules.vmd.api.model.*;
049: import org.netbeans.modules.vmd.api.model.common.DocumentSupport;
050: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
051: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
052: import org.netbeans.modules.vmd.midp.codegen.MidpCodePresenterSupport;
053: import org.netbeans.modules.vmd.midp.codegen.MidpCodeSupport;
054: import org.netbeans.modules.vmd.midp.components.MidpProjectSupport;
055: import org.netbeans.modules.vmd.midp.components.MidpTypes;
056: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
057: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
058: import org.netbeans.modules.vmd.midp.components.general.ClassCD;
059: import org.netbeans.modules.vmd.midp.components.resources.ResourcesSupport;
060: import org.netbeans.modules.vmd.midp.inspector.controllers.ResourcePC;
061: import org.netbeans.modules.vmd.midp.propertyeditors.MidpPropertiesCategories;
062: import org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorJavaString;
063: import org.netbeans.modules.vmd.midpnb.propertyeditors.PropertyEditorSVGImage;
064: import org.netbeans.modules.vmd.midp.screen.ResourceSRItemPresenter;
065: import org.openide.util.NbBundle;
066:
067: import java.util.*;
068:
069: /**
070: *
071: * @author Karol Harezlak
072: */
073: public class SVGImageCD extends ComponentDescriptor {
074:
075: public static final TypeID TYPEID = new TypeID(
076: TypeID.Kind.COMPONENT, "javax.microedition.m2g.SVGImage"); // NOI18N
077:
078: public static final String PROP_RESOURCE_PATH = "resourcePath"; // NOI18N
079: public static final String PROP_EXTERNAL_RESOURCE_HANDLER = "externalResourceHandler"; // NOI18N
080:
081: public static final String ICON_PATH = "org/netbeans/modules/vmd/midpnb/resources/svg_resource_16.png"; // NOI18N
082: public static final String ICON_LARGE_PATH = "org/netbeans/modules/vmd/midpnb/resources/svg_resource_32.png"; // NOI18N
083:
084: static {
085: MidpTypes.registerIconResource(TYPEID, ICON_PATH);
086: }
087:
088: @Override
089: public void postInitialize(DesignComponent component) {
090: MidpProjectSupport.addLibraryToProject(component.getDocument(),
091: SVGPlayerCD.MIDP_NB_SVG_LIBRARY);
092: }
093:
094: public TypeDescriptor getTypeDescriptor() {
095: return new TypeDescriptor(ClassCD.TYPEID, TYPEID, true, true);
096: }
097:
098: public VersionDescriptor getVersionDescriptor() {
099: return MidpVersionDescriptor.MIDP_2;
100: }
101:
102: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
103: return Arrays.asList(new PropertyDescriptor(PROP_RESOURCE_PATH,
104: MidpTypes.TYPEID_JAVA_LANG_STRING, PropertyValue
105: .createNull(), false, true,
106: MidpVersionable.MIDP_2), new PropertyDescriptor(
107: PROP_EXTERNAL_RESOURCE_HANDLER,
108: MidpTypes.TYPEID_JAVA_CODE, PropertyValue.createNull(),
109: true, false, MidpVersionable.MIDP_2));
110: }
111:
112: private static DefaultPropertiesPresenter createPropertiesPresenter() {
113: return new DefaultPropertiesPresenter()
114: .addPropertiesCategory(
115: MidpPropertiesCategories.CATEGORY_PROPERTIES)
116: .addProperty(
117: NbBundle.getMessage(SVGImageCD.class,
118: "DISP_SVGImage_ResourcePath"),
119: PropertyEditorSVGImage.createInstance(),
120: PROP_RESOURCE_PATH)
121: // NOI18N
122: .addProperty(
123: NbBundle
124: .getMessage(SVGImageCD.class,
125: "DISP_SVGImage_ExternalResourceHandler"),
126: PropertyEditorJavaString.createInstance(TYPEID),
127: PROP_EXTERNAL_RESOURCE_HANDLER); // NOI18N
128: }
129:
130: private static Presenter createSetterPresenter() {
131: return new CodeSetterPresenter().addSetters(new Setter() {
132: public boolean isConstructor() {
133: return true;
134: }
135:
136: public TypeID getConstructorRelatedTypeID() {
137: return TYPEID;
138: }
139:
140: public int getPriority() {
141: return 0;
142: }
143:
144: public String getSetterName() {
145: return null;
146: }
147:
148: public Versionable getVersionable() {
149: return MidpVersionable.MIDP_2;
150: }
151:
152: public void generateSetterCode(MultiGuardedSection section,
153: DesignComponent component,
154: Map<String, Parameter> name2parameter) {
155: CodeWriter writer = section.getWriter();
156: PropertyValue pathValue = component
157: .readProperty(PROP_RESOURCE_PATH);
158: String path = MidpTypes.getString(pathValue);
159: if (path == null) {
160: writer
161: .write(
162: CodeReferencePresenter
163: .generateDirectAccessCode(component))
164: .write(
165: " = SVGImage.createEmptyImage (null);\n"); // NOI18N
166: } else {
167: writer.write("try {\n"); // NOI18N
168: writer
169: .write(
170: CodeReferencePresenter
171: .generateDirectAccessCode(component))
172: .write(" = "); // NOI18N
173: writer
174: .write("(SVGImage) SVGImage.createImage (getClass ().getResourceAsStream("); // NOI18N
175: MidpCodeSupport.generateCodeForPropertyValue(
176: writer, pathValue);
177: writer.write("), "); // NOI18N
178: MidpCodeSupport
179: .generateCodeForPropertyValue(
180: writer,
181: component
182: .readProperty(PROP_EXTERNAL_RESOURCE_HANDLER));
183: writer.write(");\n"); // NOI18N
184:
185: writer.write("} catch (java.io.IOException e) {\n")
186: .commit(); // NOI18N
187: section.switchToEditable(component.getComponentID()
188: + "-@java.io.IOException"); // NOI18N
189: section.getWriter()
190: .write("e.printStackTrace ();\n").commit(); // NOI18N
191: section.switchToGuarded();
192: section.getWriter().write("}\n"); // NOI18N
193: }
194: }
195:
196: public List<String> getParameters() {
197: return Collections.emptyList();
198: }
199: });
200: }
201:
202: @Override
203: protected void gatherPresenters(ArrayList<Presenter> presenters) {
204: DocumentSupport.removePresentersOfClass(presenters,
205: InfoPresenter.class);
206: super .gatherPresenters(presenters);
207: }
208:
209: protected List<? extends Presenter> createPresenters() {
210: return Arrays.asList(
211: //info
212: ResourcesSupport.createResourceInfoResolver(),
213: // properties
214: createPropertiesPresenter(),
215: // code
216: createSetterPresenter(), MidpCodePresenterSupport
217: .createAddImportPresenter(),
218: // inspector
219: new InspectorFolderComponentPresenter(true),
220: InspectorPositionPresenter.create(new ResourcePC(),
221: FolderPositionControllerFactory
222: .createHierarchical()),
223: // screen
224: new ResourceSRItemPresenter(
225: InfoPresenter.NameType.TERTIARY));
226: }
227:
228: }
|