KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBDialog


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6
7
8 /**
9  * <p>This utility class forms the basis of a generic 'pop up'
10  * dialog, that can either be extended, or used as is. It is
11  * basically a JDialog with built in 'OK', 'Cancel' and 'Help'
12  * buttons (the help pre-wired into CBHelpSystem), and a 'main
13  * display panel' that is a CBPanel (i.e. has a built in
14  * grid bag layout manager and utility methods).</p>
15  * <p/>
16  * <p>If the class is being extended, 'ok' and 'cancel' behaviour
17  * can be modified by extending the 'doOK()' and 'doCancel()'
18  * methods, and extra components may be added in the extended
19  * constructor.</p>
20  * <p/>
21  * <p>If the class is used as is, a new action listener may
22  * be added to the 'OK' button after removing the old action
23  * listener, and new components may be added directly to the
24  * internal CBPanel 'display' object.</p>
25  */

26
27 public class CBDialog extends JDialog
28 {
29     boolean cancelled = false;
30
31     public CBButton OK, Cancel, Help; // Initialise the bottom buttons that actually do stuff
32

33     /**
34      * This is the main display component. The display panel and
35      * the buttonPane are the two top level components shown.
36      */

37
38     public CBPanel display;
39
40     /**
41      * This is the parent Frame of the dialog
42      */

43
44     protected Frame owner;
45
46     /**
47      * This pane contains the 'ok', 'cancel' and 'help' buttons.
48      */

49
50     protected JPanel buttonPanel;
51
52     public CBDialog(Frame owner, String JavaDoc title, String JavaDoc helpLink)
53     {
54         super(owner); // create modal dialog ...
55

56         this.owner = owner;
57
58         setModal(true);
59
60         setTitle(title);
61
62         display = new CBPanel();
63
64         Container pane = getContentPane();
65
66         pane.setLayout(new BorderLayout());
67
68         buttonPanel = new JPanel();
69         buttonPanel.add(OK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click here to make the changes.")));
70         buttonPanel.add(Cancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit.")));
71
72         setHelpLink(helpLink);
73
74         pane.add(display);
75         pane.add(buttonPanel, BorderLayout.SOUTH);
76
77         //TE: better way to implement keystroke listening...
78
display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter");
79         display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
80         display.getActionMap().put("enter", new MyAction(CBAction.ENTER));
81         display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE));
82
83         Cancel.addActionListener(new ActionListener()
84         {
85             public void actionPerformed(ActionEvent e)
86             {
87                 doCancel();
88             }
89         });
90
91         OK.addActionListener(new ActionListener()
92         {
93             public void actionPerformed(ActionEvent e)
94             {
95                 doOK();
96             }
97         });
98
99         addWindowListener(new WindowAdapter()
100         {
101             public void windowClosing(WindowEvent e)
102             {
103                 doCancel();
104             }
105         });
106     }
107
108     /**
109      * Apparently it is better to use key bindings rather than adding a KeyListener...
110      * "for reacting in a special way to particular keys, you usually should use key
111      * bindings instead of a key listener".
112      * This class lets the user set the key as an int. If a key is pressed and it
113      * matches the assigned int, a check is done for if it is an escape or enter key.
114      * (27 or 10). If escape, the doCancel method is called. If enter, the doClick
115      * method is called.
116      * Bug 4646.
117      *
118      * @author Trudi.
119      */

120     private class MyAction extends CBAction
121     {
122         /**
123          * Calls super constructor.
124          *
125          * @param key
126          */

127         public MyAction(int key)
128         {
129             super(key);
130         }
131
132         /**
133          * doCancel is called if the Esc key pressed,
134          * OK.doClick is called if Enter key is pressed.
135          *
136          * @param e never used.
137          */

138         public void actionPerformed(ActionEvent e)
139         {
140             if (getKey() == ESCAPE)
141                 doCancel();
142             else if (getKey() == ENTER)
143                 OK.doClick();
144         }
145     }
146
147     /**
148      * @param helpLink
149      */

150     public void setHelpLink(String JavaDoc helpLink)
151     {
152         if (helpLink != null)
153         {
154             buttonPanel.add(Help = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help.")));
155             CBHelpSystem.useDefaultHelp(Help, helpLink);
156         }
157     }
158
159     /**
160      * @return
161      */

162     public CBPanel getDisplayPanel()
163     {
164         return display;
165     }
166
167     /**
168      * Chaining method to internal CBPanel display object.
169      * (@see com.ca.directory.util.CBPanel)
170      */

171
172     public Component add(Component comp)
173     {
174         display.add(comp);
175         return comp;
176     }
177
178     /**
179      * Chaining method to internal CBPanel display object.
180      * (@see com.ca.directory.util.CBPanel)
181      */

182
183     public void addln(Component comp)
184     {
185         display.addln(comp);
186     }
187
188     /**
189      * Chaining method to internal CBPanel display object.
190      * (@see com.ca.directory.util.CBPanel)
191      */

192
193     public void makeHeavy()
194     {
195         display.makeHeavy();
196     }
197
198     /**
199      * Chaining method to internal CBPanel display object.
200      * (@see com.ca.directory.util.CBPanel)
201      */

202
203     public void makeLight()
204     {
205         display.makeLight();
206     }
207
208     /**
209      * Chaining method to internal CBPanel display object.
210      * (@see com.ca.directory.util.CBPanel)
211      */

212
213     public void makeWide()
214     {
215         display.makeWide();
216     }
217
218
219     /**
220      * When the user hits 'cancel', the window is shut down.
221      */

222
223     public void doCancel()
224     {
225         cancelled = true;
226         quit();
227     }
228
229     /**
230      * Returns whether the dialog was cancelled.
231      * (Useful after a 'setVisible(true)' to discover if the
232      * user left in a huff.)
233      */

234
235     public boolean wasCancelled()
236     {
237         return cancelled;
238     }
239
240     /**
241      * Default behaviour is the same as cancel. Over-ride to
242      * give appropriate 'OK' behaviour.
243      */

244
245     public void doOK()
246     {
247         quit();
248     }
249
250     /**
251      * Closes the window and disposes the gui.
252      * Called by doOK() and doCancel().
253      */

254
255     public void quit()
256     {
257         setVisible(false);
258         dispose();
259     }
260 }
Popular Tags