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-2007 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:
042: package org.netbeans.modules.uml.integration.ide.events;
043:
044: import org.netbeans.modules.uml.integration.ide.ChangeUtils;
045: import org.netbeans.modules.uml.integration.ide.JavaClassUtils;
046: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
047: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IOperation;
048: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IParameter;
049: import org.netbeans.modules.uml.core.support.umlsupport.Log;
050:
051: /**
052: * The MethodTransaction is use to maintain a context when updating methods.
053: * The MethodTransaction will locate the method in Describes database.
054: * When locating the method the class symbol specified by the SymbolTransaction
055: * will be searched for any attributes that match the requested attribute.
056: */
057: public class MethodTransaction extends Object {
058: /** The attribute that this context is managing. */
059: IOperation mOpe = null;
060:
061: /** Creates new MethodTransaction */
062: public MethodTransaction() {
063: setOperation(null);
064: }
065:
066: /**
067: * Create a new MemberTransaction and specify the symbol to search and
068: * method to find. If the method will be created if one is needed.
069: * @param trans The symbol transaction used when searching for the memeber.
070: * @param member The information required to locate the method.
071: */
072: public MethodTransaction(SymbolTransaction trans,
073: final ConstructorInfo method) {
074: if (method != null) {
075: setAttribute(trans, method);
076: }
077: }
078:
079: /**
080: * Retrieve the Describe representation of the method.
081: * <i>In the future this may be abstracted. </i>
082: */
083: public IOperation getOperation() {
084: return mOpe;
085: }
086:
087: /**
088: * Set the Describe representation of the method
089: * <i>In the future this may be abstracted. </i>
090: */
091: public void setOperation(IOperation attr) {
092: mOpe = attr;
093: }
094:
095: public void setAttribute(SymbolTransaction trans,
096: final ConstructorInfo method) {
097: try {
098: doSetAttribute(trans, method);
099: } catch (Exception ex) {
100: Log.stackTrace(ex);
101: }
102:
103: }
104:
105: public void doSetAttribute(SymbolTransaction trans,
106: final ConstructorInfo method) {
107: Log.out("Inside setAttribute() of MethodTransaction ........");
108: // First set the symbol to null to allow the current symbol to be GC
109: mOpe = null;
110: IClassifier sym = trans.getSymbol();
111: if (sym != null) {
112: mOpe = JavaClassUtils.findOperation(sym, method.getName(),
113: method.getParameters());
114: if (mOpe == null) {
115: Log.out("Unable to find the method ........"
116: + method.getName());
117: }
118:
119: if ((mOpe == null)
120: && (method.getChangeType() == ElementInfo.CREATE)) {
121: // **## Log.out("Creating a new Attribute...");
122: mOpe = createOperation(sym, method.getName(), method
123: .getParameters());
124:
125: // null the parameters to prevent them getting recreated.
126: //method.setParameters(null);
127: Log
128: .out("Successfully created the new operation ........");
129: }
130: } else
131: Log.out("setAttribute(): Classifier is null for - "
132: + method);
133: }
134:
135: /**
136: * Sets the Describe attribute that defines method. A Describe symbol
137: * is search for a method that matches the requested data member name and
138: * parameters. If a Describe attribute is not found then one is created for
139: * the method.
140: * @param trans The symbol transaction used to locate the method.
141: * @param member The information needed to locate the method.
142: * @param params The parameter information.
143: */
144: protected IOperation createOperation(IClassifier sym, String name,
145: MethodParameterInfo[] params) {
146: IOperation op = null;
147: EventManager.getEventManager().getEventFilter().blockEventType(
148: ChangeUtils.RDT_DEPENDENCY_ADDED);
149: try {
150: if (name.equals(sym.getName())
151: && (params == null || params.length == 0)) { // method is a empty constructor
152: op = sym.createConstructor();
153: } else {
154: if (name.equals(sym.getName()))
155: op = sym.createConstructor();
156: else
157: op = sym.createOperation("void", name);
158: for (int i = 0; i < params.length; ++i) {
159: String partype = params[i].getType();
160: int mul = MemberInfo.getMultiplicity(partype);
161: partype = MemberInfo.getTypeName(partype);
162:
163: Log.out("MethodTransaction.createOperation: "
164: + "Creating parameter of type: "
165: + JavaClassUtils.convertJavaToUML(partype));
166: IParameter param = op.createParameter(
167: JavaClassUtils.convertJavaToUML(partype),
168: params[i].getName());
169: if (mul > 0 && param != null)
170: MemberInfo.setMultiplicity(param, mul, 0);
171: op.addParameter(param);
172: }
173: }
174: } catch (Exception E) {
175: Log.stackTrace(E);
176: } finally {
177: EventManager.getEventManager().getEventFilter()
178: .unblockEventType(ChangeUtils.RDT_DEPENDENCY_ADDED);
179: }
180: return op;
181: }
182:
183: }
|