1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Container ; 25 import java.awt.event.ActionEvent ; 26 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.Box ; 30 import javax.swing.ButtonGroup ; 31 import javax.swing.JButton ; 32 import javax.swing.JCheckBox ; 33 import javax.swing.JOptionPane ; 34 import javax.swing.JRadioButton ; 35 import javax.swing.JTextField ; 36 import javax.swing.SwingUtilities ; 37 import javax.swing.event.ChangeEvent ; 38 import javax.swing.event.ChangeListener ; 39 import javax.swing.event.DocumentEvent ; 40 import javax.swing.event.DocumentListener ; 41 42 48 class CreateSampleDialog 49 extends AbstractTabularOptionDialog 50 { 51 private int m_instrumentType; 52 private JTextField m_instrumentNameField; 53 private JTextField m_instrumentDescriptionField; 54 private JTextField m_sampleDescriptionField; 55 private String m_sampleDescription; 56 57 59 private String m_lastDefaultDescription; 60 61 private JTextField m_intervalField; 62 private long m_interval; 63 private JTextField m_sizeField; 64 private int m_size; 65 private JTextField m_leaseTimeField; 66 private long m_leaseTime; 67 private JCheckBox m_maintainLeaseCheckBox; 68 private Container m_sampleTypePanel; 69 private ButtonGroup m_sampleTypeGroup; 70 private int m_sampleType; 71 private JRadioButton m_sampleTypeCounter; 72 private JRadioButton m_sampleTypeMaximum; 73 private JRadioButton m_sampleTypeMinimum; 74 private JRadioButton m_sampleTypeMean; 75 76 79 87 CreateSampleDialog( InstrumentClientFrame frame, 88 String name, 89 String description, 90 int type ) 91 { 92 super( frame, "Create Instrument Sample", 93 AbstractOptionDialog.BUTTON_OK | AbstractOptionDialog.BUTTON_CANCEL ); 94 95 m_instrumentType = type; 96 m_instrumentNameField.setText( name ); 97 m_instrumentDescriptionField.setText( description ); 98 99 buildSampleTypeComponent(); 100 101 setInterval( 1000 ); 103 setSampleCount( 600 ); setLeaseTime( 600 ); 105 setMaintainLease( true ); 106 107 m_lastDefaultDescription = 108 InstrumentSampleUtils.getDefaultDescriptionForType( m_sampleType, m_interval ); 109 setSampleDescription( m_lastDefaultDescription ); 110 111 pack(); 112 } 113 114 117 122 protected String getMessage() 123 { 124 return "Please enter the parameters for the sample to be created."; 125 } 126 127 132 protected boolean validateFields() 133 { 134 return validateFields( false ); 135 } 136 137 140 146 protected String [] getMainPanelLabels() 147 { 148 return new String [] 149 { 150 "Instrument Name:", 151 "Instrument Description:", 152 "Sample Description:", 153 "Sample Interval (milliseconds):", 154 "Number of Samples:", 155 "Lease Time (Seconds):", 156 "Maintain Lease:", 157 "Sample Type:" 158 }; 159 } 160 161 166 protected Component [] getMainPanelComponents() 167 { 168 DocumentListener dl = new DocumentListener () { 169 public void changedUpdate( DocumentEvent event ) 170 { 171 } 172 public void insertUpdate( DocumentEvent event ) 173 { 174 CreateSampleDialog.this.validateFields( true ); 175 } 176 public void removeUpdate( DocumentEvent event ) 177 { 178 if ( m_sampleDescriptionField.getText().length() > 0 ) 182 { 183 CreateSampleDialog.this.validateFields( true ); 184 } 185 } 186 }; 187 188 m_instrumentNameField = new JTextField (); 189 m_instrumentNameField.setColumns( 40 ); 190 m_instrumentNameField.setEditable( false ); 191 192 m_instrumentDescriptionField = new JTextField (); 193 m_instrumentDescriptionField.setColumns( 40 ); 194 m_instrumentDescriptionField.setEditable( false ); 195 196 m_sampleDescriptionField = new JTextField (); 197 m_sampleDescriptionField.setColumns( 40 ); 198 m_sampleDescriptionField.getDocument().addDocumentListener( dl ); 199 200 m_intervalField = new JTextField (); 201 m_intervalField.setColumns( 10 ); 202 m_intervalField.getDocument().addDocumentListener( dl ); 203 204 m_sizeField = new JTextField (); 205 m_sizeField.setColumns( 4 ); 206 m_sizeField.getDocument().addDocumentListener( dl ); 207 208 m_leaseTimeField = new JTextField (); 209 m_leaseTimeField.setColumns( 10 ); 210 m_leaseTimeField.getDocument().addDocumentListener( dl ); 211 212 m_maintainLeaseCheckBox = new JCheckBox (); 213 214 m_sampleTypePanel = Box.createVerticalBox(); 215 216 Box intervalBar = Box.createHorizontalBox(); 218 intervalBar.add( m_intervalField ); 219 intervalBar.add( Box.createHorizontalStrut( 20 ) ); 220 intervalBar.add( createIntervalButton( "1 Second", 1000, 600, 600 ) ); 221 intervalBar.add( Box.createHorizontalStrut( 5 ) ); 222 intervalBar.add( createIntervalButton( "1 Minute", 60000, 1440, 86400 ) ); 223 intervalBar.add( Box.createHorizontalStrut( 5 ) ); 224 intervalBar.add( createIntervalButton( "1 Hour", 3600000, 672, 86400 ) ); 225 226 Box leaseTimeBar = Box.createHorizontalBox(); 227 leaseTimeBar.add( m_leaseTimeField ); 228 leaseTimeBar.add( Box.createHorizontalStrut( 20 ) ); 229 leaseTimeBar.add( createLeaseTimeButton( "10 Minutes", 600 ) ); 230 leaseTimeBar.add( Box.createHorizontalStrut( 5 ) ); 231 leaseTimeBar.add( createLeaseTimeButton( "1 Hour", 3600 ) ); 232 leaseTimeBar.add( Box.createHorizontalStrut( 5 ) ); 233 leaseTimeBar.add( createLeaseTimeButton( "1 Day", 86400 ) ); 234 235 return new Component [] 236 { 237 m_instrumentNameField, 238 m_instrumentDescriptionField, 239 m_sampleDescriptionField, 240 intervalBar, 241 m_sizeField, 242 leaseTimeBar, 243 m_maintainLeaseCheckBox, 244 m_sampleTypePanel 245 }; 246 } 247 248 251 private JButton createIntervalButton( final String label, 252 final long interval, 253 final int size, 254 final long leaseTime ) 255 { 256 Action action = new AbstractAction ( label ) 257 { 258 public void actionPerformed( ActionEvent event ) 259 { 260 CreateSampleDialog.this.setInterval( interval ); 261 CreateSampleDialog.this.setSampleCount( size ); 262 CreateSampleDialog.this.setLeaseTime( leaseTime ); 263 } 264 }; 265 return new JButton ( action ); 266 } 267 268 private JButton createLeaseTimeButton( final String label, 269 final long leaseTime ) 270 { 271 Action action = new AbstractAction ( label ) 272 { 273 public void actionPerformed( ActionEvent event ) 274 { 275 CreateSampleDialog.this.setLeaseTime( leaseTime ); 276 } 277 }; 278 return new JButton ( action ); 279 } 280 281 284 private void buildSampleTypeComponent() 285 { 286 ChangeListener cl = new ChangeListener () 287 { 288 public void stateChanged( ChangeEvent event ) 289 { 290 if ( ((JRadioButton )event.getSource()).isSelected() ) 291 { 292 CreateSampleDialog.this.validateFields( true ); 294 } 295 } 296 }; 297 298 m_sampleTypeGroup = new ButtonGroup (); 299 m_sampleTypeCounter = new JRadioButton ( "Count over each sample" ); 300 m_sampleTypeMaximum = new JRadioButton ( "Maximum value over each sample" ); 301 m_sampleTypeMinimum = new JRadioButton ( "Minumum value over each sample" ); 302 m_sampleTypeMean = new JRadioButton ( "Mean value over each sample" ); 303 304 switch ( m_instrumentType ) 305 { 306 case InstrumentData.INSTRUMENT_TYPE_COUNTER: 307 m_sampleTypePanel.add( m_sampleTypeCounter ); 308 m_sampleTypeGroup.add( m_sampleTypeCounter ); 309 310 m_sampleTypeCounter.setSelected( true ); 311 m_sampleType = InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_COUNTER; 312 break; 313 case InstrumentData.INSTRUMENT_TYPE_VALUE: 314 m_sampleTypePanel.add( m_sampleTypeMaximum ); 315 m_sampleTypeGroup.add( m_sampleTypeMaximum ); 316 317 m_sampleTypePanel.add( m_sampleTypeMinimum ); 318 m_sampleTypeGroup.add( m_sampleTypeMinimum ); 319 320 m_sampleTypePanel.add( m_sampleTypeMean ); 321 m_sampleTypeGroup.add( m_sampleTypeMean ); 322 323 m_sampleTypeMaximum.setSelected( true ); 324 m_sampleType = InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM; 325 break; 326 default: 327 break; 329 } 330 331 m_sampleTypeCounter.addChangeListener( cl ); 334 m_sampleTypeMaximum.addChangeListener( cl ); 335 m_sampleTypeMinimum.addChangeListener( cl ); 336 m_sampleTypeMean.addChangeListener( cl ); 337 } 338 339 344 void setSampleDescription( String sampleDescription ) 345 { 346 m_sampleDescription = sampleDescription; 347 m_sampleDescriptionField.setText( sampleDescription ); 348 349 validateFields( true ); 351 } 352 353 358 String getSampleDescription() 359 { 360 return m_sampleDescription; 361 } 362 363 368 void setInterval( long interval ) 369 { 370 m_interval = interval; 371 m_intervalField.setText( Long.toString( interval ) ); 372 373 validateFields( true ); 375 } 376 377 382 long getInterval() 383 { 384 return m_interval; 385 } 386 387 392 void setSampleCount( int size ) 393 { 394 m_size = size; 395 m_sizeField.setText( Integer.toString( size ) ); 396 } 397 398 403 int getSampleCount() 404 { 405 return m_size; 406 } 407 408 413 void setLeaseTime( long leaseTime ) 414 { 415 m_leaseTime = leaseTime; 416 m_leaseTimeField.setText( Long.toString( leaseTime ) ); 417 } 418 419 424 long getLeaseTime() 425 { 426 return m_leaseTime; 427 } 428 429 435 void setMaintainLease( boolean maintainLease ) 436 { 437 m_maintainLeaseCheckBox.setSelected( maintainLease ); 438 } 439 440 445 boolean getMaintainLease() 446 { 447 return m_maintainLeaseCheckBox.isSelected(); 448 } 449 450 455 void setSampleType( int type ) 456 { 457 m_sampleType = type; 458 459 switch ( type ) 460 { 461 case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_COUNTER: 462 m_sampleTypeCounter.setSelected( true ); 463 break; 464 case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM: 465 m_sampleTypeMaximum.setSelected( true ); 466 break; 467 case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MEAN: 468 m_sampleTypeMean.setSelected( true ); 469 break; 470 case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MINIMUM: 471 m_sampleTypeMinimum.setSelected( true ); 472 break; 473 default: 474 break; 475 } 476 477 validateFields( true ); 479 } 480 481 486 int getSampleType() 487 { 488 return m_sampleType; 489 } 490 491 498 private boolean validateFields( boolean quiet ) 499 { 500 boolean intervalOk = true; 502 long interval = 0; 503 try 504 { 505 interval = Long.parseLong( m_intervalField.getText().trim() ); 506 } 507 catch ( NumberFormatException e ) 508 { 509 intervalOk = false; 510 } 511 if ( ( interval < 100 ) || ( interval > 24 * 60 * 60 * 1000 ) ) 512 { 513 intervalOk = false; 514 } 515 if ( intervalOk ) 516 { 517 m_interval = interval; 518 m_intervalField.setForeground( null ); 519 } 520 else 521 { 522 m_intervalField.setForeground( Color.red ); 523 if ( !quiet ) 524 { 525 JOptionPane.showMessageDialog( this, "Please enter a valid interval. (100ms - 24hrs, 86400000)", 526 "Invalid interval", JOptionPane.ERROR_MESSAGE ); 527 return false; 528 } 529 } 530 531 boolean sizeOk = true; 533 int size = 0; 534 try 535 { 536 size = Integer.parseInt( m_sizeField.getText().trim() ); 537 } 538 catch ( NumberFormatException e ) 539 { 540 sizeOk = false; 541 } 542 if ( ( size < 1 ) || ( size > 2048 ) ) 543 { 544 sizeOk = false; 545 } 546 if ( sizeOk ) 547 { 548 m_size = size; 549 m_sizeField.setForeground( null ); 550 } 551 else 552 { 553 m_sizeField.setForeground( Color.red ); 554 if ( !quiet ) 555 { 556 JOptionPane.showMessageDialog( this, "Please enter a valid size. (1 - 2048)", 557 "Invalid size", JOptionPane.ERROR_MESSAGE ); 558 return false; 559 } 560 } 561 562 boolean leaseTimeOk = true; 564 int leaseTime = 0; 565 try 566 { 567 leaseTime = Integer.parseInt( m_leaseTimeField.getText().trim() ); 568 } 569 catch ( NumberFormatException e ) 570 { 571 leaseTimeOk = false; 572 } 573 if ( ( leaseTime < 60 ) || ( leaseTime > ( size * interval / 1000 ) + 86400 ) ) 574 { 575 leaseTimeOk = false; 576 } 577 if ( leaseTimeOk ) 578 { 579 m_leaseTime = leaseTime * 1000L; 580 m_leaseTimeField.setForeground( null ); 581 } 582 else 583 { 584 m_leaseTimeField.setForeground( Color.red ); 585 if ( !quiet ) 586 { 587 JOptionPane.showMessageDialog( this, "Please enter a valid lease time. Must be " + 588 "between 1 minute (60) and 24 hours greater than the interval * size (" + 589 ( ( size * interval / 1000 ) + 86400 ) + ")", 590 "Invalid leaseTime", JOptionPane.ERROR_MESSAGE ); 591 return false; 592 } 593 } 594 595 if ( m_sampleTypeCounter.isSelected() ) 597 { 598 m_sampleType = InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_COUNTER; 599 } 600 else if ( m_sampleTypeMaximum.isSelected() ) 601 { 602 m_sampleType = InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM; 603 } 604 else if ( m_sampleTypeMean.isSelected() ) 605 { 606 m_sampleType = InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MEAN; 607 } 608 else if ( m_sampleTypeMinimum.isSelected() ) 609 { 610 m_sampleType = InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MINIMUM; 611 } 612 else 613 { 614 m_sampleType = -1; 616 } 617 618 String newDefaultDescription = 620 InstrumentSampleUtils.getDefaultDescriptionForType( m_sampleType, m_interval ); 621 622 String description = m_sampleDescriptionField.getText().trim(); 624 if ( ( description.length() == 0 ) || ( description.equals( m_lastDefaultDescription ) ) ) 625 { 626 if ( !description.equals( newDefaultDescription ) ) 627 { 628 description = newDefaultDescription; 630 631 final String setDesc = description; 634 SwingUtilities.invokeLater( new Runnable () 635 { 636 public void run() 637 { 638 CreateSampleDialog.this.m_sampleDescriptionField.setText( setDesc ); 639 } 640 } ); 641 } 642 } 643 m_sampleDescription = description; 644 645 m_lastDefaultDescription = newDefaultDescription; 647 648 return true; 649 } 650 } 651 652 | Popular Tags |