KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > frame > XJDialog


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.frame;
33
34 import org.antlr.xjlib.appkit.XJControl;
35 import org.antlr.xjlib.appkit.app.XJApplication;
36
37 import javax.swing.*;
38 import java.awt.*;
39 import java.awt.event.*;
40
41 public class XJDialog extends XJControl {
42
43     public static final int BUTTON_CANCEL = 0;
44     public static final int BUTTON_OK = 1;
45
46     protected JDialog jDialog = null;
47     protected JButton defaultButton = null;
48     protected int returnCode = 0;
49     protected Container parent;
50
51     public static Container resolveOwner(Container owner) {
52         Container parent = owner==null?XJApplication.getActiveContainer():owner;
53         if(owner instanceof Dialog || owner instanceof Frame)
54             return parent;
55         else if(owner != null)
56             return SwingUtilities.getWindowAncestor(owner);
57         else
58             return null;
59     }
60
61     public XJDialog(Container owner, boolean modal) {
62         parent = resolveOwner(owner);
63         if(parent instanceof Dialog)
64             jDialog = new JDialog((Dialog)parent);
65         else if(parent instanceof Frame)
66             jDialog = new JDialog((Frame)parent);
67         else
68             jDialog = new JDialog();
69         jDialog.setModal(modal);
70         jDialog.addWindowListener(new WindowAdapter() {
71             public void windowClosing(WindowEvent e) {
72                 dialogWillCloseCancel();
73             }
74         });
75         addEscapeHandling();
76         setDefaultSize();
77     }
78
79     public void setDefaultCloseOperation(int operation) {
80         jDialog.setDefaultCloseOperation(operation);
81     }
82
83     public Container getContentPane() {
84         return jDialog.getContentPane();
85     }
86
87     public void setTitle(String JavaDoc title) {
88         jDialog.setTitle(title);
89     }
90
91     public String JavaDoc getTitle() {
92         return jDialog.getTitle();
93     }
94
95     public void setSize(int dx, int dy) {
96         jDialog.setSize(dx, dy);
97     }
98
99     public void setSize(Dimension size) {
100         jDialog.setSize(size);
101     }
102
103     public Dimension getSize() {
104         return jDialog.getSize();
105     }
106
107     public void setDefaultSize() {
108         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
109         jDialog.setSize((int)(dim.width*0.5), (int)(dim.height*0.5));
110     }
111
112     public void setResizable(boolean flag) {
113         jDialog.setResizable(flag);
114     }
115
116     public void setDefaultButton(JButton button) {
117         this.defaultButton = button;
118     }
119
120     public void setOKButton(JButton button) {
121         button.addActionListener(new ActionListener() {
122             public void actionPerformed(ActionEvent event) {
123                 if(dialogCanCloseOK()) {
124                     dialogWillCloseOK();
125                     closeWithReturnCode(BUTTON_OK);
126                 }
127             }
128         });
129     }
130
131     public void setCancelButton(JButton button) {
132         button.addActionListener(new ActionListener() {
133             public void actionPerformed(ActionEvent event) {
134                 dialogWillCloseCancel();
135                 closeWithReturnCode(BUTTON_CANCEL);
136             }
137         });
138     }
139
140     /** This method add the necessary code to handle the escape key
141      * in order to close the dialog
142      */

143     public void addEscapeHandling() {
144         KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
145
146         ActionListener cancelAction = new AbstractAction() {
147             public void actionPerformed(ActionEvent ae) {
148                 closeWithReturnCode(BUTTON_CANCEL);
149             }
150         };
151
152         jDialog.getRootPane().registerKeyboardAction(cancelAction, "CancelAction", ks,
153                 JComponent.WHEN_IN_FOCUSED_WINDOW);
154     }
155
156     public void setReturnCode(int code) {
157         this.returnCode = code;
158     }
159
160     public void bringToFront() {
161         jDialog.toFront();
162     }
163
164     public void center() {
165         jDialog.setLocationRelativeTo(parent);
166     }
167
168     public void setPosition(int x, int y) {
169         jDialog.setLocation(x, y);
170     }
171
172     public void setPosition(Point p) {
173         jDialog.setLocation(p);
174     }
175
176     public Point getPosition() {
177         return jDialog.getLocation();
178     }
179
180     public void offsetPosition(int dx, int dy) {
181         Point p = jDialog.getLocation();
182         jDialog.setLocation(p.x+dx, p.y+dy);
183     }
184
185     public Component getJavaComponent() {
186         return jDialog;
187     }
188
189     public void closeWithReturnCode(int code) {
190         setReturnCode(code);
191         close();
192     }
193
194     public void close() {
195         jDialog.dispose();
196     }
197
198     public void pack() {
199         // do nothing
200
}
201
202     public boolean dialogCanCloseOK() {
203         return true;
204     }
205
206     public void dialogWillDisplay() {
207
208     }
209
210     public void dialogWillCloseCancel() {
211
212     }
213
214     public void dialogWillCloseOK() {
215
216     }
217
218     public void setVisible(boolean flag) {
219         jDialog.setVisible(flag);
220     }
221
222     public void display() {
223         center();
224         jDialog.setVisible(true);
225     }
226
227     public int runModal() {
228         center();
229
230         dialogWillDisplay();
231         if(defaultButton != null)
232             jDialog.getRootPane().setDefaultButton(defaultButton);
233
234         jDialog.setVisible(true);
235
236         return returnCode;
237     }
238 }
239
Popular Tags