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.*;
044: import org.netbeans.modules.vmd.api.model.*;
045: import org.netbeans.modules.vmd.midp.codegen.CodeClassInitHeaderFooterPresenter;
046: import org.netbeans.modules.vmd.midp.components.MidpTypes;
047:
048: import javax.swing.text.StyledDocument;
049: import java.util.Arrays;
050: import java.util.Collection;
051: import java.util.List;
052:
053: /**
054: * @author David Kaspar
055: */
056: public class ClassCode {
057:
058: private static final String ARRAY_SUFFIX = "Array"; // NOI18N
059: private static final String GETTER_PREFIX = "get"; // NOI18N
060:
061: static final class ClassCodeReferencePresenter extends
062: CodeReferencePresenter {
063:
064: ClassCodeReferencePresenter() {
065: }
066:
067: protected String generateAccessCode() {
068: DesignComponent component = getComponent();
069: boolean lazyInit = MidpTypes.getBoolean(component
070: .readProperty(ClassCD.PROP_LAZY_INIT));
071: String instanceName = MidpTypes.getString(component
072: .readProperty(ClassCD.PROP_INSTANCE_NAME));
073: return lazyInit ? createGetterNameFromInstanceName(instanceName)
074: + " ()"
075: : instanceName; // NOI18N
076: }
077:
078: protected String generateDirectAccessCode() {
079: return MidpTypes.getString(getComponent().readProperty(
080: ClassCD.PROP_INSTANCE_NAME));
081: }
082:
083: protected String generateTypeCode() {
084: return MidpTypes.getSimpleClassName(getComponent()
085: .getType());
086: }
087:
088: }
089:
090: /**
091: * Requires CodeReferencePresenter on the same component.
092: */
093: static final class CodeLazyInitPresenter extends
094: CodeClassLevelPresenter {
095:
096: CodeLazyInitPresenter() {
097: }
098:
099: protected void generateFieldSectionCode(
100: MultiGuardedSection section) {
101: section
102: .getWriter()
103: .write(
104: "private "
105: + CodeReferencePresenter
106: .generateTypeCode(getComponent())
107: + " "
108: + CodeReferencePresenter
109: .generateDirectAccessCode(getComponent())
110: + ";\n"); // NOI18N
111: }
112:
113: protected void generateMethodSectionCode(
114: MultiGuardedSection section) {
115: }
116:
117: public void generateInitializeSectionCode(
118: MultiGuardedSection section) {
119: if (ClassSupport.isLazyInitialized(getComponent()))
120: return;
121: InitCodeGenerator.generateInitializationCode(section,
122: getComponent());
123: }
124:
125: protected void generateClassBodyCode(StyledDocument document) {
126: if (!ClassSupport.isLazyInitialized(getComponent()))
127: return;
128:
129: MultiGuardedSection section = MultiGuardedSection.create(
130: document, getComponent().getComponentID()
131: + "-getter");// NOI18N
132: String directAccess = CodeReferencePresenter
133: .generateDirectAccessCode(getComponent());
134:
135: section.getWriter().write(
136: "//<editor-fold defaultstate=\"collapsed\" desc=\" Generated Getter: "
137: + directAccess + " \">\n"); // NOI18N
138: section
139: .getWriter()
140: .write(
141: "/**\n * Returns an initiliazed instance of "
142: + directAccess
143: + " component.\n * @return the initialized component instance\n */\n"); // NOI18N
144: section.getWriter().write(
145: "public "
146: + CodeReferencePresenter
147: .generateTypeCode(getComponent())
148: + " "
149: + CodeReferencePresenter
150: .generateAccessCode(getComponent())
151: + " {\n"); // NOI18N
152: section.getWriter().write(
153: "if (" + directAccess + " == null) {\n").commit(); // NOI18N
154:
155: section.switchToEditable(getComponent().getComponentID()
156: + "-preInit"); // NOI18N
157: section.getWriter().write(
158: " // write pre-init user code here\n").commit(); // NOI18N
159:
160: section.switchToGuarded();
161: Collection<? extends CodeClassInitHeaderFooterPresenter> headersFooters = getComponent()
162: .getPresenters(
163: CodeClassInitHeaderFooterPresenter.class);
164: for (CodeClassInitHeaderFooterPresenter header : headersFooters)
165: header.generateClassInitializationHeader(section);
166: InitCodeGenerator.generateInitializationCode(section,
167: getComponent());
168: for (CodeClassInitHeaderFooterPresenter footer : headersFooters)
169: footer.generateClassInitializationFooter(section);
170: section.getWriter().commit();
171:
172: section.switchToEditable(getComponent().getComponentID()
173: + "-postInit"); // NOI18N
174: section.getWriter().write(
175: " // write post-init user code here\n").commit(); // NOI18N
176:
177: section.switchToGuarded();
178: section.getWriter().write("}\n"); // NOI18N
179: section.getWriter().write("return " + directAccess + ";\n"); // NOI18N
180: section.getWriter().write("}\n"); // NOI18N
181: section.getWriter().write("//</editor-fold>\n").commit(); // NOI18N
182:
183: section.close();
184: }
185:
186: }
187:
188: static class CodeClassComponentDependencyPresenter extends
189: RootCode.CodeComponentDependencyPresenter {
190:
191: protected void collectRequiredComponents(
192: Collection<DesignComponent> requiredComponents) {
193: DesignComponent component = getComponent();
194: ComponentDescriptor descriptor = component
195: .getComponentDescriptor();
196: if (descriptor != null)
197: for (PropertyDescriptor property : descriptor
198: .getPropertyDescriptors())
199: Debug.collectAllComponentReferences(component
200: .readProperty(property.getName()),
201: requiredComponents);
202: }
203: }
204:
205: static class CodeClassNamePresenter extends CodeNamePresenter {
206:
207: public List<String> getReservedNames() {
208: return getReservedNamesFor(MidpTypes
209: .getString(getComponent().readProperty(
210: ClassCD.PROP_INSTANCE_NAME)));
211: }
212:
213: public List<String> getReservedNamesFor(String suggestedMainName) {
214: return Arrays
215: .asList(
216: suggestedMainName,
217: createGetterNameFromInstanceName(suggestedMainName));
218: }
219:
220: }
221:
222: private static String createGetterNameFromInstanceName(
223: String instanceName) {
224: if (instanceName == null || instanceName.length() < 0)
225: return null;
226: return GETTER_PREFIX
227: + Character.toUpperCase(instanceName.charAt(0))
228: + instanceName.substring(1);
229: }
230:
231: public static String getSuggestedMainName(TypeID type) {
232: String instanceName = type.getString();
233: int index = instanceName.lastIndexOf('.'); // NOI18N
234: if (index >= 0)
235: instanceName = instanceName.substring(index + 1);
236: if (type.getDimension() > 0)
237: instanceName += ARRAY_SUFFIX;
238: if (instanceName.length() <= 0)
239: instanceName = "object"; // NOI18N
240: char[] chars = instanceName.toCharArray();
241: for (int i = 0; i < chars.length; i++) {
242: if (Character.isLetter(chars[i])) {
243: if (Character.isUpperCase(chars[i])) {
244: if (i == 0)
245: chars[i] = Character.toLowerCase(chars[i]);
246: else if (i + 1 < chars.length) {
247: if (Character.isUpperCase(chars[i + 1]))
248: chars[i] = Character.toLowerCase(chars[i]);
249: else
250: break;
251: } else
252: chars[i] = Character.toLowerCase(chars[i]);
253: } else
254: break;
255: }
256: }
257: return new String(chars);
258: }
259:
260: }
|