KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > awt > OkCancelDialog


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.awt;
32
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.beans.*;
36
37 public class OkCancelDialog extends Dialog {
38     
39     Panel controlsPane;
40     Button okButton;
41     Button cancelButton;
42
43     public OkCancelDialog (Frame owner, String JavaDoc title) {
44         super (owner, title, true);
45         
46         add (controlsPane = new Panel (), BorderLayout.CENTER);
47         
48         Panel panel;
49         add (panel = new Panel (), BorderLayout.SOUTH);
50         panel.setLayout (new FlowLayout ());
51         
52         Panel subpanel;
53         panel.add (subpanel = new Panel ());
54         subpanel.setLayout (new GridLayout (1, 0, 4, 0));
55
56         subpanel.add (okButton = new Button ("OK"));
57         okButton.addActionListener (new ActionListener () {
58             public void actionPerformed (ActionEvent event) {
59                 ok ();
60             }
61         });
62         
63         subpanel.add (cancelButton = new Button ("Cancel"));
64         cancelButton.addActionListener (new ActionListener () {
65             public void actionPerformed (ActionEvent event) {
66                 cancel ();
67             }
68         });
69         
70         addWindowListener (new WindowAdapter () {
71             public void windowClosing (WindowEvent event) {
72                 cancel ();
73             }
74         });
75         
76     }
77
78     public static void centerWindow (Window window, Component ref) {
79         Dimension size = window.getSize();
80         Dimension refSize = (ref != null) ? ref.getSize() : Toolkit.getDefaultToolkit().getScreenSize();
81         Point origin = (ref != null) ? ref.getLocationOnScreen () : new Point (0, 0);
82         
83         if (refSize != null) {
84             int x = Math.max (0, origin.x + (refSize.width - size.width) / 2);
85             int y = Math.max (0, origin.y + (refSize.height - size.height) / 2);
86             window.setLocation (x, y);
87         }
88     }
89     
90     public void show () {
91         Container parent = getParent ();
92         if (parent != null)
93             centerWindow (this, parent);
94             
95         registerKeystrokes ();
96
97         super.show ();
98     }
99     
100     public Panel getControlsPane () {
101         return controlsPane;
102     }
103
104     public void registerKeystrokes () {
105         // NIY: bind Enter to OK, Esc to Cancel
106
}
107
108     public void ok () {
109         dispose ();
110     }
111     
112     public void cancel () {
113         dispose ();
114     }
115 }
116
Popular Tags