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.api.editor.guards.GuardedSection;
044: import org.netbeans.modules.vmd.api.codegen.CodeClassLevelPresenter;
045: import org.netbeans.modules.vmd.api.codegen.CodeMultiGuardedLevelPresenter;
046: import org.netbeans.modules.vmd.api.codegen.CodeReferencePresenter;
047: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
048: import org.netbeans.modules.vmd.api.inspector.InspectorOrderingController;
049: import org.netbeans.modules.vmd.api.inspector.InspectorOrderingPresenter;
050: import org.netbeans.modules.vmd.api.inspector.common.ArrayPropertyOrderingController;
051: import org.netbeans.modules.vmd.api.model.*;
052: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
053: import org.netbeans.modules.vmd.api.model.support.ArraySupport;
054: import org.netbeans.modules.vmd.api.properties.DefaultPropertiesPresenter;
055: import org.netbeans.modules.vmd.midp.actions.GoToSourcePresenter;
056: import org.netbeans.modules.vmd.midp.actions.MidpActionsSupport;
057: import org.netbeans.modules.vmd.midp.codegen.MidpCodeSupport;
058: import org.netbeans.modules.vmd.midp.components.MidpTypes;
059: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
060: import org.netbeans.modules.vmd.midp.components.sources.SwitchCaseEventSourceCD;
061: import org.netbeans.modules.vmd.midp.flow.FlowSwitchPointPinOrderPresenter;
062: import org.netbeans.modules.vmd.midp.general.AcceptTypePresenter;
063: import org.netbeans.modules.vmd.midp.palette.MidpPaletteProvider;
064: import org.netbeans.modules.vmd.midp.propertyeditors.MidpPropertiesCategories;
065: import org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorJavaString;
066: import org.openide.util.NbBundle;
067: import org.openide.util.Utilities;
068:
069: import javax.swing.text.StyledDocument;
070: import java.util.ArrayList;
071: import java.util.Arrays;
072: import java.util.List;
073:
074: /**
075: * @author David Kaspar
076: */
077: public class SwitchPointCD extends ComponentDescriptor {
078:
079: public static final TypeID TYPEID = new TypeID(
080: TypeID.Kind.COMPONENT, "#SwitchPoint"); // NOI18N
081:
082: public static final String ICON_PATH = "org/netbeans/modules/vmd/midp/resources/components/switch_16.png"; // NOI18N
083: public static final String LARGE_ICON_PATH = "org/netbeans/modules/vmd/midp/resources/components/switch_32.png"; // NOI18N
084:
085: public static final String PROP_OPERAND = "operand"; // NOI18N
086: public static final String PROP_CASES = "cases"; // NOI18N
087:
088: public TypeDescriptor getTypeDescriptor() {
089: return new TypeDescriptor(MethodPointCD.TYPEID,
090: SwitchPointCD.TYPEID, true, true);
091: }
092:
093: public VersionDescriptor getVersionDescriptor() {
094: return MidpVersionDescriptor.FOREVER;
095: }
096:
097: @Override
098: public void postInitialize(DesignComponent component) {
099: component.writeProperty(PROP_OPERAND, MidpTypes
100: .createJavaCodeValue("0")); // NOI18N
101: }
102:
103: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
104: return Arrays
105: .asList(
106: new PropertyDescriptor(PROP_OPERAND,
107: MidpTypes.TYPEID_JAVA_CODE,
108: PropertyValue.createNull(), false,
109: false, Versionable.FOREVER),
110: new PropertyDescriptor(
111: PROP_CASES,
112: SwitchCaseEventSourceCD.TYPEID
113: .getArrayType(),
114: PropertyValue
115: .createEmptyArray(SwitchCaseEventSourceCD.TYPEID),
116: false, false, Versionable.FOREVER));
117: }
118:
119: @Override
120: public PaletteDescriptor getPaletteDescriptor() {
121: return new PaletteDescriptor(
122: MidpPaletteProvider.CATEGORY_PROCESS_FLOW, NbBundle
123: .getMessage(SwitchPointCD.class,
124: "DISP_SwitchPoint"), NbBundle
125: .getMessage(SwitchPointCD.class,
126: "TTIP_SwitchPoint"), ICON_PATH,
127: LARGE_ICON_PATH); // NOI18N
128: }
129:
130: public DefaultPropertiesPresenter createPropertiesPresenter() {
131: return new DefaultPropertiesPresenter()
132: .addPropertiesCategory(
133: MidpPropertiesCategories.CATEGORY_PROPERTIES)
134: .addPropertiesCategory(
135: MidpPropertiesCategories.CATEGORY_CODE_PROPERTIES)
136: .addProperty(
137: NbBundle.getMessage(SwitchPointCD.class,
138: "DISP_SwitchPoint_Switch_Operand"),
139: PropertyEditorJavaString.createInstance(TYPEID),
140: PROP_OPERAND); // NOI18N
141: }
142:
143: @Override
144: protected void gatherPresenters(ArrayList<Presenter> presenters) {
145: MidpActionsSupport.addNewActionPresenter(presenters,
146: SwitchCaseEventSourceCD.TYPEID);
147: MidpActionsSupport.addCommonActionsPresenters(presenters,
148: false, true, true, true, true);
149: super .gatherPresenters(presenters);
150: }
151:
152: protected List<? extends Presenter> createPresenters() {
153: return Arrays
154: .asList(
155: // general
156: InfoPresenter.create(PointSupport
157: .createInfoResolver(Utilities
158: .loadImage(ICON_PATH),
159: MethodPointCD.PROP_METHOD_NAME,
160: "Switch")),
161: new GoToSourcePresenter() {
162: protected boolean matches(
163: GuardedSection section) {
164: return MultiGuardedSection.matches(
165: section, getComponent()
166: .getComponentID()
167: + "-switch", 1); // NOI18N
168: }
169: },
170: //inspector
171: InspectorOrderingPresenter
172: .create(createOrderingArrayController()),
173: // accept
174: new AcceptTypePresenter(
175: SwitchCaseEventSourceCD.TYPEID) {
176: @Override
177: protected void notifyCreated(
178: DesignComponent switchCase) {
179: super .notifyCreated(switchCase);
180: DesignComponent switchComponent = getComponent();
181: ArraySupport.append(switchComponent,
182: PROP_CASES, switchCase);
183: PropertyValue propertyValue = switchCase
184: .readProperty(SwitchCaseEventSourceCD.PROP_OPTION);
185: if (propertyValue.getKind() == PropertyValue.Kind.NULL) {
186: int size = switchComponent
187: .readProperty(PROP_CASES)
188: .getArray().size();
189: switchCase
190: .writeProperty(
191: SwitchCaseEventSourceCD.PROP_OPTION,
192: MidpTypes
193: .createJavaCodeValue(Integer
194: .toString(size)));
195: }
196: }
197: },
198: // properties
199: createPropertiesPresenter(),
200: // flow
201: new FlowSwitchPointPinOrderPresenter(),
202: // code
203: new CodeClassLevelPresenter.Adapter() {
204: @Override
205: protected void generateClassBodyCode(
206: StyledDocument document) {
207: DesignComponent component = getComponent();
208: MultiGuardedSection section = MultiGuardedSection
209: .create(document, component
210: .getComponentID()
211: + "-switch"); // NOI18N
212: String methodName = CodeReferencePresenter
213: .generateDirectAccessCode(component);
214: section.getWriter()
215: .write(
216: "//<editor-fold defaultstate=\"collapsed\" desc=\" Generated Method: "
217: + methodName
218: + " \">\n"); // NOI18N
219: section
220: .getWriter()
221: .write(
222: "/**\n * Performs an action assigned to the "
223: + methodName
224: + " switch-point.\n */\n"); // NOI18N
225: section.getWriter().write(
226: "public void " + methodName
227: + " () {\n").commit(); // NOI18N
228: section.switchToEditable(component
229: .getComponentID()
230: + "-preSwitch"); // NOI18N
231: section
232: .getWriter()
233: .write(
234: " // enter pre-switch user code here\n")
235: .commit(); // NOI18N
236: section.switchToGuarded();
237:
238: section.getWriter().write("switch ("); // NOI18N
239: MidpCodeSupport
240: .generateCodeForPropertyValue(
241: section.getWriter(),
242: component
243: .readProperty(PROP_OPERAND));
244: section.getWriter().write(") {\n"); // NOI18N
245: List<PropertyValue> cases = component
246: .readProperty(PROP_CASES)
247: .getArray();
248: for (PropertyValue value : cases) {
249: DesignComponent switchCase = value
250: .getComponent();
251: section.getWriter().write("case "); // NOI18N
252: MidpCodeSupport
253: .generateCodeForPropertyValue(
254: section.getWriter(),
255: switchCase
256: .readProperty(SwitchCaseEventSourceCD.PROP_OPTION));
257: section.getWriter().write(":\n"); // NOI18N
258: CodeMultiGuardedLevelPresenter
259: .generateMultiGuardedSectionCode(
260: section, switchCase);
261: section.getWriter().write(
262: "break;\n"); // NOI18N
263: }
264: section.getWriter().write("}\n")
265: .commit(); // NOI18N
266:
267: section.switchToEditable(component
268: .getComponentID()
269: + "-postSwitch"); // NOI18N
270: section
271: .getWriter()
272: .write(
273: " // enter post-switch user code here\n")
274: .commit(); // NOI18N
275: section.switchToGuarded();
276: section.getWriter().write("}\n"); // NOI18N
277: section.getWriter().write(
278: "//</editor-fold>\n").commit(); // NOI18N
279: section.close();
280: }
281: }
282:
283: );
284: }
285:
286: private InspectorOrderingController[] createOrderingArrayController() {
287: return new InspectorOrderingController[] { new ArrayPropertyOrderingController(
288: PROP_CASES, 0, SwitchCaseEventSourceCD.TYPEID) };
289: }
290: }
|