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.general;
042:
043: import org.netbeans.modules.vmd.api.codegen.CodeClassLevelPresenter;
044: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
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.DesignDocument;
048: import org.netbeans.modules.vmd.api.model.Debug;
049: import org.netbeans.modules.vmd.api.model.common.DocumentSupport;
050: import org.netbeans.modules.vmd.midp.components.MidpTypes;
051: import org.openide.util.Utilities;
052: import org.openide.util.TopologicalSortException;
053:
054: import javax.swing.text.StyledDocument;
055: import java.util.*;
056:
057: /**
058: * @author David Kaspar
059: */
060: public final class RootCode {
061:
062: private RootCode() {
063: }
064:
065: public static Presenter createInitializePresenter() {
066: return new CodeInitializeMethodPresenter();
067: }
068:
069: public static abstract class CodeComponentDependencyPresenter
070: extends Presenter {
071:
072: protected abstract void collectRequiredComponents(
073: Collection<DesignComponent> requiredComponents);
074:
075: }
076:
077: public static void collectRequiredComponents(
078: DesignComponent component,
079: Collection<DesignComponent> requiredComponents) {
080: for (CodeComponentDependencyPresenter presenter : component
081: .getPresenters(CodeComponentDependencyPresenter.class))
082: presenter.collectRequiredComponents(requiredComponents);
083: }
084:
085: private static class CodeInitializeMethodPresenter extends
086: CodeClassLevelPresenter.Adapter {
087:
088: @Override
089: protected void generateClassBodyCode(StyledDocument document) {
090: MultiGuardedSection section = MultiGuardedSection.create(
091: document, getComponent().getComponentID()
092: + "-initialize"); // NOI18N
093: section
094: .getWriter()
095: .write(
096: "//<editor-fold defaultstate=\"collapsed\" desc=\" Generated Method: initialize \">\n"); // NOI18N
097: section
098: .getWriter()
099: .write(
100: "/**\n * Initilizes the application.\n * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.\n */\n"); // NOI18N
101: section.getWriter().write("private void initialize () {\n")
102: .commit(); // NOI18N
103: section.switchToEditable(getComponent().getComponentID()
104: + "-preInitialize"); // NOI18N
105: section.getWriter().write(
106: " // write pre-initialize user code here\n")
107: .commit(); // NOI18N
108: section.switchToGuarded();
109:
110: List<DesignComponent> components = performTopologicalSort(getComponent()
111: .getDocument());
112: for (DesignComponent component : components)
113: for (CodeClassLevelPresenter presenter : component
114: .getPresenters(CodeClassLevelPresenter.class))
115: presenter.generateInitializeSectionCode(section);
116: section.getWriter().commit();
117:
118: section.switchToEditable(getComponent().getComponentID()
119: + "-postInitialize"); // NOI18N
120: section.getWriter().write(
121: " // write post-initialize user code here\n")
122: .commit(); // NOI18N
123: section.switchToGuarded();
124:
125: section.getWriter().write("}\n"); // NOI18N
126: section.getWriter().write("//</editor-fold>\n").commit(); // NOI18N
127: section.close();
128: }
129: }
130:
131: public static List<DesignComponent> performTopologicalSort(
132: DesignDocument document) {
133: ArrayList<DesignComponent> list = new ArrayList<DesignComponent>();
134: HashMap<DesignComponent, HashSet<DesignComponent>> map = new HashMap<DesignComponent, HashSet<DesignComponent>>();
135:
136: for (DesignComponent component : DocumentSupport
137: .gatherAllComponentsOfTypeID(document, ClassCD.TYPEID)) {
138: if (MidpTypes.getBoolean(component
139: .readProperty(ClassCD.PROP_LAZY_INIT)))
140: continue;
141:
142: list.add(component);
143:
144: ArrayList<DesignComponent> requiredComponents = new ArrayList<DesignComponent>();
145: collectRequiredComponents(component, requiredComponents);
146: for (DesignComponent requiredComponent : requiredComponents) {
147: HashSet<DesignComponent> edges = map
148: .get(requiredComponent);
149: if (edges == null) {
150: edges = new HashSet<DesignComponent>();
151: map.put(requiredComponent, edges);
152: }
153: edges.add(component);
154: }
155: }
156:
157: try {
158: return Utilities.topologicalSort(list, map);
159: } catch (TopologicalSortException e) {
160: Debug.warning("Topological sort failed", "UnsortableSets",
161: e.unsortableSets()); // NOI18N
162: return (List<DesignComponent>) e.partialSort();
163: }
164:
165: // IOUtils.runInAWTBlocking (new Runnable() {
166: // public void run () {
167: // // TODO - implement notification about changed components
168: // }
169: // });
170: }
171:
172: }
|