Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionNavigability |
|
| 3.0;3 |
1 | /* $Id: ActionNavigability.java 17865 2010-01-12 20:45:26Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 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 | * bobtarling | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.diagram.ui; | |
40 | ||
41 | import java.awt.event.ActionEvent; | |
42 | ||
43 | import javax.swing.Action; | |
44 | ||
45 | import org.argouml.i18n.Translator; | |
46 | import org.argouml.model.Model; | |
47 | import org.argouml.ui.UndoableAction; | |
48 | ||
49 | /** | |
50 | * A class to perform the action of changing the unidirectional or | |
51 | * bidirectional navigation of an association. | |
52 | * | |
53 | * @author Bob Tarling | |
54 | */ | |
55 | public class ActionNavigability extends UndoableAction { | |
56 | /** | |
57 | * Enumeration constant for <code>BIDIRECTIONAL</code> navigability. | |
58 | */ | |
59 | public static final int BIDIRECTIONAL = 0; | |
60 | ||
61 | /** | |
62 | * Enumeration constant for <code>STARTTOEND</code> navigability. | |
63 | */ | |
64 | public static final int STARTTOEND = 1; | |
65 | ||
66 | /** | |
67 | * Enumeration constant for <code>ENDTOSTART</code> navigability. | |
68 | */ | |
69 | public static final int ENDTOSTART = 2; | |
70 | ||
71 | /** | |
72 | * The actual navigability of this action. | |
73 | */ | |
74 | 0 | private int nav = BIDIRECTIONAL; |
75 | ||
76 | /** | |
77 | * The association start. | |
78 | */ | |
79 | private Object assocStart; | |
80 | ||
81 | /** | |
82 | * The association end. | |
83 | */ | |
84 | private Object assocEnd; | |
85 | ||
86 | /** | |
87 | * The <code>ActionNavigability</code> constructor. | |
88 | * | |
89 | * @param assocStart a <code>MAssociationEnd</code> object at the start | |
90 | * of an association. | |
91 | * @param assocEnd a <code>MAssociationEnd</code> object at the end of | |
92 | * an association. | |
93 | * @param nav the type of navigation required in the association | |
94 | * being either <ul> <li>BIDIRECTIONAL <li>STARTTOEND | |
95 | * <li>ENDTOSTART </ul> | |
96 | * | |
97 | * @return the constructed class | |
98 | */ | |
99 | public static ActionNavigability newActionNavigability(Object assocStart, | |
100 | Object assocEnd, | |
101 | int nav) { | |
102 | 0 | return new ActionNavigability(getDescription(assocStart, assocEnd, nav), |
103 | assocStart, | |
104 | assocEnd, | |
105 | nav); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Build a description string from the given association ends, | |
110 | * and the navigability. | |
111 | * | |
112 | * @param assocStart association end 1 | |
113 | * @param assocEnd association end 2 | |
114 | * @param nav the navigability | |
115 | * @return the string containing a human-readible indication | |
116 | * of the navigability | |
117 | */ | |
118 | private static String getDescription(Object assocStart, | |
119 | Object assocEnd, | |
120 | int nav) { | |
121 | 0 | String startName = |
122 | Model.getFacade().getName(Model.getFacade().getType(assocStart)); | |
123 | 0 | String endName = |
124 | Model.getFacade().getName(Model.getFacade().getType(assocEnd)); | |
125 | ||
126 | 0 | if (startName == null || startName.length() == 0) { |
127 | 0 | startName = Translator.localize("action.navigation.anon"); |
128 | } | |
129 | 0 | if (endName == null || endName.length() == 0) { |
130 | 0 | endName = Translator.localize("action.navigation.anon"); |
131 | } | |
132 | ||
133 | 0 | if (nav == STARTTOEND) { |
134 | 0 | return Translator.messageFormat( |
135 | "action.navigation.from-to", | |
136 | new Object[] { | |
137 | startName, | |
138 | endName, | |
139 | } | |
140 | ); | |
141 | 0 | } else if (nav == ENDTOSTART) { |
142 | 0 | return Translator.messageFormat( |
143 | "action.navigation.from-to", | |
144 | new Object[] { | |
145 | endName, | |
146 | startName, | |
147 | } | |
148 | ); | |
149 | } else { | |
150 | 0 | return Translator.localize("action.navigation.bidirectional"); |
151 | } | |
152 | } | |
153 | ||
154 | /** | |
155 | * The constructor. | |
156 | * | |
157 | * @param label the description as build in <code>getDescription</code> | |
158 | * @param theAssociationStart association end 1 | |
159 | * @param theAssociationEnd association end 2 | |
160 | * @param theNavigability the navigability: one of | |
161 | * BIDIRECTIONAL, STARTTOEND, ENDTOSTART | |
162 | */ | |
163 | protected ActionNavigability(String label, | |
164 | Object theAssociationStart, | |
165 | Object theAssociationEnd, | |
166 | int theNavigability) { | |
167 | 0 | super(label, null); |
168 | // Set the tooltip string: | |
169 | 0 | putValue(Action.SHORT_DESCRIPTION, label); |
170 | ||
171 | 0 | this.nav = theNavigability; |
172 | 0 | this.assocStart = theAssociationStart; |
173 | 0 | this.assocEnd = theAssociationEnd; |
174 | 0 | } |
175 | ||
176 | /* | |
177 | * @see java.awt.event.ActionListener#actionPerformed( | |
178 | * java.awt.event.ActionEvent) | |
179 | */ | |
180 | public void actionPerformed(ActionEvent ae) { | |
181 | 0 | super.actionPerformed(ae); |
182 | 0 | Model.getCoreHelper().setNavigable(assocStart, |
183 | (nav == BIDIRECTIONAL || nav == ENDTOSTART)); | |
184 | 0 | Model.getCoreHelper().setNavigable(assocEnd, |
185 | (nav == BIDIRECTIONAL || nav == STARTTOEND)); | |
186 | 0 | } |
187 | ||
188 | /** | |
189 | * The is action is always enabled. | |
190 | * | |
191 | *@return always true (the action is always enabled) | |
192 | * @see org.tigris.gef.undo.UndoableAction#isEnabled() | |
193 | */ | |
194 | public boolean isEnabled() { | |
195 | 0 | return true; |
196 | } | |
197 | } /* end class ActionNavigability */ |