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.CodeNamePresenter;
045: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
046: import org.netbeans.modules.vmd.api.inspector.InspectorFolderPresenter;
047: import org.netbeans.modules.vmd.api.inspector.InspectorPositionPresenter;
048: import org.netbeans.modules.vmd.api.model.*;
049: import org.netbeans.modules.vmd.api.model.presenters.actions.DeletePresenter;
050: import org.netbeans.modules.vmd.api.model.common.DocumentSupport;
051: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
052: import org.netbeans.modules.vmd.midp.components.handlers.PreviousScreenEventHandlerCD;
053:
054: import javax.swing.text.StyledDocument;
055: import java.util.ArrayList;
056: import java.util.Arrays;
057: import java.util.List;
058:
059: /**
060: * @author Karol Harezlak
061: */
062: public final class DisplayCD extends ComponentDescriptor {
063:
064: public static final TypeID TYPEID = new TypeID(
065: TypeID.Kind.COMPONENT, "#Display"); // NOI18N
066:
067: public TypeDescriptor getTypeDescriptor() {
068: return new TypeDescriptor(PointCD.TYPEID, TYPEID, true, true);
069: }
070:
071: public VersionDescriptor getVersionDescriptor() {
072: return MidpVersionDescriptor.MIDP;
073: }
074:
075: public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
076: return null;
077: }
078:
079: @Override
080: protected void gatherPresenters(ArrayList<Presenter> presenters) {
081: DocumentSupport.removePresentersOfClass(presenters,
082: InspectorFolderPresenter.class);
083: DocumentSupport.removePresentersOfClass(presenters,
084: InspectorPositionPresenter.class);
085: super .gatherPresenters(presenters);
086: }
087:
088: protected List<? extends Presenter> createPresenters() {
089: return Arrays.asList(
090: // code
091: createSwitchDisplayCodePresenter(), CodeNamePresenter
092: .fixed("switchDisplayable", "getDisplay",
093: "__previousDisplayables",
094: "switchToPreviousDisplayable"), // NOI18N
095: // delete
096: DeletePresenter.createIndeliblePresenter());
097: }
098:
099: private CodeClassLevelPresenter.Adapter createSwitchDisplayCodePresenter() {
100: return new CodeClassLevelPresenter.Adapter() {
101: @Override
102: protected void generateFieldSectionCode(
103: MultiGuardedSection section) {
104: if (isPreviousScreenEventHandlerUsed())
105: section
106: .getWriter()
107: .write(
108: "private java.util.Hashtable __previousDisplayables = new java.util.Hashtable ();\n"); // NOI18N
109: }
110:
111: @Override
112: protected void generateMethodSectionCode(
113: MultiGuardedSection section) {
114: if (isPreviousScreenEventHandlerUsed()) {
115: section
116: .getWriter()
117: .write(
118: "/**\n * Switches a display to previous displayable of the current displayable.\n * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.\n */\n"); // NOI18N
119: section
120: .getWriter()
121: .write(
122: "private void switchToPreviousDisplayable () {\n"); // NOI18N
123: section
124: .getWriter()
125: .write(
126: "Displayable __currentDisplayable = getDisplay ().getCurrent ();\n"); // NOI18N
127: section.getWriter().write(
128: "if (__currentDisplayable != null) {\n"); // NOI18N
129: section
130: .getWriter()
131: .write(
132: "Displayable __nextDisplayable = (Displayable) __previousDisplayables.get (__currentDisplayable);\n"); // NOI18N
133: section.getWriter().write(
134: "if (__nextDisplayable != null) {\n"); // NOI18N
135: section
136: .getWriter()
137: .write(
138: "switchDisplayable (null, __nextDisplayable);\n"); // NOI18N
139: section.getWriter().write("}\n"); // NOI18N
140: section.getWriter().write("}\n"); // NOI18N
141: section.getWriter().write("}\n"); // NOI18N
142: }
143: }
144:
145: @Override
146: protected void generateClassBodyCode(StyledDocument document) {
147: MultiGuardedSection section = MultiGuardedSection
148: .create(document, getComponent()
149: .getComponentID()
150: + "-switchDisplayable"); // NOI18N
151:
152: section
153: .getWriter()
154: .write(
155: "//<editor-fold defaultstate=\"collapsed\" desc=\" Generated Method: switchDisplayable \">\n"); // NOI18N
156: section
157: .getWriter()
158: .write(
159: "/**\n * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.\n * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately\n * @param nextDisplayable the Displayable to be set\n */\n"); // NOI18N
160: section
161: .getWriter()
162: .write(
163: "public void switchDisplayable (Alert alert, Displayable nextDisplayable) {\n")
164: .commit(); // NOI18N
165:
166: section.switchToEditable(getComponent()
167: .getComponentID()
168: + "-preSwitch"); // NOI18N
169: section.getWriter().write(
170: " // write pre-switch user code here\n")
171: .commit(); // NOI18N
172:
173: section.switchToGuarded();
174: section.getWriter().write(
175: "Display display = getDisplay ();\n"); // NOI18N
176: if (isPreviousScreenEventHandlerUsed()) {
177: section
178: .getWriter()
179: .write(
180: "Displayable __currentDisplayable = display.getCurrent ();\n"); // NOI18N
181: section
182: .getWriter()
183: .write(
184: "if (__currentDisplayable != null && nextDisplayable != null) {\n"); // NOI18N
185: section
186: .getWriter()
187: .write(
188: "__previousDisplayables.put (nextDisplayable, __currentDisplayable);\n"); // NOI18N
189: section.getWriter().write("}\n"); // NOI18N
190: }
191: section.getWriter().write("if (alert == null) {\n"); // NOI18N
192: section.getWriter().write(
193: "display.setCurrent (nextDisplayable);\n"); // NOI18N
194: section.getWriter().write("} else {\n"); // NOI18N
195: section
196: .getWriter()
197: .write(
198: "display.setCurrent (alert, nextDisplayable);\n"); // NOI18N
199: section.getWriter().write("}\n").commit(); // NOI18N
200:
201: section.switchToEditable(getComponent()
202: .getComponentID()
203: + "-postSwitch"); // NOI18N
204: section.getWriter().write(
205: " // write post-switch user code here\n")
206: .commit(); // NOI18N
207:
208: section.switchToGuarded();
209: section.getWriter().write("}\n"); // NOI18N
210: section.getWriter().write("//</editor-fold>\n")
211: .commit(); // NOI18N
212:
213: section.close();
214: }
215:
216: private boolean isPreviousScreenEventHandlerUsed() {
217: return !DocumentSupport.gatherAllComponentsOfTypeID(
218: getComponent().getDocument(),
219: PreviousScreenEventHandlerCD.TYPEID).isEmpty();
220: }
221:
222: };
223: }
224:
225: }
|