| AVR Z-LINKŪ |

# ConfigurationDialog.java

Go to the documentation of this file.

    00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
    00002 package gui;
    00003
    00004 import java.awt.event.ActionEvent;
    00005 import java.awt.event.ActionListener;
    00006 import java.awt.event.ItemEvent;
    00007 import java.awt.event.ItemListener;
    00008 import java.awt.event.WindowAdapter;
    00009 import java.awt.event.WindowEvent;
    00010 import java.beans.PropertyChangeEvent;
    00011 import java.beans.PropertyChangeListener;
    00012
    00013 import javax.swing.JCheckBox;
    00014 import javax.swing.JComboBox;
    00015 import javax.swing.JDialog;
    00016 import javax.swing.JOptionPane;
    00017 import javax.swing.JTextField;
    00018
    00019
    00036 public class ConfigurationDialog extends JDialog implements ActionListener, ItemListener, PropertyChangeListener{
    00037
    00038         private static final long serialVersionUID = 1L;
    00039         public static final int CHANNEL     = 0;
    00040         public static final int PANID       = 1;
    00041         public static final int DEVICE_TYPE = 2;
    00042
    00043         final int END_DEVICE  = 0;
    00044         final int COORDINATOR = 1;
    00045
    00046         private JOptionPane optionPane;
    00047         private JTextField panID;
    00048         private JCheckBox endDevice,
    00049                                                 coordinator;
    00050         private JComboBox channels;
    00051
    00052         private String buttonString1 = "Configure";
    00053     private String buttonString2 = "Cancel";
    00054     private String chanString = "Select channel.";
    00055     private String deviceString  = "\r\nSelect device type.";
    00056     private String panString     = "\r\nSelect PANID (Hexadecimal). 0x";
    00057     private final String[] deviceType = { "EndDevice", "Coordinator" };
    00058     //String holding users choice of Channel-PANID-DeviceType.
    00059     private String[ ] userChoices = { "0B", "BAAD", deviceType[ END_DEVICE ] };
    00060
    00061     String[ ] channelString = { "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
    00062                                             "21", "22", "23", "24", "25", "26" };
    00063
    00064     String[ ] hexLookupTable = { "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14",
    00065                                      "15", "16", "17", "18", "19", "1A" };
    00066
    00077         public ConfigurationDialog( MainFrame parent ){
    00078
    00079                 super( parent, true );
    00080                 this.setTitle( "Device Options" );
    00081
    00082                 channels = new JComboBox( channelString );
    00083                 channels.setEditable( false );
    00084                 channels.setSelectedIndex( 0 );
    00085                 channels.addActionListener( this );
    00086
    00087                 endDevice = new JCheckBox( "End Device" );
    00088                 endDevice.setSelected( true );
    00089                 endDevice.addItemListener( this );
    00090
    00091                 coordinator = new JCheckBox( "Coordinator" );
    00092                 coordinator.setSelected( false );
    00093                 coordinator.addItemListener( this );
    00094
    00095                 panID = new JTextField( "BAAD", 4 );
    00096
    00097                 Object[ ] inputFields = { chanString, channels, deviceString,
    00098                                                                   endDevice, coordinator, panString, panID };
    00099                 Object[ ] options = { buttonString1, buttonString2 };
    00100
    00101                 optionPane = new JOptionPane( inputFields,
    00102                                                                           JOptionPane.QUESTION_MESSAGE,
    00103                                                                           JOptionPane.OK_CANCEL_OPTION,
    00104                                                                           null,
    00105                                                                           options,
    00106                                                                           options[ 0 ]);
    00107
    00108                 this.setContentPane( optionPane );
    00109                 this.setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
    00110
    00111                 this.addWindowListener(new WindowAdapter() {
    00112             public void windowClosing(WindowEvent we) {
    00113
    00114             /*
    00115              * Instead of directly closing the window,
    00116              * we're going to change the JOptionPane's
    00117              * value property.
    00118              */
    00119                 optionPane.setValue( new Integer(
    00120                                     JOptionPane.CLOSED_OPTION ) );
    00121             }
    00122                 });
    00123
    00124                 optionPane.addPropertyChangeListener( this );
    00125         }
    00126
    00135         public void actionPerformed( ActionEvent ae ){
    00136
    00137                 int i;
    00138                 String channel = ( String ) ( ( JComboBox)ae.getSource( ) ).getSelectedItem( );
    00139
    00140                 for( i = 0; i < 16;i++ ){
    00141
    00142                         //Look for channel.
    00143                         if( channel.equals( channelString[ i ] ) ){
    00144                                 break;
    00145                         }
    00146                 }
    00147
    00148                 userChoices[ CHANNEL ] = hexLookupTable[ i ];
    00149         }
    00150
    00162         public void itemStateChanged( ItemEvent ie ){
    00163
    00164                 Object source = ie.getItemSelectable( );
    00165
    00166                 if( source == endDevice ){
    00167
    00168                         if( ie.getStateChange( ) == ItemEvent.DESELECTED ){
    00169
    00170                                 coordinator.setSelected( true );
    00171                                 userChoices[ DEVICE_TYPE ] = deviceType[ COORDINATOR ];
    00172                         }
    00173
    00174                         else{
    00175
    00176                                 coordinator.setSelected( false );
    00177                                 userChoices[ DEVICE_TYPE ] = deviceType[ END_DEVICE ];
    00178                         }
    00179                 }
    00180
    00181                 else{
    00182
    00183                         if( ie.getStateChange( ) == ItemEvent.DESELECTED ){
    00184
    00185                                 endDevice.setSelected( true );
    00186                                 userChoices[ DEVICE_TYPE ] = deviceType[ END_DEVICE ];
    00187                         }
    00188
    00189                         else{
    00190
    00191                                 endDevice.setSelected( false );
    00192                                 userChoices[ DEVICE_TYPE ] = deviceType[ COORDINATOR ];
    00193                         }
    00194                 }
    00195         }
    00196
    00204         public String[ ] getUserChoices( ){
    00205                 return userChoices;
    00206         }
    00207
    00216         public void propertyChange( PropertyChangeEvent pce ){
    00217
    00218                 String property = pce.getPropertyName( );
    00219
    00220                 //Handle events generated when user presses the buttons.
    00221                 //Buttons is not accessible with normal routines.
    00222                 if( isVisible( ) && ( pce.getSource( ) == optionPane )
    00223                     && ( JOptionPane.VALUE_PROPERTY.equals( property )
    00224                     || JOptionPane.INPUT_VALUE_PROPERTY.equals( property ) ) ){
    00225
    00226                         Object value = optionPane.getValue( );
    00227
    00228                     if( value == JOptionPane.UNINITIALIZED_VALUE ){
    00229                         //ignore reset
    00230                         return;
    00231                     }
    00232
    00233                     //Reset the JOptionPane's value.
    00234                     //If you don't do this, then if the user
    00235                     //presses the same button next time, no
    00236                     //property change event will be fired.
    00237                     optionPane.setValue( JOptionPane.UNINITIALIZED_VALUE );
    00238
    00239                     //OK pressed
    00240                     if( value.equals( buttonString1 ) ){
    00241
    00242                         String readPANID = panID.getText( );
    00243                         readPANID.toUpperCase( );
    00244
    00245                         //Must be a four digit number.
    00246                         if( readPANID.length(  ) < 5 ){
    00247
    00248                                 for( int i = 0; i < readPANID.length( ); i++ ){
    00249
    00250                                         if( !( ( ( readPANID.charAt( i ) >= '0' ) && ( readPANID.charAt( i ) <= '9' ) ) ||
    00251                                                  ( ( readPANID.charAt( i ) >= 'A' ) && ( readPANID.charAt( i ) <= 'F' ) ) ) ){
    00252
    00253                                                 return;
    00254                                         }
    00255                                 }
    00256
    00257                                 userChoices[ PANID ] = readPANID;
    00258                                 this.hideDialog( );
    00259                         }
    00260
    00261                         else{
    00262                                 return;
    00263                         }
    00264                     }
    00265
    00266                     //Cancel Pressed or closing window.
    00267                     else{
    00268                         this.hideDialog( );
    00269                     }
    00270                 }
    00271         }
    00272
    00278         public void hideDialog( ){
    00279                 this.setVisible( false );
    00280         }
    00281 }
    00282

@DOC_TITLE@