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.listeners;
042:
043: import org.netbeans.modules.vmd.api.codegen.CodeNamePresenter;
044: import org.netbeans.modules.vmd.api.codegen.CodeReferencePresenter;
045: import org.netbeans.modules.vmd.api.model.*;
046: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
047:
048: import java.util.Arrays;
049: import java.util.List;
050:
051: /**
052: * @author David Kaspar
053: */
054:
055: public final class ItemStateListenerCD extends ComponentDescriptor {
056:
057: public static final TypeID TYPEID = new TypeID(
058: TypeID.Kind.COMPONENT, "#ItemStateListener"); // NOI18N
059:
060: public TypeDescriptor getTypeDescriptor() {
061: return new TypeDescriptor(EventListenerCD.TYPEID,
062: ItemStateListenerCD.TYPEID, true, false);
063: }
064:
065: public VersionDescriptor getVersionDescriptor() {
066: return MidpVersionDescriptor.MIDP;
067: }
068:
069: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
070: return null;
071: }
072:
073: protected List<? extends Presenter> createPresenters() {
074: return Arrays.asList(
075: // code
076: new CodeReferencePresenter() {
077: protected String generateAccessCode() {
078: return "this";
079: } // NOI18N
080:
081: protected String generateDirectAccessCode() {
082: return "this";
083: } // NOI18N
084:
085: protected String generateTypeCode() {
086: throw Debug.illegalState();
087: }
088: },
089: // new ItemStateListenerCD.CodeCommandActionPresenter ()
090: new EventListenerCode.CodeImplementsPresenter(
091: "javax.microedition.lcdui.ItemStateListener",
092: "itemStateChanged",
093: "javax.microedition.lcdui.Item"), // NOI18N
094: CodeNamePresenter.fixed("itemStateChanged") // NOI18N
095: );
096: }
097: /*
098: public static class CodeCommandActionPresenter extends CodeClassLevelPresenter {
099:
100: protected void generateFieldSectionCode (CodeWriter writer) {
101: }
102:
103: protected void generateMethodSectionCode (CodeWriter writer) {
104: }
105:
106: protected void generateInitializeSectionCode (CodeWriter writer) {
107: }
108:
109: protected void generateClassBodyCode (StyledDocument document) {
110: List<DesignComponent> sources = DocumentSupport.gatherAllComponentsOfTypeID (getComponent ().getDocument (), CommandEventSourceCD.TYPEID);
111: if (sources.size () == 0)
112: return;
113:
114: MultiGuardedSection section = MultiGuardedSection.create (document, getComponent ().getComponentID () + "-commandAction"); // NOI18N
115: section.getWriter ().write ("public void commandAction (Command command, Displayable displayable) {\n").commit (); // NOI18N
116:
117: section.switchToEditable (getComponent ().getComponentID () + "-preCommandAction"); // NOI18N
118: section.getWriter ().write (" // write pre-action user code here\n").commit (); // NOI18N
119: section.switchToGuarded ();
120:
121: resolveFirstLevel (section, sources);
122:
123: section.switchToEditable (getComponent ().getComponentID () + "-postCommandAction"); // NOI18N
124: section.getWriter ().write (" // write post-action user code here\n").commit (); // NOI18N
125: section.switchToGuarded ();
126:
127: section.getWriter ().write ("}\n").commit (); // NOI18N
128: }
129:
130: private void resolveFirstLevel (MultiGuardedSection section, List<DesignComponent> sources) {
131: HashMap<String, ArrayList<DesignComponent>> displayables2sources = gatherDisplayables (sources);
132: ArrayList<String> displayables = new ArrayList<String> (displayables2sources.keySet ());
133: Collections.sort (displayables);
134:
135: for (int i = 0; i < displayables.size (); i ++) {
136: String displayable = displayables.get (i);
137: if (i > 0)
138: section.getWriter ().write ("else "); // NOI18N
139: section.getWriter ().write ("if (displayable == " + displayable + ") {\n"); // NOI18N
140:
141: resolveSecondLevel (section, displayables2sources.get (displayable));
142:
143: assert section.isGuarded ();
144: if (i < displayables.size () - 1)
145: section.getWriter ().write ("} "); // NOI18N
146: else
147: section.getWriter ().write ("}\n"); // NOI18N
148: }
149:
150: section.getWriter ().commit ();
151: }
152:
153: private void resolveSecondLevel (MultiGuardedSection section, List<DesignComponent> sources) {
154: HashMap<String, ArrayList<DesignComponent>> commands2sources = gatherCommands (sources);
155: ArrayList<String> commands = new ArrayList<String> (commands2sources.keySet ());
156: Collections.sort (commands);
157:
158: for (int i = 0; i < commands.size (); i ++) {
159: String command = commands.get (i);
160: if (i > 0)
161: section.getWriter ().write ("else "); // NOI18N
162: section.getWriter ().write ("if (command == " + command + ") {\n"); // NOI18N
163:
164: for (DesignComponent source : commands2sources.get (command))
165: CodeMultiGuardedLevelPresenter.generateMultiGuardedSectionCode (section, source);
166:
167: assert section.isGuarded ();
168: if (i < commands.size () - 1)
169: section.getWriter ().write ("} "); // NOI18N
170: else
171: section.getWriter ().write ("}\n"); // NOI18N
172: }
173: }
174:
175: private HashMap<String, ArrayList<DesignComponent>> gatherDisplayables (List<DesignComponent> sources) {
176: HashMap<String, ArrayList<DesignComponent>> d2s = new HashMap<String, ArrayList<DesignComponent>> ();
177: for (DesignComponent source : sources) {
178: String displayable = CodeReferencePresenter.generateDirectAccessCode (source.readProperty (CommandEventSourceCD.PROP_DISPLAYABLE).getComponent ());
179: ArrayList<DesignComponent> s = d2s.get (displayable);
180: if (s == null) {
181: s = new ArrayList<DesignComponent> ();
182: d2s.put (displayable, s);
183: }
184: s.add (source);
185: }
186: return d2s;
187: }
188:
189: private HashMap<String, ArrayList<DesignComponent>> gatherCommands (List<DesignComponent> sources) {
190: HashMap<String, ArrayList<DesignComponent>> c2s = new HashMap<String, ArrayList<DesignComponent>> ();
191: for (DesignComponent source : sources) {
192: String command = CodeReferencePresenter.generateDirectAccessCode (source.readProperty (CommandEventSourceCD.PROP_COMMAND).getComponent ());
193: ArrayList<DesignComponent> s = c2s.get (command);
194: if (s == null) {
195: s = new ArrayList<DesignComponent> ();
196: c2s.put (command, s);
197: }
198: s.add (source);
199: }
200: return c2s;
201: }
202:
203: }
204: */
205: }
|