KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > dialog > DialogReports


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.dialog;
33
34 import com.jgoodies.forms.factories.Borders;
35 import com.jgoodies.forms.factories.FormFactory;
36 import com.jgoodies.forms.layout.*;
37 import org.antlr.xjlib.appkit.frame.XJDialog;
38 import org.antlr.xjlib.appkit.utils.XJAlert;
39 import org.antlr.xjlib.appkit.utils.XJDialogProgress;
40 import org.antlr.xjlib.appkit.utils.XJDialogProgressDelegate;
41 import org.antlr.xjlib.foundation.XJSystem;
42 import org.antlr.works.stats.StatisticsManager;
43 import org.antlr.works.stats.StatisticsReporter;
44
45 import javax.swing.*;
46 import javax.swing.event.ChangeEvent JavaDoc;
47 import javax.swing.event.ChangeListener JavaDoc;
48 import java.awt.*;
49 import java.awt.event.ActionEvent JavaDoc;
50 import java.awt.event.ActionListener JavaDoc;
51
52 public class DialogReports extends XJDialog {
53
54     public static final int TYPE_GUI = 0;
55     public static final int TYPE_GRAMMAR = 1;
56     public static final int TYPE_RUNTIME = 2;
57
58     protected StatisticsManager guiManager;
59     protected StatisticsManager grammarManager;
60     protected StatisticsManager runtimeManager;
61
62     protected DialogReportsDelegate delegate;
63     protected XJDialogProgress progress;
64
65     protected DefaultComboBoxModel typeModel = new DefaultComboBoxModel();
66     protected boolean usesAWStats = true;
67
68     public DialogReports(Container parent, boolean usesAWStats) {
69         super(parent, true);
70
71         this.usesAWStats = usesAWStats;
72
73         initComponents();
74         setSize(550, 500);
75
76         if(XJSystem.isMacOS()) {
77             CellConstraints cc = new CellConstraints();
78
79             buttonBar.remove(cancelButton);
80             buttonBar.remove(submitButton);
81
82             buttonBar.add(cancelButton, cc.xy(3, 1));
83             buttonBar.add(submitButton, cc.xy(5, 1));
84         }
85
86         setDefaultButton(submitButton);
87         setOKButton(submitButton);
88         setCancelButton(cancelButton);
89
90         buildTypeCombo();
91
92         humanFormatCheck.setSelected(true);
93         statsTextArea.setTabSize(2);
94
95         typeCombo.addActionListener(new MyActionListener());
96         humanFormatCheck.addActionListener(new MyActionListener());
97         currentSpinner.addChangeListener(new ChangeListener JavaDoc() {
98             public void stateChanged(ChangeEvent JavaDoc event) {
99                 updateInfo(true);
100             }
101         });
102     }
103
104     public void setDelegate(DialogReportsDelegate delegate) {
105         this.delegate = delegate;
106     }
107
108     public void buildTypeCombo() {
109         if(usesAWStats)
110             typeModel.addElement("ANTLRWorks");
111         typeModel.addElement("ANTLR - grammar");
112         typeModel.addElement("ANTLR - runtime");
113
114         typeCombo.setModel(typeModel);
115     }
116
117     public void dialogWillDisplay() {
118         guiManager = new StatisticsManager(StatisticsReporter.TYPE_GUI);
119         grammarManager = new StatisticsManager(StatisticsReporter.TYPE_GRAMMAR);
120         runtimeManager = new StatisticsManager(StatisticsReporter.TYPE_RUNTIME);
121
122         updateInfo(false);
123     }
124
125     public void dialogWillCloseCancel() {
126         SwingUtilities.invokeLater(new Runnable JavaDoc() {
127             public void run() {
128                 if(delegate != null)
129                     delegate.reportsCancelled();
130             }
131         });
132     }
133
134     public void dialogWillCloseOK() {
135         new SendReport().send();
136     }
137
138     protected int getTypeIndex() {
139         if(usesAWStats)
140             return typeCombo.getSelectedIndex();
141         else
142             return typeCombo.getSelectedIndex()+1;
143     }
144
145     protected void updateInfo(boolean textOnly) {
146         StatisticsManager sm = null;
147         boolean rangeEnabled = true;
148
149         switch(getTypeIndex()) {
150             case TYPE_GUI:
151                 rangeEnabled = false;
152                 sm = guiManager;
153                 break;
154             case TYPE_GRAMMAR:
155                 rangeEnabled = true;
156                 sm = grammarManager;
157                 break;
158             case TYPE_RUNTIME:
159                 rangeEnabled = true;
160                 sm = runtimeManager;
161                 break;
162         }
163
164         if(!textOnly)
165             setRangeEnabled(rangeEnabled, sm);
166
167         if(sm != null)
168             setText(sm);
169     }
170
171     protected void setRangeEnabled(boolean flag, StatisticsManager sm) {
172         currentSpinner.setEnabled(flag);
173         infoLabel.setEnabled(flag);
174
175         currentSpinner.setValue(new Integer JavaDoc(1));
176         SpinnerNumberModel spm = (SpinnerNumberModel)currentSpinner.getModel();
177         spm.setMaximum(new Integer JavaDoc(sm.getStatsCount()));
178
179         if(flag)
180             infoLabel.setText("of "+sm.getStatsCount());
181         else
182             infoLabel.setText("");
183     }
184
185     protected void setText(StatisticsManager sm) {
186         int index = ((Integer JavaDoc)currentSpinner.getValue()).intValue()-1;
187         String JavaDoc text = isHumanReadable() ? sm.getReadableString(index) : sm.getRawString(index);
188         if(text == null)
189             text = "* no statistics available *";
190
191         statsTextArea.setText(text);
192         statsTextArea.setCaretPosition(0);
193     }
194
195     protected boolean isHumanReadable() {
196         return humanFormatCheck.isSelected();
197     }
198
199     protected class SendReport implements Runnable JavaDoc, XJDialogProgressDelegate {
200
201         public boolean error = false;
202         public boolean cancel = false;
203         public StatisticsReporter reporter;
204         public StatisticsManager managers[] = { guiManager, grammarManager, runtimeManager };
205
206         public void send() {
207             progress = new XJDialogProgress(parent);
208             progress.setDelegate(this);
209             progress.setCancellable(true);
210             progress.setProgressMax(managers.length);
211             progress.setIndeterminate(false);
212             progress.setInfo("Sending statistics...");
213             progress.display();
214
215             error = false;
216             cancel = false;
217             reporter = new StatisticsReporter();
218
219             new Thread JavaDoc(this).start();
220         }
221
222         public boolean submit() {
223             for (int i = 0; i < managers.length; i++) {
224                 StatisticsManager manager = managers[i];
225
226                 if(manager == guiManager && !usesAWStats)
227                     continue;
228
229                 progress.setProgress(i+1);
230                 if(!reporter.submitStats(manager))
231                     return true;
232
233                 if(cancel)
234                     return false;
235             }
236             return false;
237         }
238
239         public void run() {
240             try {
241                 error = submit();
242             } finally {
243                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
244                     public void run() {
245                         finished();
246                     }
247                 });
248             }
249         }
250
251         public void finished() {
252             if(!cancel) {
253                 guiManager.reset();
254                 grammarManager.reset();
255                 runtimeManager.reset();
256             }
257
258             progress.close();
259
260             String JavaDoc title;
261             String JavaDoc info;
262
263             if(cancel) {
264                 title = "Submission cancelled";
265                 info = "The submission has been cancelled.";
266             } else {
267                 if(error) {
268                     title = "Submission failed";
269                     info = "An error has occurred when sending the statistics:\n"+reporter.getError();
270                 } else {
271                     title = "Thank you";
272                     info = "The statistics have been successfully transmitted.";
273                 }
274             }
275             XJAlert.display(getJavaComponent(), title, info);
276
277             if(delegate != null) {
278                 if(cancel)
279                     delegate.reportsCancelled();
280                 else
281                     delegate.reportsSend(!error);
282             }
283         }
284
285         public void dialogDidCancel() {
286             cancel = true;
287             reporter.cancel();
288         }
289     }
290
291     protected class MyActionListener implements ActionListener JavaDoc {
292         public void actionPerformed(ActionEvent JavaDoc event) {
293             updateInfo(false);
294         }
295     }
296
297     private void initComponents() {
298         // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
299
// Generated using JFormDesigner Open Source Project license - ANTLR (www.antlr.org)
300
dialogPane = new JPanel();
301         contentPane = new JPanel();
302         label1 = new JLabel();
303         typeCombo = new JComboBox();
304         label22 = new JLabel();
305         humanFormatCheck = new JCheckBox();
306         currentSpinner = new JSpinner();
307         label2 = new JLabel();
308         infoLabel = new JLabel();
309         scrollPane1 = new JScrollPane();
310         statsTextArea = new JTextArea();
311         buttonBar = new JPanel();
312         submitButton = new JButton();
313         cancelButton = new JButton();
314         CellConstraints cc = new CellConstraints();
315
316         //======== this ========
317
setTitle("Review reports before submission");
318         Container contentPane2 = getContentPane();
319         contentPane2.setLayout(new BorderLayout());
320
321         //======== dialogPane ========
322
{
323             dialogPane.setBorder(Borders.DIALOG_BORDER);
324             dialogPane.setPreferredSize(new Dimension(500, 500));
325             dialogPane.setLayout(new BorderLayout());
326
327             //======== contentPane ========
328
{
329                 contentPane.setLayout(new FormLayout(
330                     new ColumnSpec[] {
331                         FormFactory.DEFAULT_COLSPEC,
332                         FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
333                         new ColumnSpec("max(min;30dlu)"),
334                         FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
335                         FormFactory.RELATED_GAP_COLSPEC,
336                         FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
337                         new ColumnSpec("max(min;30dlu):grow"),
338                         FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
339                         new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
340                     },
341                     new RowSpec[] {
342                         FormFactory.DEFAULT_ROWSPEC,
343                         FormFactory.LINE_GAP_ROWSPEC,
344                         FormFactory.DEFAULT_ROWSPEC,
345                         FormFactory.LINE_GAP_ROWSPEC,
346                         FormFactory.DEFAULT_ROWSPEC,
347                         FormFactory.LINE_GAP_ROWSPEC,
348                         FormFactory.DEFAULT_ROWSPEC,
349                         FormFactory.LINE_GAP_ROWSPEC,
350                         new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
351                     }));
352
353                 //---- label1 ----
354
label1.setText("View report for:");
355                 label1.setHorizontalAlignment(SwingConstants.RIGHT);
356                 contentPane.add(label1, cc.xy(1, 1));
357
358                 //---- typeCombo ----
359
typeCombo.setModel(new DefaultComboBoxModel(new String JavaDoc[] {
360                     "ANTLRWorks",
361                     "ANTLR - grammar",
362                     "ANTLR - runtime"
363                 }));
364                 contentPane.add(typeCombo, cc.xywh(3, 1, 5, 1));
365
366                 //---- label22 ----
367
label22.setText("Format:");
368                 label22.setHorizontalAlignment(SwingConstants.RIGHT);
369                 contentPane.add(label22, cc.xy(1, 3));
370
371                 //---- humanFormatCheck ----
372
humanFormatCheck.setText("Human readable");
373                 humanFormatCheck.setSelected(true);
374                 contentPane.add(humanFormatCheck, cc.xywh(3, 3, 7, 1));
375
376                 //---- currentSpinner ----
377
currentSpinner.setModel(new SpinnerNumberModel(new Integer JavaDoc(1), new Integer JavaDoc(1), null, new Integer JavaDoc(1)));
378                 contentPane.add(currentSpinner, cc.xywh(3, 5, 2, 1));
379
380                 //---- label2 ----
381
label2.setText("Report:");
382                 label2.setHorizontalAlignment(SwingConstants.RIGHT);
383                 contentPane.add(label2, cc.xy(1, 5));
384
385                 //---- infoLabel ----
386
infoLabel.setText("of 999");
387                 contentPane.add(infoLabel, cc.xywh(7, 5, 3, 1));
388
389                 //======== scrollPane1 ========
390
{
391
392                     //---- statsTextArea ----
393
statsTextArea.setEditable(false);
394                     statsTextArea.setLineWrap(false);
395                     statsTextArea.setWrapStyleWord(false);
396                     statsTextArea.setTabSize(2);
397                     scrollPane1.setViewportView(statsTextArea);
398                 }
399                 contentPane.add(scrollPane1, cc.xywh(1, 7, 9, 3));
400             }
401             dialogPane.add(contentPane, BorderLayout.CENTER);
402
403             //======== buttonBar ========
404
{
405                 buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
406                 buttonBar.setLayout(new FormLayout(
407                     new ColumnSpec[] {
408                         new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
409                         FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
410                         FormFactory.BUTTON_COLSPEC,
411                         FormFactory.RELATED_GAP_COLSPEC,
412                         FormFactory.BUTTON_COLSPEC
413                     },
414                     RowSpec.decodeSpecs("pref")));
415
416                 //---- submitButton ----
417
submitButton.setText("Submit");
418                 buttonBar.add(submitButton, cc.xy(3, 1));
419
420                 //---- cancelButton ----
421
cancelButton.setText("Cancel");
422                 buttonBar.add(cancelButton, cc.xy(5, 1));
423             }
424             dialogPane.add(buttonBar, BorderLayout.SOUTH);
425         }
426         contentPane2.add(dialogPane, BorderLayout.CENTER);
427         pack();
428         // JFormDesigner - End of component initialization //GEN-END:initComponents
429
}
430
431     // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
432
// Generated using JFormDesigner Open Source Project license - ANTLR (www.antlr.org)
433
private JPanel dialogPane;
434     private JPanel contentPane;
435     private JLabel label1;
436     private JComboBox typeCombo;
437     private JLabel label22;
438     private JCheckBox humanFormatCheck;
439     private JSpinner currentSpinner;
440     private JLabel label2;
441     private JLabel infoLabel;
442     private JScrollPane scrollPane1;
443     private JTextArea statsTextArea;
444     private JPanel buttonBar;
445     private JButton submitButton;
446     private JButton cancelButton;
447     // JFormDesigner - End of variables declaration //GEN-END:variables
448

449
450 }
451
Popular Tags