00001
00002 import javax.swing.*;
00003 import javax.swing.event.*;
00004 import java.awt.*;
00005 import java.awt.event.*;
00006 import java.io.*;
00007
00008
00009 public class NodeConfigDialog extends JDialog
00010 implements ActionListener, ItemListener
00011 {
00012 int width;
00013 int height;
00014 Frame frame;
00015 Node node;
00016 Network network;
00017 TopologyPanel parent;
00018 String grp_selected, orig_grp;
00019 int selected_index, orig_grp_idx;
00020 JCheckBox rand;
00021 JComboBox grps_box;
00022 JTextField x_field, y_field, z_field;
00023 boolean addition = false;
00024
00025 public NodeConfigDialog( Frame f, Node n,
00026 TopologyPanel tp, boolean a ) {
00027 super( f, "Configure Node "+n.getId(), true );
00028 frame = f;
00029 width = TopologyPanel.dialog_width;
00030 height = TopologyPanel.dialog_height;
00031 node = n;
00032 parent = tp;
00033 network = tp.configd_net;
00034 selected_index = 0;
00035 addition = a;
00036 getContentPane().setLayout( new BorderLayout( width, height ));
00037 setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
00038
00039
00040 if ( node == null ) {
00041 addition = true;
00042 node = new Node();
00043 }
00044
00045 if ( addition ) {
00046 grp_selected = orig_grp = "New";
00047 } else {
00048
00049
00050
00051
00052
00053 }
00054
00055 rand = new JCheckBox( "Random" );
00056 rand.setSelected( node.getRand_loc());
00057 rand.addItemListener( this );
00058 JLabel loc_label = new JLabel( " Node" + node.getId() + " location:" );
00059 JLabel x_label = new JLabel( "x =" );
00060 x_field = new JTextField( node.getX()+"", 5 );
00061 JLabel y_label = new JLabel( "y =" );
00062 y_field = new JTextField( node.getY()+"", 5 );
00063 JLabel z_label = new JLabel( "z =" );
00064 z_field = new JTextField( node.getZ()+"", 5 );
00065
00066 JLabel grp_label = new JLabel( "Choose a node group:" );
00067 grps_box = new JComboBox();
00068 int i=0;
00069 orig_grp_idx = -1;
00070 for ( ; i<network.getGroups().size(); i++ ) {
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 }
00081 grps_box.addItem( "New" );
00082 if ( orig_grp_idx < 0 )
00083 orig_grp_idx = i;
00084
00085 grps_box.setSelectedIndex( orig_grp_idx );
00086 grps_box.addActionListener( this );
00087 grps_box.setLightWeightPopupEnabled( false );
00088
00089 JPanel grps_p = new JPanel();
00090 grps_p.setLayout( new BoxLayout( grps_p, BoxLayout.X_AXIS ));
00091 grps_p.add( grp_label );
00092 grps_p.add( Box.createRigidArea( new Dimension(5,0)));
00093 grps_p.add( grps_box );
00094
00095 JPanel rand_grps_p = new JPanel();
00096 rand_grps_p.setLayout( new BoxLayout( rand_grps_p, BoxLayout.X_AXIS ));
00097 rand_grps_p.add( rand );
00098 rand_grps_p.add( Box.createRigidArea( new Dimension(10,0)));
00099 rand_grps_p.add( grps_p );
00100
00101 JPanel xyz_p = new JPanel();
00102 xyz_p.setLayout( new BoxLayout( xyz_p, BoxLayout.X_AXIS ));
00103 xyz_p.add( Box.createRigidArea( new Dimension(5,0)));
00104 xyz_p.add( x_label );
00105 xyz_p.add( x_field );
00106 xyz_p.add( Box.createRigidArea( new Dimension(10,0)));
00107 xyz_p.add( y_label );
00108 xyz_p.add( y_field );
00109 xyz_p.add( Box.createRigidArea( new Dimension(10,0)));
00110 xyz_p.add( z_label );
00111 xyz_p.add( z_field );
00112 xyz_p.add( Box.createRigidArea( new Dimension(5,0)));
00113
00114 JPanel ok_p = new JPanel();
00115 ok_p.setLayout( new BoxLayout( ok_p, BoxLayout.X_AXIS ));
00116 Button ok_button = new Button( "Add Node to Network" );
00117 ok_button.addActionListener( this );
00118 Button cancel_button = new Button( "Cancel" );
00119 cancel_button.addActionListener( this );
00120 ok_p.add( ok_button );
00121 ok_p.add( cancel_button );
00122
00123 Panel p = new Panel();
00124 p.setLayout( new BorderLayout( width, height ));
00125 p.add( "North", rand_grps_p );
00126 p.add( "Center", xyz_p );
00127 p.add( "South", ok_p );
00128
00129 getContentPane().add( "Center", p );
00130 }
00131
00132 public void itemStateChanged( ItemEvent e ) {
00133 Object source = e.getItemSelectable();
00134 if ( source == rand ) {
00135 if ( rand.isSelected()) {
00136 node.setRand_loc( true );
00137 x_field.setText( node.getX()+"" );
00138 y_field.setText( node.getY()+"" );
00139 z_field.setText( node.getZ()+"" );
00140 }
00141 }
00142 }
00143
00144 public void actionPerformed( ActionEvent ae ) {
00145 String s = ae.getActionCommand();
00146 if ( s.equalsIgnoreCase( "Cancel" )) {
00147 node = null;
00148
00149 this.dispose();
00150 } else if ( s.equalsIgnoreCase( "Add Node to Network" )) {
00151 node.setX( Double.parseDouble( x_field.getText()));
00152 node.setY( Double.parseDouble( y_field.getText()));
00153 node.setZ( Double.parseDouble( z_field.getText()));
00154 if ( addition )
00155 network.getNodes().addElement( node );
00156
00157 if ( grp_selected.equalsIgnoreCase( "New" ) || selected_index == -1 ) {
00158
00159
00160
00161
00162
00163
00164
00165
00166 } else {
00167 if ( !orig_grp.equals( grp_selected )) {
00168 if ( !addition )
00169 ((NodeGroup)network.getGroups().elementAt(
00170 orig_grp_idx )).getNodes().removeElement( node );
00171 node.setMygroup(
00172 (NodeGroup)network.getGroups().elementAt( selected_index ));
00173 node.setSeq_num(((NodeGroup)network.getGroups().elementAt(
00174 selected_index )).getNodes().size());
00175 ((NodeGroup)network.getGroups().elementAt(
00176 selected_index )).getNodes().addElement( node );
00177
00178 }
00179 }
00180 parent.repaint();
00181 parent.refresher.repaint();
00182
00183 this.dispose();
00184
00185 } else {
00186 grp_selected = (String)((JComboBox)ae.getSource()).getSelectedItem();
00187 selected_index = ((JComboBox)ae.getSource()).getSelectedIndex();
00188 }
00189 }
00190 }
00191