KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > CreateSampleDialog


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.Box JavaDoc;
30 import javax.swing.ButtonGroup JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JCheckBox JavaDoc;
33 import javax.swing.JOptionPane JavaDoc;
34 import javax.swing.JRadioButton JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.event.ChangeEvent JavaDoc;
38 import javax.swing.event.ChangeListener JavaDoc;
39 import javax.swing.event.DocumentEvent JavaDoc;
40 import javax.swing.event.DocumentListener JavaDoc;
41
42 /**
43  *
44  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
45  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
46  * @since 4.1
47  */

48 class CreateSampleDialog
49     extends AbstractTabularOptionDialog
50 {
51     private int m_instrumentType;
52     private JTextField JavaDoc m_instrumentNameField;
53     private JTextField JavaDoc m_instrumentDescriptionField;
54     private JTextField JavaDoc m_sampleDescriptionField;
55     private String JavaDoc m_sampleDescription;
56     
57     /** Remembers the last default description so we can tell whether or not the
58      * user has modified the description manually. */

59     private String JavaDoc m_lastDefaultDescription;
60     
61     private JTextField JavaDoc m_intervalField;
62     private long m_interval;
63     private JTextField JavaDoc m_sizeField;
64     private int m_size;
65     private JTextField JavaDoc m_leaseTimeField;
66     private long m_leaseTime;
67     private JCheckBox JavaDoc m_maintainLeaseCheckBox;
68     private Container JavaDoc m_sampleTypePanel;
69     private ButtonGroup JavaDoc m_sampleTypeGroup;
70     private int m_sampleType;
71     private JRadioButton JavaDoc m_sampleTypeCounter;
72     private JRadioButton JavaDoc m_sampleTypeMaximum;
73     private JRadioButton JavaDoc m_sampleTypeMinimum;
74     private JRadioButton JavaDoc m_sampleTypeMean;
75     
76     /*---------------------------------------------------------------
77      * Constructors
78      *-------------------------------------------------------------*/

79     /**
80      * Creates a new CreateSampleDialog.
81      *
82      * @param frame Frame which owns the dialog.
83      * @param name Name of the instrument.
84      * @param description Description of the instrument.
85      * @param type Type of the instrument.
86      */

87     CreateSampleDialog( InstrumentClientFrame frame,
88                         String JavaDoc name,
89                         String JavaDoc 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         // Set the default values.
102
setInterval( 1000 );
103         setSampleCount( 600 ); // 10 minutes of history
104
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     /*---------------------------------------------------------------
115      * AbstractOptionDialog Methods
116      *-------------------------------------------------------------*/

117     /**
118      * Returns the message to show at the top of the dialog.
119      *
120      * @return The text of the message.
121      */

122     protected String JavaDoc getMessage()
123     {
124         return "Please enter the parameters for the sample to be created.";
125     }
126     
127     /**
128      * Goes through and validates the fields in the dialog.
129      *
130      * @return True if the fields were Ok.
131      */

132     protected boolean validateFields()
133     {
134         return validateFields( false );
135     }
136     
137     /*---------------------------------------------------------------
138      * AbstractTabularOptionDialog Methods
139      *-------------------------------------------------------------*/

140     /**
141      * Returns an array of labels to use for the components returned from
142      * getMainPanelComponents().
143      *
144      * @returns An array of labels.
145      */

146     protected String JavaDoc[] getMainPanelLabels()
147     {
148         return new String JavaDoc[]
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     /**
162      * Returns an array of components to show in the main panel of the dialog.
163      *
164      * @returns An array of components.
165      */

166     protected Component JavaDoc[] getMainPanelComponents()
167     {
168         DocumentListener JavaDoc dl = new DocumentListener JavaDoc() {
169             public void changedUpdate( DocumentEvent JavaDoc event )
170             {
171             }
172             public void insertUpdate( DocumentEvent JavaDoc event )
173             {
174                 CreateSampleDialog.this.validateFields( true );
175             }
176             public void removeUpdate( DocumentEvent JavaDoc event )
177             {
178                 // When the description is replaced, a remove event is fired when its
179
// value is "". If we validate on that then we will go into an infinite
180
// loop.
181
if ( m_sampleDescriptionField.getText().length() > 0 )
182                 {
183                     CreateSampleDialog.this.validateFields( true );
184                 }
185             }
186         };
187         
188         m_instrumentNameField = new JTextField JavaDoc();
189         m_instrumentNameField.setColumns( 40 );
190         m_instrumentNameField.setEditable( false );
191         
192         m_instrumentDescriptionField = new JTextField JavaDoc();
193         m_instrumentDescriptionField.setColumns( 40 );
194         m_instrumentDescriptionField.setEditable( false );
195         
196         m_sampleDescriptionField = new JTextField JavaDoc();
197         m_sampleDescriptionField.setColumns( 40 );
198         m_sampleDescriptionField.getDocument().addDocumentListener( dl );
199         
200         m_intervalField = new JTextField JavaDoc();
201         m_intervalField.setColumns( 10 );
202         m_intervalField.getDocument().addDocumentListener( dl );
203         
204         m_sizeField = new JTextField JavaDoc();
205         m_sizeField.setColumns( 4 );
206         m_sizeField.getDocument().addDocumentListener( dl );
207         
208         m_leaseTimeField = new JTextField JavaDoc();
209         m_leaseTimeField.setColumns( 10 );
210         m_leaseTimeField.getDocument().addDocumentListener( dl );
211         
212         m_maintainLeaseCheckBox = new JCheckBox JavaDoc();
213         
214         m_sampleTypePanel = Box.createVerticalBox();
215         
216         // Create a series of buttons to help the users work efficiently
217
Box JavaDoc 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 JavaDoc 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 JavaDoc[]
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     /*---------------------------------------------------------------
249      * Methods
250      *-------------------------------------------------------------*/

251     private JButton JavaDoc createIntervalButton( final String JavaDoc label,
252                                           final long interval,
253                                           final int size,
254                                           final long leaseTime )
255     {
256         Action JavaDoc action = new AbstractAction JavaDoc( label )
257         {
258             public void actionPerformed( ActionEvent JavaDoc event )
259             {
260                 CreateSampleDialog.this.setInterval( interval );
261                 CreateSampleDialog.this.setSampleCount( size );
262                 CreateSampleDialog.this.setLeaseTime( leaseTime );
263             }
264         };
265         return new JButton JavaDoc( action );
266     }
267     
268     private JButton JavaDoc createLeaseTimeButton( final String JavaDoc label,
269                                            final long leaseTime )
270     {
271         Action JavaDoc action = new AbstractAction JavaDoc( label )
272         {
273             public void actionPerformed( ActionEvent JavaDoc event )
274             {
275                 CreateSampleDialog.this.setLeaseTime( leaseTime );
276             }
277         };
278         return new JButton JavaDoc( action );
279     }
280     
281     /**
282      * Builds the sample type component.
283      */

284     private void buildSampleTypeComponent()
285     {
286         ChangeListener JavaDoc cl = new ChangeListener JavaDoc()
287         {
288             public void stateChanged( ChangeEvent JavaDoc event )
289             {
290                 if ( ((JRadioButton JavaDoc)event.getSource()).isSelected() )
291                 {
292                     // Only validate on the selected value
293
CreateSampleDialog.this.validateFields( true );
294                 }
295             }
296         };
297         
298         m_sampleTypeGroup = new ButtonGroup JavaDoc();
299         m_sampleTypeCounter = new JRadioButton JavaDoc( "Count over each sample" );
300         m_sampleTypeMaximum = new JRadioButton JavaDoc( "Maximum value over each sample" );
301         m_sampleTypeMinimum = new JRadioButton JavaDoc( "Minumum value over each sample" );
302         m_sampleTypeMean = new JRadioButton JavaDoc( "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             // Unknown Type
328
break;
329         }
330         
331         // Add the change listeners down here so the initialization does not cause
332
// them to fire.
333
m_sampleTypeCounter.addChangeListener( cl );
334         m_sampleTypeMaximum.addChangeListener( cl );
335         m_sampleTypeMinimum.addChangeListener( cl );
336         m_sampleTypeMean.addChangeListener( cl );
337     }
338     
339     /**
340      * Sets the initial sample description to be shown in the TextField.
341      *
342      * @param sampleDescription The initial sample description.
343      */

344     void setSampleDescription( String JavaDoc sampleDescription )
345     {
346         m_sampleDescription = sampleDescription;
347         m_sampleDescriptionField.setText( sampleDescription );
348         
349         // Validate the fields quietly to update other values correctly.
350
validateFields( true );
351     }
352     
353     /**
354      * Returns the sample description set in the dialog.
355      *
356      * @return The sample description.
357      */

358     String JavaDoc getSampleDescription()
359     {
360         return m_sampleDescription;
361     }
362     
363     /**
364      * Sets the initial interval to be shown in the interval TextField.
365      *
366      * @param interval The initial interval.
367      */

368     void setInterval( long interval )
369     {
370         m_interval = interval;
371         m_intervalField.setText( Long.toString( interval ) );
372         
373         // Validate the fields quietly to update other values correctly.
374
validateFields( true );
375     }
376     
377     /**
378      * Returns the interval set in the dialog.
379      *
380      * @return The interval.
381      */

382     long getInterval()
383     {
384         return m_interval;
385     }
386     
387     /**
388      * Sets the initial size to be shown in the size TextField.
389      *
390      * @param size The initial size.
391      */

392     void setSampleCount( int size )
393     {
394         m_size = size;
395         m_sizeField.setText( Integer.toString( size ) );
396     }
397     
398     /**
399      * Returns the size set in the dialog.
400      *
401      * @return The size.
402      */

403     int getSampleCount()
404     {
405         return m_size;
406     }
407     
408     /**
409      * Sets the initial lease time to be shown in the lease time TextField.
410      *
411      * @param leaseTime The initial lease time.
412      */

413     void setLeaseTime( long leaseTime )
414     {
415         m_leaseTime = leaseTime;
416         m_leaseTimeField.setText( Long.toString( leaseTime ) );
417     }
418     
419     /**
420      * Returns the lease time set in the dialog.
421      *
422      * @return The lease time.
423      */

424     long getLeaseTime()
425     {
426         return m_leaseTime;
427     }
428     
429     /**
430      * Sets the initial maintain lease flag to be shown in the maintain lease
431      * CheckBox.
432      *
433      * @param maintainLease The initial maintain lease flag.
434      */

435     void setMaintainLease( boolean maintainLease )
436     {
437         m_maintainLeaseCheckBox.setSelected( maintainLease );
438     }
439     
440     /**
441      * Returns the maintain lease flag set in the dialog.
442      *
443      * @return The maintain lease flag.
444      */

445     boolean getMaintainLease()
446     {
447         return m_maintainLeaseCheckBox.isSelected();
448     }
449     
450     /**
451      * Sets the initial size to be shown in the size TextField.
452      *
453      * @param size The initial size.
454      */

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         // Validate the fields quietly to update other values correctly.
478
validateFields( true );
479     }
480     
481     /**
482      * Returns the type set in the dialog.
483      *
484      * @return The type.
485      */

486     int getSampleType()
487     {
488         return m_sampleType;
489     }
490     
491     /**
492      * Goes through and validates the fields in the dialog.
493      *
494      * @param quiet True if problems should be ignored.
495      *
496      * @return True if the fields were Ok.
497      */

498     private boolean validateFields( boolean quiet )
499     {
500         // Check the interval.
501
boolean intervalOk = true;
502         long interval = 0;
503         try
504         {
505             interval = Long.parseLong( m_intervalField.getText().trim() );
506         }
507         catch ( NumberFormatException JavaDoc 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         // Check the size.
532
boolean sizeOk = true;
533         int size = 0;
534         try
535         {
536             size = Integer.parseInt( m_sizeField.getText().trim() );
537         }
538         catch ( NumberFormatException JavaDoc 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         // Check the leaseTime.
563
boolean leaseTimeOk = true;
564         int leaseTime = 0;
565         try
566         {
567             leaseTime = Integer.parseInt( m_leaseTimeField.getText().trim() );
568         }
569         catch ( NumberFormatException JavaDoc 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         // Store the sample type
596
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             // Should never get here.
615
m_sampleType = -1;
616         }
617         
618         // Update the default description
619
String JavaDoc newDefaultDescription =
620             InstrumentSampleUtils.getDefaultDescriptionForType( m_sampleType, m_interval );
621         
622         // Check the description.
623
String JavaDoc description = m_sampleDescriptionField.getText().trim();
624         if ( ( description.length() == 0 ) || ( description.equals( m_lastDefaultDescription ) ) )
625         {
626             if ( !description.equals( newDefaultDescription ) )
627             {
628                 // Set the description to the default.
629
description = newDefaultDescription;
630                 
631                 // We can't change the description field directly because this is called from
632
// its change listener.
633
final String JavaDoc setDesc = description;
634                 SwingUtilities.invokeLater( new Runnable JavaDoc()
635                     {
636                         public void run()
637                         {
638                             CreateSampleDialog.this.m_sampleDescriptionField.setText( setDesc );
639                         }
640                     } );
641             }
642         }
643         m_sampleDescription = description;
644         
645         // Always remember the new default description
646
m_lastDefaultDescription = newDefaultDescription;
647         
648         return true;
649     }
650 }
651
652
Popular Tags