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.midpnb.codegen;
042:
043: import org.netbeans.modules.vmd.api.codegen.*;
044: import org.netbeans.modules.vmd.api.model.DescriptorRegistry;
045: import org.netbeans.modules.vmd.api.model.DesignComponent;
046: import org.netbeans.modules.vmd.api.model.Presenter;
047: import org.netbeans.modules.vmd.api.model.PropertyValue;
048: import org.netbeans.modules.vmd.midp.codegen.MidpCodeSupport;
049: import org.netbeans.modules.vmd.midp.codegen.MidpParameter;
050: import org.netbeans.modules.vmd.midp.components.MidpTypes;
051: import org.netbeans.modules.vmd.midp.components.displayables.DisplayableCD;
052: import org.netbeans.modules.vmd.midp.components.displayables.DisplayableCode;
053: import org.netbeans.modules.vmd.midp.components.general.ClassCD;
054: import org.netbeans.modules.vmd.midp.components.sources.CommandEventSourceCD;
055: import org.netbeans.modules.vmd.midpnb.components.commands.*;
056: import org.netbeans.modules.vmd.midpnb.components.displayables.PIMBrowserCD;
057: import org.netbeans.modules.vmd.midpnb.components.displayables.SplashScreenCD;
058: import org.netbeans.modules.vmd.midpnb.components.sources.SVGMenuElementEventSourceCD;
059: import org.netbeans.modules.vmd.midpnb.components.svg.SVGMenuCD;
060: import org.netbeans.modules.vmd.midpnb.components.svg.SVGSplashScreenCD;
061:
062: import javax.swing.text.StyledDocument;
063: import java.util.Arrays;
064: import java.util.List;
065:
066: /**
067: * @author David Kaspar
068: */
069: public final class MidpCustomCodePresenterSupport {
070:
071: public static final String PARAM_DISPLAY = "display"; // NOI18N
072: public static final String PARAM_TIMEOUT = "timeout"; // NOI18N
073: public static final String PARAM_SVG_TIMEOUT = "timeout"; // NOI18N
074: public static final String PARAM_SVG_MENU_ELEMENT = "menuElement"; // NOI18N
075: public static final String PARAM_PIM_LIST_TYPE = "pimListType"; //NOI18N
076:
077: private static final String SVG_MENU_ACTION_METHOD_SUFFIX = "Action"; // NOI18N
078:
079: private static final Parameter PARAMETER_DISPLAY = new DisplayParameter();
080: private static final Parameter PARAMETER_TIMEOUT = new TimeoutParameter();
081: private static final Parameter PARAMETER_SVG_TIMEOUT = new SVGTimeoutParameter();
082: private static final Parameter PARAMETER_WAITSCREEN_COMMAND = new WaitScreenCommandParameter();
083: private static final Parameter PARAMETER_SPLASHSCREEN_COMMAND = new SplashScreenCommandParameter();
084: private static final Parameter PARAMETER_SVG_WAITSCREEN_COMMAND = new SVGWaitScreenCommandParameter();
085: private static final Parameter PARAMETER_SVG_SPLASHSCREEN_COMMAND = new SVGSplashScreenCommandParameter();
086: private static final Parameter PARAMETER_SVG_MENU_ELEMENT = new SVGMenuElementParameter();
087: private static final Parameter PARAMETER_PIM_LIST_TYPES = new PIMListTypesParameter();
088:
089: private MidpCustomCodePresenterSupport() {
090: }
091:
092: public static Parameter createDisplayParameter() {
093: return PARAMETER_DISPLAY;
094: }
095:
096: public static Parameter createPIMListTypesParameter() {
097: return PARAMETER_PIM_LIST_TYPES;
098: }
099:
100: public static Parameter createTimeoutParameter() {
101: return PARAMETER_TIMEOUT;
102: }
103:
104: public static Parameter createSVGTimeoutParameter() {
105: return PARAMETER_SVG_TIMEOUT;
106: }
107:
108: public static Parameter createWaitScreenCommandParameter() {
109: return PARAMETER_WAITSCREEN_COMMAND;
110: }
111:
112: public static Parameter createSplashScreenCommandParameter() {
113: return PARAMETER_SPLASHSCREEN_COMMAND;
114: }
115:
116: public static Parameter createSVGWaitScreenCommandParameter() {
117: return PARAMETER_SVG_WAITSCREEN_COMMAND;
118: }
119:
120: public static Parameter createSVGSplashScreenCommandParameter() {
121: return PARAMETER_SVG_SPLASHSCREEN_COMMAND;
122: }
123:
124: public static Parameter createSVGMenuElementParameter() {
125: return PARAMETER_SVG_MENU_ELEMENT;
126: }
127:
128: public static Presenter createSVGMenuEventHandlerCodeNamePresenter() {
129: return new SVGMenuActionCodeNamePresenter();
130: }
131:
132: public static Presenter createSVGMenuCodePresenter() {
133: return new SVGMenuActionCodeClassLevelPresenter();
134: }
135:
136: public static String getSVGMenuActionMethodAccessCode(
137: DesignComponent menu) {
138: if (menu == null
139: || !menu
140: .getDocument()
141: .getDescriptorRegistry()
142: .isInHierarchy(SVGMenuCD.TYPEID, menu.getType()))
143: return null;
144: return MidpTypes.getString(menu
145: .readProperty(ClassCD.PROP_INSTANCE_NAME))
146: + SVG_MENU_ACTION_METHOD_SUFFIX;
147: }
148:
149: private static class DisplayParameter implements Parameter {
150:
151: public String getParameterName() {
152: return PARAM_DISPLAY;
153: }
154:
155: public int getParameterPriority() {
156: return 0;
157: }
158:
159: public void generateParameterCode(DesignComponent component,
160: MultiGuardedSection section, int index) {
161: section.getWriter().write("getDisplay ()"); // NOI18N
162: }
163:
164: public boolean isRequiredToBeSet(DesignComponent component) {
165: return false;
166: }
167:
168: public int getCount(DesignComponent component) {
169: return -1;
170: }
171:
172: public boolean isRequiredToBeSet(DesignComponent component,
173: int index) {
174: return false;
175: }
176:
177: }
178:
179: private static class PIMListTypesParameter implements Parameter {
180:
181: public String getParameterName() {
182: return PARAM_PIM_LIST_TYPE;
183: }
184:
185: public int getParameterPriority() {
186: return 0;
187: }
188:
189: public void generateParameterCode(DesignComponent component,
190: MultiGuardedSection section, int index) {
191: Integer pimListType = (Integer) component.readProperty(
192: PIMBrowserCD.PROP_PIM_TYPE).getPrimitiveValue();
193: String listTypeParameter = null;
194: switch (pimListType) {
195: case PIMBrowserCD.VALUE_CONTACT_LIST:
196: listTypeParameter = "PIM.CONTACT_LIST"; //NOI18N
197: break;
198: case PIMBrowserCD.VALUE_EVENT_LIST:
199: listTypeParameter = "PIM.EVENT_LIST"; //NOI18N
200: break;
201: case PIMBrowserCD.VALUE_TODO_LIST:
202: listTypeParameter = "PIM.TODO_LIST"; //NOI18N
203: break;
204: default:
205: throw new IllegalStateException(
206: "Illegal value of pimListParameter"); // NOI18N
207: }
208:
209: section.getWriter().write(listTypeParameter); // NOI18N
210: }
211:
212: public boolean isRequiredToBeSet(DesignComponent component) {
213: return true;
214: }
215:
216: public int getCount(DesignComponent component) {
217: return 1;
218: }
219:
220: public boolean isRequiredToBeSet(DesignComponent component,
221: int index) {
222: return true;
223: }
224: }
225:
226: private static class TimeoutParameter extends MidpParameter {
227:
228: protected TimeoutParameter() {
229: super (PARAM_TIMEOUT);
230: }
231:
232: public void generateParameterCode(DesignComponent component,
233: MultiGuardedSection section, int index) {
234: PropertyValue value = component
235: .readProperty(SplashScreenCD.PROP_TIMEOUT);
236: if (value.getKind() == PropertyValue.Kind.VALUE)
237: if (MidpTypes.getInteger(value) == 0) {
238: section.getWriter().write("SplashScreen.TIMEOUT"); // NOI18N
239: return;
240: }
241: super .generateParameterCode(component, section, index);
242: }
243: }
244:
245: private static class SVGTimeoutParameter extends MidpParameter {
246:
247: protected SVGTimeoutParameter() {
248: super (PARAM_SVG_TIMEOUT);
249: }
250:
251: public void generateParameterCode(DesignComponent component,
252: MultiGuardedSection section, int index) {
253: PropertyValue value = component
254: .readProperty(SVGSplashScreenCD.PROP_TIMEOUT);
255: if (value.getKind() == PropertyValue.Kind.VALUE)
256: if (MidpTypes.getInteger(value) == 0) {
257: section.getWriter()
258: .write("SVGSplashScreen.TIMEOUT"); // NOI18N
259: return;
260: }
261: super .generateParameterCode(component, section, index);
262: }
263: }
264:
265: private static final class WaitScreenCommandParameter extends
266: DisplayableCode.CommandParameter {
267:
268: public int getParameterPriority() {
269: return super .getParameterPriority() + 1;
270: }
271:
272: public boolean isRequiredToBeSet(DesignComponent component,
273: int index) {
274: DescriptorRegistry descriptorRegistry = component
275: .getDocument().getDescriptorRegistry();
276: List<PropertyValue> array = component.readProperty(
277: DisplayableCD.PROP_COMMANDS).getArray();
278: DesignComponent commandEventSource = array.get(index)
279: .getComponent();
280: DesignComponent command = commandEventSource.readProperty(
281: CommandEventSourceCD.PROP_COMMAND).getComponent();
282: if (command != null
283: && descriptorRegistry.isInHierarchy(
284: WaitScreenSuccessCommandCD.TYPEID, command
285: .getType()))
286: return false;
287: if (command != null
288: && descriptorRegistry.isInHierarchy(
289: WaitScreenFailureCommandCD.TYPEID, command
290: .getType()))
291: return false;
292: return super .isRequiredToBeSet(command, index);
293: }
294:
295: }
296:
297: private static final class SplashScreenCommandParameter extends
298: DisplayableCode.CommandParameter {
299:
300: public int getParameterPriority() {
301: return super .getParameterPriority() + 1;
302: }
303:
304: public boolean isRequiredToBeSet(DesignComponent component,
305: int index) {
306: DescriptorRegistry descriptorRegistry = component
307: .getDocument().getDescriptorRegistry();
308: List<PropertyValue> array = component.readProperty(
309: DisplayableCD.PROP_COMMANDS).getArray();
310: DesignComponent commandEventSource = array.get(index)
311: .getComponent();
312: DesignComponent command = commandEventSource.readProperty(
313: CommandEventSourceCD.PROP_COMMAND).getComponent();
314: if (command != null
315: && descriptorRegistry.isInHierarchy(
316: SplashScreenDismissCommandCD.TYPEID,
317: command.getType()))
318: return false;
319: return super .isRequiredToBeSet(command, index);
320: }
321:
322: }
323:
324: private static final class SVGWaitScreenCommandParameter extends
325: DisplayableCode.CommandParameter {
326:
327: public int getParameterPriority() {
328: return super .getParameterPriority() + 1;
329: }
330:
331: public boolean isRequiredToBeSet(DesignComponent component,
332: int index) {
333: DescriptorRegistry descriptorRegistry = component
334: .getDocument().getDescriptorRegistry();
335: List<PropertyValue> array = component.readProperty(
336: DisplayableCD.PROP_COMMANDS).getArray();
337: DesignComponent commandEventSource = array.get(index)
338: .getComponent();
339: DesignComponent command = commandEventSource.readProperty(
340: CommandEventSourceCD.PROP_COMMAND).getComponent();
341: if (command != null
342: && descriptorRegistry.isInHierarchy(
343: SVGWaitScreenSuccessCommandCD.TYPEID,
344: command.getType()))
345: return false;
346: if (command != null
347: && descriptorRegistry.isInHierarchy(
348: SVGWaitScreenFailureCommandCD.TYPEID,
349: command.getType()))
350: return false;
351: return super .isRequiredToBeSet(command, index);
352: }
353:
354: }
355:
356: private static final class SVGSplashScreenCommandParameter extends
357: DisplayableCode.CommandParameter {
358:
359: public int getParameterPriority() {
360: return super .getParameterPriority() + 1;
361: }
362:
363: public boolean isRequiredToBeSet(DesignComponent component,
364: int index) {
365: DescriptorRegistry descriptorRegistry = component
366: .getDocument().getDescriptorRegistry();
367: List<PropertyValue> array = component.readProperty(
368: DisplayableCD.PROP_COMMANDS).getArray();
369: DesignComponent commandEventSource = array.get(index)
370: .getComponent();
371: DesignComponent command = commandEventSource.readProperty(
372: CommandEventSourceCD.PROP_COMMAND).getComponent();
373: if (command != null
374: && descriptorRegistry.isInHierarchy(
375: SVGSplashScreenDismissCommandCD.TYPEID,
376: command.getType()))
377: return false;
378: return super .isRequiredToBeSet(command, index);
379: }
380:
381: }
382:
383: private static final class SVGMenuElementParameter implements
384: Parameter {
385:
386: public String getParameterName() {
387: return PARAM_SVG_MENU_ELEMENT;
388: }
389:
390: public int getParameterPriority() {
391: return 0;
392: }
393:
394: public void generateParameterCode(DesignComponent component,
395: MultiGuardedSection section, int index) {
396: List<PropertyValue> elements = component.readProperty(
397: SVGMenuCD.PROP_ELEMENTS).getArray();
398: PropertyValue value = elements.get(index);
399: DesignComponent element = value.getComponent();
400: PropertyValue string = element
401: .readProperty(SVGMenuElementEventSourceCD.PROP_STRING);
402: MidpCodeSupport.generateCodeForPropertyValue(section
403: .getWriter(), string);
404: }
405:
406: public boolean isRequiredToBeSet(DesignComponent component) {
407: List<PropertyValue> array = component.readProperty(
408: SVGMenuCD.PROP_ELEMENTS).getArray();
409: return array != null && array.size() != 0;
410: }
411:
412: public int getCount(DesignComponent component) {
413: List<PropertyValue> array = component.readProperty(
414: SVGMenuCD.PROP_ELEMENTS).getArray();
415: return array != null ? array.size() : 0;
416: }
417:
418: public boolean isRequiredToBeSet(DesignComponent component,
419: int index) {
420: return true;
421: }
422:
423: }
424:
425: private static final class SVGMenuActionCodeNamePresenter extends
426: CodeNamePresenter {
427:
428: public List<String> getReservedNames() {
429: return getReservedNamesFor(MidpTypes
430: .getString(getComponent().readProperty(
431: ClassCD.PROP_INSTANCE_NAME)));
432: }
433:
434: public List<String> getReservedNamesFor(String suggestedMainName) {
435: return Arrays.asList(suggestedMainName
436: + SVG_MENU_ACTION_METHOD_SUFFIX);
437: }
438:
439: }
440:
441: private static final class SVGMenuActionCodeClassLevelPresenter
442: extends CodeClassLevelPresenter.Adapter {
443:
444: protected void generateClassBodyCode(StyledDocument document) {
445: DesignComponent menu = getComponent();
446: List<PropertyValue> array = menu.readProperty(
447: SVGMenuCD.PROP_ELEMENTS).getArray();
448:
449: MultiGuardedSection section = MultiGuardedSection.create(
450: document, menu.getComponentID() + "-action"); // NOI18N
451: String menuName = CodeReferencePresenter
452: .generateDirectAccessCode(menu);
453: String methodName = menuName
454: + SVG_MENU_ACTION_METHOD_SUFFIX;
455: section.getWriter().write(
456: "//<editor-fold defaultstate=\"collapsed\" desc=\" Generated Method: "
457: + methodName + " \">\n"); // NOI18N
458: section.getWriter().write(
459: "/**\n * Performs an action assigned to the selected SVG menu element in the "
460: + menuName + " component.\n */\n"); // NOI18N
461: section.getWriter().write(
462: "public void " + methodName + " () {\n").commit(); // NOI18N
463: section.switchToEditable(menu.getComponentID()
464: + "-preAction"); // NOI18N
465: section.getWriter().write(
466: " // enter pre-action user code here\n").commit(); // NOI18N
467: section.switchToGuarded();
468:
469: String menuInstanceName = CodeReferencePresenter
470: .generateAccessCode(menu);
471: boolean indexBased = MidpTypes.getBoolean(menu
472: .readProperty(SVGMenuCD.PROP_INDEX_BASED_SWITCH));
473: if (!indexBased) {
474: section.getWriter().write(
475: "String __selectedElement = "
476: + menuInstanceName
477: + ".getMenuElementID ("
478: + menuInstanceName
479: + ".getSelectedIndex ());\n"); // NOI18N
480: }
481:
482: if (array.size() > 0) {
483: if (indexBased) {
484: section.getWriter().write(
485: "switch (" + menuInstanceName
486: + ".getSelectedIndex ()) {\n"); // NOI18N
487: } else {
488: section.getWriter().write(
489: "if (__selectedElement != null) {\n"); // NOI18N
490: }
491:
492: for (int i = 0; i < array.size(); i++) {
493: PropertyValue value = array.get(i);
494: DesignComponent source = value.getComponent();
495:
496: if (indexBased) {
497: section.getWriter().write("case " + i + ":\n"); // NOI18N
498: } else {
499: if (i > 0)
500: section.getWriter().write("} else "); // NOI18N
501: section.getWriter().write(
502: "if (__selectedElement.equals ("); // NOI18N
503: MidpCodeSupport
504: .generateCodeForPropertyValue(
505: section.getWriter(),
506: source
507: .readProperty(SVGMenuElementEventSourceCD.PROP_STRING));
508: section.getWriter().write(")) {\n"); // NOI18N
509: }
510:
511: CodeMultiGuardedLevelPresenter
512: .generateMultiGuardedSectionCode(section,
513: source);
514:
515: if (indexBased)
516: section.getWriter().write("break;\n"); // NOI18N
517: }
518:
519: if (!indexBased)
520: section.getWriter().write("}\n"); // NOI18N
521: section.getWriter().write("}\n"); // NOI18N
522: }
523:
524: section.getWriter().commit();
525: section.switchToEditable(menu.getComponentID()
526: + "-postAction"); // NOI18N
527: section.getWriter().write(
528: " // enter post-action user code here\n").commit(); // NOI18N
529: section.switchToGuarded();
530: section.getWriter().write("}\n"); // NOI18N
531: section.getWriter().write("//</editor-fold>\n").commit(); // NOI18N
532: section.close();
533: }
534:
535: }
536:
537: }
|