001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMDException;
004: import org.acm.seguin.pmd.Rule;
005: import org.acm.seguin.pmd.RuleSet;
006: import org.acm.seguin.pmd.swingui.event.ListenerList;
007: import org.acm.seguin.pmd.swingui.event.RuleSetChangedEvent;
008: import org.acm.seguin.pmd.swingui.event.RuleSetEvent;
009: import org.acm.seguin.pmd.swingui.event.RulesInMemoryEvent;
010: import org.acm.seguin.pmd.swingui.event.RulesInMemoryEventListener;
011: import org.acm.seguin.pmd.swingui.event.RulesTreeModelEvent;
012: import org.acm.seguin.pmd.swingui.event.RulesTreeModelEventListener;
013:
014: import javax.swing.Icon;
015: import javax.swing.JMenu;
016: import javax.swing.JMenuBar;
017: import javax.swing.JMenuItem;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.JSeparator;
021: import javax.swing.JSplitPane;
022: import javax.swing.KeyStroke;
023: import javax.swing.UIManager;
024: import java.awt.BorderLayout;
025: import java.awt.Color;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029: import java.awt.event.MouseAdapter;
030: import java.awt.event.MouseEvent;
031: import java.util.ArrayList;
032: import java.util.Enumeration;
033: import java.util.List;
034:
035: /**
036: *
037: * @author Donald A. Leckie
038: * @since August 29, 2002
039: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
040: */
041: class RulesEditor extends JPanel {
042:
043: private RulesTree m_tree;
044: private JSplitPane m_splitPane;
045: private RuleEditingTabbedPane m_editingTabbedPane;
046: private JMenuBar m_menuBar;
047: private JMenuItem m_printSelectedRuleMenuItem;
048:
049: /**
050: *******************************************************************************
051: *
052: * @param parentWindow
053: */
054: protected RulesEditor() throws PMDException {
055: super (new BorderLayout());
056:
057: m_tree = new RulesTree();
058:
059: JScrollPane treeScrollPane;
060:
061: treeScrollPane = createTreeScrollPane();
062: m_editingTabbedPane = new RuleEditingTabbedPane(m_tree);
063: m_splitPane = ComponentFactory.createHorizontalSplitPane(
064: treeScrollPane, m_editingTabbedPane);
065:
066: JPanel contentPanel = new JPanel(new BorderLayout());
067: contentPanel.add(m_splitPane, BorderLayout.CENTER);
068: add(contentPanel, BorderLayout.CENTER);
069:
070: createMenuBar();
071:
072: ListenerList
073: .addListener((RulesInMemoryEventListener) new RulesInMemoryEventHandler());
074: }
075:
076: /**
077: *******************************************************************************
078: *
079: * @return
080: */
081: private JScrollPane createTreeScrollPane() {
082: JScrollPane scrollPane = ComponentFactory
083: .createScrollPane(m_tree);
084: Color background = UIManager.getColor("pmdTreeBackground");
085: scrollPane.getViewport().setBackground(background);
086:
087: return scrollPane;
088: }
089:
090: /**
091: *********************************************************************************
092: *
093: */
094: private void createMenuBar() {
095: m_menuBar = new JMenuBar();
096: m_menuBar.add(new FileMenu());
097: m_menuBar.add(new HelpMenu());
098: }
099:
100: /**
101: *********************************************************************************
102: *
103: */
104: public void adjustSplitPaneDividerLocation() {
105: m_splitPane.setDividerLocation(0.4);
106: }
107:
108: /**
109: *********************************************************************************
110: *
111: */
112: protected void setMenuBar() {
113: PMDViewer.getViewer().setJMenuBar(m_menuBar);
114: }
115:
116: /**
117: ********************************************************************
118: *
119: */
120: protected void saveData() {
121: m_editingTabbedPane.saveData();
122: saveData((RulesTreeNode) m_tree.getModel().getRoot());
123: }
124:
125: /**
126: ********************************************************************
127: *
128: * @param treeNode
129: */
130: private void saveData(RulesTreeNode treeNode) {
131: treeNode.saveData();
132:
133: Enumeration children = treeNode.children();
134:
135: while (children.hasMoreElements()) {
136: saveData((RulesTreeNode) children.nextElement());
137: }
138: }
139:
140: /**
141: *******************************************************************************
142: *******************************************************************************
143: *******************************************************************************
144: */
145: private class SaveActionListener implements ActionListener {
146:
147: /**
148: ********************************************************************
149: *
150: * @param event
151: */
152: public void actionPerformed(ActionEvent event) {
153: saveData();
154: writeRuleSets((RulesTreeNode) m_tree.getModel().getRoot());
155: RuleSetChangedEvent.notifyRuleSetsChanged(this );
156: }
157:
158: /**
159: ********************************************************************
160: *
161: * @param treeNode
162: */
163: private void writeRuleSets(RulesTreeNode rootNode) {
164: List ruleSetList = new ArrayList();
165: Enumeration ruleSetNodes = rootNode.children();
166:
167: while (ruleSetNodes.hasMoreElements()) {
168: RulesTreeNode ruleSetNode = (RulesTreeNode) ruleSetNodes
169: .nextElement();
170: RuleSet ruleSet = ruleSetNode.getRuleSet();
171: Enumeration ruleNodes = ruleSetNode.children();
172:
173: ruleSetList.add(ruleSet);
174: ruleSet.getRules().clear();
175:
176: while (ruleNodes.hasMoreElements()) {
177: RulesTreeNode ruleNode = (RulesTreeNode) ruleNodes
178: .nextElement();
179:
180: ruleSet.addRule(ruleNode.getRule());
181: }
182: }
183:
184: RuleSetEvent.notifySaveRuleSets(this , ruleSetList);
185: }
186: }
187:
188: /**
189: *******************************************************************************
190: *******************************************************************************
191: *******************************************************************************
192: */
193: private class CancelActionListener implements ActionListener {
194:
195: /**
196: ********************************************************************
197: *
198: * @param event
199: */
200: public void actionPerformed(ActionEvent event) {
201: RulesEditor.this .setVisible(false);
202: }
203: }
204:
205: /**
206: *********************************************************************************
207: *********************************************************************************
208: *********************************************************************************
209: */
210: private class FileMenu extends JMenu {
211:
212: /**
213: ********************************************************************
214: *
215: * @param menuBar
216: */
217: private FileMenu() {
218: super ("File");
219:
220: setMnemonic('F');
221:
222: Icon icon;
223: JMenuItem menuItem;
224:
225: //
226: // Save menu item
227: //
228: icon = UIManager.getIcon("save");
229: menuItem = new JMenuItem("Save Changes", icon);
230: menuItem
231: .addActionListener((ActionListener) new SaveActionListener());
232: menuItem.setMnemonic('S');
233: menuItem.setAccelerator(KeyStroke.getKeyStroke(
234: KeyEvent.VK_S, KeyEvent.CTRL_MASK));
235: add(menuItem);
236:
237: //
238: // Save As menu item
239: //
240: icon = UIManager.getIcon("cancel");
241: menuItem = new JMenuItem("Cancel Changes", icon);
242: menuItem
243: .addActionListener((ActionListener) new CancelActionListener());
244: menuItem.setMnemonic('C');
245: menuItem.setAccelerator(KeyStroke.getKeyStroke(
246: KeyEvent.VK_C, KeyEvent.CTRL_MASK));
247: add(menuItem);
248:
249: //
250: // Separator
251: //
252: add(new JSeparator());
253:
254: //
255: // Print Rules menu item
256: //
257: icon = UIManager.getIcon("print");
258: menuItem = new JMenuItem("Print Rules...", icon);
259: menuItem
260: .addActionListener((ActionListener) new PrintRulesActionListener());
261: menuItem.setMnemonic('R');
262: menuItem.setAccelerator(KeyStroke.getKeyStroke(
263: KeyEvent.VK_R, KeyEvent.CTRL_MASK));
264: add(menuItem);
265:
266: //
267: // Print Selected Rule menu item
268: //
269: icon = UIManager.getIcon("print");
270: m_printSelectedRuleMenuItem = new JMenuItem(
271: "Print Selected Rule...", icon);
272: m_printSelectedRuleMenuItem
273: .addActionListener((ActionListener) new PrintRulesActionListener());
274: m_printSelectedRuleMenuItem.setMnemonic('E');
275: m_printSelectedRuleMenuItem.setAccelerator(KeyStroke
276: .getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK));
277: add(m_printSelectedRuleMenuItem);
278:
279: //
280: // Page Setup menu item
281: //
282: menuItem = new JMenuItem("Page Setup...");
283: menuItem
284: .addActionListener((ActionListener) new PageSetupActionListener());
285: menuItem.setMnemonic('A');
286: menuItem.setAccelerator(KeyStroke.getKeyStroke(
287: KeyEvent.VK_A, KeyEvent.CTRL_MASK));
288: add(menuItem);
289:
290: //
291: // Separator
292: //
293: add(new JSeparator());
294:
295: //
296: // Exit menu item
297: //
298: menuItem = new JMenuItem("Exit...");
299: menuItem
300: .addActionListener((ActionListener) new ExitActionListener());
301: menuItem.setMnemonic('X');
302: add(menuItem);
303:
304: addMouseListener(new FileMenuMouseListener());
305: }
306: }
307:
308: /**
309: *********************************************************************************
310: *********************************************************************************
311: *********************************************************************************
312: */
313: private class PrintRulesActionListener implements ActionListener {
314:
315: public void actionPerformed(ActionEvent event) {
316: MessageDialog.show(PMDViewer.getViewer(),
317: "Printing not available yet.");
318: }
319: }
320:
321: /**
322: *********************************************************************************
323: *********************************************************************************
324: *********************************************************************************
325: */
326: private class PageSetupActionListener implements ActionListener {
327:
328: public void actionPerformed(ActionEvent event) {
329: MessageDialog.show(PMDViewer.getViewer(),
330: "Page setup not available yet.");
331: }
332: }
333:
334: /**
335: *********************************************************************************
336: *********************************************************************************
337: *********************************************************************************
338: */
339: private class ExitActionListener implements ActionListener {
340:
341: public void actionPerformed(ActionEvent event) {
342: System.exit(0);
343: }
344: }
345:
346: /**
347: *********************************************************************************
348: *********************************************************************************
349: *********************************************************************************
350: */
351: private class FileMenuMouseListener extends MouseAdapter implements
352: RulesTreeModelEventListener {
353:
354: private Rule m_rule;
355:
356: /**
357: *****************************************************************************
358: *
359: * @param event
360: */
361: public void mouseEntered(MouseEvent event) {
362: try {
363: ListenerList
364: .addListener((RulesTreeModelEventListener) this );
365: RulesTreeModelEvent.notifyRequestSelectedRule(this );
366: boolean enable = (m_rule != null);
367: m_printSelectedRuleMenuItem.setEnabled(enable);
368: } finally {
369: ListenerList
370: .removeListener((RulesTreeModelEventListener) this );
371: }
372: }
373:
374: /**
375: ****************************************************************************
376: *
377: * @param event
378: */
379: public void reload(RulesTreeModelEvent event) {
380: }
381:
382: /**
383: ****************************************************************************
384: *
385: * @param event
386: */
387: public void requestSelectedRule(RulesTreeModelEvent event) {
388: }
389:
390: /**
391: ****************************************************************************
392: *
393: * @param event
394: */
395: public void returnedSelectedRule(RulesTreeModelEvent event) {
396: m_rule = event.getRule();
397: }
398: }
399:
400: /**
401: *******************************************************************************
402: *******************************************************************************
403: *******************************************************************************
404: */
405: private class RulesInMemoryEventHandler implements
406: RulesInMemoryEventListener {
407:
408: /**
409: ****************************************************************************
410: *
411: * @param event
412: */
413: public void requestAllRules(RulesInMemoryEvent event) {
414: RuleSet rules = new RuleSet();
415: RulesTreeNode rootNode = (RulesTreeNode) m_tree.getModel()
416: .getRoot();
417: getRules(rootNode, rules, new IncludeAllRuleFilter());
418: RulesInMemoryEvent.notifyReturnedRules(this , rules);
419: }
420:
421: /**
422: ****************************************************************************
423: *
424: * @param event
425: */
426: public void requestIncludedRules(RulesInMemoryEvent event) {
427: try {
428: RuleSet rules = new RuleSet();
429: RulesTreeNode rootNode = (RulesTreeNode) m_tree
430: .getModel().getRoot();
431: int lowestPriority = Preferences.getPreferences()
432: .getLowestPriorityForAnalysis();
433: getRules(rootNode, rules,
434: new IncludeSelectedRuleFilter(lowestPriority));
435: RulesInMemoryEvent.notifyReturnedRules(this , rules);
436: } catch (PMDException pmdException) {
437: String message = pmdException.getMessage();
438: Exception exception = pmdException.getReason();
439: MessageDialog.show(PMDViewer.getViewer(), message,
440: exception);
441: }
442: }
443:
444: /**
445: ****************************************************************************
446: *
447: * @param event
448: */
449: private void getRules(RulesTreeNode rootNode, RuleSet rules,
450: RuleFilter ruleFilter) {
451: Enumeration ruleSetNodes = rootNode.children();
452:
453: while (ruleSetNodes.hasMoreElements()) {
454: RulesTreeNode ruleSetNode = (RulesTreeNode) ruleSetNodes
455: .nextElement();
456:
457: if (ruleFilter.include(ruleSetNode)) {
458: Enumeration ruleNodes = ruleSetNode.children();
459:
460: while (ruleNodes.hasMoreElements()) {
461: RulesTreeNode ruleNode = (RulesTreeNode) ruleNodes
462: .nextElement();
463:
464: if (ruleFilter.include(ruleNode)) {
465: rules.addRule(ruleNode.getRule());
466: }
467: }
468: }
469: }
470: }
471:
472: /**
473: ****************************************************************************
474: *
475: * @param event
476: */
477: public void returnedRules(RulesInMemoryEvent event) {
478: }
479: }
480:
481: /**
482: *******************************************************************************
483: *******************************************************************************
484: *******************************************************************************
485: */
486: private abstract class RuleFilter {
487:
488: /**
489: ***************************************************************************
490: *
491: * @param rule
492: *
493: * @return
494: */
495: protected abstract boolean include(RulesTreeNode treeNode);
496: }
497:
498: /**
499: *******************************************************************************
500: *******************************************************************************
501: *******************************************************************************
502: */
503: private class IncludeAllRuleFilter extends RuleFilter {
504:
505: /**
506: ***************************************************************************
507: *
508: * @param rule
509: *
510: * @return
511: */
512: protected boolean include(RulesTreeNode treeNode) {
513: return true;
514: }
515: }
516:
517: /**
518: *******************************************************************************
519: *******************************************************************************
520: *******************************************************************************
521: */
522: private class IncludeSelectedRuleFilter extends RuleFilter {
523: private int m_lowestPriority;
524:
525: /**
526: ***************************************************************************
527: *
528: * @param rule
529: *
530: * @return
531: */
532: private IncludeSelectedRuleFilter(int lowestPriority) {
533: m_lowestPriority = lowestPriority;
534: }
535:
536: /**
537: ***************************************************************************
538: *
539: * @param rule
540: *
541: * @return
542: */
543: protected boolean include(RulesTreeNode treeNode) {
544: return treeNode.include()
545: && (treeNode.getPriority() <= m_lowestPriority);
546: }
547: }
548: }
|