Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionGenerateProjectCode |
|
| 6.25;6.25 |
1 | /* $Id: ActionGenerateProjectCode.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.uml.diagram.ArgoDiagram; | |
51 | import org.argouml.uml.diagram.DiagramUtils; | |
52 | import org.argouml.uml.generator.GeneratorManager; | |
53 | import org.argouml.uml.generator.ui.ClassGenerationDialog; | |
54 | import org.argouml.ui.UndoableAction; | |
55 | ||
56 | /** | |
57 | * Action to trigger code generation for all classes/interfaces in the | |
58 | * project, which have a source code path set in tagged value 'src_path'. | |
59 | * | |
60 | * @stereotype singleton | |
61 | */ | |
62 | public class ActionGenerateProjectCode extends UndoableAction { | |
63 | ||
64 | /** | |
65 | * The constructor. | |
66 | */ | |
67 | public ActionGenerateProjectCode() { | |
68 | 1800 | super(Translator.localize("action.generate-code-for-project"), |
69 | null); | |
70 | // Set the tooltip string: | |
71 | 1800 | putValue(Action.SHORT_DESCRIPTION, |
72 | Translator.localize("action.generate-code-for-project")); | |
73 | 1800 | } |
74 | ||
75 | ||
76 | /* | |
77 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) | |
78 | */ | |
79 | @Override | |
80 | public void actionPerformed(ActionEvent ae) { | |
81 | 0 | super.actionPerformed(ae); |
82 | 0 | List classes = new ArrayList(); |
83 | 0 | ArgoDiagram activeDiagram = DiagramUtils.getActiveDiagram(); |
84 | 0 | if (activeDiagram == null) { |
85 | 0 | return; |
86 | } | |
87 | 0 | Object ns = activeDiagram.getNamespace(); |
88 | 0 | if (ns == null) { |
89 | 0 | return; |
90 | } | |
91 | 0 | while (Model.getFacade().getNamespace(ns) != null) { |
92 | 0 | ns = Model.getFacade().getNamespace(ns); |
93 | } | |
94 | 0 | Collection elems = |
95 | Model.getModelManagementHelper() | |
96 | .getAllModelElementsOfKind( | |
97 | ns, | |
98 | Model.getMetaTypes().getClassifier()); | |
99 | //Project p = ProjectManager.getManager().getCurrentProject(); | |
100 | //Collection elems = | |
101 | //ModelManagementHelper.getHelper() | |
102 | // .getAllModelElementsOfKind(MClassifier.class); | |
103 | 0 | for (Object cls : elems) { |
104 | 0 | if (isCodeRelevantClassifier(cls)) { |
105 | 0 | classes.add(cls); |
106 | } | |
107 | } | |
108 | 0 | ClassGenerationDialog cgd = new ClassGenerationDialog(classes, true); |
109 | 0 | cgd.setVisible(true); |
110 | 0 | } |
111 | ||
112 | /** | |
113 | * Check if the diagram is enabled | |
114 | * | |
115 | * @return true if enabled | |
116 | * @see org.tigris.gef.undo.UndoableAction#isEnabled() | |
117 | */ | |
118 | public boolean isEnabled() { | |
119 | 900 | return true; |
120 | } | |
121 | ||
122 | /** | |
123 | * @param cls the classifier that is candidate for generation | |
124 | * @return true if the candidate is sound | |
125 | */ | |
126 | private boolean isCodeRelevantClassifier(Object cls) { | |
127 | 0 | if (cls == null) { |
128 | 0 | return false; |
129 | } | |
130 | 0 | if (!Model.getFacade().isAClass(cls) |
131 | && !Model.getFacade().isAInterface(cls)) { | |
132 | 0 | return false; |
133 | } | |
134 | 0 | String path = GeneratorManager.getCodePath(cls); |
135 | 0 | String name = Model.getFacade().getName(cls); |
136 | 0 | if (name == null |
137 | || name.length() == 0 | |
138 | || Character.isDigit(name.charAt(0))) { | |
139 | 0 | return false; |
140 | } | |
141 | 0 | if (path != null) { |
142 | 0 | return (path.length() > 0); |
143 | } | |
144 | 0 | Object parent = Model.getFacade().getNamespace(cls); |
145 | 0 | while (parent != null) { |
146 | 0 | path = GeneratorManager.getCodePath(parent); |
147 | 0 | if (path != null) { |
148 | 0 | return (path.length() > 0); |
149 | } | |
150 | 0 | parent = Model.getFacade().getNamespace(parent); |
151 | } | |
152 | 0 | return false; |
153 | } | |
154 | ||
155 | ||
156 | } |