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.core.metamodel.core.foundation.IElement;
045: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamespace;
046: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
047: import org.netbeans.modules.uml.core.metamodel.core.foundation.IReference;
048: import org.netbeans.modules.uml.core.metamodel.diagrams.IDiagram;
049: import org.netbeans.modules.uml.core.metamodel.diagrams.IProxyDiagram;
050: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
051: import org.netbeans.modules.uml.ui.controls.drawingarea.IUIDiagram;
052: import org.netbeans.modules.uml.ui.controls.projecttree.IProjectTreeControl;
053: import org.netbeans.modules.uml.ui.support.applicationmanager.IProduct;
054: import org.netbeans.modules.uml.ui.support.applicationmanager.IProductDiagramManager;
055: import org.netbeans.modules.uml.ui.support.diagramsupport.IProxyDiagramManager;
056: import org.netbeans.modules.uml.ui.support.diagramsupport.IPresentationTarget;
057: import org.netbeans.modules.uml.ui.support.diagramsupport.PresentationFinder;
058: import org.netbeans.modules.uml.ui.support.diagramsupport.ProxyDiagramManager;
059: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
060: import org.netbeans.modules.uml.core.support.umlutils.ETList;
061: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
062: import java.util.Arrays;
063:
064: /**
065: * Theres lots of common code among the various dialogs. This class helps the
066: * dialogs build and maintain their various lists. It also can perform some
067: * various actions.
068: *
069: * @author Trey Spiva
070: */
071: public class DiagramBuilder {
072: /**
073: * Retrieve the presentation targets for the element.
074: *
075: * @param element The element to display.
076: * @return The presentation targets.
077: */
078: public ETList<IPresentationTarget> getPresentationTargets(
079: IElement element) {
080: ETList<IPresentationTarget> retVal = new ETArrayList<IPresentationTarget>();
081:
082: PresentationFinder finder = new PresentationFinder();
083:
084: // Projects do not have presentation element. So, make sure that we
085: // do not have a project.
086: if (!(element instanceof IProject)) {
087: retVal = finder.getPresentationTargets(element);
088: }
089: return retVal;
090: }
091:
092: /**
093: * Navigates to the presentation element.
094: *
095: * @param thisTarget The presentation element to navigate to.
096: */
097: public void navigateToTarget(IPresentationTarget target) {
098: IProductDiagramManager manager = ProductHelper
099: .getProductDiagramManager();
100: if (manager != null && target != null) {
101: IDiagram diagram = target.getOpenDiagram();
102: if (diagram != null) {
103: manager.raiseWindow(diagram);
104: } else {
105: diagram = manager.openDiagram(target
106: .getDiagramFilename(), true, null);
107: }
108:
109: if (diagram != null) {
110: String presentationId = target.getPresentationID();
111: String toplevelId = target.getTopLevelID();
112: String meid = target.getModelElementID();
113: if (presentationId != null
114: && presentationId.length() > 0) {
115: diagram.centerPresentationElement2(target
116: .getPresentationID(), true, true);
117: } else if (meid != null && meid.length() > 0
118: && toplevelId != null
119: && toplevelId.length() > 0) {
120: centerOnME(diagram, toplevelId, meid);
121: }
122: }
123: }
124: }
125:
126: /**
127: * Get the scoped diagrams for this element
128: */
129: public ETList<IProxyDiagram> getScopedDiagrams(IElement element) {
130: IProxyDiagramManager manager = ProxyDiagramManager.instance();
131:
132: ETList<IProxyDiagram> diagrams = null;
133: if (element instanceof IProject) {
134: IProject project = (IProject) element;
135: diagrams = manager.getDiagramsInProject(project);
136: } else if (element instanceof INamespace) {
137: INamespace space = (INamespace) element;
138: diagrams = manager.getDiagramsInNamespace(space);
139: }
140:
141: // ETList < IProxyDiagram > retVal = null;
142: // if((diagrams != null) && (diagrams.length > 0))
143: // {
144: // retVal = new ETArrayList < IProxyDiagram >();
145: // retVal.addAll(Arrays.asList(diagrams));
146: // }
147:
148: return diagrams;
149: }
150:
151: /**
152: * Navigate to the first PE
153: */
154: public void navigateToFirstTarget(
155: ETList<IPresentationTarget> targets) {
156: if (targets != null && targets.size() > 0) {
157: IPresentationTarget pTarget = targets.get(0);
158: navigateToTarget(pTarget);
159: }
160: }
161:
162: /**
163: * Navigate to the first diagram
164: */
165: public void navigateToFirstDiagram(ETList<IProxyDiagram> diagrams) {
166: if (diagrams != null && diagrams.size() > 0) {
167: IProxyDiagram pDiagram = diagrams.get(0);
168: IProductDiagramManager pDiaMgr = ProductHelper
169: .getProductDiagramManager();
170: if (pDiaMgr != null) {
171: IDiagram openedDia = pDiaMgr.openDiagram2(pDiagram,
172: true, null);
173: if (openedDia != null) {
174: pDiaMgr.raiseWindow(openedDia);
175: }
176: }
177: }
178: }
179:
180: /**
181: * Navigates to the diagram.
182: *
183: * If a presentation element xmiid is provided then that pe will be selected in the diagram
184: * If a toplevel and modelelement xmiid is provided then the first PE for that ME is selected.
185: *
186: * @param sDefaultDiagramFilename [in] The full filename for the diagram to open
187: * @param sPresentationXMIID [in] Optional presentation element xmiid.
188: * @param sTopLevelXMIID [in] Optional toplevel element xmiid.
189: * @param sModelElementXMIID [in] Optional model element xmiid.
190: */
191: public void navigateToDiagram(String fileName, String presId,
192: String topLevelId, String meid) {
193: if (fileName != null && fileName.length() > 0) {
194: IProductDiagramManager diaMgr = ProductHelper
195: .getProductDiagramManager();
196: if (diaMgr != null) {
197: IDiagram openDia = diaMgr.openDiagram(fileName, true,
198: null);
199: if (openDia != null) {
200: diaMgr.raiseWindow(openDia);
201: if (presId != null && presId.length() > 0) {
202: openDia.centerPresentationElement2(presId,
203: true, true);
204: } else if (topLevelId != null
205: && topLevelId.length() > 0 && meid != null
206: && meid.length() > 0) {
207: centerOnME(openDia, meid, topLevelId);
208: }
209: }
210: }
211: }
212: }
213:
214: /**
215: * Centers on the first PE of a model element
216: */
217: private void centerOnME(IDiagram openDia, String meid,
218: String topLevelId) {
219: if (topLevelId != null && topLevelId.length() > 0
220: && meid != null && meid.length() > 0) {
221: // The presentation target comes from a stub diagram so the presentation elements were
222: // not available - only the model element.
223: IDrawingAreaControl control = null;
224: if (openDia != null && openDia instanceof IUIDiagram) {
225: control = ((IUIDiagram) openDia).getDrawingArea();
226: }
227: if (control != null) {
228: ETList<IPresentationElement> pPEs = control
229: .getAllItems(topLevelId, meid);
230: if (pPEs != null) {
231: int count = pPEs.size();
232: if (count > 0) {
233: IPresentationElement pPE = pPEs.get(0);
234: control.centerPresentationElement(pPE, true,
235: true);
236: }
237: }
238: }
239: }
240: }
241:
242: /**
243: * Navigate to the element in the project tree
244: */
245: public void navigateToElementInTree(IElement pElement) {
246: IProduct prod = ProductHelper.getProduct();
247: if (prod != null) {
248: IProjectTreeControl pTree = prod.getProjectTree();
249: if (pTree != null) {
250: pTree.findAndSelectInTree(pElement);
251: }
252: }
253: }
254:
255: /**
256: * Get the associated elements for this element
257: */
258: public ETList<IElement> getAssociatedElements(IElement pElement) {
259: if (pElement != null) {
260: ETList<IElement> retElems = new ETArrayList<IElement>();
261:
262: ETList<IReference> pReferences = pElement
263: .getReferencingReferences();
264: if (pReferences != null) {
265: int count = pReferences.size();
266: for (int i = 0; i < count; i++) {
267: IReference pRef = pReferences.get(i);
268: IElement pEle = pRef.getReferredElement();
269: if (pEle != null) {
270: retElems.add(pEle);
271: }
272: }
273: }
274: return retElems.size() > 0 ? retElems : null;
275: }
276: return null;
277: }
278:
279: /**
280: * Get the associated diagrams for this element
281: */
282: public ETList<IProxyDiagram> getAssociatedDiagrams(IElement pElement) {
283: ETList<IProxyDiagram> retDias = null;
284: if (pElement != null) {
285: IProxyDiagramManager diaMgr = ProxyDiagramManager
286: .instance();
287: retDias = diaMgr.getAssociatedDiagramsForElement(pElement);
288: }
289: return retDias;
290: }
291:
292: }
|