FileDocCategorySizeDatePackage
YesNoDialog.javaAPI DocExample7915Mon Sep 22 13:30:32 BST 1997oreilly.beans.yesno

YesNoDialog

public class YesNoDialog extends Object

Fields Summary
protected String
message
protected String
title
protected String
yesLabel
protected String
noLabel
protected String
cancelLabel
protected int
alignment
protected Font
font
protected Color
background
protected Color
foreground
public static final int
LEFT
public static final int
RIGHT
public static final int
CENTER
protected Vector
listeners
This field holds a list of registered ActionListeners. Vector is internally synchronized to prevent race conditions
Constructors Summary
public YesNoDialog()
The no-argument bean constructor, with default property values

    this("Question", "Your\nMessage\nHere", "Yes", "No", "Cancel", LEFT);
  
public YesNoDialog(String title, String message, String yesLabel, String noLabel, String cancelLabel, int alignment)
A constructor for programmers using this class "by hand"

    this.title = title;
    this.message = message;
    this.yesLabel = yesLabel;
    this.noLabel = noLabel;
    this.cancelLabel = cancelLabel;
    this.alignment = alignment;
  
Methods Summary
public voidaddAnswerListener(AnswerListener l)
Register an action listener to be notified when a button is pressed


               
      
    listeners.addElement(l);
  
public voiddisplay()
This method makes the bean display the dialog box

    // Create a frame with the specified title.  It would be nice to
    // use a Dialog, but that needs to be passed a Frame argument, and
    // the BDK beanbox tool only seems to work with no-argument methods.
    final Frame frame = new Frame(title);

    // Specify a LayoutManager for it.
    frame.setLayout(new BorderLayout(15, 15));

    // Specify font and colors, if any are specified.
    if (font != null) frame.setFont(font);
    if (background != null) frame.setBackground(background);
    if (foreground != null) frame.setForeground(foreground);

    // Put the message label in the middle of the window.
    frame.add("Center", new MultiLineLabel(message, 20, 20, alignment));

    // Create an action listener for use by the buttons of the dialog.
    // When a button is pressed, this listener first closes the dialog box.
    // Then, it creates an AnswerEvent object that corresponds to the
    // button that was pressed, and send that new event to all registered
    // listeners, using the fireEvent() method defined above.
    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        frame.dispose();     // pop down window
        if (listeners != null) {      // notify any registered listeners
          String cmd = e.getActionCommand();
          int type;
          if (cmd.equals("yes")) type = AnswerEvent.YES;
          else if (cmd.equals("no")) type = AnswerEvent.NO;

          else type = AnswerEvent.CANCEL;
          fireEvent(new AnswerEvent(YesNoDialog.this, type));
        }
      }
    };

    // Create a panel for the dialog buttons and put it at the bottom
    // of the dialog.  Specify a FlowLayout layout manager for it.
    Panel buttonbox = new Panel();
    buttonbox.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 15));
    frame.add("South", buttonbox);

    // Create each specified button, specifying the action listener
    // and action command for each, and adding them to the buttonbox
    if ((yesLabel != null) && (yesLabel.length() > 0)) {
      Button yes = new Button(yesLabel);        // Create button.
      yes.setActionCommand("yes");              // Set action command.
      yes.addActionListener(listener);          // Set listener.
      buttonbox.add(yes);                       // Add button to the panel.
    }
    if ((noLabel != null) && (noLabel.length() > 0)) {
      Button no = new Button(noLabel);
      no.setActionCommand("no");
      no.addActionListener(listener);
      buttonbox.add(no);
    }
    if ((cancelLabel != null) && (cancelLabel.length() > 0)) {
      Button cancel = new Button(cancelLabel);
      cancel.setActionCommand("cancel");
      cancel.addActionListener(listener);
      buttonbox.add(cancel);
    }

    // Finally, set the dialog to its preferred size and display it.
    frame.pack();
    frame.show();
  
public voidfireEvent(AnswerEvent e)
Send an event to all registered listeners

    // Make a copy of the list and fire the events using that copy.
    // This means that listeners can be added or removed from the original
    // list in response to this event.  We ought to be able to just use an
    // enumeration for the vector, but that doesn't copy the list internally.
    Vector list = (Vector) listeners.clone();
    for(int i = 0; i < list.size(); i++) {
      AnswerListener listener = (AnswerListener)list.elementAt(i);
      switch(e.getID()) {
      case AnswerEvent.YES: listener.yes(e); break;
      case AnswerEvent.NO:  listener.no(e); break;
      case AnswerEvent.CANCEL: listener.cancel(e); break;
      }
    }
  
public intgetAlignment()

 return alignment; 
public java.awt.ColorgetBackground()

 return background; 
public java.lang.StringgetCancelLabel()

 return cancelLabel; 
public java.awt.FontgetFont()

 return font; 
public java.awt.ColorgetForeground()

 return foreground; 
public java.lang.StringgetMessage()


  // Methods to query all of the bean properties.
      return message; 
public java.lang.StringgetNoLabel()

 return noLabel; 
public java.lang.StringgetTitle()

 return title; 
public java.lang.StringgetYesLabel()

 return yesLabel; 
public static voidmain(java.lang.String[] args)
A main method that demonstrates how to use this class, and allows testing

    // Create an instance of InfoDialog, with title and message specified:
    YesNoDialog d =
      new YesNoDialog("YesNoDialog Test",
                      "There are unsaved files.\n" +
                      "Do you want to save them before quitting?",
                      "Yes, save and quit",
                      "No, quit without saving",
                      "Cancel; don't quit",
                      YesNoDialog.CENTER);
    // Register an action listener for the dialog.  This one just prints
    // the results out to the console.
    d.addAnswerListener(new AnswerListener() {
      public void yes(AnswerEvent e) { System.out.println("Yes"); }
      public void no(AnswerEvent e) { System.out.println("No"); }
      public void cancel(AnswerEvent e) { System.out.println("Cancel"); }
    });

    // Now pop the dialog up.  It will pop itself down automatically.
    d.display();
  
public voidremoveAnswerListener(AnswerListener l)
Remove an Answer listener from our list of interested listeners

    listeners.removeElement(l);
  
public voidsetAlignment(int a)

 alignment = a; 
public voidsetBackground(java.awt.Color bg)

 background = bg; 
public voidsetCancelLabel(java.lang.String l)

 cancelLabel = l; 
public voidsetFont(java.awt.Font f)

 font = f; 
public voidsetForeground(java.awt.Color fg)

 foreground = fg; 
public voidsetMessage(java.lang.String m)

 message = m; 
public voidsetNoLabel(java.lang.String l)

 noLabel = l; 
public voidsetTitle(java.lang.String t)

 title=t; 
public voidsetYesLabel(java.lang.String l)

 yesLabel = l;