KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > gui > lib > DefaultDialogBox


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2005 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, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================
26 $Id: DefaultDialogBox.java,v 1.2 2005/07/06 15:36:00 moroy Exp $
27 ====================================================================*/

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

58 public class DefaultDialogBox
59     extends JDialog JavaDoc
60     implements DialogBox
61 {
62
63     //==================================================================
64
//
65
// Internal states.
66
//
67
//==================================================================
68

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

90     /**
91      * Empty constructor
92      */

93     public DefaultDialogBox(){
94         this("");
95     }
96
97     /**
98      * Constructs a DialogBox with a title
99      * @param title
100      */

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

118     /**
119      * Creates a composite Box: a box which contains the ElementBox
120      * @return The GUI box.
121      */

122     protected Box JavaDoc createCompositeBox(){
123         Box JavaDoc b = Box.createHorizontalBox();
124         b.add(Box.createHorizontalStrut(10));
125         compositeBox_ = Box.createVerticalBox();
126         compositeBox_.add(Box.createVerticalStrut(10));
127         b.add(compositeBox_);
128         b.add(Box.createHorizontalStrut(10));
129         return b;
130     }
131
132     /**
133      * Creates the main box: A box and two buttons.
134      * @return The GUI box.
135      */

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

161     /**
162      * Adds an element into the current JDialog without being concerned about the display.
163      * @param box The element to add.
164      */

165     public void addElementBox(ElementBox box){
166         elements_.add(box);
167         compositeBox_.add(box.getBox());
168         compositeBox_.add(Box.createVerticalStrut(10));
169     }
170     
171     /**
172      * Returns the JDialog contains by the DialogBox.
173      * @return The JDialog contains by the DialogBox.
174      */

175     public JDialog JavaDoc getJDialog(){
176         return this;
177     }
178
179     /**
180      * Shows the dialog box
181      */

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

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

223     public void setValidateAction(DialogAction action){
224         validateDialogAction_ = action;
225     }
226
227     /**
228      * Fixes the label of the validate button (default value is "OK").
229      * @param label The label of the validate button
230      */

231     public void setValidateLabel(String JavaDoc label){
232         validateAction_.putValue(Action.NAME, label);
233     }
234     
235     /**
236      * Fixes the action to use for the cancel button
237      * @param action The action to use.
238      */

239     public void setCancelAction(DialogAction action){
240         cancelDialogAction_ = action;
241     }
242     
243     /**
244      * Fixes the label of the cancel button (default value is "Cancel").
245      * @param label The label of the cancel button
246      */

247     public void setCancelLabel(String JavaDoc label){
248         cancelAction_.putValue(Action.NAME, label);
249     }
250
251     /**
252      * Validates all the elements contains by the JDialog.
253      * @return The corresponding ValidateReport.
254      */

255     public ValidateReport validateDialog(){
256         Iterator JavaDoc it = elements_.iterator();
257         while (it.hasNext()) {
258             ElementBox element = (ElementBox) it.next();
259             ValidateReport report = element.validateBox();
260             if(!report.getResult()){
261                 return report;
262             }
263         }
264         return new DefaultValidateReport();
265     }
266     
267     //==================================================================
268
//
269
// Internal classes.
270
//
271
//==================================================================
272

273     /**
274      * Default action for OK button
275      */

276     protected class DefaultOKAction extends AbstractAction JavaDoc{
277
278         /**
279          * Default constructor
280          */

281         protected DefaultOKAction() {
282             super("OK", null);
283         }
284
285         /**
286          * Action to execute on click
287          */

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

312     protected class DefaultCancelAction extends AbstractAction JavaDoc {
313
314         /**
315          * Default constructor
316          */

317         protected DefaultCancelAction() {
318             super("Cancel", null);
319         }
320
321         /**
322          * Action to execute on click
323          */

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