001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMDException;
004: import org.acm.seguin.pmd.RuleSet;
005: import org.acm.seguin.pmd.swingui.event.DirectoryTableEvent;
006: import org.acm.seguin.pmd.swingui.event.DirectoryTableEventListener;
007: import org.acm.seguin.pmd.swingui.event.HTMLAnalysisResultsEvent;
008: import org.acm.seguin.pmd.swingui.event.HTMLAnalysisResultsEventListener;
009: import org.acm.seguin.pmd.swingui.event.ListenerList;
010: import org.acm.seguin.pmd.swingui.event.TextAnalysisResultsEvent;
011: import org.acm.seguin.pmd.swingui.event.TextAnalysisResultsEventListener;
012:
013: import javax.swing.Icon;
014: import javax.swing.JButton;
015: import javax.swing.JFileChooser;
016: import javax.swing.JMenu;
017: import javax.swing.JMenuBar;
018: import javax.swing.JMenuItem;
019: import javax.swing.JPanel;
020: import javax.swing.JScrollPane;
021: import javax.swing.JSeparator;
022: import javax.swing.JSplitPane;
023: import javax.swing.KeyStroke;
024: import javax.swing.UIManager;
025: import javax.swing.border.CompoundBorder;
026: import javax.swing.border.EmptyBorder;
027: import javax.swing.border.EtchedBorder;
028: import javax.swing.border.TitledBorder;
029: import javax.swing.filechooser.FileFilter;
030: import java.awt.BorderLayout;
031: import java.awt.Color;
032: import java.awt.Font;
033: import java.awt.Toolkit;
034: import java.awt.datatransfer.Clipboard;
035: import java.awt.datatransfer.ClipboardOwner;
036: import java.awt.datatransfer.StringSelection;
037: import java.awt.datatransfer.Transferable;
038: import java.awt.event.ActionEvent;
039: import java.awt.event.ActionListener;
040: import java.awt.event.KeyEvent;
041: import java.awt.event.MouseAdapter;
042: import java.awt.event.MouseEvent;
043: import java.io.File;
044: import java.io.FileWriter;
045: import java.io.IOException;
046: import java.util.Enumeration;
047:
048: /**
049: *
050: * @author Donald A. Leckie
051: * @since January 6, 2003
052: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
053: */
054: class SearchViewer extends JPanel {
055:
056: private DirectoryTree m_directoryTree;
057: private StatusBar m_statusBar;
058: private JPanel m_directoryTreePanel;
059: private RulesTree m_rulesTree;
060: private JPanel m_rulesTreePanel;
061: private JSplitPane m_directoryRuleSplitPane;
062: private SearchResultsViewer m_resultsViewer;
063: private JPanel m_resultsViewerPanel;
064: private JSplitPane m_mainSplitPane;
065: private JButton m_searchButton;
066: private JPanel m_buttonPanel;
067: private JMenuBar m_menuBar;
068: private PMDClipboard m_clipboardOwner = new PMDClipboard();
069: private JMenuItem m_saveMenuItem;
070: private JMenuItem m_saveAsMenuItem;
071: private JMenuItem m_printAnalysisMenuItem;
072: private JMenuItem m_copyHTMLResultsMenuItem;
073: private JMenuItem m_copyTextResultsMenuItem;
074:
075: /**
076: **********************************************************************************
077: *
078: */
079: protected SearchViewer() throws PMDException {
080: super (new BorderLayout());
081:
082: createStatusBar();
083: createDirectoryTreePanel();
084: createRulesTreePanel();
085: createDirectoryRuleSplitPane();
086: createResultsViewer();
087: createResultsViewerScrollPane();
088: createMainSplitPane();
089: createButtonPanel();
090: createMenuBar();
091: setMenuBar();
092: add(createContentPanel(10));
093: }
094:
095: /**
096: *********************************************************************************
097: *
098: * @param windowMargin
099: */
100: private void createStatusBar() {
101: String defaultMessage;
102: CompoundBorder compoundBorder;
103: EmptyBorder emptyBorder;
104:
105: defaultMessage = "Select a source file directory and a rule to view the analysis below.";
106: m_statusBar = new StatusBar(defaultMessage);
107: emptyBorder = new EmptyBorder(0, 0, 5, 0);
108: compoundBorder = new CompoundBorder(emptyBorder, m_statusBar
109: .getBorder());
110: m_statusBar.setBorder(compoundBorder);
111: }
112:
113: /**
114: *********************************************************************************
115: *
116: */
117: private void createDirectoryTreePanel() {
118: Color background;
119: JScrollPane scrollPane;
120:
121: m_directoryTree = new DirectoryTree("File Directories");
122: scrollPane = ComponentFactory.createScrollPane(m_directoryTree);
123: background = UIManager.getColor("pmdTreeBackground");
124: scrollPane.getViewport().setBackground(background);
125: m_directoryTreePanel = new JPanel(new BorderLayout());
126: m_directoryTreePanel
127: .setBorder(createTitledBorder(" Directory "));
128: m_directoryTreePanel.add(scrollPane, BorderLayout.CENTER);
129: }
130:
131: /**
132: *********************************************************************************
133: *
134: */
135: private void createRulesTreePanel() throws PMDException {
136: Color background;
137: JScrollPane scrollPane;
138:
139: m_rulesTree = new RulesTree();
140: m_rulesTree.setDisablePopupMenu(true);
141: m_rulesTree.setDisableEditing(true);
142: m_rulesTree.expandNode(m_rulesTree.getRootNode());
143: scrollPane = ComponentFactory.createScrollPane(m_rulesTree);
144: background = UIManager.getColor("pmdTableBackground");
145: scrollPane.getViewport().setBackground(background);
146: m_rulesTreePanel = new JPanel(new BorderLayout());
147: m_rulesTreePanel.setBorder(createTitledBorder(" Rule "));
148: m_rulesTreePanel.add(scrollPane, BorderLayout.CENTER);
149: }
150:
151: /**
152: *********************************************************************************
153: *
154: */
155: private void createDirectoryRuleSplitPane() {
156: m_directoryRuleSplitPane = ComponentFactory
157: .createHorizontalSplitPane(m_directoryTreePanel,
158: m_rulesTreePanel);
159: }
160:
161: /**
162: *********************************************************************************
163: *
164: */
165: private void createResultsViewer() {
166: m_resultsViewer = new SearchResultsViewer();
167: m_resultsViewer.setSelectionColor(Color.blue);
168: }
169:
170: /**
171: *********************************************************************************
172: *
173: */
174: private void createResultsViewerScrollPane() {
175: JScrollPane scrollPane;
176:
177: scrollPane = ComponentFactory.createScrollPane(m_resultsViewer);
178: m_resultsViewerPanel = new JPanel(new BorderLayout());
179: m_resultsViewerPanel
180: .setBorder(createTitledBorder(" Search Results "));
181: m_resultsViewerPanel.add(scrollPane, BorderLayout.CENTER);
182: }
183:
184: /**
185: *********************************************************************************
186: *
187: */
188: private void createMainSplitPane() {
189: m_mainSplitPane = ComponentFactory.createVerticalSplitPane(
190: m_directoryRuleSplitPane, m_resultsViewerPanel);
191: }
192:
193: /**
194: ********************************************************************************
195: *
196: */
197: private void createButtonPanel() {
198: Color background;
199:
200: m_buttonPanel = ComponentFactory.createButtonPanel();
201: background = UIManager.getColor("pmdBlue");
202: m_searchButton = ComponentFactory.createButton("Search",
203: background, Color.white);
204: m_searchButton
205: .addActionListener(new SearchButtonActionEventHandler());
206: m_buttonPanel.add(m_searchButton);
207: }
208:
209: /**
210: *********************************************************************************
211: *
212: * @param margin
213: */
214: private JPanel createContentPanel(int margin) {
215: JPanel contentPanel = new JPanel(new BorderLayout());
216:
217: EtchedBorder outsideBorder = new EtchedBorder(
218: EtchedBorder.RAISED);
219: EmptyBorder insideBorder = new EmptyBorder(margin, margin,
220: margin, margin);
221: CompoundBorder compoundBorder = new CompoundBorder(
222: outsideBorder, insideBorder);
223:
224: contentPanel.setBorder(compoundBorder);
225: contentPanel.add(m_statusBar, BorderLayout.NORTH);
226: contentPanel.add(m_mainSplitPane, BorderLayout.CENTER);
227: contentPanel.add(m_buttonPanel, BorderLayout.SOUTH);
228:
229: return contentPanel;
230: }
231:
232: /**
233: *********************************************************************************
234: *
235: * @param title
236: */
237: private TitledBorder createTitledBorder(String title) {
238: EtchedBorder etchedBorder;
239: TitledBorder titledBorder;
240: Font font;
241:
242: etchedBorder = new EtchedBorder(EtchedBorder.RAISED);
243: titledBorder = new TitledBorder(etchedBorder, title);
244: font = titledBorder.getTitleFont();
245: font = new Font(font.getName(), Font.BOLD, font.getSize());
246: titledBorder.setTitleFont(font);
247:
248: return titledBorder;
249: }
250:
251: /**
252: *********************************************************************************
253: *
254: */
255: protected void adjustSplitPaneDividerLocation() {
256: m_mainSplitPane.setDividerLocation(0.4);
257: m_directoryRuleSplitPane.setDividerLocation(0.5);
258: }
259:
260: /**
261: *********************************************************************************
262: *
263: */
264: private void createMenuBar() {
265: m_menuBar = new JMenuBar();
266:
267: m_menuBar.add(new FileMenu());
268: m_menuBar.add(new EditMenu());
269: m_menuBar.add(new HelpMenu());
270: }
271:
272: /**
273: *********************************************************************************
274: *
275: */
276: protected void setMenuBar() {
277: PMDViewer.getViewer().setJMenuBar(m_menuBar);
278: }
279:
280: /**
281: ********************************************************************************
282: *
283: */
284: protected void analyze() {
285: m_resultsViewer.analyze();
286: }
287:
288: /**
289: *********************************************************************************
290: *********************************************************************************
291: *********************************************************************************
292: */
293: private class PrintAnalysisActionListener implements ActionListener {
294:
295: public void actionPerformed(ActionEvent event) {
296: (new PrintAnalysisResults()).print();
297: }
298: }
299:
300: /**
301: *********************************************************************************
302: *********************************************************************************
303: *********************************************************************************
304: */
305: private class ExitActionListener implements ActionListener {
306:
307: public void actionPerformed(ActionEvent event) {
308: System.exit(0);
309: }
310: }
311:
312: /**
313: *********************************************************************************
314: *********************************************************************************
315: *********************************************************************************
316: */
317: private class CopyHTMLResultsActionListener implements
318: ActionListener, HTMLAnalysisResultsEventListener {
319: private String m_htmlText;
320:
321: /**
322: *****************************************************************************
323: *
324: * @param event
325: */
326: public void actionPerformed(ActionEvent event) {
327: try {
328: ListenerList
329: .addListener((HTMLAnalysisResultsEventListener) this );
330: HTMLAnalysisResultsEvent.notifyRequestHTMLText(this );
331:
332: if ((m_htmlText != null) && (m_htmlText.length() > 0)) {
333: Clipboard clipboard = Toolkit.getDefaultToolkit()
334: .getSystemClipboard();
335: StringSelection contents = new StringSelection(
336: m_htmlText);
337: clipboard.setContents(contents, m_clipboardOwner);
338: }
339: } finally {
340: ListenerList
341: .removeListener((HTMLAnalysisResultsEventListener) this );
342: }
343: }
344:
345: /**
346: ******************************************************************************
347: *
348: * @param event
349: */
350: public void requestHTMLAnalysisResults(
351: HTMLAnalysisResultsEvent event) {
352: }
353:
354: /**
355: ******************************************************************************
356: *
357: * @param event
358: */
359: public void returnedHTMLAnalysisResults(
360: HTMLAnalysisResultsEvent event) {
361: m_htmlText = event.getHTMLText();
362: }
363: }
364:
365: /**
366: *********************************************************************************
367: *********************************************************************************
368: *********************************************************************************
369: */
370: private class CopyTextResultsActionListener implements
371: ActionListener, TextAnalysisResultsEventListener {
372: private String m_text;
373:
374: public void actionPerformed(ActionEvent event) {
375: try {
376: ListenerList
377: .addListener((TextAnalysisResultsEventListener) this );
378: TextAnalysisResultsEvent.notifyRequestText(this );
379:
380: if ((m_text != null) && (m_text.length() > 0)) {
381: Clipboard clipboard = Toolkit.getDefaultToolkit()
382: .getSystemClipboard();
383: StringSelection contents = new StringSelection(
384: m_text);
385: clipboard.setContents(contents, m_clipboardOwner);
386: }
387: } finally {
388: ListenerList
389: .removeListener((TextAnalysisResultsEventListener) this );
390: }
391: }
392:
393: /**
394: ****************************************************************************
395: *
396: * @param event
397: */
398: public void requestTextAnalysisResults(
399: TextAnalysisResultsEvent event) {
400: }
401:
402: /**
403: ****************************************************************************
404: *
405: * @param event
406: */
407: public void returnedTextAnalysisResults(
408: TextAnalysisResultsEvent event) {
409: m_text = event.getText();
410: }
411: }
412:
413: /**
414: *********************************************************************************
415: *********************************************************************************
416: *********************************************************************************
417: */
418: private class FileMenuMouseListener extends MouseAdapter implements
419: HTMLAnalysisResultsEventListener {
420:
421: private String m_htmlText;
422:
423: /**
424: *****************************************************************************
425: *
426: * @param event
427: */
428: public void mouseEntered(MouseEvent event) {
429: try {
430: ListenerList
431: .addListener((HTMLAnalysisResultsEventListener) this );
432: HTMLAnalysisResultsEvent.notifyRequestHTMLText(this );
433: boolean enable = (m_htmlText.length() > 0);
434: m_saveMenuItem.setEnabled(enable);
435: m_saveAsMenuItem.setEnabled(enable);
436: m_printAnalysisMenuItem.setEnabled(enable);
437: } finally {
438: ListenerList
439: .removeListener((HTMLAnalysisResultsEventListener) this );
440: }
441: }
442:
443: /**
444: ****************************************************************************
445: *
446: * @param event
447: */
448: public void requestHTMLAnalysisResults(
449: HTMLAnalysisResultsEvent event) {
450: }
451:
452: /**
453: ****************************************************************************
454: *
455: * @param event
456: */
457: public void returnedHTMLAnalysisResults(
458: HTMLAnalysisResultsEvent event) {
459: m_htmlText = event.getHTMLText();
460: }
461: }
462:
463: /**
464: *********************************************************************************
465: *********************************************************************************
466: *********************************************************************************
467: */
468: private class EditMenuMouseListener extends MouseAdapter implements
469: HTMLAnalysisResultsEventListener {
470:
471: String m_htmlText;
472:
473: /**
474: *****************************************************************************
475: *
476: * @param event
477: */
478: public void mouseEntered(MouseEvent event) {
479: try {
480: ListenerList
481: .addListener((HTMLAnalysisResultsEventListener) this );
482: HTMLAnalysisResultsEvent.notifyRequestHTMLText(this );
483: boolean enable = (m_htmlText.length() > 0);
484: m_copyHTMLResultsMenuItem.setEnabled(enable);
485: m_copyTextResultsMenuItem.setEnabled(enable);
486: } finally {
487: ListenerList
488: .removeListener((HTMLAnalysisResultsEventListener) this );
489: }
490: }
491:
492: /**
493: ****************************************************************************
494: *
495: * @param event
496: */
497: public void requestHTMLAnalysisResults(
498: HTMLAnalysisResultsEvent event) {
499: }
500:
501: /**
502: ****************************************************************************
503: *
504: * @param event
505: */
506: public void returnedHTMLAnalysisResults(
507: HTMLAnalysisResultsEvent event) {
508: m_htmlText = event.getHTMLText();
509: }
510: }
511:
512: /**
513: *********************************************************************************
514: *********************************************************************************
515: *********************************************************************************
516: */
517: private class FileMenu extends JMenu {
518:
519: /**
520: ********************************************************************
521: *
522: */
523: private FileMenu() {
524: super ("File");
525:
526: setMnemonic('F');
527:
528: Icon icon;
529: JMenuItem menuItem;
530:
531: //
532: // Save menu item
533: //
534: icon = UIManager.getIcon("save");
535: m_saveMenuItem = new JMenuItem("Save Search Results", icon);
536: m_saveMenuItem
537: .addActionListener((ActionListener) new SaveActionListener());
538: m_saveMenuItem.setMnemonic('S');
539: m_saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(
540: KeyEvent.VK_S, KeyEvent.CTRL_MASK));
541: m_saveMenuItem.setDisabledIcon(icon);
542: add(m_saveMenuItem);
543:
544: //
545: // Save As menu item
546: //
547: icon = UIManager.getIcon("saveAs");
548: m_saveAsMenuItem = new JMenuItem(
549: "Save Search Results As...", icon);
550: m_saveAsMenuItem
551: .addActionListener((ActionListener) new SaveAsActionListener());
552: m_saveAsMenuItem.setMnemonic('A');
553: m_saveAsMenuItem.setAccelerator(KeyStroke.getKeyStroke(
554: KeyEvent.VK_A, KeyEvent.CTRL_MASK));
555: m_saveAsMenuItem.setDisabledIcon(icon);
556: add(m_saveAsMenuItem);
557:
558: //
559: // Separator
560: //
561: add(new JSeparator());
562:
563: //
564: // Print Analysis menu item
565: //
566: icon = UIManager.getIcon("print");
567: m_printAnalysisMenuItem = new JMenuItem(
568: "Print Search Results...", icon);
569: m_printAnalysisMenuItem
570: .addActionListener((ActionListener) new PrintAnalysisActionListener());
571: m_printAnalysisMenuItem.setMnemonic('P');
572: m_printAnalysisMenuItem.setAccelerator(KeyStroke
573: .getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK));
574: m_printAnalysisMenuItem.setDisabledIcon(icon);
575: add(m_printAnalysisMenuItem);
576:
577: //
578: // Separator
579: //
580: add(new JSeparator());
581:
582: //
583: // Exit menu item
584: //
585: menuItem = new JMenuItem("Exit...");
586: menuItem
587: .addActionListener((ActionListener) new ExitActionListener());
588: menuItem.setMnemonic('X');
589: add(menuItem);
590:
591: addMouseListener(new FileMenuMouseListener());
592: }
593: }
594:
595: /**
596: *********************************************************************************
597: *********************************************************************************
598: *********************************************************************************
599: */
600: private class EditMenu extends JMenu {
601:
602: /**
603: ********************************************************************
604: *
605: */
606: private EditMenu() {
607: super ("Edit");
608:
609: setMnemonic('E');
610:
611: Icon icon;
612:
613: //
614: // Copy Results menu item
615: //
616: icon = UIManager.getIcon("copy");
617: m_copyHTMLResultsMenuItem = new JMenuItem(
618: "Copy Results Results as HTML", icon);
619: m_copyHTMLResultsMenuItem
620: .addActionListener((ActionListener) new CopyHTMLResultsActionListener());
621: m_copyHTMLResultsMenuItem.setMnemonic('C');
622: m_copyHTMLResultsMenuItem.setAccelerator(KeyStroke
623: .getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK));
624: m_copyHTMLResultsMenuItem.setEnabled(false);
625: m_copyHTMLResultsMenuItem.setDisabledIcon(icon);
626: add(m_copyHTMLResultsMenuItem);
627:
628: //
629: // Copy Results menu item
630: //
631: icon = UIManager.getIcon("copy");
632: m_copyTextResultsMenuItem = new JMenuItem(
633: "Copy Results Results as Text", icon);
634: m_copyTextResultsMenuItem
635: .addActionListener((ActionListener) new CopyTextResultsActionListener());
636: m_copyTextResultsMenuItem.setMnemonic('Y');
637: m_copyTextResultsMenuItem.setAccelerator(KeyStroke
638: .getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK));
639: m_copyTextResultsMenuItem.setEnabled(false);
640: m_copyTextResultsMenuItem.setDisabledIcon(icon);
641: add(m_copyTextResultsMenuItem);
642:
643: addMouseListener(new EditMenuMouseListener());
644: }
645: }
646:
647: /**
648: *********************************************************************************
649: *********************************************************************************
650: *********************************************************************************
651: */
652: private class SaveSaveAs implements
653: HTMLAnalysisResultsEventListener {
654:
655: private String m_htmlText;
656:
657: /**
658: ****************************************************************************
659: *
660: * @param outputFile
661: */
662: private void perform(File outputFile) {
663: if (outputFile != null) {
664: try {
665: ListenerList
666: .addListener((HTMLAnalysisResultsEventListener) this );
667: HTMLAnalysisResultsEvent
668: .notifyRequestHTMLText(this );
669: FileWriter writer = null;
670:
671: if (outputFile.exists()) {
672: outputFile.delete();
673: }
674:
675: try {
676: writer = new FileWriter(outputFile);
677: writer.write(m_htmlText);
678: String message = "Saved Search results to file \""
679: + outputFile.getPath() + "\".";
680: MessageDialog.show(PMDViewer.getViewer(),
681: message);
682: } catch (IOException ioException) {
683: String message = "Could not save Search results to a file.";
684: PMDException pmdException = new PMDException(
685: message, ioException);
686: pmdException.fillInStackTrace();
687: throw pmdException;
688: } finally {
689: try {
690: writer.close();
691: } catch (IOException exception) {
692: }
693: }
694: } catch (PMDException pmdException) {
695: String message = pmdException.getMessage();
696: Exception exception = pmdException.getReason();
697: MessageDialog.show(PMDViewer.getViewer(), message,
698: exception);
699: } finally {
700: ListenerList
701: .removeListener((HTMLAnalysisResultsEventListener) this );
702: }
703: }
704: }
705:
706: /**
707: ****************************************************************************
708: *
709: * Implements AnalyzeFileEventListener
710: *
711: * @param event
712: */
713: public void requestHTMLAnalysisResults(
714: HTMLAnalysisResultsEvent event) {
715: }
716:
717: /**
718: ****************************************************************************
719: *
720: * Implements AnalyzeFileEventListener
721: *
722: * @param event
723: */
724: public void returnedHTMLAnalysisResults(
725: HTMLAnalysisResultsEvent event) {
726: m_htmlText = event.getHTMLText();
727: }
728: }
729:
730: /**
731: *********************************************************************************
732: *********************************************************************************
733: *********************************************************************************
734: */
735: private class SaveActionListener implements ActionListener,
736: DirectoryTableEventListener {
737:
738: private File m_selectedFile;
739:
740: /**
741: ****************************************************************************
742: *
743: * @param event
744: */
745: public void actionPerformed(ActionEvent event) {
746: try {
747: ListenerList
748: .addListener((DirectoryTableEventListener) this );
749: DirectoryTableEvent.notifyRequestFileSelected(this );
750:
751: if (m_selectedFile != null) {
752: String fileName = m_selectedFile.getName();
753: int index = fileName.lastIndexOf('.');
754:
755: if (index >= 0) {
756: fileName = fileName.substring(0, index);
757: }
758:
759: String directory = Preferences.getPreferences()
760: .getAnalysisResultsPath();
761: String path = directory + File.separator + fileName
762: + ".html";
763: File outputFile = new File(path);
764: (new SaveSaveAs()).perform(outputFile);
765: }
766: } catch (PMDException pmdException) {
767: String message = pmdException.getMessage();
768: Exception exception = pmdException.getReason();
769: MessageDialog.show(PMDViewer.getViewer(), message,
770: exception);
771: } finally {
772: ListenerList
773: .removeListener((DirectoryTableEventListener) this );
774: }
775: }
776:
777: /**
778: ****************************************************************************
779: *
780: * Implements DirectoryTableEventListener
781: *
782: * @param event
783: */
784: public void requestSelectedFile(DirectoryTableEvent event) {
785: }
786:
787: /**
788: ****************************************************************************
789: *
790: * Implements DirectoryTableEventListener
791: *
792: * @param event
793: */
794: public void fileSelectionChanged(DirectoryTableEvent event) {
795: }
796:
797: /**
798: ****************************************************************************
799: *
800: * Implements DirectoryTableEventListener
801: *
802: * @param event
803: */
804: public void fileSelected(DirectoryTableEvent event) {
805: m_selectedFile = event.getSelectedFile();
806: }
807: }
808:
809: /**
810: *********************************************************************************
811: *********************************************************************************
812: *********************************************************************************
813: */
814: private class SaveAsActionListener implements ActionListener,
815: DirectoryTableEventListener {
816:
817: private File m_selectedFile;
818:
819: public void actionPerformed(ActionEvent event) {
820: try {
821: ListenerList
822: .addListener((DirectoryTableEventListener) this );
823: DirectoryTableEvent.notifyRequestFileSelected(this );
824:
825: if (m_selectedFile != null) {
826: String fileName = m_selectedFile.getName();
827: int index = fileName.lastIndexOf('.');
828:
829: if (index >= 0) {
830: fileName = fileName.substring(0, index);
831: }
832:
833: String path = Preferences.getPreferences()
834: .getAnalysisResultsPath();
835: File lastSavedDirectory = new File(path);
836: File selectedFile = new File(path + File.separator
837: + fileName + ".html");
838: JFileChooser fileChooser = new JFileChooser(
839: lastSavedDirectory);
840: fileChooser
841: .addChoosableFileFilter(new HTMLFileFilter());
842: fileChooser.setSelectedFile(selectedFile);
843: int result = fileChooser.showSaveDialog(PMDViewer
844: .getViewer());
845:
846: if (result == JFileChooser.APPROVE_OPTION) {
847: (new SaveSaveAs()).perform(fileChooser
848: .getSelectedFile());
849: }
850: }
851: } catch (PMDException pmdException) {
852: String message = pmdException.getMessage();
853: Exception exception = pmdException.getReason();
854: MessageDialog.show(PMDViewer.getViewer(), message,
855: exception);
856: } finally {
857: ListenerList
858: .removeListener((DirectoryTableEventListener) this );
859: }
860: }
861:
862: /**
863: ****************************************************************************
864: *
865: * Implements DirectoryTableEventListener
866: *
867: * @param event
868: */
869: public void requestSelectedFile(DirectoryTableEvent event) {
870: }
871:
872: /**
873: ****************************************************************************
874: *
875: * Implements DirectoryTableEventListener
876: *
877: * @param event
878: */
879: public void fileSelectionChanged(DirectoryTableEvent event) {
880: }
881:
882: /**
883: ****************************************************************************
884: *
885: * Implements DirectoryTableEventListener
886: *
887: * @param event
888: */
889: public void fileSelected(DirectoryTableEvent event) {
890: m_selectedFile = event.getSelectedFile();
891: }
892: }
893:
894: /**
895: *********************************************************************************
896: *********************************************************************************
897: *********************************************************************************
898: */
899: private class HTMLFileFilter extends FileFilter {
900:
901: /**
902: *****************************************************************************
903: * @param file
904: * @return
905: */
906: public boolean accept(File file) {
907: String fileName = file.getName();
908:
909: return fileName.endsWith(".html")
910: || fileName.endsWith(".htm");
911: }
912:
913: /**
914: ******************************************************************************
915: */
916: public String getDescription() {
917: return "HTML Anaysis Result File (*.html, *.htm)";
918: }
919: }
920:
921: /**
922: *********************************************************************************
923: *********************************************************************************
924: *********************************************************************************
925: */
926: private class PMDClipboard implements ClipboardOwner {
927:
928: /**
929: ************************************************************************
930: *
931: */
932: public void lostOwnership(Clipboard clipboard,
933: Transferable contents) {
934: }
935: }
936:
937: /**
938: *********************************************************************************
939: *********************************************************************************
940: *********************************************************************************
941: */
942: private class SearchButtonActionEventHandler implements
943: ActionListener {
944:
945: /**
946: **************************************************************************
947: */
948: public void actionPerformed(ActionEvent event) {
949: RulesTreeNode rulesNode;
950: RuleSet ruleSet;
951:
952: rulesNode = m_rulesTree.getSelectedNode();
953:
954: if (rulesNode.isRoot()) {
955: ruleSet = new RuleSet();
956: Enumeration ruleSets = rulesNode.children();
957:
958: while (ruleSets.hasMoreElements()) {
959: RulesTreeNode childNode = (RulesTreeNode) ruleSets
960: .nextElement();
961: ruleSet
962: .addRuleSet((RuleSet) childNode
963: .getRuleSet());
964: }
965: } else if (rulesNode.isRuleSet()) {
966: ruleSet = rulesNode.getRuleSet();
967: } else if (rulesNode.isRule()) {
968: ruleSet = new RuleSet();
969: ruleSet.addRule(rulesNode.getRule());
970: } else {
971: rulesNode = (RulesTreeNode) rulesNode.getParent();
972: ruleSet = new RuleSet();
973: ruleSet.addRule(rulesNode.getRule());
974: }
975:
976: DirectoryTreeNode directoryNode = m_directoryTree
977: .getSelectedNode();
978: File directory = (File) directoryNode.getUserObject();
979:
980: m_resultsViewer.analyze(directory, ruleSet);
981: }
982: }
983: }
|