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.vmd.midp.components.points;
042:
043: import org.netbeans.modules.vmd.api.codegen.CodeClassLevelPresenter;
044: import org.netbeans.modules.vmd.api.codegen.CodeMultiGuardedLevelPresenter;
045: import org.netbeans.modules.vmd.api.codegen.CodeReferencePresenter;
046: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
047: import org.netbeans.modules.vmd.api.model.*;
048: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
049: import org.netbeans.modules.vmd.api.model.presenters.actions.DeleteDependencyPresenter;
050: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
051: import org.netbeans.modules.vmd.midp.actions.MidpActionsSupport;
052: import org.netbeans.modules.vmd.midp.actions.GoToSourcePresenter;
053: import org.netbeans.modules.vmd.midp.codegen.MidpCodeSupport;
054: import org.netbeans.modules.vmd.midp.components.MidpTypes;
055: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
056: import org.netbeans.modules.vmd.midp.components.sources.IfFalseEventSourceCD;
057: import org.netbeans.modules.vmd.midp.components.sources.IfTrueEventSourceCD;
058: import org.netbeans.modules.vmd.midp.flow.FlowIfPointPinOrderPresenter;
059: import org.netbeans.modules.vmd.midp.propertyeditors.MidpPropertiesCategories;
060: import org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorJavaString;
061: import org.netbeans.api.editor.guards.GuardedSection;
062: import org.openide.util.Utilities;
063: import org.openide.util.NbBundle;
064:
065: import javax.swing.text.StyledDocument;
066: import java.util.ArrayList;
067: import java.util.Arrays;
068:
069: /**
070: * @author David Kaspar
071: */
072: public class IfPointCD extends ComponentDescriptor {
073:
074: public static final TypeID TYPEID = new TypeID(
075: TypeID.Kind.COMPONENT, "#IfPoint"); // NOI18N
076:
077: public static final String ICON_PATH = "org/netbeans/modules/vmd/midp/resources/components/if_16.png"; // NOI18N
078: public static final String LARGE_ICON_PATH = "org/netbeans/modules/vmd/midp/resources/components/if_32.png"; // NOI18N
079:
080: public static final String PROP_CONDITION = "condition"; // NOI18N
081: public static final String PROP_TRUE = "true"; // NOI18N
082: public static final String PROP_FALSE = "false"; // NOI18N
083:
084: public TypeDescriptor getTypeDescriptor() {
085: return new TypeDescriptor(MethodPointCD.TYPEID, TYPEID, true,
086: true);
087: }
088:
089: public VersionDescriptor getVersionDescriptor() {
090: return MidpVersionDescriptor.FOREVER;
091: }
092:
093: public java.util.List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
094: return Arrays.asList(new PropertyDescriptor(PROP_CONDITION,
095: MidpTypes.TYPEID_JAVA_CODE, MidpTypes
096: .createJavaCodeValue("true"), false, false,
097: Versionable.FOREVER), // NOI18N
098: new PropertyDescriptor(PROP_TRUE,
099: IfTrueEventSourceCD.TYPEID, PropertyValue
100: .createNull(), false, false,
101: Versionable.FOREVER), new PropertyDescriptor(
102: PROP_FALSE, IfFalseEventSourceCD.TYPEID,
103: PropertyValue.createNull(), false, false,
104: Versionable.FOREVER));
105: }
106:
107: public DefaultPropertiesPresenter createPropertiesPresenter() {
108: return new DefaultPropertiesPresenter()
109: .addPropertiesCategory(
110: MidpPropertiesCategories.CATEGORY_PROPERTIES)
111: .addPropertiesCategory(
112: MidpPropertiesCategories.CATEGORY_CODE_PROPERTIES)
113: .addProperty(
114: NbBundle.getMessage(IfPointCD.class,
115: "DISP_IfPoint_Condition_Code"),
116: PropertyEditorJavaString.createInstance(TYPEID),
117: PROP_CONDITION);
118: }
119:
120: @Override
121: protected void gatherPresenters(ArrayList<Presenter> presenters) {
122: MidpActionsSupport.addCommonActionsPresenters(presenters,
123: false, true, true, true, true);
124: super .gatherPresenters(presenters);
125: }
126:
127: protected java.util.List<? extends Presenter> createPresenters() {
128: return Arrays.asList(
129: // flow
130: new FlowIfPointPinOrderPresenter(),
131: // general
132: InfoPresenter.create(PointSupport.createInfoResolver(
133: Utilities.loadImage(ICON_PATH),
134: MethodPointCD.PROP_METHOD_NAME, NbBundle
135: .getMessage(IfPointCD.class,
136: "TYPE_IfPoint"))),
137: new GoToSourcePresenter() {
138: protected boolean matches(GuardedSection section) {
139: return MultiGuardedSection
140: .matches(section, getComponent()
141: .getComponentID()
142: + "-if", 1); // NOI18N
143: }
144: },
145: // properties
146: createPropertiesPresenter(),
147: // code
148: new CodeClassLevelPresenter.Adapter() {
149: @Override
150: protected void generateClassBodyCode(
151: StyledDocument document) {
152: DesignComponent component = getComponent();
153: MultiGuardedSection section = MultiGuardedSection
154: .create(document, component
155: .getComponentID()
156: + "-if"); // NOI18N
157: String methodName = CodeReferencePresenter
158: .generateDirectAccessCode(component);
159: section.getWriter().write(
160: "//<editor-fold defaultstate=\"collapsed\" desc=\" Generated Method: "
161: + methodName + " \">\n"); // NOI18N
162: section.getWriter().write(
163: "/**\n * Performs an action assigned to the "
164: + methodName
165: + " if-point.\n */\n"); // NOI18N
166: section.getWriter()
167: .write(
168: "public void " + methodName
169: + " () {\n").commit(); // NOI18N
170: section.switchToEditable(component
171: .getComponentID()
172: + "-preIf"); // NOI18N
173: section.getWriter().write(
174: " // enter pre-if user code here\n")
175: .commit(); // NOI18N
176: section.switchToGuarded();
177:
178: section.getWriter().write("if ("); // NOI18N
179: MidpCodeSupport.generateCodeForPropertyValue(
180: section.getWriter(), component
181: .readProperty(PROP_CONDITION));
182: section.getWriter().write(") {\n"); // NOI18N
183: CodeMultiGuardedLevelPresenter
184: .generateMultiGuardedSectionCode(
185: section,
186: component.readProperty(
187: PROP_TRUE)
188: .getComponent());
189: section.getWriter().write("} else {\n"); // NOI18N
190: CodeMultiGuardedLevelPresenter
191: .generateMultiGuardedSectionCode(
192: section, component
193: .readProperty(
194: PROP_FALSE)
195: .getComponent());
196: section.getWriter().write("}\n").commit(); // NOI18N
197:
198: section.switchToEditable(component
199: .getComponentID()
200: + "-postIf"); // NOI18N
201: section.getWriter().write(
202: " // enter post-if user code here\n")
203: .commit(); // NOI18N
204: section.switchToGuarded();
205: section.getWriter().write("}\n"); // NOI18N
206: section.getWriter().write("//</editor-fold>\n")
207: .commit(); // NOI18N
208: section.close();
209: }
210: },
211: // delete
212: DeleteDependencyPresenter
213: .createNullableComponentReferencePresenter(PROP_TRUE),
214: DeleteDependencyPresenter
215: .createNullableComponentReferencePresenter(PROP_FALSE));
216: }
217:
218: }
|