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.codegen;
042:
043: import org.netbeans.modules.vmd.api.codegen.*;
044: import org.netbeans.modules.vmd.api.model.TypeID;
045: import org.netbeans.modules.vmd.api.model.DesignComponent;
046: import org.netbeans.modules.vmd.api.model.Versionable;
047: import org.netbeans.modules.vmd.midp.components.MidpTypes;
048:
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.List;
052: import java.util.Map;
053:
054: /**
055: * @author David Kaspar
056: */
057: public class MidpSetter implements Setter {
058:
059: private TypeID constructorRelatedTypeID;
060: private String instanceNameSuffix;
061: private Versionable versionable;
062:
063: private String arrayParameterName;
064: private ArrayList<String> parameterNames = new ArrayList<String>();
065: private ArrayList<String> exceptions = new ArrayList<String>();
066:
067: private MidpSetter(TypeID constructorRelatedTypeID,
068: String instanceNameSuffix, Versionable versionable) {
069: this .constructorRelatedTypeID = constructorRelatedTypeID;
070: this .instanceNameSuffix = instanceNameSuffix;
071: this .versionable = versionable;
072: }
073:
074: public MidpSetter setArrayParameter(String arrayParameterName) {
075: this .arrayParameterName = arrayParameterName;
076: return this ;
077: }
078:
079: public MidpSetter addParameters(String... parameterNames) {
080: this .parameterNames.addAll(Arrays.asList(parameterNames));
081: return this ;
082: }
083:
084: public MidpSetter addExceptions(String... exceptions) {
085: this .exceptions.addAll(Arrays.asList(exceptions));
086: return this ;
087: }
088:
089: public static MidpSetter createConstructor(
090: TypeID constructorRelatedTypeID, Versionable versionable) {
091: return new MidpSetter(constructorRelatedTypeID, " = new "
092: + MidpTypes
093: .getSimpleClassName(constructorRelatedTypeID),
094: versionable); // NOI18N
095: }
096:
097: public static MidpSetter createFactoryMethod(
098: TypeID constructorRelatedTypeID, String classCast,
099: String className, String methodName, Versionable versionable) {
100: return new MidpSetter(constructorRelatedTypeID, " = "
101: + (classCast != null ? ("(" + classCast + ") ") : "")
102: + className + "." + methodName, versionable); // NOI18N
103: }
104:
105: public static MidpSetter createSetter(String setterName,
106: Versionable versionable) {
107: return new MidpSetter(null, "." + setterName, versionable); // NOI18N
108: }
109:
110: public boolean isConstructor() {
111: return constructorRelatedTypeID != null;
112: }
113:
114: public TypeID getConstructorRelatedTypeID() {
115: return constructorRelatedTypeID;
116: }
117:
118: public int getPriority() {
119: return 0;
120: }
121:
122: public String getSetterName() {
123: return instanceNameSuffix;
124: }
125:
126: public Versionable getVersionable() {
127: return versionable;
128: }
129:
130: public void generateSetterCode(MultiGuardedSection section,
131: DesignComponent component,
132: Map<String, Parameter> name2parameter) {
133: if (!exceptions.isEmpty())
134: section.getWriter().write("try {\n"); // NOI18N
135:
136: if (arrayParameterName != null) {
137: Parameter arrayParameter = name2parameter
138: .get(arrayParameterName);
139: final int count = arrayParameter.getCount(component);
140: for (int index = 0; index < count; index++) {
141: if (!arrayParameter.isRequiredToBeSet(component, index))
142: continue;
143: section.getWriter().write(
144: CodeReferencePresenter
145: .generateDirectAccessCode(component));
146: section.getWriter().write(instanceNameSuffix);
147: section.getWriter().write(" ("); // NOI18N
148: for (int paramIndex = 0; paramIndex < parameterNames
149: .size(); paramIndex++) {
150: if (paramIndex > 0)
151: section.getWriter().write(", "); // NOI18N
152: String parameterName = parameterNames
153: .get(paramIndex);
154: Parameter parameter = name2parameter
155: .get(parameterName);
156: parameter.generateParameterCode(component, section,
157: index);
158: }
159: section.getWriter().write(");\n"); // NOI18N
160: }
161: } else {
162: section.getWriter().write(
163: CodeReferencePresenter
164: .generateDirectAccessCode(component));
165: section.getWriter().write(instanceNameSuffix);
166: section.getWriter().write(" ("); // NOI18N
167: for (int paramIndex = 0; paramIndex < parameterNames.size(); paramIndex++) {
168: if (paramIndex > 0)
169: section.getWriter().write(", "); // NOI18N
170: String parameterName = parameterNames.get(paramIndex);
171: Parameter parameter = name2parameter.get(parameterName);
172: parameter.generateParameterCode(component, section, -1);
173: }
174: section.getWriter().write(");\n"); // NOI18N
175: }
176:
177: if (!exceptions.isEmpty()) {
178: for (String exception : exceptions) {
179: section.getWriter().write(
180: "} catch (" + exception + " e) {\n").commit(); // NOI18N
181: section.switchToEditable(component.getComponentID()
182: + "-@" + exception); // NOI18N
183: section.getWriter().write("e.printStackTrace ();\n")
184: .commit(); // NOI18N
185: section.switchToGuarded();
186: }
187: section.getWriter().write("}\n"); // NOI18N
188: }
189: }
190:
191: public List<String> getParameters() {
192: return parameterNames;
193: }
194:
195: public List<String> getExceptions() {
196: return exceptions;
197: }
198:
199: }
|