import java.awt.*; import java.awt.event.*; import java.util.TooManyListenersException; public class SignMe extends Panel implements ActionListener { protected SigningSurface scribble; // a fancy Canvas protected Button eraseButton, acceptButton; protected SignatureEventListener sigListener = null; //------------------------------------------------------------------- // SignMe constructor // build GUI as a BorderLayout-managed Panel containing: // - in its center slot: the SigningSurface // - in its south slot: a sub-Panel containing two sub-sub panels each holding: // - an Erase button // - an Accept button // register us as event listener for the 2 buttons //------------------------------------------------------------------- public SignMe() { Panel buttonsPanel, but1Panel, but2Panel; // "Erase" "Accept" setLayout(new BorderLayout()); // override default FlowLayout // instantiate individual GUI elements scribble = new SigningSurface(); buttonsPanel = new Panel(); but1Panel = new Panel(); but2Panel = new Panel(); eraseButton = new Button("Erase"); acceptButton = new Button("Accept"); // construct button panel but1Panel.add(eraseButton); but2Panel.add(acceptButton); buttonsPanel.add(but1Panel); buttonsPanel.add(but2Panel); add(scribble, "Center"); add(buttonsPanel, "South"); // register ourselves as interested click listener for both buttons eraseButton.addActionListener(this); acceptButton.addActionListener(this); } //------------------------------------------------------------------- // The ActionEvent listener method. The button clicks arrive here. //------------------------------------------------------------------- public void actionPerformed(ActionEvent buttonClick) { if ( buttonClick.getSource() == eraseButton ) { scribble.wipeSignature(); } if ( buttonClick.getSource() == acceptButton ) { System.out.println("user accepted signature!"); fireSignedEvent(); } } //------------------------------------------------------------------- // SignMe event listener (de)registration methods //------------------------------------------------------------------- public void addSignatureListener( SignatureEventListener listener) throws java.util.TooManyListenersException { if (sigListener != null) { throw new TooManyListenersException( "SignMe already has an event listener!"); } sigListener = listener; } //------------------------------------------------------------------- public void removeSignatureListener( SignatureEventListener listener) { if (sigListener == null) { throw new IllegalArgumentException( "Can't remove SignMe event listener because none currently set!"); } if (sigListener != listener) { throw new IllegalArgumentException( "Can't remove SignMe event listener because different one from currently set!"); } sigListener = null; } //------------------------------------------------------------------- // Internal event raising method //------------------------------------------------------------------- protected void fireSignedEvent() { SignatureEvent event; if ( sigListener != null ) { event = new SignatureEvent(this, scribble.getImage() ); sigListener.userSigned( event ); } } } // End of Class SignMe