1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
package org.argouml.cognitive.critics.ui; |
40 | |
|
41 | |
import java.awt.BorderLayout; |
42 | |
import java.awt.Dimension; |
43 | |
import java.awt.FlowLayout; |
44 | |
import java.awt.GridBagConstraints; |
45 | |
import java.awt.GridBagLayout; |
46 | |
import java.awt.Insets; |
47 | |
import java.awt.event.ActionEvent; |
48 | |
import java.awt.event.ActionListener; |
49 | |
import java.awt.event.ItemEvent; |
50 | |
import java.awt.event.ItemListener; |
51 | |
import java.util.Observable; |
52 | |
import java.util.Observer; |
53 | |
|
54 | |
import javax.swing.BorderFactory; |
55 | |
import javax.swing.JButton; |
56 | |
import javax.swing.JComboBox; |
57 | |
import javax.swing.JLabel; |
58 | |
import javax.swing.JPanel; |
59 | |
import javax.swing.JScrollPane; |
60 | |
import javax.swing.JTextArea; |
61 | |
import javax.swing.JTextField; |
62 | |
import javax.swing.event.DocumentEvent; |
63 | |
import javax.swing.event.DocumentListener; |
64 | |
import javax.swing.event.ListSelectionEvent; |
65 | |
import javax.swing.event.ListSelectionListener; |
66 | |
import javax.swing.event.TableModelEvent; |
67 | |
import javax.swing.event.TableModelListener; |
68 | |
import javax.swing.text.Document; |
69 | |
|
70 | |
import org.apache.log4j.Logger; |
71 | |
import org.argouml.cognitive.Critic; |
72 | |
import org.argouml.cognitive.ToDoItem; |
73 | |
import org.argouml.cognitive.Translator; |
74 | |
import org.argouml.util.ArgoDialog; |
75 | |
import org.argouml.util.osdep.StartBrowser; |
76 | |
import org.tigris.swidgets.BorderSplitPane; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public class CriticBrowserDialog extends ArgoDialog |
85 | |
implements ActionListener, |
86 | |
ListSelectionListener, |
87 | |
ItemListener, |
88 | |
DocumentListener, |
89 | |
TableModelListener, |
90 | |
Observer { |
91 | 20 | private static final Logger LOG = |
92 | |
Logger.getLogger(CriticBrowserDialog.class); |
93 | |
|
94 | 20 | private static int numCriticBrowser = 0; |
95 | |
|
96 | |
private static final int NUM_COLUMNS = 25; |
97 | |
|
98 | 20 | private static final String HIGH = |
99 | |
Translator.localize("misc.level.high"); |
100 | 20 | private static final String MEDIUM = |
101 | |
Translator.localize("misc.level.medium"); |
102 | 20 | private static final String LOW = |
103 | |
Translator.localize("misc.level.low"); |
104 | 20 | private static final String[] PRIORITIES = { |
105 | |
HIGH, MEDIUM, LOW, |
106 | |
}; |
107 | |
|
108 | 20 | private static final String ALWAYS = |
109 | |
Translator.localize("dialog.browse.use-clarifier.always"); |
110 | 20 | private static final String IF_ONLY_ONE = |
111 | |
Translator.localize("dialog.browse.use-clarifier.if-only-one"); |
112 | 20 | private static final String NEVER = |
113 | |
Translator.localize("dialog.browse.use-clarifier.never"); |
114 | 20 | private static final String[] USE_CLAR = { |
115 | |
ALWAYS, IF_ONLY_ONE, NEVER, |
116 | |
}; |
117 | |
|
118 | |
private static final int INSET_PX = 3; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | 20 | private JLabel criticsLabel = new JLabel( |
124 | |
Translator.localize("dialog.browse.label.critics")); |
125 | 20 | private JLabel clsNameLabel = new JLabel( |
126 | |
Translator.localize("dialog.browse.label.critic-class")); |
127 | 20 | private JLabel headlineLabel = new JLabel( |
128 | |
Translator.localize("dialog.browse.label.headline")); |
129 | 20 | private JLabel priorityLabel = new JLabel( |
130 | |
Translator.localize("dialog.browse.label.priority")); |
131 | 20 | private JLabel moreInfoLabel = new JLabel( |
132 | |
Translator.localize("dialog.browse.label.more-info")); |
133 | 20 | private JLabel descLabel = new JLabel( |
134 | |
Translator.localize("dialog.browse.label.description")); |
135 | 20 | private JLabel clarifierLabel = new JLabel( |
136 | |
Translator.localize("dialog.browse.label.use-clarifier")); |
137 | |
|
138 | |
private TableCritics table; |
139 | |
|
140 | 20 | private JTextField className = new JTextField("", NUM_COLUMNS); |
141 | 20 | private JTextField headline = new JTextField("", NUM_COLUMNS); |
142 | 20 | private JComboBox priority = new JComboBox(PRIORITIES); |
143 | 20 | private JTextField moreInfo = new JTextField("", NUM_COLUMNS - 4); |
144 | 20 | private JTextArea desc = new JTextArea("", 6, NUM_COLUMNS); |
145 | 20 | private JComboBox useClar = new JComboBox(USE_CLAR); |
146 | |
|
147 | 20 | private JButton wakeButton = new JButton( |
148 | |
Translator.localize("dialog.browse.button.wake")); |
149 | 20 | private JButton configButton = new JButton( |
150 | |
Translator.localize("dialog.browse.button.configure")); |
151 | 20 | private JButton networkButton = new JButton( |
152 | |
Translator.localize("dialog.browse.button.edit-network")); |
153 | 20 | private JButton goButton = new JButton( |
154 | |
Translator.localize("dialog.browse.button.go")); |
155 | 20 | private JButton advancedButton = new JButton( |
156 | |
Translator.localize("dialog.browse.button.advanced")); |
157 | |
|
158 | |
private Critic target; |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public CriticBrowserDialog() { |
165 | 20 | super(Translator.localize("dialog.browse.label.critics"), false); |
166 | |
|
167 | 20 | JPanel mainContent = new JPanel(); |
168 | 20 | mainContent.setLayout(new BorderLayout(10, 10)); |
169 | 20 | BorderSplitPane bsp = new BorderSplitPane(); |
170 | |
|
171 | |
|
172 | 20 | JPanel tablePanel = new JPanel(new BorderLayout(5, 5)); |
173 | 20 | table = new TableCritics(new TableModelCritics(false), this, this); |
174 | 20 | criticsLabel.setText(criticsLabel.getText() + " (" |
175 | |
+ table.getModel().getRowCount() + ")"); |
176 | 20 | tablePanel.add(criticsLabel, BorderLayout.NORTH); |
177 | 20 | JScrollPane tableSP = new JScrollPane(table); |
178 | 20 | tablePanel.add(tableSP, BorderLayout.CENTER); |
179 | |
|
180 | |
|
181 | |
|
182 | 20 | tableSP.setPreferredSize(table.getInitialSize()); |
183 | 20 | bsp.add(tablePanel, BorderSplitPane.CENTER); |
184 | |
|
185 | |
|
186 | 20 | JPanel detailsPanel = new JPanel(new GridBagLayout()); |
187 | 20 | detailsPanel.setBorder(BorderFactory.createTitledBorder( |
188 | |
Translator.localize( |
189 | |
"dialog.browse.titled-border.critic-details"))); |
190 | |
|
191 | 20 | GridBagConstraints labelConstraints = new GridBagConstraints(); |
192 | 20 | labelConstraints.anchor = GridBagConstraints.EAST; |
193 | 20 | labelConstraints.fill = GridBagConstraints.BOTH; |
194 | 20 | labelConstraints.gridy = 0; |
195 | 20 | labelConstraints.gridx = 0; |
196 | 20 | labelConstraints.gridwidth = 1; |
197 | 20 | labelConstraints.gridheight = 1; |
198 | 20 | labelConstraints.insets = new Insets(0, 10, 5, 4); |
199 | |
|
200 | 20 | GridBagConstraints fieldConstraints = new GridBagConstraints(); |
201 | 20 | fieldConstraints.anchor = GridBagConstraints.WEST; |
202 | 20 | fieldConstraints.fill = GridBagConstraints.BOTH; |
203 | 20 | fieldConstraints.gridy = 0; |
204 | 20 | fieldConstraints.gridx = 1; |
205 | 20 | fieldConstraints.gridwidth = 3; |
206 | 20 | fieldConstraints.gridheight = 1; |
207 | 20 | fieldConstraints.weightx = 1.0; |
208 | 20 | fieldConstraints.insets = new Insets(0, 4, 5, 10); |
209 | |
|
210 | 20 | className.setBorder(null); |
211 | 20 | labelConstraints.gridy = 0; |
212 | 20 | fieldConstraints.gridy = 0; |
213 | 20 | detailsPanel.add(clsNameLabel, labelConstraints); |
214 | 20 | detailsPanel.add(className, fieldConstraints); |
215 | |
|
216 | 20 | labelConstraints.gridy = 1; |
217 | 20 | fieldConstraints.gridy = 1; |
218 | 20 | detailsPanel.add(headlineLabel, labelConstraints); |
219 | 20 | detailsPanel.add(headline, fieldConstraints); |
220 | |
|
221 | 20 | labelConstraints.gridy = 2; |
222 | 20 | fieldConstraints.gridy = 2; |
223 | 20 | detailsPanel.add(priorityLabel, labelConstraints); |
224 | 20 | detailsPanel.add(priority, fieldConstraints); |
225 | |
|
226 | 20 | labelConstraints.gridy = 3; |
227 | 20 | fieldConstraints.gridy = 3; |
228 | 20 | detailsPanel.add(moreInfoLabel, labelConstraints); |
229 | 20 | JPanel moreInfoPanel = |
230 | |
new JPanel(new GridBagLayout()); |
231 | 20 | GridBagConstraints gridConstraints = new GridBagConstraints(); |
232 | 20 | gridConstraints.anchor = GridBagConstraints.WEST; |
233 | 20 | gridConstraints.gridx = 0; |
234 | 20 | gridConstraints.gridy = 0; |
235 | 20 | gridConstraints.weightx = 100; |
236 | 20 | gridConstraints.fill = GridBagConstraints.BOTH; |
237 | 20 | gridConstraints.insets = new Insets(0, 0, 5, 0); |
238 | 20 | moreInfoPanel.add(moreInfo, gridConstraints); |
239 | |
|
240 | 20 | gridConstraints.anchor = GridBagConstraints.EAST; |
241 | 20 | gridConstraints.gridx = 1; |
242 | 20 | gridConstraints.fill = GridBagConstraints.NONE; |
243 | 20 | gridConstraints.insets = new Insets(0, 10, 5, 0); |
244 | 20 | gridConstraints.weightx = 0; |
245 | 20 | moreInfoPanel.add(goButton, gridConstraints); |
246 | 20 | moreInfoPanel.setMinimumSize(new Dimension(priority.getWidth(), |
247 | |
priority.getHeight())); |
248 | 20 | detailsPanel.add(moreInfoPanel, fieldConstraints); |
249 | |
|
250 | 20 | labelConstraints.gridy = 4; |
251 | 20 | fieldConstraints.gridy = 4; |
252 | 20 | fieldConstraints.weighty = 3.0; |
253 | 20 | labelConstraints.anchor = GridBagConstraints.NORTHEAST; |
254 | 20 | detailsPanel.add(descLabel, labelConstraints); |
255 | 20 | detailsPanel.add(new JScrollPane(desc), fieldConstraints); |
256 | 20 | desc.setLineWrap(true); |
257 | 20 | desc.setWrapStyleWord(true); |
258 | 20 | desc.setMargin(new Insets(INSET_PX, INSET_PX, INSET_PX, INSET_PX)); |
259 | |
|
260 | 20 | labelConstraints.anchor = GridBagConstraints.EAST; |
261 | 20 | labelConstraints.gridy = 5; |
262 | 20 | fieldConstraints.gridy = 5; |
263 | 20 | fieldConstraints.weighty = 0; |
264 | 20 | detailsPanel.add(clarifierLabel, labelConstraints); |
265 | 20 | detailsPanel.add(useClar, fieldConstraints); |
266 | |
|
267 | 20 | labelConstraints.gridy = 6; |
268 | 20 | fieldConstraints.gridy = 6; |
269 | 20 | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
270 | 20 | buttonPanel.add(wakeButton); |
271 | 20 | buttonPanel.add(advancedButton); |
272 | |
|
273 | |
|
274 | |
|
275 | 20 | detailsPanel.add(new JLabel(""), labelConstraints); |
276 | 20 | detailsPanel.add(buttonPanel, fieldConstraints); |
277 | 20 | bsp.add(detailsPanel, BorderSplitPane.EAST); |
278 | |
|
279 | 20 | this.addListeners(); |
280 | 20 | this.enableFieldsAndButtons(); |
281 | |
|
282 | 20 | mainContent.add(bsp); |
283 | 20 | setResizable(true); |
284 | 20 | setContent(mainContent); |
285 | 20 | numCriticBrowser++; |
286 | 20 | } |
287 | |
|
288 | |
private void addListeners() { |
289 | 20 | goButton.addActionListener(this); |
290 | 20 | networkButton.addActionListener(this); |
291 | 20 | wakeButton.addActionListener(this); |
292 | 20 | advancedButton.addActionListener(this); |
293 | 20 | configButton.addActionListener(this); |
294 | 20 | headline.getDocument().addDocumentListener(this); |
295 | 20 | moreInfo.getDocument().addDocumentListener(this); |
296 | 20 | desc.getDocument().addDocumentListener(this); |
297 | 20 | priority.addItemListener(this); |
298 | 20 | useClar.addItemListener(this); |
299 | 20 | } |
300 | |
|
301 | |
private void enableFieldsAndButtons() { |
302 | 20 | className.setEditable(false); |
303 | 20 | headline.setEditable(false); |
304 | 20 | priority.setEnabled(false); |
305 | 20 | desc.setEditable(false); |
306 | 20 | moreInfo.setEditable(false); |
307 | |
|
308 | 20 | goButton.setEnabled(false); |
309 | 20 | wakeButton.setEnabled(false); |
310 | 20 | advancedButton.setEnabled(true); |
311 | 20 | networkButton.setEnabled(false); |
312 | 20 | configButton.setEnabled(false); |
313 | |
|
314 | 20 | useClar.setSelectedItem(null); |
315 | 20 | useClar.repaint(); |
316 | 20 | } |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
private void setTarget(Critic cr) { |
322 | 0 | if (cr == null) { |
323 | 0 | enableFieldsAndButtons(); |
324 | 0 | className.setText(""); |
325 | 0 | headline.setText(""); |
326 | 0 | priority.setSelectedItem(null); |
327 | 0 | priority.repaint(); |
328 | 0 | moreInfo.setText(""); |
329 | 0 | desc.setText(""); |
330 | 0 | return; |
331 | |
} |
332 | 0 | updateButtonsEnabled(); |
333 | 0 | className.setText(cr.getClass().getName()); |
334 | 0 | headline.setText(cr.getHeadline()); |
335 | |
|
336 | 0 | int p = cr.getPriority(); |
337 | 0 | if (p == ToDoItem.HIGH_PRIORITY) { |
338 | 0 | priority.setSelectedItem(HIGH); |
339 | 0 | } else if (p == ToDoItem.MED_PRIORITY) { |
340 | 0 | priority.setSelectedItem(MEDIUM); |
341 | |
} else { |
342 | 0 | priority.setSelectedItem(LOW); |
343 | |
} |
344 | 0 | priority.repaint(); |
345 | |
|
346 | 0 | moreInfo.setText(cr.getMoreInfoURL()); |
347 | 0 | desc.setText(cr.getDescriptionTemplate()); |
348 | 0 | desc.setCaretPosition(0); |
349 | 0 | useClar.setSelectedItem(ALWAYS); |
350 | 0 | useClar.repaint(); |
351 | 0 | } |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
protected void updateButtonsEnabled() { |
357 | 0 | this.configButton.setEnabled(false); |
358 | 0 | this.goButton.setEnabled(this.target != null |
359 | |
&& this.target.getMoreInfoURL() != null |
360 | |
&& this.target.getMoreInfoURL().length() > 0); |
361 | 0 | this.networkButton.setEnabled(false); |
362 | 0 | this.wakeButton.setEnabled(this.target != null |
363 | |
&& (this.target.isSnoozed() |
364 | |
|| !this.target.isEnabled())); |
365 | 0 | } |
366 | |
|
367 | |
private void setTargetHeadline() { |
368 | 0 | if (target == null) return; |
369 | 0 | String h = headline.getText(); |
370 | 0 | target.setHeadline(h); |
371 | 0 | } |
372 | |
|
373 | |
private void setTargetPriority() { |
374 | 0 | if (target == null) return; |
375 | 0 | String p = (String) priority.getSelectedItem(); |
376 | 0 | if (p == null) return; |
377 | 0 | if (p.equals(PRIORITIES[0])) |
378 | 0 | target.setPriority(ToDoItem.HIGH_PRIORITY); |
379 | 0 | if (p.equals(PRIORITIES[1])) |
380 | 0 | target.setPriority(ToDoItem.MED_PRIORITY); |
381 | 0 | if (p.equals(PRIORITIES[2])) |
382 | 0 | target.setPriority(ToDoItem.LOW_PRIORITY); |
383 | 0 | } |
384 | |
|
385 | |
private void setTargetMoreInfo() { |
386 | 0 | if (target == null) return; |
387 | 0 | String mi = moreInfo.getText(); |
388 | 0 | target.setMoreInfoURL(mi); |
389 | 0 | } |
390 | |
|
391 | |
private void setTargetDesc() { |
392 | 0 | if (target == null) return; |
393 | 0 | String d = desc.getText(); |
394 | 0 | target.setDescription(d); |
395 | 0 | } |
396 | |
|
397 | |
private void setTargetUseClarifiers() { |
398 | 20 | LOG.debug("setting clarifier usage rule"); |
399 | 20 | } |
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
public void actionPerformed(ActionEvent e) { |
409 | 20 | super.actionPerformed(e); |
410 | 20 | if (e.getSource() == goButton) { |
411 | 0 | StartBrowser.openUrl(moreInfo.getText()); |
412 | 0 | return; |
413 | |
} |
414 | 20 | if (e.getSource() == networkButton) { |
415 | 0 | LOG.debug("TODO: network!"); |
416 | 0 | return; |
417 | |
} |
418 | 20 | if (e.getSource() == configButton) { |
419 | 0 | LOG.debug("TODO: config!"); |
420 | 0 | return; |
421 | |
} |
422 | 20 | if (e.getSource() == wakeButton) { |
423 | 0 | target.unsnooze(); |
424 | 0 | target.setEnabled(true); |
425 | 0 | table.repaint(); |
426 | 0 | return; |
427 | |
} |
428 | 20 | if (e.getSource() == advancedButton) { |
429 | 0 | table.setAdvanced(true); |
430 | 0 | advancedButton.setEnabled(false); |
431 | |
} |
432 | 20 | LOG.debug("unknown src in CriticBrowserDialog: " + e.getSource()); |
433 | 20 | } |
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
public void valueChanged(ListSelectionEvent lse) { |
439 | 0 | if (lse.getValueIsAdjusting()) return; |
440 | 0 | Object src = lse.getSource(); |
441 | 0 | if (src != table.getSelectionModel()) { |
442 | 0 | LOG.debug("src = " + src); |
443 | 0 | return; |
444 | |
} |
445 | 0 | LOG.debug("got valueChanged from " + src); |
446 | 0 | int row = table.getSelectedRow(); |
447 | 0 | if (this.target != null) { |
448 | 0 | this.target.deleteObserver(this); |
449 | |
} |
450 | 0 | setTarget((row == -1) ? null : table.getCriticAtRow(row)); |
451 | 0 | if (this.target != null) { |
452 | 0 | this.target.addObserver(this); |
453 | |
} |
454 | 0 | } |
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
public void tableChanged(TableModelEvent e) { |
462 | 0 | updateButtonsEnabled(); |
463 | 0 | table.repaint(); |
464 | 0 | } |
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
public void insertUpdate(DocumentEvent e) { |
470 | 0 | LOG.debug(getClass().getName() + " insert"); |
471 | 0 | Document hDoc = headline.getDocument(); |
472 | 0 | Document miDoc = moreInfo.getDocument(); |
473 | 0 | Document dDoc = desc.getDocument(); |
474 | 0 | if (e.getDocument() == hDoc) setTargetHeadline(); |
475 | 0 | if (e.getDocument() == miDoc) setTargetMoreInfo(); |
476 | 0 | if (e.getDocument() == dDoc) setTargetDesc(); |
477 | 0 | } |
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | 0 | public void removeUpdate(DocumentEvent e) { insertUpdate(e); } |
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
public void changedUpdate(DocumentEvent e) { |
488 | 0 | LOG.debug(getClass().getName() + " changed"); |
489 | |
|
490 | 0 | } |
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
public void itemStateChanged(ItemEvent e) { |
496 | 20 | Object src = e.getSource(); |
497 | 20 | if (src == priority) { |
498 | 0 | setTargetPriority(); |
499 | |
} |
500 | 20 | else if (src == useClar) { |
501 | 20 | setTargetUseClarifiers(); |
502 | |
} else { |
503 | 0 | LOG.debug("unknown itemStateChanged src: " + src); |
504 | |
} |
505 | 20 | } |
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
public void update(Observable o, Object arg) { |
513 | 0 | table.repaint(); |
514 | 0 | } |
515 | |
|
516 | |
} |