Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionGenerateAll |
|
| 5.0;5 |
1 | /* $Id: ActionGenerateAll.java 17881 2010-01-12 21:09:28Z 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-2008 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.ui; | |
40 | ||
41 | import java.awt.event.ActionEvent; | |
42 | import java.util.ArrayList; | |
43 | import java.util.Collection; | |
44 | import java.util.List; | |
45 | ||
46 | import javax.swing.Action; | |
47 | ||
48 | import org.argouml.i18n.Translator; | |
49 | import org.argouml.model.Model; | |
50 | import org.argouml.ui.targetmanager.TargetManager; | |
51 | import org.argouml.uml.diagram.ArgoDiagram; | |
52 | import org.argouml.uml.diagram.DiagramUtils; | |
53 | import org.argouml.uml.diagram.static_structure.ui.UMLClassDiagram; | |
54 | import org.argouml.uml.generator.ui.ClassGenerationDialog; | |
55 | import org.argouml.ui.UndoableAction; | |
56 | ||
57 | /** | |
58 | * Action to trigger code generation for one or more classes. | |
59 | * <p> | |
60 | * In fact, only all named classes and interfaces | |
61 | * on the active diagram are generated. | |
62 | * Or, if this delivers an empty collection, all selected classes, interfaces | |
63 | * and the contents of selected packages are generated | |
64 | * (independent if they are named or not). <p> | |
65 | * TODO: Implement a more logical behaviour. | |
66 | */ | |
67 | public class ActionGenerateAll extends UndoableAction { | |
68 | ||
69 | ||
70 | /** | |
71 | * Constructor. | |
72 | */ | |
73 | public ActionGenerateAll() { | |
74 | 1800 | super(Translator.localize("action.generate-all-classes"), null); |
75 | // Set the tooltip string: | |
76 | 1800 | putValue(Action.SHORT_DESCRIPTION, |
77 | Translator.localize("action.generate-all-classes")); | |
78 | 1800 | } |
79 | ||
80 | ||
81 | /* | |
82 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) | |
83 | */ | |
84 | @Override | |
85 | public void actionPerformed(ActionEvent ae) { | |
86 | 0 | super.actionPerformed(ae); |
87 | 0 | ArgoDiagram activeDiagram = DiagramUtils.getActiveDiagram(); |
88 | 0 | if (!(activeDiagram instanceof UMLClassDiagram)) { |
89 | 0 | return; |
90 | } | |
91 | ||
92 | 0 | UMLClassDiagram d = (UMLClassDiagram) activeDiagram; |
93 | 0 | List classes = new ArrayList(); |
94 | 0 | List nodes = d.getNodes(); |
95 | 0 | for (Object owner : nodes) { |
96 | 0 | if (!Model.getFacade().isAClass(owner) |
97 | && !Model.getFacade().isAInterface(owner)) { | |
98 | 0 | continue; |
99 | } | |
100 | 0 | String name = Model.getFacade().getName(owner); |
101 | 0 | if (name == null |
102 | || name.length() == 0 | |
103 | || Character.isDigit(name.charAt(0))) { | |
104 | ||
105 | 0 | continue; |
106 | ||
107 | } | |
108 | 0 | classes.add(owner); |
109 | 0 | } |
110 | ||
111 | 0 | if (classes.size() == 0) { |
112 | ||
113 | 0 | Collection selectedObjects = |
114 | TargetManager.getInstance().getTargets(); | |
115 | 0 | for (Object selected : selectedObjects) { |
116 | 0 | if (Model.getFacade().isAPackage(selected)) { |
117 | 0 | addCollection(Model.getModelManagementHelper() |
118 | .getAllModelElementsOfKind( | |
119 | selected, | |
120 | Model.getMetaTypes().getUMLClass()), | |
121 | classes); | |
122 | 0 | addCollection(Model.getModelManagementHelper() |
123 | .getAllModelElementsOfKind( | |
124 | selected, | |
125 | Model.getMetaTypes().getInterface()), | |
126 | classes); | |
127 | 0 | } else if (Model.getFacade().isAClass(selected) |
128 | || Model.getFacade().isAInterface(selected)) { | |
129 | 0 | if (!classes.contains(selected)) { |
130 | 0 | classes.add(selected); |
131 | } | |
132 | } | |
133 | } | |
134 | } | |
135 | 0 | ClassGenerationDialog cgd = new ClassGenerationDialog(classes); |
136 | 0 | cgd.setVisible(true); |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * @return true if the action is enabled and the active diagram is a class | |
141 | * diagram | |
142 | * @see org.tigris.gef.undo.UndoableAction#isEnabled() | |
143 | */ | |
144 | @Override | |
145 | public boolean isEnabled() { | |
146 | // TODO: this seems to be called at startup only so no check so far | |
147 | 900 | return true; |
148 | //ArgoDiagram activeDiagram = DiagramUtils.getActiveDiagram(); | |
149 | //return (activeDiagram instanceof UMLClassDiagram); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Adds elements from collection without duplicates. | |
154 | */ | |
155 | private void addCollection(Collection c, Collection v) { | |
156 | 0 | for (Object o : c) { |
157 | 0 | if (!v.contains(o)) { |
158 | 0 | v.add(o); |
159 | } | |
160 | } | |
161 | 0 | } |
162 | } |