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.resources;
043:
044: import org.netbeans.modules.vmd.api.codegen.CodeSetterPresenter;
045: import org.netbeans.modules.vmd.api.codegen.CodeWriter;
046: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
047: import org.netbeans.modules.vmd.api.codegen.Parameter;
048: import org.netbeans.modules.vmd.api.model.*;
049: import org.netbeans.modules.vmd.midp.codegen.MidpCodePresenterSupport;
050: import org.netbeans.modules.vmd.midp.codegen.MidpSetter;
051: import org.netbeans.modules.vmd.midp.components.MidpTypes;
052: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
053: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
054:
055: import java.util.Arrays;
056: import java.util.List;
057: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
058: import org.netbeans.modules.vmd.api.properties.DesignEventFilterResolver;
059: import org.netbeans.modules.vmd.midp.propertyeditors.MidpPropertiesCategories;
060: import org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorGoToSource;
061: import org.netbeans.modules.vmd.midpnb.propertyeditors.MidpNbPropertiesCategories;
062:
063: /**
064: *
065: * @author Karol Harezlak
066: */
067: public class SimpleCancellableTaskCD extends ComponentDescriptor {
068:
069: public static final TypeID TYPEID = new TypeID(
070: TypeID.Kind.COMPONENT,
071: "org.netbeans.microedition.util.SimpleCancellableTask"); // NOI18N
072:
073: public static final String PROP_CODE = "executableMethodBody"; //NOI18N
074:
075: public SimpleCancellableTaskCD() {
076: }
077:
078: public TypeDescriptor getTypeDescriptor() {
079: return new TypeDescriptor(CancellableTaskCD.TYPEID, TYPEID,
080: true, true);
081: }
082:
083: public VersionDescriptor getVersionDescriptor() {
084: return MidpVersionDescriptor.MIDP;
085: }
086:
087: @Override
088: public void postInitialize(DesignComponent component) {
089: component
090: .writeProperty(
091: PROP_CODE,
092: MidpTypes
093: .createJavaCodeValue("// write task-execution user code here")); // NOI18N
094: }
095:
096: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
097: return Arrays.asList(new PropertyDescriptor(PROP_CODE,
098: MidpTypes.TYPEID_JAVA_CODE, PropertyValue.createNull(),
099: false, true, MidpVersionable.MIDP_2, false, false));
100: }
101:
102: private static DefaultPropertiesPresenter createPropertiesPresenter() {
103: return new DefaultPropertiesPresenter(
104: DesignEventFilterResolver.THIS_COMPONENT)
105: .addPropertiesCategory(
106: MidpPropertiesCategories.CATEGORY_PROPERTIES)
107: .addProperty("Executable Code",
108: PropertyEditorGoToSource.createInstance(),
109: PROP_CODE); // NOI18N
110: }
111:
112: private static Presenter createSetterPresenter() {
113: return new CodeSetterPresenter().addParameters(
114: new ExecutableParameter()).addSetters(
115: MidpSetter.createConstructor(TYPEID,
116: MidpVersionable.MIDP_2)).addSetters(
117: MidpSetter.createSetter("setExecutable",
118: MidpVersionable.MIDP_2).addParameters(
119: ExecutableParameter.PARAM_EXECUTABLE)); // NOI18N
120: }
121:
122: protected List<? extends Presenter> createPresenters() {
123: return Arrays.asList(
124: // properties
125: createPropertiesPresenter(),
126: // setter
127: createSetterPresenter(), MidpCodePresenterSupport
128: .createAddImportPresenter());
129: }
130:
131: private static class ExecutableParameter implements Parameter {
132:
133: public static final String PARAM_EXECUTABLE = "executable"; // NOI18N
134:
135: public String getParameterName() {
136: return PARAM_EXECUTABLE;
137: }
138:
139: public int getParameterPriority() {
140: return 0;
141: }
142:
143: public void generateParameterCode(DesignComponent component,
144: MultiGuardedSection section, int index) {
145: CodeWriter writer = section.getWriter();
146: writer
147: .write("new org.netbeans.microedition.util.Executable() {\n"); // NOI18N
148: writer.write("public void execute () throws Exception {\n"); // NOI18N
149: writer.commit();
150:
151: section.switchToEditable(component.getComponentID()
152: + "-execute"); // NOI18N
153: writer = section.getWriter();
154: String code = MidpTypes.getJavaCode(component
155: .readProperty(PROP_CODE));
156: if (code != null)
157: writer.write(code);
158: if (code == null || !code.endsWith("\n")) // NOI18N
159: writer.write("\n"); // NOI18N
160: writer.commit();
161:
162: section.switchToGuarded();
163: writer = section.getWriter();
164: writer.write("}\n"); // NOI18N
165: writer.write("}"); // NOI18N
166: }
167:
168: public boolean isRequiredToBeSet(DesignComponent component) {
169: return true;
170: }
171:
172: public int getCount(DesignComponent component) {
173: return -1;
174: }
175:
176: public boolean isRequiredToBeSet(DesignComponent component,
177: int index) {
178: return false;
179: }
180:
181: }
182:
183: }
|