Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WizStep |
|
| 2.1;2.1 |
1 | /* $Id: WizStep.java 17818 2010-01-12 18:39:46Z 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 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 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.cognitive.ui; | |
40 | ||
41 | import java.awt.BorderLayout; | |
42 | import java.awt.FlowLayout; | |
43 | import java.awt.GridLayout; | |
44 | import java.awt.Insets; | |
45 | import java.awt.event.ActionEvent; | |
46 | import java.awt.event.ActionListener; | |
47 | ||
48 | import javax.swing.ImageIcon; | |
49 | import javax.swing.JButton; | |
50 | import javax.swing.JPanel; | |
51 | import javax.swing.event.DocumentEvent; | |
52 | import javax.swing.event.DocumentListener; | |
53 | ||
54 | import org.argouml.application.helpers.ResourceLoaderWrapper; | |
55 | import org.argouml.cognitive.ToDoItem; | |
56 | import org.argouml.cognitive.Translator; | |
57 | import org.argouml.cognitive.critics.Wizard; | |
58 | import org.argouml.swingext.SpacerPanel; | |
59 | import org.argouml.ui.ProjectBrowser; | |
60 | import org.argouml.ui.TabToDoTarget; | |
61 | import org.argouml.ui.targetmanager.TargetEvent; | |
62 | import org.argouml.util.osdep.StartBrowser; | |
63 | ||
64 | /** | |
65 | * Each Critic may provide a Wizard to help fix the problem it | |
66 | * identifies. The "Next>" button will advance through the steps of | |
67 | * the wizard, and increase the colored progress bar on the ToDoItem | |
68 | * "sticky note" icon in ToDo tree pane. | |
69 | * | |
70 | * @see org.argouml.cognitive.Critic | |
71 | * @see org.argouml.cognitive.critics.Wizard | |
72 | */ | |
73 | ||
74 | public class WizStep extends JPanel | |
75 | implements TabToDoTarget, ActionListener, DocumentListener { | |
76 | //////////////////////////////////////////////////////////////// | |
77 | // constants | |
78 | 900 | private static final ImageIcon WIZ_ICON = |
79 | ResourceLoaderWrapper | |
80 | .lookupIconResource("Wiz", "Wiz"); | |
81 | ||
82 | //////////////////////////////////////////////////////////////// | |
83 | // instance variables | |
84 | ||
85 | 900 | private JPanel mainPanel = new JPanel(); |
86 | 900 | private JButton backButton = |
87 | new JButton(Translator.localize("button.back")); | |
88 | 900 | private JButton nextButton = |
89 | new JButton(Translator.localize("button.next")); | |
90 | 900 | private JButton finishButton = |
91 | new JButton(Translator.localize("button.finish")); | |
92 | 900 | private JButton helpButton = |
93 | new JButton(Translator.localize("button.help")); | |
94 | 900 | private JPanel buttonPanel = new JPanel(); |
95 | ||
96 | /** | |
97 | * The current target. | |
98 | */ | |
99 | private Object target; | |
100 | ||
101 | /** | |
102 | * @return Returns the main Panel. | |
103 | */ | |
104 | protected JPanel getMainPanel() { | |
105 | 1800 | return mainPanel; |
106 | } | |
107 | /** | |
108 | * @return Returns the WIZ_ICON. | |
109 | */ | |
110 | protected static ImageIcon getWizardIcon() { | |
111 | 0 | return WIZ_ICON; |
112 | } | |
113 | ||
114 | /** | |
115 | * @param b the button to set the mnemonic for | |
116 | * @param key the mnemonic | |
117 | */ | |
118 | protected static final void setMnemonic(JButton b, String key) { | |
119 | 3600 | String m = Translator.localize(key); |
120 | 3600 | if (m == null) { |
121 | 0 | return; |
122 | } | |
123 | 3600 | if (m.length() == 1) { |
124 | 3600 | b.setMnemonic(m.charAt(0)); |
125 | } | |
126 | 3600 | } |
127 | ||
128 | ||
129 | /** | |
130 | * The constructor. | |
131 | */ | |
132 | 900 | public WizStep() { |
133 | 900 | setMnemonic(backButton, "mnemonic.button.back"); |
134 | 900 | setMnemonic(nextButton, "mnemonic.button.next"); |
135 | 900 | setMnemonic(finishButton, "mnemonic.button.finish"); |
136 | 900 | setMnemonic(helpButton, "mnemonic.button.help"); |
137 | 900 | buttonPanel.setLayout(new GridLayout(1, 5)); |
138 | 900 | buttonPanel.add(backButton); |
139 | 900 | buttonPanel.add(nextButton); |
140 | 900 | buttonPanel.add(new SpacerPanel()); |
141 | 900 | buttonPanel.add(finishButton); |
142 | 900 | buttonPanel.add(new SpacerPanel()); |
143 | 900 | buttonPanel.add(helpButton); |
144 | ||
145 | 900 | backButton.setMargin(new Insets(0, 0, 0, 0)); |
146 | 900 | nextButton.setMargin(new Insets(0, 0, 0, 0)); |
147 | 900 | finishButton.setMargin(new Insets(0, 0, 0, 0)); |
148 | 900 | helpButton.setMargin(new Insets(0, 0, 0, 0)); |
149 | ||
150 | 900 | JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
151 | 900 | southPanel.add(buttonPanel); |
152 | ||
153 | 900 | setLayout(new BorderLayout()); |
154 | 900 | add(mainPanel, BorderLayout.CENTER); |
155 | 900 | add(southPanel, BorderLayout.SOUTH); |
156 | ||
157 | 900 | backButton.addActionListener(this); |
158 | 900 | nextButton.addActionListener(this); |
159 | 900 | finishButton.addActionListener(this); |
160 | 900 | helpButton.addActionListener(this); |
161 | 900 | } |
162 | ||
163 | //////////////////////////////////////////////////////////////// | |
164 | // accessors | |
165 | ||
166 | /** | |
167 | * @param item the target item | |
168 | */ | |
169 | public void setTarget(Object item) { | |
170 | 2291 | target = item; |
171 | 2291 | enableButtons(); |
172 | 2291 | } |
173 | ||
174 | /** | |
175 | * Enable/Disable the buttons. | |
176 | */ | |
177 | public void enableButtons() { | |
178 | 2291 | if (target == null) { |
179 | 2185 | backButton.setEnabled(false); |
180 | 2185 | nextButton.setEnabled(false); |
181 | 2185 | finishButton.setEnabled(false); |
182 | 2185 | helpButton.setEnabled(false); |
183 | 106 | } else if (target instanceof ToDoItem) { |
184 | 0 | ToDoItem tdi = (ToDoItem) target; |
185 | 0 | Wizard w = getWizard(); |
186 | 0 | backButton.setEnabled(w != null ? w.canGoBack() : false); |
187 | 0 | nextButton.setEnabled(w != null ? w.canGoNext() : false); |
188 | 0 | finishButton.setEnabled(w != null ? w.canFinish() : false); |
189 | ||
190 | 0 | if (tdi.getMoreInfoURL() == null |
191 | || "".equals(tdi.getMoreInfoURL())) { | |
192 | 0 | helpButton.setEnabled(false); |
193 | } else { | |
194 | 0 | helpButton.setEnabled(true); |
195 | } | |
196 | 0 | } else { |
197 | 106 | return; |
198 | } | |
199 | 2185 | } |
200 | ||
201 | /** | |
202 | * Set the target anew. | |
203 | * | |
204 | * TODO: This method is never used. What is its intention? Remove it? | |
205 | */ | |
206 | 0 | public void refresh() { setTarget(target); } |
207 | ||
208 | /** | |
209 | * @return the Wizard, or null | |
210 | */ | |
211 | public Wizard getWizard() { | |
212 | 0 | if (target instanceof ToDoItem) { |
213 | 0 | return ((ToDoItem) target).getWizard(); |
214 | } | |
215 | 0 | return null; |
216 | } | |
217 | ||
218 | //////////////////////////////////////////////////////////////// | |
219 | // actions | |
220 | ||
221 | /** | |
222 | * The Back button has been pressed, so we do the "back" action. | |
223 | */ | |
224 | public void doBack() { | |
225 | 0 | Wizard w = getWizard(); |
226 | 0 | if (w != null) { |
227 | 0 | w.back(); |
228 | 0 | updateTabToDo(); |
229 | } | |
230 | 0 | } |
231 | /** | |
232 | * The Next button has been pressed, so we do the "next" action. | |
233 | */ | |
234 | public void doNext() { | |
235 | 0 | Wizard w = getWizard(); |
236 | 0 | if (w != null) { |
237 | 0 | w.next(); |
238 | 0 | updateTabToDo(); |
239 | } | |
240 | 0 | } |
241 | ||
242 | /** | |
243 | * The Finish button has been pressed, so we do the "finish" action. | |
244 | */ | |
245 | public void doFinsh() { | |
246 | 0 | Wizard w = getWizard(); |
247 | 0 | if (w != null) { |
248 | 0 | w.finish(); |
249 | 0 | updateTabToDo(); |
250 | } | |
251 | 0 | } |
252 | ||
253 | /** | |
254 | * Called when the Help button is pressed. | |
255 | */ | |
256 | public void doHelp() { | |
257 | 0 | if (!(target instanceof ToDoItem)) { |
258 | 0 | return; |
259 | } | |
260 | 0 | ToDoItem item = (ToDoItem) target; |
261 | 0 | String urlString = item.getMoreInfoURL(); |
262 | 0 | StartBrowser.openUrl(urlString); |
263 | 0 | } |
264 | ||
265 | /** | |
266 | * Set the target and make visible. | |
267 | */ | |
268 | protected void updateTabToDo() { | |
269 | // TODO: TabToDo should listen for an event that this fires so that we | |
270 | // can decouple from the ProjectBrowser. - tfm | |
271 | 0 | TabToDo ttd = |
272 | (TabToDo) ProjectBrowser.getInstance().getTab(TabToDo.class); | |
273 | 0 | JPanel ws = getWizard().getCurrentPanel(); |
274 | 0 | if (ws instanceof WizStep) { |
275 | 0 | ((WizStep) ws).setTarget(target); |
276 | } | |
277 | 0 | ttd.showStep(ws); |
278 | 0 | } |
279 | ||
280 | //////////////////////////////////////////////////////////////// | |
281 | // ActionListener implementation | |
282 | ||
283 | /* | |
284 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) | |
285 | */ | |
286 | public void actionPerformed(ActionEvent ae) { | |
287 | 0 | Object src = ae.getSource(); |
288 | 0 | if (src == backButton) { |
289 | 0 | doBack(); |
290 | 0 | } else if (src == nextButton) { |
291 | 0 | doNext(); |
292 | 0 | } else if (src == finishButton) { |
293 | 0 | doFinsh(); |
294 | 0 | } else if (src == helpButton) { |
295 | 0 | doHelp(); |
296 | } | |
297 | 0 | } |
298 | ||
299 | //////////////////////////////////////////////////////////////// | |
300 | // DocumentListener implementation | |
301 | ||
302 | /* | |
303 | * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent) | |
304 | */ | |
305 | public void insertUpdate(DocumentEvent e) { | |
306 | 0 | enableButtons(); |
307 | 0 | } |
308 | ||
309 | /* | |
310 | * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent) | |
311 | */ | |
312 | 0 | public void removeUpdate(DocumentEvent e) { insertUpdate(e); } |
313 | ||
314 | /* | |
315 | * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent) | |
316 | */ | |
317 | public void changedUpdate(DocumentEvent e) { | |
318 | // Apparently, this method is never called. | |
319 | 0 | } |
320 | ||
321 | //////////////////////////////////////////////////////////////// | |
322 | // TargetListener implementation | |
323 | ||
324 | /* | |
325 | * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent) | |
326 | */ | |
327 | public void targetAdded(TargetEvent e) { | |
328 | 0 | setTarget(e.getNewTarget()); |
329 | 0 | } |
330 | ||
331 | /* | |
332 | * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent) | |
333 | */ | |
334 | public void targetRemoved(TargetEvent e) { | |
335 | // how to handle empty target lists? | |
336 | // probably the wizstep should only show an empty pane in that case | |
337 | 0 | setTarget(e.getNewTarget()); |
338 | 0 | } |
339 | ||
340 | /* | |
341 | * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent) | |
342 | */ | |
343 | public void targetSet(TargetEvent e) { | |
344 | 0 | setTarget(e.getNewTarget()); |
345 | 0 | } |
346 | ||
347 | ||
348 | /** | |
349 | * The UID. | |
350 | */ | |
351 | private static final long serialVersionUID = 8845081753813440684L; | |
352 | } /* end class WizStep */ |