001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMDException;
004: import org.acm.seguin.pmd.swingui.event.DirectoryTableEvent;
005: import org.acm.seguin.pmd.swingui.event.DirectoryTableEventListener;
006: import org.acm.seguin.pmd.swingui.event.HTMLAnalysisResultsEvent;
007: import org.acm.seguin.pmd.swingui.event.HTMLAnalysisResultsEventListener;
008: import org.acm.seguin.pmd.swingui.event.ListenerList;
009: import org.acm.seguin.pmd.swingui.event.TextAnalysisResultsEvent;
010: import org.acm.seguin.pmd.swingui.event.TextAnalysisResultsEventListener;
011:
012: import javax.swing.Icon;
013: import javax.swing.JFileChooser;
014: import javax.swing.JLabel;
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 javax.swing.border.CompoundBorder;
025: import javax.swing.border.EmptyBorder;
026: import javax.swing.border.EtchedBorder;
027: import javax.swing.border.TitledBorder;
028: import javax.swing.filechooser.FileFilter;
029: import java.awt.BorderLayout;
030: import java.awt.Color;
031: import java.awt.Font;
032: import java.awt.Toolkit;
033: import java.awt.datatransfer.Clipboard;
034: import java.awt.datatransfer.ClipboardOwner;
035: import java.awt.datatransfer.StringSelection;
036: import java.awt.datatransfer.Transferable;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039: import java.awt.event.KeyEvent;
040: import java.awt.event.MouseAdapter;
041: import java.awt.event.MouseEvent;
042: import java.io.File;
043: import java.io.FileWriter;
044: import java.io.IOException;
045:
046: /**
047: *
048: * @author Donald A. Leckie
049: * @since December 25, 2002
050: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
051: */
052: class AnalysisViewer extends JPanel {
053:
054: private DirectoryTree m_directoryTree;
055: private JLabel m_message;
056: private StatusBar m_statusBar;
057: private JPanel m_directoryTreePanel;
058: private DirectoryTable m_directoryTable;
059: private JPanel m_directoryTablePanel;
060: private JSplitPane m_directorySplitPane;
061: private AnalysisResultsViewer m_resultsViewer;
062: private JPanel m_resultsViewerPanel;
063: private JSplitPane m_mainSplitPane;
064: private JMenuBar m_menuBar;
065: private PMDClipboard m_clipboardOwner = new PMDClipboard();
066: private JMenuItem m_saveMenuItem;
067: private JMenuItem m_saveAsMenuItem;
068: private JMenuItem m_printAnalysisMenuItem;
069: private JMenuItem m_copyHTMLResultsMenuItem;
070: private JMenuItem m_copyTextResultsMenuItem;
071:
072: /**
073: **********************************************************************************
074: *
075: */
076: protected AnalysisViewer() {
077: super (new BorderLayout());
078:
079: createStatusBar();
080: createDirectoryTreePanel();
081: createDirectoryTablePanel();
082: createDirectorySplitPane();
083: createResultsViewer();
084: createResultsViewerScrollPane();
085: createMainSplitPane();
086: createMenuBar();
087: setMenuBar();
088: add(createContentPanel(10));
089: }
090:
091: /**
092: *********************************************************************************
093: *
094: */
095: private void createStatusBar() {
096: String defaultMessage;
097: CompoundBorder compoundBorder;
098: EmptyBorder emptyBorder;
099:
100: defaultMessage = "Select a source file to view its analysis below.";
101: m_statusBar = new StatusBar(defaultMessage);
102: emptyBorder = new EmptyBorder(0, 0, 5, 0);
103: compoundBorder = new CompoundBorder(emptyBorder, m_statusBar
104: .getBorder());
105: m_statusBar.setBorder(compoundBorder);
106: }
107:
108: /**
109: *********************************************************************************
110: *
111: */
112: private void createDirectoryTreePanel() {
113: Color background;
114: JScrollPane scrollPane;
115:
116: m_directoryTree = new DirectoryTree("File Directories");
117: scrollPane = ComponentFactory.createScrollPane(m_directoryTree);
118: background = UIManager.getColor("pmdTreeBackground");
119: scrollPane.getViewport().setBackground(background);
120: m_directoryTreePanel = new JPanel(new BorderLayout());
121: m_directoryTreePanel
122: .setBorder(createTitledBorder(" Directory "));
123: m_directoryTreePanel.add(scrollPane, BorderLayout.CENTER);
124: }
125:
126: /**
127: *********************************************************************************
128: *
129: */
130: private void createDirectoryTablePanel() {
131: Color background;
132: JScrollPane scrollPane;
133:
134: m_directoryTable = new DirectoryTable(m_directoryTree, ".java");
135: scrollPane = ComponentFactory
136: .createScrollPane(m_directoryTable);
137: background = UIManager.getColor("pmdTableBackground");
138: scrollPane.getViewport().setBackground(background);
139: m_directoryTablePanel = new JPanel(new BorderLayout());
140: m_directoryTablePanel
141: .setBorder(createTitledBorder(" Java Source Code "));
142: m_directoryTablePanel.add(scrollPane, BorderLayout.CENTER);
143: }
144:
145: /**
146: *********************************************************************************
147: *
148: */
149: private void createDirectorySplitPane() {
150: m_directorySplitPane = ComponentFactory
151: .createHorizontalSplitPane(m_directoryTreePanel,
152: m_directoryTablePanel);
153:
154: }
155:
156: /**
157: *********************************************************************************
158: *
159: */
160: private void createResultsViewer() {
161: m_resultsViewer = new AnalysisResultsViewer();
162: m_resultsViewer.setSelectionColor(Color.blue);
163: }
164:
165: /**
166: *********************************************************************************
167: *
168: */
169: private void createResultsViewerScrollPane() {
170: JScrollPane scrollPane;
171:
172: scrollPane = ComponentFactory.createScrollPane(m_resultsViewer);
173: m_resultsViewerPanel = new JPanel(new BorderLayout());
174: m_resultsViewerPanel
175: .setBorder(createTitledBorder(" Analysis Results "));
176: m_resultsViewerPanel.add(scrollPane, BorderLayout.CENTER);
177: }
178:
179: /**
180: *********************************************************************************
181: *
182: */
183: private void createMainSplitPane() {
184: m_mainSplitPane = ComponentFactory.createVerticalSplitPane(
185: m_directorySplitPane, m_resultsViewerPanel);
186: }
187:
188: /**
189: *********************************************************************************
190: *
191: * @param margin
192: */
193: private JPanel createContentPanel(int margin) {
194: JPanel contentPanel = new JPanel(new BorderLayout());
195:
196: EtchedBorder outsideBorder = new EtchedBorder(
197: EtchedBorder.RAISED);
198: EmptyBorder insideBorder = new EmptyBorder(margin, margin,
199: margin, margin);
200: CompoundBorder compoundBorder = new CompoundBorder(
201: outsideBorder, insideBorder);
202:
203: contentPanel.setBorder(compoundBorder);
204: contentPanel.add(m_statusBar, BorderLayout.NORTH);
205: contentPanel.add(m_mainSplitPane, BorderLayout.CENTER);
206:
207: return contentPanel;
208: }
209:
210: /**
211: *********************************************************************************
212: *
213: * @param title
214: */
215: private TitledBorder createTitledBorder(String title) {
216: EtchedBorder etchedBorder;
217: TitledBorder titledBorder;
218: Font font;
219:
220: etchedBorder = new EtchedBorder(EtchedBorder.RAISED);
221: titledBorder = new TitledBorder(etchedBorder, title);
222: font = titledBorder.getTitleFont();
223: font = new Font(font.getName(), Font.BOLD, font.getSize());
224: titledBorder.setTitleFont(font);
225:
226: return titledBorder;
227: }
228:
229: /**
230: *********************************************************************************
231: *
232: */
233: protected void setDefaultMessage() {
234: setMessage("Select a source file to view its analysis below.");
235: }
236:
237: /**
238: *********************************************************************************
239: *
240: * @param message The message to be displayed in the status area.
241: */
242: protected void setMessage(String message) {
243: if (message == null) {
244: message = "";
245: }
246:
247: m_message.setText(message);
248: }
249:
250: /**
251: *********************************************************************************
252: *
253: */
254: protected void adjustSplitPaneDividerLocation() {
255: m_mainSplitPane.setDividerLocation(0.4);
256: m_directorySplitPane.setDividerLocation(0.4);
257: }
258:
259: /**
260: *********************************************************************************
261: *
262: */
263: private void createMenuBar() {
264: m_menuBar = new JMenuBar();
265:
266: m_menuBar.add(new FileMenu());
267: m_menuBar.add(new EditMenu());
268: m_menuBar.add(new HelpMenu());
269: }
270:
271: /**
272: *********************************************************************************
273: *
274: */
275: protected void setMenuBar() {
276: PMDViewer.getViewer().setJMenuBar(m_menuBar);
277: }
278:
279: /**
280: ********************************************************************************
281: *
282: */
283: protected void analyze() {
284: m_resultsViewer.analyze();
285: }
286:
287: /**
288: *********************************************************************************
289: *********************************************************************************
290: *********************************************************************************
291: */
292: private class PrintAnalysisActionListener implements ActionListener {
293:
294: public void actionPerformed(ActionEvent event) {
295: (new PrintAnalysisResults()).print();
296: }
297: }
298:
299: /**
300: *********************************************************************************
301: *********************************************************************************
302: *********************************************************************************
303: */
304: private class ExitActionListener implements ActionListener {
305:
306: public void actionPerformed(ActionEvent event) {
307: System.exit(0);
308: }
309: }
310:
311: /**
312: *********************************************************************************
313: *********************************************************************************
314: *********************************************************************************
315: */
316: private class CopyHTMLResultsActionListener implements
317: ActionListener, HTMLAnalysisResultsEventListener {
318: private String m_htmlText;
319:
320: /**
321: *****************************************************************************
322: *
323: * @param event
324: */
325: public void actionPerformed(ActionEvent event) {
326: try {
327: ListenerList
328: .addListener((HTMLAnalysisResultsEventListener) this );
329: HTMLAnalysisResultsEvent.notifyRequestHTMLText(this );
330:
331: if ((m_htmlText != null) && (m_htmlText.length() > 0)) {
332: Clipboard clipboard = Toolkit.getDefaultToolkit()
333: .getSystemClipboard();
334: StringSelection contents = new StringSelection(
335: m_htmlText);
336: clipboard.setContents(contents, m_clipboardOwner);
337: }
338: } finally {
339: ListenerList
340: .removeListener((HTMLAnalysisResultsEventListener) this );
341: }
342: }
343:
344: /**
345: ******************************************************************************
346: *
347: * @param event
348: */
349: public void requestHTMLAnalysisResults(
350: HTMLAnalysisResultsEvent event) {
351: }
352:
353: /**
354: ******************************************************************************
355: *
356: * @param event
357: */
358: public void returnedHTMLAnalysisResults(
359: HTMLAnalysisResultsEvent event) {
360: m_htmlText = event.getHTMLText();
361: }
362: }
363:
364: /**
365: *********************************************************************************
366: *********************************************************************************
367: *********************************************************************************
368: */
369: private class CopyTextResultsActionListener implements
370: ActionListener, TextAnalysisResultsEventListener {
371: private String m_text;
372:
373: public void actionPerformed(ActionEvent event) {
374: try {
375: ListenerList
376: .addListener((TextAnalysisResultsEventListener) this );
377: TextAnalysisResultsEvent.notifyRequestText(this );
378:
379: if ((m_text != null) && (m_text.length() > 0)) {
380: Clipboard clipboard = Toolkit.getDefaultToolkit()
381: .getSystemClipboard();
382: StringSelection contents = new StringSelection(
383: m_text);
384: clipboard.setContents(contents, m_clipboardOwner);
385: }
386: } finally {
387: ListenerList
388: .removeListener((TextAnalysisResultsEventListener) this );
389: }
390: }
391:
392: /**
393: ****************************************************************************
394: *
395: * @param event
396: */
397: public void requestTextAnalysisResults(
398: TextAnalysisResultsEvent event) {
399: }
400:
401: /**
402: ****************************************************************************
403: *
404: * @param event
405: */
406: public void returnedTextAnalysisResults(
407: TextAnalysisResultsEvent event) {
408: m_text = event.getText();
409: }
410: }
411:
412: /**
413: *********************************************************************************
414: *********************************************************************************
415: *********************************************************************************
416: */
417: private class FileMenuMouseListener extends MouseAdapter implements
418: HTMLAnalysisResultsEventListener {
419:
420: private String m_htmlText;
421:
422: /**
423: *****************************************************************************
424: *
425: * @param event
426: */
427: public void mouseEntered(MouseEvent event) {
428: try {
429: ListenerList
430: .addListener((HTMLAnalysisResultsEventListener) this );
431: HTMLAnalysisResultsEvent.notifyRequestHTMLText(this );
432: boolean enable = (m_htmlText.length() > 0);
433: m_saveMenuItem.setEnabled(enable);
434: m_saveAsMenuItem.setEnabled(enable);
435: m_printAnalysisMenuItem.setEnabled(enable);
436: } finally {
437: ListenerList
438: .removeListener((HTMLAnalysisResultsEventListener) this );
439: }
440: }
441:
442: /**
443: ****************************************************************************
444: *
445: * @param event
446: */
447: public void requestHTMLAnalysisResults(
448: HTMLAnalysisResultsEvent event) {
449: }
450:
451: /**
452: ****************************************************************************
453: *
454: * @param event
455: */
456: public void returnedHTMLAnalysisResults(
457: HTMLAnalysisResultsEvent event) {
458: m_htmlText = event.getHTMLText();
459: }
460: }
461:
462: /**
463: *********************************************************************************
464: *********************************************************************************
465: *********************************************************************************
466: */
467: private class EditMenuMouseListener extends MouseAdapter implements
468: HTMLAnalysisResultsEventListener {
469:
470: String m_htmlText;
471:
472: /**
473: *****************************************************************************
474: *
475: * @param event
476: */
477: public void mouseEntered(MouseEvent event) {
478: try {
479: ListenerList
480: .addListener((HTMLAnalysisResultsEventListener) this );
481: HTMLAnalysisResultsEvent.notifyRequestHTMLText(this );
482: boolean enable = (m_htmlText.length() > 0);
483: m_copyHTMLResultsMenuItem.setEnabled(enable);
484: m_copyTextResultsMenuItem.setEnabled(enable);
485: } finally {
486: ListenerList
487: .removeListener((HTMLAnalysisResultsEventListener) this );
488: }
489: }
490:
491: /**
492: ****************************************************************************
493: *
494: * @param event
495: */
496: public void requestHTMLAnalysisResults(
497: HTMLAnalysisResultsEvent event) {
498: }
499:
500: /**
501: ****************************************************************************
502: *
503: * @param event
504: */
505: public void returnedHTMLAnalysisResults(
506: HTMLAnalysisResultsEvent event) {
507: m_htmlText = event.getHTMLText();
508: }
509: }
510:
511: /**
512: *********************************************************************************
513: *********************************************************************************
514: *********************************************************************************
515: */
516: private class FileMenu extends JMenu {
517:
518: /**
519: ********************************************************************
520: *
521: */
522: private FileMenu() {
523: super ("File");
524:
525: setMnemonic('F');
526:
527: Icon icon;
528: JMenuItem menuItem;
529:
530: //
531: // Save menu item
532: //
533: icon = UIManager.getIcon("save");
534: m_saveMenuItem = new JMenuItem("Save Analysis Results",
535: 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 Analysis 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 Analysis 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 Analysis 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 Analysis 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 Analysis results to file \""
679: + outputFile.getPath() + "\".";
680: MessageDialog.show(PMDViewer.getViewer(),
681: message);
682: } catch (IOException ioException) {
683: String message = "Could not save Analysis 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: }
|