KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui > RunAnalysisDialog


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 /*
21  * RunAnalysisDialog.java
22  *
23  * Created on April 1, 2003, 3:22 PM
24  */

25
26 package edu.umd.cs.findbugs.gui;
27
28 import java.awt.event.WindowEvent JavaDoc;
29
30 import javax.swing.JOptionPane JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32
33 import edu.umd.cs.findbugs.FindBugsProgress;
34 import edu.umd.cs.findbugs.L10N;
35
36 /**
37  * A modal dialog to run the actual FindBugs analysis on a project.
38  * The analysis is done in a separate thread, so that the GUI can
39  * still stay current while the analysis is running. We provide support
40  * for reporting the progress of the analysis, and for asynchronously
41  * cancelling the analysis before it completes.
42  *
43  * @author David Hovemeyer
44  */

45 public class RunAnalysisDialog extends javax.swing.JDialog JavaDoc {
46     private static final long serialVersionUID = 1L;
47
48     private class RunAnalysisProgress implements FindBugsProgress {
49         private int goal, count;
50
51         private synchronized int getGoal() {
52             return goal;
53         }
54
55         private synchronized int getCount() {
56             return count;
57         }
58
59         public void reportNumberOfArchives(final int numArchives) {
60             beginStage(L10N.getLocalString("msg.scanningarchives_txt", "Scanning archives"), numArchives);
61         }
62
63         public void finishArchive() {
64             step();
65         }
66
67         public void startAnalysis(int numClasses) {
68             beginStage(L10N.getLocalString("msg.analysingclasses_txt", "Analyzing classes"), numClasses);
69         }
70
71         public void finishClass() {
72             step();
73         }
74
75         public void finishPerClassAnalysis() {
76             SwingUtilities.invokeLater(new Runnable JavaDoc() {
77                 public void run() {
78                     stageNameLabel.setText(L10N.getLocalString("msg.finishedanalysis_txt", "Finishing analysis"));
79                 }
80             });
81         }
82
83         private void beginStage(final String JavaDoc stageName, final int goal) {
84             synchronized (this) {
85                 this.count = 0;
86                 this.goal = goal;
87             }
88
89             SwingUtilities.invokeLater(new Runnable JavaDoc() {
90                 public void run() {
91                     int goal = getGoal();
92                     stageNameLabel.setText(stageName);
93                     countValueLabel.setText("0/" + goal);
94                     progressBar.setMaximum(goal);
95                     progressBar.setValue(0);
96                 }
97             });
98         }
99
100         private void step() {
101             synchronized (this) {
102                 count++;
103             }
104
105             SwingUtilities.invokeLater(new Runnable JavaDoc() {
106                 public void run() {
107                     int count = getCount();
108                     int goal = getGoal();
109                     countValueLabel.setText(count + "/" + goal);
110                     progressBar.setValue(count);
111                 }
112             });
113         }
114
115         /* (non-Javadoc)
116          * @see edu.umd.cs.findbugs.FindBugsProgress#predictPassCount(int[])
117          */

118         public void predictPassCount(int[] classesPerPass) {
119             // TODO Auto-generated method stub
120

121         }
122
123     }
124
125     private final AnalysisRun analysisRun;
126     private Thread JavaDoc analysisThread;
127     private boolean completed;
128     private Exception JavaDoc fatalException;
129
130     /**
131      * Creates new form RunAnalysisDialog
132      */

133     public RunAnalysisDialog(java.awt.Frame JavaDoc parent, AnalysisRun analysisRun_) {
134         super(parent, true);
135         initComponents();
136         this.analysisRun = analysisRun_;
137         this.completed = false;
138
139         // Create a progress callback to give the user feedback
140
// about how far along we are.
141
final FindBugsProgress progress = new RunAnalysisProgress();
142
143         // This is the thread that will actually run the analysis.
144
this.analysisThread = new Thread JavaDoc() {
145             @Override JavaDoc
146             public void run() {
147                 try {
148                     analysisRun.execute(progress);
149                     setCompleted(true);
150                 } catch (java.io.IOException JavaDoc e) {
151                     setException(e);
152                 } catch (InterruptedException JavaDoc e) {
153                     // We don't need to do anything here.
154
// The completed flag is not set, so the frame
155
// will know that the analysis did not complete.
156
} catch (Exception JavaDoc e) {
157                     setException(e);
158                 }
159
160                 // Send a message to the dialog that it should close
161
// That way, it goes away without any need for user intervention
162
SwingUtilities.invokeLater(new Runnable JavaDoc() {
163                     public void run() {
164                         closeDialog(new WindowEvent JavaDoc(RunAnalysisDialog.this, WindowEvent.WINDOW_CLOSING));
165                     }
166                 });
167             }
168         };
169     }
170
171     public synchronized void setCompleted(boolean completed) {
172         this.completed = completed;
173     }
174
175     /**
176      * The creator of the dialog may call this method to find out whether
177      * or not the analysis completed normally.
178      */

179     public synchronized boolean isCompleted() {
180         return completed;
181     }
182
183     public synchronized void setException(Exception JavaDoc e) {
184         fatalException = e;
185     }
186
187     /**
188      * Determine whether or not a fatal exception occurred
189      * during analysis.
190      */

191     public synchronized boolean exceptionOccurred() {
192         return fatalException != null;
193     }
194
195     /**
196      * Get the exception that abnormally terminated the analysis.
197      */

198     public synchronized Exception JavaDoc getException() {
199         return fatalException;
200     }
201
202     /**
203      * This method is called from within the constructor to
204      * initialize the form.
205      * WARNING: Do NOT modify this code. The content of this method is
206      * always regenerated by the Form Editor.
207      */

208     private void initComponents() {//GEN-BEGIN:initComponents
209
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
210
211         findBugsLabel = new javax.swing.JLabel JavaDoc();
212         countLabel = new javax.swing.JLabel JavaDoc();
213         progressLabel = new javax.swing.JLabel JavaDoc();
214         progressBar = new javax.swing.JProgressBar JavaDoc();
215         cancelButton = new javax.swing.JButton JavaDoc();
216         jSeparator1 = new javax.swing.JSeparator JavaDoc();
217         stageLabel = new javax.swing.JLabel JavaDoc();
218         stageNameLabel = new javax.swing.JLabel JavaDoc();
219         topVerticalFiller = new javax.swing.JLabel JavaDoc();
220         bottomVerticalFiller = new javax.swing.JLabel JavaDoc();
221         countValueLabel = new javax.swing.JLabel JavaDoc();
222
223         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
224
225         setTitle("Run Analysis");
226         this.setTitle(L10N.getLocalString("dlg.runanalysis_ttl", "Run Analysis"));
227         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
228             @Override JavaDoc
229             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
230                 closeDialog(evt);
231             }
232             @Override JavaDoc
233             public void windowOpened(java.awt.event.WindowEvent JavaDoc evt) {
234                 formWindowOpened(evt);
235             }
236         });
237
238         findBugsLabel.setBackground(new java.awt.Color JavaDoc(0, 0, 204));
239         findBugsLabel.setFont(new java.awt.Font JavaDoc("Dialog", 1, 24));
240         findBugsLabel.setForeground(new java.awt.Color JavaDoc(255, 255, 255));
241         findBugsLabel.setText("Find Bugs!");
242         findBugsLabel.setOpaque(true);
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
244         gridBagConstraints.gridwidth = 2;
245         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
246         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
247         gridBagConstraints.weightx = 1.0;
248         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 3, 0);
249         getContentPane().add(findBugsLabel, gridBagConstraints);
250
251         countLabel.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
252         countLabel.setText("Count:");
253         countLabel.setText(L10N.getLocalString("dlg.count_lbl", "Count:"));
254         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
255         gridBagConstraints.gridx = 0;
256         gridBagConstraints.gridy = 3;
257         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
258         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
259         getContentPane().add(countLabel, gridBagConstraints);
260
261         progressLabel.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
262         progressLabel.setText("Progress:");
263         progressLabel.setText(L10N.getLocalString("dlg.progress_lbl", "Progress:"));
264         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
265         gridBagConstraints.gridx = 0;
266         gridBagConstraints.gridy = 5;
267         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
268         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
269         getContentPane().add(progressLabel, gridBagConstraints);
270
271         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
272         gridBagConstraints.gridx = 1;
273         gridBagConstraints.gridy = 5;
274         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
275         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 3, 0, 3);
276         getContentPane().add(progressBar, gridBagConstraints);
277
278         cancelButton.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
279         cancelButton.setText("Cancel");
280         cancelButton.setText(L10N.getLocalString("dlg.cancel_btn", "Cancel"));
281         cancelButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
282             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
283                 cancelButtonActionPerformed(evt);
284             }
285         });
286
287         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
288         gridBagConstraints.gridx = 0;
289         gridBagConstraints.gridy = 8;
290         gridBagConstraints.gridwidth = 2;
291         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 0, 3, 0);
292         getContentPane().add(cancelButton, gridBagConstraints);
293
294         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
295         gridBagConstraints.gridx = 0;
296         gridBagConstraints.gridy = 7;
297         gridBagConstraints.gridwidth = 2;
298         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
299         getContentPane().add(jSeparator1, gridBagConstraints);
300
301         stageLabel.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
302         stageLabel.setText("Stage:");
303         stageLabel.setText(L10N.getLocalString("dlg.stage_lbl", "Stage:"));
304         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
305         gridBagConstraints.gridx = 0;
306         gridBagConstraints.gridy = 2;
307         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
308         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
309         getContentPane().add(stageLabel, gridBagConstraints);
310
311         stageNameLabel.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
312         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
313         gridBagConstraints.gridx = 1;
314         gridBagConstraints.gridy = 2;
315         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
316         getContentPane().add(stageNameLabel, gridBagConstraints);
317
318         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
319         gridBagConstraints.gridx = 0;
320         gridBagConstraints.gridy = 6;
321         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
322         gridBagConstraints.weighty = 0.5;
323         getContentPane().add(topVerticalFiller, gridBagConstraints);
324
325         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
326         gridBagConstraints.gridx = 0;
327         gridBagConstraints.gridy = 1;
328         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
329         gridBagConstraints.weighty = 0.5;
330         getContentPane().add(bottomVerticalFiller, gridBagConstraints);
331
332         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
333         gridBagConstraints.gridx = 1;
334         gridBagConstraints.gridy = 3;
335         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
336         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 3, 0, 0);
337         getContentPane().add(countValueLabel, gridBagConstraints);
338
339         pack();
340     }//GEN-END:initComponents
341

342     private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_cancelButtonActionPerformed
343
int option = JOptionPane.showConfirmDialog(this, L10N.getLocalString("msg.cancelanalysis_txt", "Cancel analysis?"), L10N.getLocalString("msg.analyze_txt", "Analysis"),
344                 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
345
346         if (option == JOptionPane.YES_OPTION) {
347             // All we need to do to cancel the analysis is to interrupt
348
// the analysis thread.
349
analysisThread.interrupt();
350         }
351     }//GEN-LAST:event_cancelButtonActionPerformed
352

353     private void formWindowOpened(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_formWindowOpened
354
// Here is where we actually kick off the analysis thread.
355

356         // Lower the priority of the analysis thread to leave more
357
// CPU for interactive tasks.
358
analysisThread.setPriority(Thread.NORM_PRIORITY - 1);
359         
360         analysisThread.start();
361     }//GEN-LAST:event_formWindowOpened
362

363     /**
364      * Closes the dialog
365      */

366     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
367
setVisible(false);
368         dispose();
369     }//GEN-LAST:event_closeDialog
370

371     // Variables declaration - do not modify//GEN-BEGIN:variables
372
private javax.swing.JLabel JavaDoc bottomVerticalFiller;
373     private javax.swing.JButton JavaDoc cancelButton;
374     private javax.swing.JLabel JavaDoc countLabel;
375     private javax.swing.JLabel JavaDoc countValueLabel;
376     private javax.swing.JLabel JavaDoc findBugsLabel;
377     private javax.swing.JSeparator JavaDoc jSeparator1;
378     private javax.swing.JProgressBar JavaDoc progressBar;
379     private javax.swing.JLabel JavaDoc progressLabel;
380     private javax.swing.JLabel JavaDoc stageLabel;
381     private javax.swing.JLabel JavaDoc stageNameLabel;
382     private javax.swing.JLabel JavaDoc topVerticalFiller;
383     // End of variables declaration//GEN-END:variables
384

385 }
386
Popular Tags