// JDraw - QuestionDialog class
// Written by Jim Calciano, implemented into JDraw by the rest of us
// November 18, 1998
// Class to help ask the user if they really want to do something
import java.awt.*;
import java.awt.event.*;
public class QuestionDialog extends Dialog{
Button yesbutton = new Button();
Button nobutton = new Button();
Button cbutton = new Button();
Label label = new Label();
Panel panel = new Panel();
public static final int YES = 1;
public static final int NO = 0;
public static final int CANCEL = 2;
int state;
public QuestionDialog(Frame parent){
super(parent,true);
add("North", label);
yesbutton.addActionListener(new PressListener());
nobutton.addActionListener(new PressListener());
cbutton.addActionListener(new PressListener());
panel.add(yesbutton);
panel.add(nobutton);
panel.add(cbutton);
add("South",panel);
pack();
}
public QuestionDialog(Frame parent, String message, String title){
super(parent,true);
label.setText(message);
add("North",label);
setTitle(title);
yesbutton.setLabel("Yes");
nobutton.setLabel("No");
cbutton.setLabel("Cancel");
yesbutton.addActionListener(new PressListener());
nobutton.addActionListener(new PressListener());
cbutton.addActionListener(new PressListener());
panel.add(yesbutton);
panel.add(nobutton);
panel.add(cbutton);
add("South",panel);
pack();
}
class PressListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals(yesbutton.getLabel())){
state = YES;
setVisible(false);
}
else if (e.getActionCommand().equals(nobutton.getLabel())){
state = NO;
setVisible(false);
}
else if (e.getActionCommand().equals(cbutton.getLabel())){
state = CANCEL;
setVisible(false);
}
}
}
public int getState(){
return(state);
}
public void setQuestion(String question){
label.setText(question);
label.setAlignment(1);
pack();
}
public void setYesText(String text){
yesbutton.setLabel(text);
panel.add(yesbutton);
add("South", panel);
pack();
}
public void setNoText(String text){
nobutton.setLabel(text);
panel.add(nobutton);
add("South",panel);
pack();
}
public void setCancelText(String text){
cbutton.setLabel(text);
panel.add(cbutton);
add("South",panel);
pack();
}
public void setTitle(String title){
super.setTitle(title);
pack();
}
public void setVisible(boolean yesorno){
if (yesorno) {
pack();
center();
}
super.setVisible(yesorno);
}
private void center(){
Dimension d = getSize();
Toolkit t = Toolkit.getDefaultToolkit();
Dimension s =t.getScreenSize();
setLocation((s.width - d.width)/2,
(s.height - d.height)/2);
}
}