/* File: PopupTextFieldDialog.java */ package O2PlibSS.gui; import java.awt.*; import java.awt.event.*; /** * Class PopupTextFieldDialog provides a popup text field with * a Done and Cancel buttons. The result will be in *

*

* This code is available at the HTMLtools project on SourceForge at * http://htmltools.sourceforge.org/ * under the "Common Public License Version 1.0" * * http://www.opensource.org/licenses/cpl1.0.php.

*

* It was derived and refactored from the open source * MAExplorer (http://maexplorer.sourceforge.org/), and * Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules. *

* $Date: 2009/07/03 11:45:56 $ $Revision: 1.28 $ *
* Copyright 2008, 2009 by Peter Lemkin * E-Mail: lemkin@users.sourceforge.net * http://lemkingroup.com/ */ public class PopupTextFieldDialog extends Dialog implements ActionListener, WindowListener { /* Popup */ /** frame id */ final static long serialVersionUID= 0; /** Frame where dialog box is added */ private Frame f; /** Message to display */ private String msg; /** Returned value from text field */ public String answer; /** Returned true if pressed DONE */ public boolean okFlag; /** text field */ private TextField tf; /** * PopupTextFieldDialog () - constructor * @param msg to display */ public PopupTextFieldDialog(Frame f, String msg, String defaultAnswer) { /* Popup */ super(f, "Message", true); this.f= f; this.msg= msg; answer= (defaultAnswer==null) ? "" : defaultAnswer; okFlag= false; popupTextField(); } /* PopupTextFieldDialog */ /** * popupTextField() - popup text field. When OK or Cancel is pressed * the okFlag is set true or false and the answer field is set * for true. */ public void popupTextField() { /* popupTextAreaViewer */ if(msg==null) msg= "Enter response and press Done"; else if(msg.length()<20) { /* add trailing spaces */ while(msg.length()<20) msg = msg + " "; } this.add("North",new Label(msg)); Panel ctrlPanel= new Panel(), textPanel= new Panel(new BorderLayout()); this.add("Center",textPanel); this.add("South",ctrlPanel); /* Add text area */ tf= new TextField(answer); textPanel.add("Center",tf); /* add buttons */ Button b; b= new Button("Done"); b.addActionListener(this); ctrlPanel.add(b); b= new Button("Cancel"); ctrlPanel.add(b); b.addActionListener(this); /* set size */ this.setSize(400,35); this.setTitle(msg); this.pack(); /* put the Dialog box in the middle of the frame */ Dimension myDim= this.getSize(), frameDim= f.getSize(), screenSize= getToolkit().getScreenSize(); Point loc= f.getLocation(); loc.translate((frameDim.width - myDim.width)/2, (frameDim.height - myDim.height)/2); loc.x= Math.max(0,Math.min(loc.x,screenSize.width-this.getSize().width)); loc.y= Math.max(0,Math.min(loc.y,screenSize.height-this.getSize().height)); this.setLocation(loc.x,loc.y); this.setVisible(true); } /* popupTextAreaViewer */ /** * actionPerformed() - handle action events * @param ae is the ActionEvent */ public void actionPerformed(java.awt.event.ActionEvent ae) { /* actionPerformed */ String cmd= ae.getActionCommand(); if(cmd.equals("Done")) { answer= tf.getText(); okFlag= true; close(); } else if(cmd.equals("Cancel")) { okFlag= false; close(); } } /* actionPerformed */ /** * close() - close this popup */ private void close() { /* close */ this.dispose(); } /* close */ /** * windowClosing() - close down the window - assume false. */ public void windowClosing(WindowEvent e) { /* close */ close(); } /* close */ /* Others not used at this time */ public void windowOpened(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) {} public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } } /* End of class Popup */