package Nodepad; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * ButtonPanel.java * * * Created: Tue Dec 24 13:43:52 2002 * * @author Stuart C. Shapiro * @version * * A panel of buttons for a NodePad. * */ public class ButtonPanel extends JPanel implements ActionListener { Pad pad; // The sketchpad controlled by these buttons. int initVal; // The integer value to be given to the next node created. JTextField initReg; // Contains initVal as a string. JRadioButton moveButton, transferButton; // To select the action. /** * Creates a button panel to control a node sketchpad. * * @param p the Pad controlled by these buttons. */ public ButtonPanel (Pad p){ pad = p; ButtonGroup group = new ButtonGroup(); /* * If the transferButton is selected, * mouse dragging moves a value from one node to another. */ transferButton = new JRadioButton("Transfer"); {Font currFont = transferButton.getFont(); transferButton.setFont(currFont.deriveFont(2*currFont.getSize2D()));} group.add(transferButton); add(transferButton); /* * if the moveButton is selected, * mouse dragging moves a node. */ moveButton = new JRadioButton("Move"); {Font currFont = moveButton.getFont(); moveButton.setFont(currFont.deriveFont(2*currFont.getSize2D()));} moveButton.setSelected(true); group.add(moveButton); add(moveButton); JLabel label = new JLabel("Initial value:", JLabel.RIGHT); {Font currFont = label.getFont(); label.setFont(currFont.deriveFont(2*currFont.getSize2D()));} add(label); // A text field for setting the value of the next node. initVal = (int)(Math.random()*1000); initReg=new JTextField(""+initVal, 4); {Font currFont = initReg.getFont(); initReg.setFont(currFont.deriveFont(2*currFont.getSize2D()));} initReg.selectAll(); initReg.addActionListener(this); add(initReg); moveButton.addItemListener(new ItemListener() { // Sets the state to node moving. public void itemStateChanged(ItemEvent event){ if (moveButton.isSelected()) { pad.setMovingNodes(true); } // end of if (moveButton.isSelected()) } } ); transferButton.addItemListener(new ItemListener() { // Sets the state to node value transfer. public void itemStateChanged(ItemEvent event){ if (transferButton.isSelected()) { pad.setMovingNodes(false); } // end of if (transferButton.isSelected()) } } ); } /** * Makes the transferButton the selected one. * */ public void selectTransfer () { transferButton.setSelected(true); } /** * Adds a new node to the Pad controlled by this ButtonPanel. * The value of the new node is the integer entered into the JTextField. * That int must be in the range [-999, 9999]. * If not, no node is created, * and the text is returned to its previous value. * * @param evt the ActionEvent of pressing CR in the JTextField. */ public void actionPerformed(ActionEvent evt) { int newVal = initVal; try { newVal = Integer.parseInt(initReg.getText()); if (newVal<-999 || newVal>9999) { throw(new NumberFormatException()); } initVal = newVal; Node nde = new Node(pad,2,2,initVal); pad.add(nde); pad.repaint(nde.getBounds()); } catch (Exception e) { initReg.setText(""+initVal); Toolkit.getDefaultToolkit().beep(); } // end of try-catch initReg.selectAll(); } }// ButtonPanel