001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMD;
004: import org.acm.seguin.pmd.PMDException;
005: import org.acm.seguin.pmd.Report;
006: import org.acm.seguin.pmd.RuleContext;
007: import org.acm.seguin.pmd.RuleSet;
008: import org.acm.seguin.pmd.swingui.event.HTMLAnalysisResultsEvent;
009: import org.acm.seguin.pmd.swingui.event.HTMLAnalysisResultsEventListener;
010: import org.acm.seguin.pmd.swingui.event.ListenerList;
011: import org.acm.seguin.pmd.swingui.event.StatusBarEvent;
012: import org.acm.seguin.pmd.swingui.event.TextAnalysisResultsEvent;
013: import org.acm.seguin.pmd.swingui.event.TextAnalysisResultsEventListener;
014:
015: import javax.swing.JEditorPane;
016: import javax.swing.JScrollPane;
017: import javax.swing.UIManager;
018: import javax.swing.text.html.HTMLEditorKit;
019: import java.awt.Component;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023:
024: /**
025: *
026: * @author Donald A. Leckie
027: * @since August 27, 2002
028: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
029: */
030: abstract class ResultsViewer extends JEditorPane {
031:
032: private File[] m_sourceFiles;
033: private String m_htmlText;
034: private PMD m_pmd;
035: private RuleSet m_ruleSet;
036:
037: /**
038: ********************************************************************************
039: */
040: protected ResultsViewer() {
041: super ();
042:
043: setEditorKit(new HTMLEditorKit());
044: setEditable(false);
045: setBackground(UIManager.getColor("pmdGray"));
046:
047: m_pmd = new PMD();
048: m_ruleSet = new RuleSet();
049:
050: //
051: // Add listeners
052: //
053: ListenerList
054: .addListener((HTMLAnalysisResultsEventListener) new HTMLAnalysisResultsEventHandler());
055: ListenerList
056: .addListener((TextAnalysisResultsEventListener) new TextAnalysisResultsEventHandler());
057: }
058:
059: /**
060: ********************************************************************************
061: *
062: */
063: private void scrollToTop() {
064: Component component = getParent();
065:
066: while ((component instanceof JScrollPane) == false) {
067: component = component.getParent();
068: }
069:
070: if (component != null) {
071: JScrollPane parentScrollPane = (JScrollPane) component;
072:
073: parentScrollPane.getHorizontalScrollBar().setValue(0);
074: parentScrollPane.getVerticalScrollBar().setValue(0);
075: parentScrollPane.repaint();
076: }
077: }
078:
079: /**
080: ********************************************************************************
081: *
082: * @return
083: */
084: protected String getHTMLText(File file) {
085:
086: return m_htmlText;
087: }
088:
089: /**
090: ********************************************************************************
091: *
092: * @return
093: */
094: protected String getHTMLText() {
095: return m_htmlText;
096: }
097:
098: /**
099: ********************************************************************************
100: *
101: * @return
102: */
103: protected String getPlainText() {
104: String fullText = "";
105:
106: if (m_sourceFiles != null) {
107: synchronized (m_sourceFiles) {
108: try {
109: RuleContext ruleContext = new RuleContext();
110: TextRenderer renderer = new TextRenderer();
111: renderer.beginRendering(m_sourceFiles.length == 1);
112:
113: for (int n = 0; n < m_sourceFiles.length; n++) {
114: ruleContext
115: .setSourceCodeFilename(m_sourceFiles[n]
116: .getPath());
117: ruleContext.setReport(new Report());
118: m_pmd.processFile(new FileInputStream(
119: m_sourceFiles[n]), m_ruleSet,
120: ruleContext);
121:
122: String filePath = m_sourceFiles[n].getPath();
123: Report report = ruleContext.getReport();
124: renderer.render(filePath, report);
125: }
126:
127: fullText = renderer.endRendering();
128: } catch (FileNotFoundException exception) {
129: MessageDialog.show(PMDViewer.getViewer(), null,
130: exception);
131: } catch (PMDException pmdException) {
132: String message = pmdException.getMessage();
133: Exception exception = pmdException.getReason();
134: MessageDialog.show(PMDViewer.getViewer(), message,
135: exception);
136: }
137: }
138: }
139:
140: return fullText;
141: }
142:
143: /**
144: ********************************************************************************
145: *
146: */
147: protected void analyze() {
148: if ((m_sourceFiles != null) && (m_ruleSet != null)) {
149: (new AnalyzeThread()).start();
150: }
151: }
152:
153: /**
154: ********************************************************************************
155: *
156: */
157: protected void analyze(File[] selectedFile, RuleSet ruleSet) {
158: if ((selectedFile != null) && (ruleSet != null)) {
159: m_sourceFiles = selectedFile;
160: m_ruleSet = ruleSet;
161: (new AnalyzeThread()).start();
162: }
163: }
164:
165: /**
166: ********************************************************************************
167: ********************************************************************************
168: ********************************************************************************
169: */
170: private class AnalyzeThread extends Thread {
171: private ResultsViewer m_resultsViewer;
172:
173: private AnalyzeThread() {
174: super ("Analyze");
175:
176: m_resultsViewer = ResultsViewer.this ;
177: }
178:
179: /**
180: ***************************************************************************
181: *
182: */
183: public void run() {
184: setup();
185: process();
186: cleanup();
187: }
188:
189: /**
190: ***************************************************************************
191: *
192: */
193: protected void setup() {
194: PMDViewer.getViewer().setEnableViewer(false);
195: StatusBarEvent.notifyStartAnimation(this );
196: }
197:
198: /**
199: ***************************************************************************
200: *
201: */
202: protected void process() {
203: if (m_sourceFiles == null) {
204: return;
205: }
206:
207: try {
208: StatusBarEvent.notifyShowMessage(this ,
209: "Analyzing. Please wait...");
210:
211: RuleContext ruleContext;
212: HTMLResultRenderer renderer;
213: boolean reportNoViolations;
214:
215: ruleContext = new RuleContext();
216: renderer = new HTMLResultRenderer();
217: reportNoViolations = (m_sourceFiles.length == 1);
218: renderer.beginRendering(reportNoViolations);
219:
220: for (int n = 0; n < m_sourceFiles.length; n++) {
221: ruleContext.setSourceCodeFilename(m_sourceFiles[n]
222: .getPath());
223: ruleContext.setReport(new Report());
224: m_pmd.processFile(new FileInputStream(
225: m_sourceFiles[n]), m_ruleSet, ruleContext);
226:
227: StatusBarEvent
228: .notifyShowMessage(this ,
229: "Rendering analysis results into HTML page. Please wait...");
230:
231: String filePath;
232: Report report;
233:
234: filePath = m_sourceFiles[n].getPath();
235: report = ruleContext.getReport();
236: renderer.render(filePath, report);
237: }
238:
239: m_htmlText = renderer.endRendering();
240: StatusBarEvent
241: .notifyShowMessage(this ,
242: "Storing HTML page into viewer. Please wait...");
243: setText(m_htmlText);
244: } catch (FileNotFoundException exception) {
245: MessageDialog.show(PMDViewer.getViewer(), null,
246: exception);
247: } catch (PMDException pmdException) {
248: String message = pmdException.getMessage();
249: Exception exception = pmdException.getReason();
250: MessageDialog.show(PMDViewer.getViewer(), message,
251: exception);
252: } catch (OutOfMemoryError error) {
253: MessageDialog.show(PMDViewer.getViewer(),
254: "Out of memory.");
255: } finally {
256: m_resultsViewer.scrollToTop();
257: StatusBarEvent.notifyShowMessage(this , "Finished");
258: }
259: }
260:
261: /**
262: ***************************************************************************
263: *
264: */
265: protected void cleanup() {
266: StatusBarEvent.notifyStopAnimation(this );
267: PMDViewer.getViewer().setEnableViewer(true);
268: }
269: }
270:
271: /**
272: ***********************************************************************************
273: ***********************************************************************************
274: ***********************************************************************************
275: */
276: private class HTMLAnalysisResultsEventHandler implements
277: HTMLAnalysisResultsEventListener {
278:
279: /**
280: ****************************************************************************
281: *
282: * @param event
283: */
284: public void requestHTMLAnalysisResults(
285: HTMLAnalysisResultsEvent event) {
286: if (m_htmlText == null) {
287: m_htmlText = "";
288: }
289:
290: HTMLAnalysisResultsEvent.notifyReturnedHTMLText(this ,
291: m_htmlText);
292: }
293:
294: /**
295: ****************************************************************************
296: *
297: * @param event
298: */
299: public void returnedHTMLAnalysisResults(
300: HTMLAnalysisResultsEvent event) {
301: }
302: }
303:
304: /**
305: ***********************************************************************************
306: ***********************************************************************************
307: ***********************************************************************************
308: */
309: private class TextAnalysisResultsEventHandler implements
310: TextAnalysisResultsEventListener {
311:
312: /**
313: ****************************************************************************
314: *
315: * @param event
316: */
317: public void requestTextAnalysisResults(
318: TextAnalysisResultsEvent event) {
319: TextAnalysisResultsEvent.notifyReturnedText(this ,
320: getPlainText());
321: }
322:
323: /**
324: ****************************************************************************
325: *
326: * @param event
327: */
328: public void returnedTextAnalysisResults(
329: TextAnalysisResultsEvent event) {
330: }
331: }
332: }
|