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 java.awt.Frame;
045: import java.util.Iterator;
046:
047: import org.netbeans.modules.uml.core.support.umlutils.ETList;
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
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.support.commondialogs.INavigationDialog;
052: import org.netbeans.modules.uml.ui.support.diagramsupport.IPresentationTarget;
053:
054: /**
055: *
056: * @author Trey Spiva
057: */
058: public class DiagramAndPresentationNavigator implements
059: IDiagramAndPresentationNavigator {
060: private boolean m_ForceDialogDisplay = false;
061:
062: public boolean navigateToPresentationTarget(
063: IElement parentModelElement,
064: ETList<IPresentationTarget> possibleTargets, boolean isShift) {
065: boolean retVal = false;
066:
067: IProject project = null;
068: if (parentModelElement instanceof IProject) {
069: project = (IProject) parentModelElement;
070: }
071:
072: boolean didNavigate = navigateToAssociated(parentModelElement,
073: isShift, project);
074:
075: if (didNavigate == false) {
076: DiagramBuilder diagramBuilder = new DiagramBuilder();
077:
078: ETList<IProxyDiagram> diagrams = null;
079: int numDiagrams = 0;
080: // get the number of scoped diagrams that this element has
081: diagrams = diagramBuilder
082: .getScopedDiagrams(parentModelElement);
083: if (diagrams != null) {
084: numDiagrams = diagrams.size();
085: }
086:
087: ETList<IProxyDiagram> assocDiagrams = null;
088: int numAssocDiagrams = 0;
089: // get the number of associated diagrams this element has
090: assocDiagrams = diagramBuilder
091: .getAssociatedDiagrams(parentModelElement);
092: if (assocDiagrams != null) {
093: numAssocDiagrams = assocDiagrams.size();
094: }
095:
096: // now get the number of presentation elements this element has, unless we have a project
097: if (project == null) {
098: ETList<IPresentationTarget> targets = possibleTargets;
099: if (possibleTargets == null) {
100: targets = diagramBuilder
101: .getPresentationTargets(parentModelElement);
102: }
103:
104: int numPEs = 0;
105: if (targets != null) {
106: numPEs = targets.size();
107: }
108:
109: ETList<IElement> pElements = diagramBuilder
110: .getAssociatedElements(parentModelElement);
111: int numAssocElements = 0;
112: if (pElements != null) {
113: numAssocElements = pElements.size();
114: }
115:
116: int numTargets = numDiagrams + numAssocDiagrams
117: + numPEs + numAssocElements;
118:
119: if ((numTargets == 1) && (isShift == false)) {
120: if (numDiagrams > 0) {
121: diagramBuilder.navigateToFirstDiagram(diagrams);
122: } else if (numPEs > 0) {
123: diagramBuilder.navigateToFirstTarget(targets);
124: } else if (numAssocDiagrams > 0) {
125: diagramBuilder
126: .navigateToFirstDiagram(assocDiagrams);
127: } else if (numAssocElements > 0) {
128: IElement pElem = pElements.get(0);
129: diagramBuilder.navigateToElementInTree(pElem);
130: }
131: } else if (numTargets > 1) {
132: // Show the dialog.
133: INavigationDialog dialog = UIFactory
134: .createNavigationDialog();
135: dialog.display(parentModelElement, diagrams,
136: targets, assocDiagrams, pElements);
137: }
138: } else {
139: // we have a project
140: if (numDiagrams == 1 && !isShift
141: && !m_ForceDialogDisplay) {
142: diagramBuilder.navigateToFirstDiagram(diagrams);
143: } else if (numDiagrams != 0) {
144: // Show the dialog.
145: INavigationDialog dialog = UIFactory
146: .createNavigationDialog();
147: dialog.display(parentModelElement, diagrams, null,
148: assocDiagrams, null);
149: }
150: }
151: }
152:
153: return retVal;
154: }
155:
156: /* (non-Javadoc)
157: * @see org.netbeans.modules.uml.ui.support.IDiagramAndPresentationNavigator#navigateToPresentationTarget(int, com.embarcadero.describe.foundation.IElement, com.embarcadero.describe.gui.support.diagramsupport.IPresentationTargets)
158: */
159: public boolean navigateToPresentationTarget(int pParent,
160: IElement pParentModelElement,
161: ETList<IPresentationTarget> pPossibleTargets) {
162: return navigateToPresentationTarget(pParentModelElement,
163: pPossibleTargets, false);
164: }
165:
166: /* (non-Javadoc)
167: * @see org.netbeans.modules.uml.ui.support.IDiagramAndPresentationNavigator#handleNavigation(int, com.embarcadero.describe.foundation.IElement, boolean)
168: */
169: public boolean handleNavigation(int pParent, IElement pElement,
170: boolean isShift) {
171: return navigateToPresentationTarget(pElement, null, isShift);
172: }
173:
174: /**
175: * Bring up the project diagram dialog which shows all the closed diagrams in the project.
176: *
177: * @param pParent[in]
178: * @param pCurrentProject[in]
179: */
180: public void showScopedDiagrams(Frame pParent,
181: IProject pCurrentProject) {
182: m_ForceDialogDisplay = true;
183: navigateToPresentationTarget(pCurrentProject, null, false);
184: m_ForceDialogDisplay = false;
185: }
186:
187: /**
188: * Navigates to the argument target. If the diagram is already up
189: * the it makes sure to raise the window.
190: *
191: * @param pParent[in]
192: * @param pCurrentProject[in]
193: */
194: public void doNavigate(IPresentationTarget pTarget) {
195: DiagramBuilder builder = new DiagramBuilder();
196: builder.navigateToTarget(pTarget);
197: }
198:
199: //**************************************************
200: // Helper Methods
201: //**************************************************
202: protected boolean navigateToAssociated(IElement parentModelElement,
203: boolean isShift, IProject project) {
204: boolean retVal = false;
205:
206: boolean onlyShowWhenShiftIsDown = false;
207: boolean hasDefault = false;
208: String xmid = "";
209: UserSettings settings = new UserSettings();
210:
211: if (project == null) {
212: onlyShowWhenShiftIsDown = settings
213: .isOnlyShowNavigateWhenShift(parentModelElement);
214: hasDefault = settings
215: .hasDefaultNavigationTarget(parentModelElement);
216: xmid = settings
217: .getDefaultNavigationTargetXMID(parentModelElement);
218: }
219:
220: if ((hasDefault == true) && (onlyShowWhenShiftIsDown == true)
221: && (isShift == false)) {
222: DiagramBuilder builder = new DiagramBuilder();
223:
224: // Navigate to the default target
225: // are we navigating to a diagram
226: if (settings
227: .isDefaultNavigationTargetADiagram(parentModelElement) == true) {
228: // now we find out if the passed in diagram id matches an id in the scoped
229: // diagrams of the element
230: ETList<IProxyDiagram> diags = builder
231: .getScopedDiagrams(parentModelElement);
232: if (diags != null) {
233: IProxyDiagram proxyDia = getProxyDiagramWithID(
234: diags, xmid);
235: if (proxyDia != null) {
236: String filename = proxyDia.getFilename();
237: if (filename != null && filename.length() > 0) {
238: builder.navigateToDiagram(filename, "", "",
239: "");
240: retVal = true;
241: }
242: }
243: }
244:
245: // must not have been a scoped diagram, so the other would be an associated
246: // diagram
247: if (!retVal) {
248: ETList<IProxyDiagram> pDiagrams = builder
249: .getAssociatedDiagrams(parentModelElement);
250: if (pDiagrams != null) {
251: IProxyDiagram proxyDia = getProxyDiagramWithID(
252: pDiagrams, xmid);
253: if (proxyDia != null) {
254: String filename = proxyDia.getFilename();
255: if (filename != null
256: && filename.length() > 0) {
257: builder.navigateToDiagram(filename, "",
258: "", "");
259: retVal = true;
260: }
261: }
262: }
263: }
264: } else {
265: // the id that we are looking for is not a diagram, so is it among
266: // the presentation elements for this element
267: ETList<IPresentationTarget> targets = builder
268: .getPresentationTargets(parentModelElement);
269: if (targets != null) {
270: IPresentationTarget this Target = getTargetWithID(
271: targets, xmid);
272: //IPresentationTarget thisTarget = targets.get(xmid);
273: if (this Target != null) {
274: builder.navigateToTarget(this Target);
275: retVal = true;
276: }
277: }
278:
279: // not a presentation element either, so one final check to see if it
280: // is an associated element
281: if (!retVal) {
282: ETList<IElement> pElements = builder
283: .getAssociatedElements(parentModelElement);
284: if (pElements != null) {
285: int count = pElements.size();
286: IElement pElem = null;
287: for (int i = 0; i < count; i++) {
288: IElement tempEle = pElements.get(i);
289: String id = tempEle.getXMIID();
290: if (id != null && id.equals(xmid)) {
291: pElem = tempEle;
292: break;
293: }
294: }
295: if (pElem != null) {
296: builder.navigateToElementInTree(pElem);
297: retVal = true;
298: }
299: }
300: }
301: }
302:
303: // If we navigated ok then don't bring up the dialog, else clear out
304: // the registry and bring up the dialog
305: if (retVal == false) {
306: settings.clearDefaultTarget(parentModelElement);
307: }
308: }
309: return retVal;
310: }
311:
312: protected IPresentationTarget getTargetWithID(
313: ETList<IPresentationTarget> targets, String id) {
314: IPresentationTarget retVal = null;
315: for (Iterator<IPresentationTarget> iter = targets.iterator(); (iter
316: .hasNext() == true)
317: && (retVal == null);) {
318: IPresentationTarget element = iter.next();
319: if (id.equals(element.getPresentationID())) {
320: retVal = element;
321: break;
322: }
323: }
324: return retVal;
325: }
326:
327: protected IProxyDiagram getProxyDiagramWithID(
328: ETList<IProxyDiagram> targets, String id) {
329: IProxyDiagram retVal = null;
330: for (Iterator<IProxyDiagram> iter = targets.iterator(); (iter
331: .hasNext() == true)
332: && (retVal == null);) {
333: IProxyDiagram element = iter.next();
334: if (id != null && id.equals(element.getXMIID())) {
335: retVal = element;
336: break;
337: }
338: }
339: return retVal;
340: }
341:
342: }
|