KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > gui > lib > DefaultDialogBox


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.browser.gui.lib;
28
29 /** The Browser API's imports */
30 import java.awt.Dimension JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import javax.swing.AbstractAction JavaDoc;
36 import javax.swing.Action JavaDoc;
37 import javax.swing.Box JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JDialog JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.WindowConstants JavaDoc;
42
43 import org.objectweb.util.browser.gui.api.DialogAction;
44 import org.objectweb.util.browser.gui.api.DialogBox;
45 import org.objectweb.util.browser.gui.api.ElementBox;
46 import org.objectweb.util.browser.gui.api.ValidateReport;
47
48 /**
49  * Basic implementation of a DialogBox.
50  *
51  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
52  *
53  * @version 0.1
54  */

55 public class DefaultDialogBox
56     extends JDialog JavaDoc
57     implements DialogBox{
58
59     //==================================================================
60
//
61
// Internal states.
62
//
63
//==================================================================
64

65     /** Indicates if it's the first show method invocation. */
66     protected boolean firstShowInvocation_ = true;
67     
68     /** The OK and Cancel buttons */
69     protected Action JavaDoc validateAction_, cancelAction_;
70     
71     /** The actions to execute */
72     protected DialogAction validateDialogAction_, cancelDialogAction_;
73
74     /** The composite Box */
75     protected Box JavaDoc compositeBox_;
76     
77     /** The list of ElementBox */
78     protected List JavaDoc elements_;
79
80     //==================================================================
81
//
82
// Constructors.
83
//
84
//==================================================================
85

86     /**
87      * Empty constructor
88      */

89     public DefaultDialogBox(){
90         this("");
91     }
92
93     /**
94      * Constructs a DialogBox with a title
95      * @param title
96      */

97     public DefaultDialogBox(String JavaDoc title){
98         super();
99         
100         elements_ = new Vector JavaDoc();
101         
102         setModal(true);
103         setTitle(title);
104       
105         getContentPane().add(createMainBox());
106     }
107
108     //==================================================================
109
//
110
// Internal methods.
111
//
112
//==================================================================
113

114     /**
115      * Creates a composite Box: a box which contains the ElementBox
116      */

117     protected Box JavaDoc createCompositeBox(){
118         Box JavaDoc b = Box.createHorizontalBox();
119         b.add(Box.createHorizontalStrut(10));
120         compositeBox_ = Box.createVerticalBox();
121         compositeBox_.add(Box.createVerticalStrut(10));
122         b.add(compositeBox_);
123         b.add(Box.createHorizontalStrut(10));
124         return b;
125     }
126
127     /**
128      * Creates the main box: A box and two buttons.
129      */

130     protected Box JavaDoc createMainBox(){
131         Box JavaDoc mainBox = Box.createVerticalBox();
132         mainBox.add(createCompositeBox());
133         
134         Box JavaDoc buttonBox = Box.createHorizontalBox();
135         buttonBox.add(Box.createHorizontalGlue());
136         validateAction_ = new DefaultOKAction();
137         buttonBox.add(new JButton JavaDoc(validateAction_));
138         buttonBox.add(Box.createHorizontalStrut(10));
139         cancelAction_ = new DefaultCancelAction();
140         buttonBox.add(new JButton JavaDoc(cancelAction_));
141         buttonBox.add(Box.createHorizontalGlue());
142         
143         mainBox.add(buttonBox);
144         mainBox.add(Box.createVerticalStrut(10));
145         
146         return mainBox;
147     }
148
149     //==================================================================
150
//
151
// Public methods for DialogBox interface.
152
//
153
//==================================================================
154

155     /**
156      * Adds an element into the current JDialog without being concerned about the display.
157      * @param box The element to add.
158      */

159     public void addElementBox(ElementBox box){
160         elements_.add(box);
161         compositeBox_.add(box.getBox());
162         compositeBox_.add(Box.createVerticalStrut(10));
163     }
164     
165     /**
166      * Returns the JDialog contains by the DialogBox.
167      * @return The JDialog contains by the DialogBox.
168      */

169     public JDialog JavaDoc getJDialog(){
170         return this;
171     }
172
173     /**
174      * Shows the dialog box
175      */

176     public void show(){
177         // Initializes the elements
178
Iterator JavaDoc it = elements_.iterator();
179         while (it.hasNext()) {
180             ElementBox element = (ElementBox) it.next();
181             element.preInitialize();
182         }
183         // Packs the window.
184
pack();
185         // Initialize the windows at the first time
186
if(firstShowInvocation_){
187             firstShowInvocation_ = false;
188             setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
189             setResizable(false);
190             pack();
191
192             Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
193             setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
194         }
195         // Displays the window
196
super.show();
197     }
198     
199     /**
200      * Hides the dialog box
201      */

202     public void hide(){
203         // Post initializes the elements
204
Iterator JavaDoc it = elements_.iterator();
205         while (it.hasNext()) {
206             ElementBox element = (ElementBox) it.next();
207             element.postInitialize();
208         }
209         // Hides the window
210
super.hide();
211     }
212     
213     /**
214      * Fixes the action to use for the validate button
215      * @param action The action to use.
216      */

217     public void setValidateAction(DialogAction action){
218         validateDialogAction_ = action;
219     }
220
221     /**
222      * Fixes the label of the validate button (default value is "OK").
223      * @param label The label of the validate button
224      */

225     public void setValidateLabel(String JavaDoc label){
226         validateAction_.putValue(Action.NAME, label);
227     }
228     
229     /**
230      * Fixes the action to use for the cancel button
231      * @param action The action to use.
232      */

233     public void setCancelAction(DialogAction action){
234         cancelDialogAction_ = action;
235     }
236     
237     /**
238      * Fixes the label of the cancel button (default value is "Cancel").
239      * @param label The label of the cancel button
240      */

241     public void setCancelLabel(String JavaDoc label){
242         cancelAction_.putValue(Action.NAME, label);
243     }
244
245     /**
246      * Validates all the elements contains by the JDialog.
247      * @return The corresponding ValidateReport.
248      */

249     public ValidateReport validateDialog(){
250         Iterator JavaDoc it = elements_.iterator();
251         while (it.hasNext()) {
252             ElementBox element = (ElementBox) it.next();
253             ValidateReport report = element.validateBox();
254             if(!report.getResult()){
255                 return report;
256             }
257         }
258         return new DefaultValidateReport();
259     }
260     
261     //==================================================================
262
//
263
// Internal classes.
264
//
265
//==================================================================
266

267     /**
268      * Default action for OK button
269      */

270     protected class DefaultOKAction extends AbstractAction JavaDoc{
271
272         /**
273          * Default constructor
274          */

275         protected DefaultOKAction() {
276             super("OK", null);
277         }
278
279         /**
280          * Action to execute on click
281          */

282         public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
283             ValidateReport report = validateDialog();
284             if(report.getResult()){
285                 boolean exception = false;
286                 if(DefaultDialogBox.this.validateDialogAction_!=null){
287                     try{
288                         DefaultDialogBox.this.validateDialogAction_.executeAction();
289                     }catch(Exception JavaDoc e){
290                         exception = true;
291                         JOptionPane.showMessageDialog(null, e.getClass().getName() + ":\n" + e.getMessage(), "Exception (" + ae.getActionCommand() + ")", JOptionPane.ERROR_MESSAGE);
292                     }
293                 }
294                 if(!exception)
295                     //DefaultDialogBox.this.setVisible(false);
296
hide();
297             }else
298                 JOptionPane.showMessageDialog(null, report.getMessage(), "Params error !", JOptionPane.ERROR_MESSAGE);
299         }
300
301     }
302     
303     /**
304      * Default action for Cancel button
305      */

306     protected class DefaultCancelAction extends AbstractAction JavaDoc {
307
308         /**
309          * Default constructor
310          */

311         protected DefaultCancelAction() {
312             super("Cancel", null);
313         }
314
315         /**
316          * Action to execute on click
317          */

318         public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
319             boolean exception = false;
320             if(DefaultDialogBox.this.cancelDialogAction_!=null){
321                 try{
322                     DefaultDialogBox.this.cancelDialogAction_.executeAction();
323                 }catch(Exception JavaDoc e){
324                     exception = true;
325                     JOptionPane.showMessageDialog(null, e.getMessage(), ae.getActionCommand(), JOptionPane.ERROR_MESSAGE);
326                 }
327             } else
328                 //DefaultDialogBox.this.setVisible(false);
329
hide();
330         }
331
332     }
333
334 }
Popular Tags