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: /*
043: * File : TransformPreRequest.java
044: * Created on : Nov 12, 2003
045: * Author : Aztec
046: */
047: package org.netbeans.modules.uml.core.roundtripframework;
048:
049: import org.netbeans.modules.uml.core.eventframework.IEventPayload;
050: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
051: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IAssociationEnd;
052: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
053: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.INavigableEnd;
054:
055: /**
056: * @author Aztec
057: */
058: public class TransformPreRequest extends PreRequest implements
059: ITransformPreRequest {
060: private IClassifier m_Classifier;
061:
062: public TransformPreRequest(IElement preElement, IElement pClone,
063: IElement elementWithArtifact, IRequestProcessor proc,
064: int detail, IEventPayload payload, IElement clonedOwner) {
065: super (preElement, pClone, elementWithArtifact, proc, detail,
066: payload, clonedOwner);
067:
068: INavigableEnd pEnd = (preElement instanceof INavigableEnd) ? (INavigableEnd) preElement
069: : null;
070: if (pEnd != null) {
071: m_Classifier = pEnd.getReferencingClassifier();
072: }
073: }
074:
075: /* (non-Javadoc)
076: * @see org.netbeans.modules.uml.core.roundtripframework.IPreRequest#createChangeRequest(org.netbeans.modules.uml.core.metamodel.core.foundation.IElement, int, int)
077: */
078: public IChangeRequest createChangeRequest(IElement pElement,
079: int type, int detail) {
080: if (pElement == null)
081: return null;
082:
083: IChangeRequest newReq = null;
084:
085: IClassifier pClass = (pElement instanceof IClassifier) ? (IClassifier) pElement
086: : null;
087: IAssociationEnd pEnd = (pElement instanceof IAssociationEnd) ? (IAssociationEnd) pElement
088: : null;
089: if (pEnd != null) {
090: newReq = new AssociationEndTransformChangeRequest();
091: } else if (pClass != null) {
092: newReq = new ClassTransformChangeRequest();
093: } else {
094: newReq = new TransformChangeRequest();
095: }
096: populateChangeRequest(newReq);
097:
098: newReq.setAfter(pElement);
099: newReq.setState(type);
100:
101: // Now allow the PreRequest object make sure this ChangeRequest is absolutely
102: // ready to go...
103:
104: preProcessRequest(newReq);
105:
106: return newReq;
107: }
108:
109: /* (non-Javadoc)
110: * @see org.netbeans.modules.uml.core.roundtripframework.IPreRequest#populateChangeRequest(org.netbeans.modules.uml.core.roundtripframework.IChangeRequest)
111: */
112: public void populateChangeRequest(IChangeRequest req) {
113: super .populateChangeRequest(req);
114: // if this is an assoc end transform, we need to save the classifier
115: IAssociationEndTransformChangeRequest pTransform = (req instanceof IAssociationEndTransformChangeRequest) ? (IAssociationEndTransformChangeRequest) req
116: : null;
117: if (pTransform != null) {
118: pTransform.setOldReferencingClassifier(m_Classifier);
119: }
120: }
121:
122: /* (non-Javadoc)
123: * @see org.netbeans.modules.uml.core.roundtripframework.IPreRequest#preProcessRequest(org.netbeans.modules.uml.core.roundtripframework.IChangeRequest)
124: */
125: public void preProcessRequest(IChangeRequest req) {
126: // Unlike PreRequest, this routine uses the AFTER. For example,
127: // if the after is a class and the before is an actor, this is a
128: // create. However, if the after is a class and the before is an
129: // interface, this is not a create. We might also use this to
130: // generate DELETES (classes to actors, for example).
131:
132: if (req == null)
133: return;
134:
135: IElement preElement = req.getBefore();
136:
137: IElement postElement = req.getAfter();
138:
139: if (preElement != null && postElement != null) {
140: String preType = preElement.getElementType();
141: String postType = postElement.getElementType();
142:
143: boolean preIsClass = false;
144: boolean preIsInterface = false;
145: boolean preIsEnumeration = false;
146: boolean postIsClass = false;
147: boolean postIsInterface = false;
148: boolean postIsEnumeration = false;
149: boolean preIsNavigable = false;
150: boolean postIsNavigable = false;
151:
152: if ("Class".equals(preType)) {
153: preIsClass = true;
154: } else if ("Interface".equals(preType)) {
155: preIsInterface = true;
156: } else if ("Enumeration".equals(preType)) {
157: preIsEnumeration = true;
158: } else if ("NavigableEnd".equals(preType)) {
159: preIsNavigable = true;
160: }
161:
162: if ("Class".equals(postType)) {
163: postIsClass = true;
164: } else if ("Interface".equals(postType)) {
165: postIsInterface = true;
166: } else if ("Enumeration".equals(postType)) {
167: postIsEnumeration = true;
168: } else if ("NavigableEnd".equals(postType)) {
169: postIsNavigable = true;
170: }
171:
172: if ((preIsClass || preIsInterface || preIsEnumeration)
173: && (!postIsClass && !postIsInterface && !postIsEnumeration)) {
174: // This is a class delete
175: req.setState(ChangeKind.CT_DELETE);
176: }
177:
178: if ((!preIsClass && !preIsInterface && !preIsEnumeration)
179: && (postIsClass || postIsInterface || postIsEnumeration)) {
180: // this is a class create
181: req.setState(ChangeKind.CT_CREATE);
182: }
183:
184: if (preIsNavigable && !postIsNavigable) {
185: // This is an attribute delete
186: req.setState(ChangeKind.CT_DELETE);
187: }
188:
189: if (!preIsNavigable && postIsNavigable) {
190: // This is an attribute create
191: req.setState(ChangeKind.CT_CREATE);
192: }
193: }
194: }
195: }
|