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.ui.support;
043:
044: import org.netbeans.modules.uml.common.generics.ETPairT;
045: import java.util.Iterator;
046:
047: import org.netbeans.modules.uml.common.generics.IteratorT;
048: import org.netbeans.modules.uml.core.eventframework.EventDispatchNameKeeper;
049: import org.netbeans.modules.uml.core.eventframework.EventState;
050: import org.netbeans.modules.uml.core.eventframework.IEventContext;
051: import org.netbeans.modules.uml.core.eventframework.IEventDispatcher;
052: import org.netbeans.modules.uml.core.metamodel.core.foundation.EventContextManager;
053: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
054: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
055: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationReference;
056: import org.netbeans.modules.uml.core.metamodel.core.foundation.IReference;
057: import org.netbeans.modules.uml.core.metamodel.infrastructure.IRelationFactory;
058: import org.netbeans.modules.uml.core.metamodel.infrastructure.RelationFactory;
059: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
060: import org.netbeans.modules.uml.core.support.umlutils.ETList;
061: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
062:
063: /**
064: * @author sumitabhk
065: *
066: */
067: public class PresentationReferenceHelper {
068:
069: /**
070: *
071: */
072: public PresentationReferenceHelper() {
073: super ();
074: }
075:
076: /**
077: * Creates an IPresentationReference relationship
078: *
079: * @param pReferencing [in] The referencing PE
080: * @param pReferred [in] The referred PE
081: * @param pCreatedRelationship [out,retval] The created relationship
082: */
083: public static IPresentationReference createPresentationReference(
084: IPresentationElement pReferencing,
085: IPresentationElement pReferred) {
086: if (pReferencing == null || pReferred == null)
087: return null;
088:
089: IPresentationReference pCreatedRelationship = null;
090:
091: try {
092:
093: IRelationFactory pFactory = new RelationFactory();
094: // This is needed in order to NOT cause version control dialogs
095: // to appear when reestablishing these relationships on a diagram
096: // reopen. This was happening when opening a Component diagram that was
097: // checked in.
098:
099: EventContextManager manager = new EventContextManager();
100: ETPairT<IEventContext, IEventDispatcher> contextInfo = manager
101: .getNoEffectContext(pReferencing,
102: EventDispatchNameKeeper.modifiedName(),
103: "PresentationReferenceAdded");
104:
105: IEventDispatcher disp = contextInfo.getParamTwo();
106: IEventContext presContext = contextInfo.getParamOne();
107:
108: ETPairT<IEventContext, IEventDispatcher> contextInfo2 = manager
109: .getNoEffectContext(pReferred,
110: EventDispatchNameKeeper.modifiedName(),
111: "PresentationReferenceAdded");
112:
113: IEventDispatcher disp2 = contextInfo2.getParamTwo();
114: IEventContext referredContext = contextInfo2.getParamOne();
115:
116: EventState state = new EventState(disp, presContext);
117: EventState state2 = new EventState(disp2, referredContext);
118:
119: try {
120: // Create a IPresentationReference relationship with pReferencing
121: // being the owner of the relationship.
122: pCreatedRelationship = pFactory
123: .createPresentationReference(pReferencing,
124: pReferred);
125: } finally {
126: state.existState();
127: state2.existState();
128: }
129: } catch (Exception e) {
130: e.printStackTrace();
131: }
132: return pCreatedRelationship;
133: }
134:
135: /**
136: * Returns all the IPresentationElements that are referred to by the presentation element pReferencing.
137: *
138: * @param pReferencing [in] The referencing PE
139: * @param pReferredElements [out,retval] All the referred elements
140: */
141: public static ETList<IPresentationElement> getAllReferredElements(
142: IPresentationElement pReferencing) {
143: ETList<IPresentationElement> retObj = new ETArrayList<IPresentationElement>();
144: if (pReferencing != null) {
145: ETList<IElement> pElements = pReferencing.getElements();
146: if (pElements != null) {
147: int count = pElements.size();
148:
149: // Gather up all the IPresentationReferences and get the ReferredElement which
150: // is the presentation element for an IPresentationReference
151: for (int i = 0; i < count; i++) {
152: IElement elem = pElements.get(i);
153: if (elem instanceof IPresentationReference) {
154: IPresentationElement pEle = ((IPresentationReference) elem)
155: .getPresentationElement();
156: if (pEle != null) {
157: retObj.add(pEle);
158: }
159: }
160: }
161: }
162: }
163:
164: return retObj;
165: }
166:
167: /**
168: * Returns true if there are referred elements. If not then pReferredElements is NULL.
169: *
170: * @param pReferencing [in] The referencing PE
171: * @param pReferredElements [out,retval] All the referred elements
172: * @return true if there are referred elements.
173: */
174: public static ETList<IPresentationElement> getHasReferredElements(
175: IPresentationElement pReferencing) {
176: ETList<IPresentationElement> retObj = new ETArrayList<IPresentationElement>();
177: if (pReferencing != null) {
178: ETList<IElement> pElements = pReferencing.getElements();
179: if (pElements != null) {
180: int count = pElements.size();
181: for (int i = 0; i < count; i++) {
182: IElement elem = pElements.get(i);
183: if (elem instanceof IPresentationReference) {
184: IPresentationElement pEle = ((IPresentationReference) elem)
185: .getPresentationElement();
186: if (pEle != null) {
187: retObj.add(pEle);
188: }
189: }
190: }
191: }
192: }
193: return retObj;
194: }
195:
196: /**
197: * Returns all the IPresentationElements that are referred to by the presentation element pReferencing.
198: *
199: * @param pReferencing [in] The referencing PE
200: * @param pElements [out,retval] All the referrenced presentation elements' subjects
201: */
202: public static ETList<IElement> getAllReferredSubjects(
203: IPresentationElement pReferencing) {
204: if (pReferencing == null)
205: return null;
206:
207: try {
208: ETList<IElement> pFoundElements = new ETArrayList<IElement>();
209:
210: // Get all the elements off the referencing element
211: ETList<IElement> pReferredElements = pReferencing
212: .getElements();
213:
214: if (pFoundElements != null && pReferredElements != null) {
215: // Gather up all the IPresentationReferences and get the ReferredElement which
216: // is the presentation element for an IPresentationReference
217: Iterator<IElement> iter = pReferredElements.iterator();
218: while (iter.hasNext()) {
219: IElement pThisElement = iter.next();
220: // Get all the relationships
221: IPresentationReference pThisReference = pThisElement instanceof IPresentationReference ? (IPresentationReference) pThisElement
222: : null;
223:
224: if (pThisReference != null) {
225: IPresentationElement pPE = pThisReference
226: .getPresentationElement();
227: if (pPE != null) {
228: IElement pThisPEsElement = pPE
229: .getFirstSubject();
230: if (pThisPEsElement != null)
231: pFoundElements.add(pThisPEsElement);
232: }
233: }
234: }
235: return pFoundElements.size() > 0 ? pFoundElements
236: : null;
237: }
238: } catch (Exception e) {
239: e.printStackTrace();
240: }
241: return null;
242: }
243:
244: /**
245: * Returns all the IPresentationElements that are referring pReferences (ie the parent or owner objects of the relationship)
246: */
247: public static ETList<IPresentationElement> getAllReferencingElements(
248: IPresentationElement pReferencedElement) {
249: if (pReferencedElement == null)
250: return null;
251:
252: try {
253: ETList<IPresentationElement> pFoundPEs = new ETArrayList<IPresentationElement>();
254: if (pFoundPEs != null) {
255: ETList<IReference> pReferences = pReferencedElement
256: .getReferredReferences();
257: IteratorT<IReference> iter = new IteratorT<IReference>(
258: pReferences);
259: while (iter.hasNext()) {
260: IReference pReference = iter.next();
261: if (pReference != null) {
262: IElement pTempElement = pReference
263: .getReferencingElement();
264: IPresentationElement pTempPE = pTempElement instanceof IPresentationElement ? (IPresentationElement) pTempElement
265: : null;
266: if (pTempPE != null) {
267: pFoundPEs.add(pTempPE);
268: }
269: }
270: }
271: return pFoundPEs.size() > 0 ? pFoundPEs : null;
272: }
273: } catch (Exception e) {
274: e.printStackTrace();
275: }
276: return null;
277: }
278:
279: /**
280: * Removes all presentation references
281: *
282: * @param pReferred [in] The child whose presentation references should be removed
283: */
284: public static void removeAllPresentationReferences(
285: IPresentationElement pReferred) {
286: if (pReferred != null) {
287: ETList<IReference> pReferences = pReferred
288: .getReferredReferences();
289: ETList<IPresentationReference> allReferences = null;
290: if (pReferences != null) {
291: int count = pReferences.size();
292: // Gather up all the IPresentationReferences and whack em all
293: for (int i = 0; i < count; i++) {
294: IReference ref = pReferences.get(i);
295: if (ref instanceof IPresentationReference) {
296: if (allReferences == null) {
297: allReferences = new ETArrayList<IPresentationReference>();
298: }
299: allReferences.add((IPresentationReference) ref);
300: }
301: }
302: }
303:
304: // Now do the deletes
305: if (allReferences != null) {
306: int count = allReferences.size();
307: for (int i = 0; i < count; i++) {
308: IPresentationReference ref = allReferences.get(i);
309: ref.delete();
310: }
311: }
312: }
313: }
314:
315: }
|