| OkCancelDialog |

    1   /*
    2    *  Copyright (c) 1998-2001, The University of Sheffield.
    3    *
    4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    5    *  software, licenced under the GNU Library General Public License,
    6    *  Version 2, June 1991 (in the distribution as file licence.html,
    7    *  and also available at http://gate.ac.uk/gate/licence.html).
    8    *
    9    *  Valentin Tablan 16/07/2001
    10   *
    11   *  $Id: OkCancelDialog.java,v 1.7 2001/11/16 15:15:28 valyt Exp $
    12   *
    13   */
    14
    15  package gate.gui;
    16
    17  import javax.swing.*;
    18  import javax.swing.event.*;
    19  import java.awt.*;
    20  import java.awt.event.*;
    21
    22  /**
    23   * A simple modal dialog that displays a component provided by the user along
    24   * with two buttons ("OK" and "Cancel").
    25   */
    26  public class OkCancelDialog extends JDialog {
    27
    28    protected OkCancelDialog(Frame owner, String title, Component contents){
    29      super(owner, title);
    30      init(contents);
    31    }
    32
    33    protected OkCancelDialog(Dialog owner, String title, Component contents){
    34      super(owner, title);
    35      init(contents);
    36    }
    37
    38    protected OkCancelDialog(String title, Component contents){
    39      super();
    40      setTitle(title);
    41      init(contents);
    42    }
    43
    44    protected void init(Component contents){
    45      MainFrame.getGuiRoots().add(this);
    46      //fill in the contents
    47      JPanel vBox = new JPanel();
    48      vBox.setLayout(new BoxLayout(vBox, BoxLayout.Y_AXIS));
    49
    50      JPanel contentsPanel = new JPanel();
    51      contentsPanel.add(contents);
    52      contentsPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    53
    54      vBox.add(contentsPanel);
    55
    56      JPanel buttonsBox = new JPanel();
    57      buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS));
    58      buttonsBox.setAlignmentX(Component.CENTER_ALIGNMENT);
    59      okButton = new JButton("OK");
    60      cancelButton = new JButton("Cancel");
    61      buttonsBox.add(Box.createHorizontalGlue());
    62      buttonsBox.add(okButton);
    63      buttonsBox.add(Box.createHorizontalStrut(20));
    64      buttonsBox.add(cancelButton);
    65      buttonsBox.add(Box.createHorizontalGlue());
    66
    67      vBox.add(buttonsBox);
    68      vBox.add(Box.createVerticalStrut(10));
    69
    70      getContentPane().add(vBox, BorderLayout.CENTER);
    71
    72
    73      okButton.addActionListener(new ActionListener() {
    74        public void actionPerformed(ActionEvent e) {
    75          userHasPressedOK = true;
    76          hide();
    77        }
    78      });
    79
    80      cancelButton.addActionListener(new ActionListener() {
    81        public void actionPerformed(ActionEvent e) {
    82          userHasPressedCancel = true;
    83          hide();
    84        }
    85      });
    86    }
    87
    88    public void dispose(){
    89      MainFrame.getGuiRoots().remove(this);
    90      super.dispose();
    91    }
    92
    93
    94    public void show(){
    95      setModal(true);
    96      userHasPressedOK = false;
    97      userHasPressedCancel = false;
    98      super.show();
    99    }
    100
    101   /**
    102    * @returns true if the user has selected the "OK" button.
    103    */
    104   public static boolean showDialog(Component parentComponent,
    105                                    Component contents,
    106                                    String title){
    107     //construct the dialog
    108     Window parent = null;
    109     if(parentComponent != null){
    110       parent = SwingUtilities.getWindowAncestor(parentComponent);
    111     }
    112     OkCancelDialog dialog;
    113     if(parent == null) dialog = new OkCancelDialog(title, contents);
    114     else if(parent instanceof Frame){
    115       dialog = new OkCancelDialog((Frame)parent, title, contents);
    116     } else{
    117       dialog = new OkCancelDialog((Dialog)parent, title, contents);
    118     }
    119
    120     //position the dialog
    121     dialog.pack();
    122     dialog.setLocationRelativeTo(parentComponent);
    123
    124     //kalina: make it fit the screen
    125     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    126     Dimension dialogSize = dialog.getSize();
    127     if (dialogSize.height > screenSize.height)
    128       dialogSize.height = screenSize.height;
    129     if (dialogSize.width > screenSize.width)
    130       dialogSize.width = screenSize.width;
    131     dialog.setSize(dialogSize);
    132     //end kalina
    133
    134     //show the dialog
    135     dialog.show();
    136     return dialog.userHasPressedOK;
    137   }
    138
    139   protected JButton okButton;
    140   protected JButton cancelButton;
    141   protected boolean userHasPressedOK;
    142   protected static boolean userHasPressedCancel;
    143 }

| OkCancelDialog |