/* File: PopupDialogQuery.java */ package O2PlibSS.gui; import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener; import java.awt.event.WindowListener; /** * Class PopupDialogQuery is a generic popup query dialog window. * It displays a dialog window containing a editable * TextField. There are also 2 buttons ("Ok" and "Cancel") to pass the * information on. If you have one button the label is "Continue". *
*
* 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 PopupDialogQuery extends Dialog
implements ActionListener, ItemListener, WindowListener
{
/** frame id */
final static long
serialVersionUID= 0;
/** default # of columns */
final static int
DEF_COL_SIZE= 80;
/** for returning data back to caller */
public String
data;
/** # of columns to display */
private int
colSize;
/** popup frame instance */
private Frame
frame;
/** place text to be edited here */
TextField
textField;
/** for data label */
private Label
label;
/** for options */
private Panel
optionPanel;
/** opt. option choice list */
private Choice
optionChoice;
/** # of buttons to add.
* if 0, then none,
* if 1, then add CONTINUE,
* if 2 then add OK and CANCEL.
*/
private int
addButtonsCnt;
/** button pressed flag */
boolean
alertDone;
/** wait for button to be pushed */
boolean
sleepFlag;
/** Tried this instead of "this" */
ActionListener
listener;
/** DEF_COL_SIZE spaces */
private String
spaces;
/** list of option values if present */
String
optionValues[]= null;
/** optionValues[0:nOptions] */
int
nOptions= 0;
/**
* PopupDialogQuery() - Constructor
* @param f is frame of parent
* @param addButtonsCnt is # of buttons to use where: 1 is (OK),
* 2 is (Continue, Cancel), 3 is OptionsChoice & (Continue, Cancel)
* @see #startPopupDialog
*/
public PopupDialogQuery(Frame f, int addButtonsCnt )
{ /* PopupDialogQuery */
super(f,"dialog box",true);
/* [1] set some defaults */
this.addButtonsCnt= addButtonsCnt;
colSize= DEF_COL_SIZE;
data= spaces;
frame= f;
alertDone= false;
spaces=
" ";
/* [2] create popup and hide it for use later */
this.startPopupDialog("Dialog",colSize);
} /* PopupDialogQuery */
/**
* startPopupDialog() - create a hidden dialog panel within a frame.
* @param windowTitle is the title of the dialog window
* @param colSize is the size of the textField
*/
public void startPopupDialog(String windowTitle, int colSize)
{ /* startPopupDialog */
Panel buttonPanel= null; /* place buttons here */
Button
ok, /* update data */
cancel; /* use default data */
GridLayout gl; /* for layout of text fields, label, etc */
/* [1] Initialize */
gl= new GridLayout(4,1);
this.setLayout(gl); /* set gridlayout to frame */
/* [1.1] Create User instruction label */
label= new Label(spaces);
/* [2] Create the buttons and arrange to handle button clicks */
if(addButtonsCnt>0)
{ /* add button panel */
buttonPanel= new Panel();
if(addButtonsCnt==1)
ok= new Button("Continue");
else ok= new Button("Ok");
ok.addActionListener(this);
buttonPanel.add("Center",ok);
if(addButtonsCnt==2)
{
cancel= new Button(" Cancel");
cancel.addActionListener(this);
buttonPanel.add("Center", cancel);
}
} /* add button panel */
/* [3] add data text fields to panel */
this.add(label); /* add to grid. data description label */
if(addButtonsCnt>=2)
{
optionPanel= new Panel();
this.add(optionPanel);
textField= new TextField(colSize);
this.add(textField); /* editable text */
}
/* [4] add buttons panel */
if(buttonPanel!=null)
this.add(buttonPanel); /* buttons (ok & cancel) */
this.addWindowListener(this); /* listener for window events */
/* [5] add components and create frame */
this.setTitle(windowTitle); /* frame title */
this.pack();
/* Center frame on the screen, PC only */
Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
Point pos= new Point((screen.width-frame.getSize().width)/2,
(screen.height-frame.getSize().height)/2);
this.setLocation(pos);
this.setVisible(false); /* hide frame which can be shown later */
} /* startPopupDialog */
/**
* updatePopupDialog() - display/unhide popup dialog frame and
* set new values.
* Remove recreate actionListeners & components.
* @param defaultDataMsg is the label for textField
* @param defaultDatais the data for textField
* @param optionValues is the list of option values
* @param nOptions is the number of options
*/
public void updatePopupDialog(String defaultDataMsg, String defaultData,
String optionValues[], int nOptions)
{ /* updatePopupDialog */
/* [1] remove components so they can updated below */
alertDone= false; /* reset the flag */
data= defaultData; /* store default data */
String dataMsg= (addButtonsCnt==2)
? "Enter "+defaultDataMsg
: defaultDataMsg; /* setup data label */
label.setText(dataMsg);
/* [2] add data text fields to panel */
/* Remove old option entries */
if(optionChoice!=null)
optionPanel.remove(optionChoice);
optionChoice= null;
if(addButtonsCnt==2)
{
if(optionValues!=null)
{ /* add option choice */
optionChoice= new Choice();
optionPanel.add(optionChoice);
for(int i=0;i