1
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
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
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
112 public static boolean showDialog(Component parentComponent,
113 Component contents,
114 String title){
115 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 dialog.pack();
130 dialog.setLocationRelativeTo(parentComponent);
131
132 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
142 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 }