1 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 ; 47 import javax.swing.event.ChangeListener ; 48 import java.awt.*; 49 import java.awt.event.ActionEvent ; 50 import java.awt.event.ActionListener ; 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 () { 98 public void stateChanged(ChangeEvent 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 () { 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 (1)); 176 SpinnerNumberModel spm = (SpinnerNumberModel)currentSpinner.getModel(); 177 spm.setMaximum(new Integer (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 )currentSpinner.getValue()).intValue()-1; 187 String 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 , 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 (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 () { 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 title; 261 String 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 { 292 public void actionPerformed(ActionEvent event) { 293 updateInfo(false); 294 } 295 } 296 297 private void initComponents() { 298 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 setTitle("Review reports before submission"); 318 Container contentPane2 = getContentPane(); 319 contentPane2.setLayout(new BorderLayout()); 320 321 { 323 dialogPane.setBorder(Borders.DIALOG_BORDER); 324 dialogPane.setPreferredSize(new Dimension(500, 500)); 325 dialogPane.setLayout(new BorderLayout()); 326 327 { 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.setText("View report for:"); 355 label1.setHorizontalAlignment(SwingConstants.RIGHT); 356 contentPane.add(label1, cc.xy(1, 1)); 357 358 typeCombo.setModel(new DefaultComboBoxModel(new String [] { 360 "ANTLRWorks", 361 "ANTLR - grammar", 362 "ANTLR - runtime" 363 })); 364 contentPane.add(typeCombo, cc.xywh(3, 1, 5, 1)); 365 366 label22.setText("Format:"); 368 label22.setHorizontalAlignment(SwingConstants.RIGHT); 369 contentPane.add(label22, cc.xy(1, 3)); 370 371 humanFormatCheck.setText("Human readable"); 373 humanFormatCheck.setSelected(true); 374 contentPane.add(humanFormatCheck, cc.xywh(3, 3, 7, 1)); 375 376 currentSpinner.setModel(new SpinnerNumberModel(new Integer (1), new Integer (1), null, new Integer (1))); 378 contentPane.add(currentSpinner, cc.xywh(3, 5, 2, 1)); 379 380 label2.setText("Report:"); 382 label2.setHorizontalAlignment(SwingConstants.RIGHT); 383 contentPane.add(label2, cc.xy(1, 5)); 384 385 infoLabel.setText("of 999"); 387 contentPane.add(infoLabel, cc.xywh(7, 5, 3, 1)); 388 389 { 391 392 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 { 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.setText("Submit"); 418 buttonBar.add(submitButton, cc.xy(3, 1)); 419 420 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 } 430 431 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 449 450 } 451 | Popular Tags |