001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 16/07/2001
011 *
012 * $Id: OkCancelDialog.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 *
014 */
015
016 package gate.gui;
017
018 import java.awt.*;
019 import java.awt.event.ActionEvent;
020 import java.awt.event.ActionListener;
021
022 import javax.swing.*;
023
024 /**
025 * A simple modal dialog that displays a component provided by the user along
026 * with two buttons ("OK" and "Cancel").
027 */
028 public class OkCancelDialog extends JDialog {
029
030 protected OkCancelDialog(Frame owner, String title, Component contents){
031 super(owner, title);
032 init(contents);
033 }
034
035 protected OkCancelDialog(Dialog owner, String title, Component contents){
036 super(owner, title);
037 init(contents);
038 }
039
040 protected OkCancelDialog(String title, Component contents){
041 super();
042 setTitle(title);
043 init(contents);
044 }
045
046 protected void init(Component contents){
047 MainFrame.getGuiRoots().add(this);
048
049 getContentPane().setLayout(new BorderLayout());
050
051 // //fill in the contents
052 // JPanel vBox = new JPanel();
053 // vBox.setLayout(new BoxLayout(vBox, BoxLayout.Y_AXIS));
054 //
055 // JPanel contentsPanel = new JPanel();
056 // contentsPanel.add(contents);
057 // contentsPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
058 //
059 // vBox.add(contentsPanel);
060
061 getContentPane().add(contents, BorderLayout.CENTER);
062
063 JPanel buttonsBox = new JPanel();
064 buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS));
065 buttonsBox.setAlignmentX(Component.CENTER_ALIGNMENT);
066 okButton = new JButton("OK");
067 cancelButton = new JButton("Cancel");
068 buttonsBox.add(Box.createHorizontalGlue());
069 buttonsBox.add(okButton);
070 buttonsBox.add(Box.createHorizontalStrut(20));
071 buttonsBox.add(cancelButton);
072 buttonsBox.add(Box.createHorizontalGlue());
073
074 Box vBox = Box.createVerticalBox();
075 vBox.add(Box.createVerticalStrut(10));
076 vBox.add(buttonsBox);
077 vBox.add(Box.createVerticalStrut(10));
078
079 getContentPane().add(vBox, BorderLayout.SOUTH);
080
081 Action applyAction = new AbstractAction() {
082 public void actionPerformed(ActionEvent e) {
083 userHasPressedOK = true;
084 setVisible(false);
085 }
086 };
087
088 Action cancelAction = new AbstractAction() {
089 public void actionPerformed(ActionEvent e) {
090 userHasPressedCancel = true;
091 setVisible(false);
092 }
093 };
094
095 // define keystrokes action bindings at the level of the main window
096 InputMap inputMap = ((JComponent)this.getContentPane()).
097 getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
098 ActionMap actionMap =
099 ((JComponent)this.getContentPane()).getActionMap();
100 inputMap.put(KeyStroke.getKeyStroke("ENTER"), "Apply");
101 actionMap.put("Apply", applyAction);
102 inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "Cancel");
103 actionMap.put("Cancel", cancelAction);
104
105 okButton.addActionListener(applyAction);
106 getRootPane().setDefaultButton(okButton);
107 cancelButton.addActionListener(cancelAction);
108 }
109
110 public void dispose(){
111 MainFrame.getGuiRoots().remove(this);
112 super.dispose();
113 }
114
115 /**
116 * @return true if the user has selected the "OK" button.
117 */
118 public static boolean showDialog(Component parentComponent,
119 Component contents,
120 String title){
121 //construct the dialog
122 Window parent = null;
123 if(parentComponent != null){
124 parent = SwingUtilities.getWindowAncestor(parentComponent);
125 }
126 OkCancelDialog dialog;
127 if(parent == null) dialog = new OkCancelDialog(title, contents);
128 else if(parent instanceof Frame){
129 dialog = new OkCancelDialog((Frame)parent, title, contents);
130 } else{
131 dialog = new OkCancelDialog((Dialog)parent, title, contents);
132 }
133
134 //position the dialog
135 dialog.pack();
136 dialog.setLocationRelativeTo(parentComponent);
137
138 //kalina: make it fit the screen
139 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
140 Dimension dialogSize = dialog.getSize();
141 if (dialogSize.height > screenSize.height)
142 dialogSize.height = screenSize.height;
143 if (dialogSize.width > screenSize.width)
144 dialogSize.width = screenSize.width;
145 dialog.setSize(dialogSize);
146 //end kalina
147
148 //show the dialog
149 dialog.setModal(true);
150 dialog.userHasPressedOK = false;
151 dialog.userHasPressedCancel = false;
152 dialog.setVisible(true);
153 //
154 // dialog.show();
155 return dialog.userHasPressedOK;
156 }
157
158 protected JButton okButton;
159 protected JButton cancelButton;
160 protected boolean userHasPressedOK;
161 protected static boolean userHasPressedCancel;
162 }
|