KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > AnalyzingDialog


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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 package edu.umd.cs.findbugs.gui2;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.WindowAdapter JavaDoc;
25 import java.awt.event.WindowEvent JavaDoc;
26
27 import javax.swing.BoxLayout JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JProgressBar JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32
33 import edu.umd.cs.findbugs.FindBugsProgress;
34 import edu.umd.cs.findbugs.Project;
35 import edu.umd.cs.findbugs.annotations.NonNull;
36
37 @SuppressWarnings JavaDoc("serial")
38 //Note: Don't remove the final, if anyone extends this class, bad things could happen, since a thread is started in this class's constructor.
39
/**
40  *Creating an instance of this class runs a FindBugs analysis, and pops up a nice progress window
41  */

42 public final class AnalyzingDialog extends FBDialog implements FindBugsProgress
43 {
44     private volatile boolean analysisFinished = false;
45     @NonNull private Project project;
46     private AnalysisCallback callback;
47     private AnalysisThread analysisThread = new AnalysisThread();
48     
49     private int count;
50     private int goal;
51     
52     private JLabel JavaDoc statusLabel;
53     private JProgressBar JavaDoc progressBar;
54     private JButton JavaDoc cancelButton;
55     
56     public AnalyzingDialog(@NonNull final Project project)
57     {
58         this(project, new AnalysisCallback()
59         {
60             public void analysisFinished(BugSet results)
61                 {
62                     ProjectSettings.newInstance();
63                     ((BugTreeModel)MainFrame.getInstance().getTree().getModel()).getOffListenerList();
64                     MainFrame.getInstance().getTree().setModel(new BugTreeModel(MainFrame.getInstance().getTree(), MainFrame.getInstance().getSorter(), results));
65                     MainFrame.getInstance().setProject(project);
66                 }
67             
68             public void analysisInterrupted() {}
69         }, false);
70     }
71     
72     
73     /**
74      *
75      * @param project The Project to analyze
76      * @param callback contains what to do if the analysis is interrupted and what to do if it finishes normally
77      * @param joinThread Whether or not this constructor should return before the analysis is complete. If true, the constructor does not return until the analysis is either finished or interrupted.
78      */

79     public AnalyzingDialog(@NonNull Project project, AnalysisCallback callback, boolean joinThread)
80     {
81         assert project != null;
82         this.project = project;
83         this.callback = callback;
84         initComponents();
85         analysisThread.start();
86         if (joinThread)
87             try {analysisThread.join();} catch (InterruptedException JavaDoc e) {}
88     }
89     
90     public void initComponents()
91     {
92         statusLabel = new JLabel JavaDoc(" ");
93         progressBar = new JProgressBar JavaDoc();
94         progressBar.setStringPainted(true);
95         cancelButton = new JButton JavaDoc(edu.umd.cs.findbugs.L10N.getLocalString("dlg.cancel_btn", "Cancel"));
96         cancelButton.addActionListener(new ActionListener JavaDoc()
97         {
98             public void actionPerformed(ActionEvent JavaDoc evt)
99             {
100                 cancel();
101             }
102         });
103         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
104         addWindowListener(new WindowAdapter JavaDoc()
105         {
106             @Override JavaDoc
107             public void windowClosed(WindowEvent JavaDoc evt)
108             {
109                 cancel();
110             }
111         });
112         
113         SwingUtilities.invokeLater(new Runnable JavaDoc()
114         {
115             public void run()
116             {
117                 setLayout(new BoxLayout JavaDoc(getContentPane(), BoxLayout.Y_AXIS));
118                 add(statusLabel);
119                 add(progressBar);
120                 add(cancelButton);
121                 statusLabel.setAlignmentX(CENTER_ALIGNMENT);
122                 progressBar.setAlignmentX(CENTER_ALIGNMENT);
123                 cancelButton.setAlignmentX(CENTER_ALIGNMENT);
124                 pack();
125                 setSize(300, getHeight());
126                 setLocationRelativeTo(MainFrame.getInstance());
127                 setResizable(false);
128                 setModal(false);
129                 setVisible(true);
130             }
131         });
132     }
133     
134     private void cancel()
135     {
136         if (!analysisFinished)
137         {
138             analysisThread.interrupt();
139             setVisible(false);
140             // TODO there should be a call to dispose() here, but it seems to cause repainting issues
141
}
142     }
143     
144     private void updateStage(String JavaDoc stage)
145     {
146         statusLabel.setText(stage);
147     }
148     
149     private void incrementCount()
150     {
151         count++;
152         SwingUtilities.invokeLater(new Runnable JavaDoc()
153         {
154             public void run()
155             {
156                 progressBar.setString(count + "/" + goal);
157                 progressBar.setValue(count);
158             }
159         });
160     }
161     
162     private void updateCount(final int count, final int goal)
163     {
164         this.count = count;
165         this.goal = goal;
166         SwingUtilities.invokeLater(new Runnable JavaDoc()
167         {
168             public void run()
169             {
170                 progressBar.setString(count + "/" + goal);
171                 progressBar.setValue(count);
172                 progressBar.setMaximum(goal);
173             }
174         });
175     }
176     
177     public void finishArchive()
178     {
179         incrementCount();
180     }
181
182     public void finishClass()
183     {
184         incrementCount();
185     }
186
187     public void finishPerClassAnalysis()
188     {
189         updateStage(edu.umd.cs.findbugs.L10N.getLocalString("progress.finishing_analysis", "Finishing analysis..."));
190     }
191
192     public void reportNumberOfArchives(int numArchives)
193     {
194         updateStage(edu.umd.cs.findbugs.L10N.getLocalString("progress.scanning_archives", "Scanning archives..."));
195         updateCount(0, numArchives);
196     }
197
198     public void startAnalysis(int numClasses)
199     {
200         updateStage(edu.umd.cs.findbugs.L10N.getLocalString("progress.analyzing_classes", "Analyzing classes..."));
201         updateCount(0, numClasses);
202     }
203
204     private class AnalysisThread extends Thread JavaDoc
205     {
206         {
207             // Give the analysis thread lower priority than the UI
208
setPriority(NORM_PRIORITY - 1);
209         }
210         
211         public void run()
212         {
213             assert project != null;
214             BugSet data = BugLoader.doAnalysis(project, AnalyzingDialog.this);
215             if (data == null) // We were interrupted
216
{
217                 callback.analysisInterrupted();
218                 return;
219             }
220             analysisFinished = true;
221             AnalyzingDialog.this.dispose();
222             callback.analysisFinished(data);
223             MainFrame.getInstance().newProject();
224         }
225     }
226
227     /* (non-Javadoc)
228      * @see edu.umd.cs.findbugs.FindBugsProgress#predictPassCount(int[])
229      */

230     public void predictPassCount(int[] classesPerPass) {
231         // TODO Auto-generated method stub
232

233     }
234 }
Popular Tags