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