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.integration.ide.UMLSupport;
047: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IAttribute;
048: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
049: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.INavigableEnd;
050: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IStructuralFeature;
051: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
052: import org.netbeans.modules.uml.core.roundtripframework.IAttributeChangeFacility;
053: import org.netbeans.modules.uml.core.support.umlsupport.Log;
054:
055: /**
056: * The MemberTransaction is use to maintain a context when updating data
057: * members. The MemberTransaction will locate the data member in Describes
058: * database. When locating the data member the class symbol specified by
059: * the SymbolTransaction will be searched for any attributes that match
060: * the requested attribute.
061: *
062: * Revision History
063: * No. Date Who What
064: * --- ---- --- ----
065: * 1 2002-06-19 Darshan Fixed fullScopeName not using the fully scoped
066: * name in setAttribute().
067: * 2 2002-06-21 Darshan Added support for IStructuralFeature instead of
068: * IAttribute and extended createAttribute to
069: * create a navigable association where required.
070: */
071: public class MemberTransaction {
072: /**
073: * The attribute that this context is managing. Note that this will be an
074: * instance of INavigableEnd or IAttribute and can be downcast without
075: * creating a proxy.
076: */
077: private IStructuralFeature mAttribute = null;
078:
079: /**
080: * If this attribute is an implmentation attribute the mMemberRel will contain
081: * the relationship that contains teh attribute.
082: */
083: // private IGDRelation mMemberRel = null;
084: /** The system that is to be updated. */
085: private IProject mProj = null;
086:
087: /** Flag to indicate if this is an implementation attribute. */
088: private boolean mIsImplAttr = false;
089:
090: private IClassifier mSym = null;
091:
092: /**
093: * Creates new MemberTransaction and specify which system to search for
094: * the attribute.
095: */
096: public MemberTransaction(IProject proj) {
097: setAttribute(null);
098: setProject(proj);
099: }
100:
101: /**
102: * Create a new MemberTransaction and specify the symbol to search and
103: * data member to find. If the member will be created if one is needed.
104: * @param trans The symbol transaction used when searching for the memeber.
105: * @param member The information required to locate the data member.
106: */
107: public MemberTransaction(SymbolTransaction trans,
108: final MemberInfo member) {
109: this (UMLSupport.getCurrentProject());
110: if (member != null) {
111: if (trans.getSymbol() != null)
112: setSymbol(trans.getSymbol());
113: setAttribute(trans, member);
114: }
115: }
116:
117: /**
118: * Sets the Describe system to use when retrieving the symbols information. Therefore,
119: * the symbol will also reside in the Describe system.
120: * @param value The Describe system.
121: */
122: protected void setProject(IProject proj) {
123: mProj = proj;
124: }
125:
126: /**
127: * Retrieves the Describe system to use when retrieving the symbols information. Therefore,
128: * the symbol will also reside in the Describe system.
129: * @param value The Describe system.
130: */
131: public IProject getProject() {
132: return mProj;
133: }
134:
135: /**
136: * Retrieve the Describe representation of the data member.
137: * <i>In the future this may be abstracted. </i>
138: */
139: public IStructuralFeature getAttribute() {
140: return mAttribute;
141: }
142:
143: /**
144: * Set the Describe representation of the data member.
145: * <i>In the future this may be abstracted. </i>
146: */
147: public void setAttribute(IStructuralFeature attr) {
148: mAttribute = attr;
149: }
150:
151: /**
152: * Checks if the data memeber is an implementation attribute.
153: * @return true if the an implementation attribute, false otherwise.
154: */
155: public boolean isImplAttribute() {
156: return mIsImplAttr;
157: }
158:
159: /**
160: * Sets if the data memeber is an implementation attribute.
161: * @param value true if the an implementation attribute, false otherwise.
162: */
163: public void setIsImplAttribute(boolean value) {
164: mIsImplAttr = value;
165: }
166:
167: /**
168: * Sets the Describe attribute that defines data member. A Describe symbol
169: * is search for a data member that matches the requested data member name. If
170: * a Describe attribute is not found then one is created for the data member.
171: * @param trans The symbol transaction used to locate the data memeber.
172: * @param member The information needed to locate the data memeber.
173: */
174: public void setAttribute(SymbolTransaction trans,
175: final MemberInfo member) {
176: // First set the symbol to null to allow the current symbol to be GC
177: setAttribute(null);
178: IClassifier sym = trans.getSymbol();
179:
180: if (sym != null) {
181: String fullScopeName = member.getQualifiedType();
182: String sourceName = member.getType();
183: Log.out("the fullScopeName = " + fullScopeName);
184:
185: // if(member.getChangeType() != ElementInfo.CREATE)
186: mAttribute = JavaClassUtils.findAttribute(sym, member
187: .getName());
188:
189: if (mAttribute == null
190: && member.getChangeType() == ElementInfo.CREATE)
191: mAttribute = createAttribute(sym, member.getName(),
192: fullScopeName, sourceName, member
193: .getmodifierMask());
194: }
195: }
196:
197: public void setSymbol(IClassifier clazz) {
198: mSym = clazz;
199: }
200:
201: public IClassifier getSymbol() {
202: return mSym;
203: }
204:
205: /**
206: * Create a new Describe attribute to represent the data member. This is helper
207: * routine.
208: * @param sym The symbol that will contain the attribute.
209: * @param name The name of the attribute.
210: * @param The type of the data member.
211: */
212: protected IStructuralFeature createAttribute(IClassifier sym,
213: String name, String fullName, String sourceName,
214: int modifierMask) {
215: IAttribute retVal = null;
216: EventManager.getEventManager().getEventFilter().blockEventType(
217: ChangeUtils.RDT_DEPENDENCY_ADDED);
218: try {
219: int mul = MemberInfo.getMultiplicity(sourceName);
220: sourceName = MemberInfo.getTypeName(sourceName);
221: fullName = MemberInfo.getTypeName(fullName);
222:
223: if (JavaClassUtils.findAttribute(sym, name) == null) {
224: IAttributeChangeFacility facility = EventManager
225: .getAttributeFacility();
226: if (facility != null) {
227: Log.out("createAttribute: Creating attribute : "
228: + name + ": " + sourceName + " : "
229: + fullName);
230: //retVal = facility.createAttribute(name, sourceName, sym);
231: String umlFullName = JavaClassUtils
232: .convertJavaToUML(fullName);
233: retVal = facility.addAttribute3(name, umlFullName,
234: sym, true, false, modifierMask);
235: if (retVal != null)
236: MemberInfo.setMultiplicity(retVal, mul, 0);
237: else
238: Log
239: .err("createAttribute(): Couldn't create attribute");
240:
241: if (retVal != null
242: && retVal instanceof INavigableEnd) {
243: IDEProcessor.addRelationshipLinkToDiagram(
244: retVal, null);
245: }
246: } else {
247: Log
248: .err("createAttribute(): IAttributeChangeFacility is null");
249: }
250: // IClassifier clazz = JavaClassUtils.findClassSymbol(fullName);
251: //
252: // if (clazz == null && !JavaClassUtils.isPrimitive(fullName))
253: // clazz = JavaClassUtils.createDataType(fullName);
254: //
255: // if (clazz == null) {
256: // retVal = sym.createAttribute(sourceName, name);
257: // sym.addAttribute(retVal);
258: // sym.addOwnedElement(retVal);
259: // Log.out("Setting multiplicity for '" + name
260: // + "' to " + mul);
261: // MemberInfo.setMultiplicity(retVal, mul, 0);
262: // }
263: // else {
264: // IStructuralFeature feat = IDEProcessor.
265: // makeNavigableAssociation(sym, clazz, name);
266: // MemberInfo.setMultiplicity(feat, mul, 0);
267: // return feat;
268: // }
269: Log
270: .out("Successfully added the attrib ....... "
271: + name);
272: } else {
273: Log.out("Attrib with same name already exists .....");
274: }
275: } catch (Exception e) {
276: Log.stackTrace(e);
277: } finally {
278: EventManager.getEventManager().getEventFilter()
279: .unblockEventType(ChangeUtils.RDT_DEPENDENCY_ADDED);
280: }
281: return retVal;
282: }
283:
284: /**
285: * Checks if the type is a Java primitive.
286: * @return true if the type is a Java primitive.
287: */
288: protected boolean isPrimative(String type) {
289: boolean retVal = false;
290:
291: if ((type.equals("int") == true)
292: || (type.equals("short") == true)
293: || (type.equals("char") == true)
294: || (type.equals("boolean") == true)
295: || (type.equals("double") == true)
296: || (type.equals("float") == true)
297: || (type.equals("long") == true)
298: || (type.equals("byte") == true)) {
299: retVal = true;
300: }
301:
302: return retVal;
303: }
304: }
|