Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionStateDiagram |
|
| 3.6;3.6 |
1 | /* $Id: ActionStateDiagram.java 18682 2010-08-24 23:27:58Z bobtarling $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009-2010 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | * mvw | |
12 | ***************************************************************************** | |
13 | * | |
14 | * Some portions of this file was previously release using the BSD License: | |
15 | */ | |
16 | ||
17 | // Copyright (c) 1996-2008 The Regents of the University of California. All | |
18 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
19 | // software and its documentation without fee, and without a written | |
20 | // agreement is hereby granted, provided that the above copyright notice | |
21 | // and this paragraph appear in all copies. This software program and | |
22 | // documentation are copyrighted by The Regents of the University of | |
23 | // California. The software program and documentation are supplied "AS | |
24 | // IS", without any accompanying services from The Regents. The Regents | |
25 | // does not warrant that the operation of the program will be | |
26 | // uninterrupted or error-free. The end-user understands that the program | |
27 | // was developed for research purposes and is advised not to rely | |
28 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
29 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
30 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
31 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
32 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
33 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
34 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
35 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
36 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
37 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
38 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
39 | ||
40 | package org.argouml.uml.ui; | |
41 | ||
42 | import org.argouml.kernel.Project; | |
43 | import org.argouml.kernel.ProjectManager; | |
44 | import org.argouml.model.Model; | |
45 | import org.argouml.ui.targetmanager.TargetManager; | |
46 | import org.argouml.uml.diagram.ArgoDiagram; | |
47 | import org.argouml.uml.diagram.DiagramFactory; | |
48 | import org.argouml.uml.diagram.DiagramSettings; | |
49 | import org.argouml.uml.diagram.state.ui.UMLStateDiagram; | |
50 | ||
51 | /** | |
52 | * Action to create a new statechart diagram. | |
53 | */ | |
54 | public class ActionStateDiagram extends ActionNewDiagram { | |
55 | ||
56 | /** | |
57 | * Constructor. | |
58 | */ | |
59 | public ActionStateDiagram() { | |
60 | 4500 | super("action.state-diagram"); |
61 | 4500 | } |
62 | ||
63 | protected ArgoDiagram createDiagram(Object namespace, | |
64 | DiagramSettings settings) { | |
65 | 39 | Object machine = buildMachine(namespace, getTarget(namespace)); |
66 | ||
67 | 39 | return DiagramFactory.getInstance().create( |
68 | DiagramFactory.DiagramType.State, | |
69 | machine, settings); | |
70 | } | |
71 | ||
72 | ||
73 | private Object getTarget(Object namespace) { | |
74 | 39 | Object target = TargetManager.getInstance().getModelTarget(); |
75 | 39 | if (Model.getFacade().isAUMLElement(target) |
76 | && Model.getModelManagementHelper().isReadOnly(target)) { | |
77 | 0 | target = namespace; |
78 | } | |
79 | 39 | return target; |
80 | } | |
81 | ||
82 | private Object buildMachine(Object namespace, Object target) { | |
83 | 39 | Object machine = null; |
84 | 39 | if (Model.getStateMachinesHelper().isAddingStatemachineAllowed( |
85 | target)) { | |
86 | /* The target is a valid context. */ | |
87 | 0 | machine = Model.getStateMachinesFactory().buildStateMachine(target); |
88 | 39 | } else if (Model.getFacade().isAStateMachine(target) |
89 | && hasNoDiagramYet(target)) { | |
90 | /* This target is a statemachine, | |
91 | * for which no diagram exists yet, | |
92 | * so, let's use it. */ | |
93 | 0 | machine = target; |
94 | } else { | |
95 | /* Let's just build a Statemachine, | |
96 | * and put it in a suitable namespace. */ | |
97 | 39 | machine = Model.getStateMachinesFactory().createStateMachine(); |
98 | 39 | if (Model.getFacade().isANamespace(target)) { |
99 | 0 | namespace = target; |
100 | /* Follow well-formedness rule for a Class [2]. | |
101 | * Determine the owning namespace for the statemachine: */ | |
102 | 0 | while (Model.getFacade().isAClass(namespace)) { |
103 | 0 | Object parent = Model.getFacade().getNamespace(namespace); |
104 | 0 | if (parent == null) { |
105 | 0 | break; |
106 | } | |
107 | 0 | namespace = parent; |
108 | 0 | } |
109 | } | |
110 | 39 | Model.getCoreHelper().setNamespace(machine, namespace); |
111 | ||
112 | 39 | if (Model.getFacade().getUmlVersion().charAt(0) == '1') { |
113 | 39 | Model.getStateMachinesFactory() |
114 | .buildCompositeStateOnStateMachine(machine); | |
115 | } | |
116 | } | |
117 | 39 | return machine; |
118 | } | |
119 | ||
120 | private boolean hasNoDiagramYet(Object machine) { | |
121 | 0 | Project p = ProjectManager.getManager().getCurrentProject(); |
122 | 0 | for (ArgoDiagram d : p.getDiagramList()) { |
123 | 0 | if (d instanceof UMLStateDiagram) { |
124 | 0 | if (((UMLStateDiagram) d).getStateMachine() == machine) { |
125 | 0 | return false; |
126 | } | |
127 | } | |
128 | } | |
129 | 0 | return true; |
130 | } | |
131 | ||
132 | } |