KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > common > InputDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * InputDialog.java
21  *
22  * Created on October 4, 2003, 7:34 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 // AWT
35
import java.awt.BorderLayout JavaDoc;
36 import java.awt.Component JavaDoc;
37 import java.awt.Dimension JavaDoc;
38 import java.awt.Frame JavaDoc;
39 import java.awt.GridBagLayout JavaDoc;
40 import java.awt.GridBagConstraints JavaDoc;
41 import java.awt.Insets JavaDoc;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44 import java.awt.event.WindowAdapter JavaDoc;
45 import java.awt.event.WindowEvent JavaDoc;
46
47 // Swing
48
import javax.swing.JButton JavaDoc;
49 import javax.swing.JDialog JavaDoc;
50 import javax.swing.JLabel JavaDoc;
51 import javax.swing.JPanel JavaDoc;
52
53 // NetBeans
54
import org.openide.util.HelpCtx;
55
56 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
57
58 /* A modal dialog object with Ok, Cancel, Help buttons and an optional required
59  * field notation.
60  *
61  * This object supports inline error message reporting.
62  *
63  * @author Rajeshwar Patil
64  * Enhancements by Peter Williams
65  * @version %I%, %G%
66  */

67
68 public abstract class InputDialog extends JDialog JavaDoc implements HelpCtx.Provider {
69
70     /** Represents clicking on the Cancel button or closing the dialog
71      */

72     public static final int CANCEL_OPTION = 0;
73     
74     /** Represents clicking on the OK button
75      */

76     public static final int OK_OPTION = 1;
77     
78     /** Represents clicking on the HELP button
79      */

80     public static final int HELP_OPTION = 2;
81
82     private static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
83         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); //NOI18N
84

85     
86     private int chosenOption;
87     private JPanel JavaDoc buttonPanel;
88     private JButton JavaDoc okButton;
89     private JButton JavaDoc helpButton;
90     private JPanel JavaDoc messagePanel;
91     private List JavaDoc errorList;
92     private List JavaDoc warningList;
93
94     /** Creates a new instance of modal InputDialog
95      * @param panel the panel from this dialog is opened
96      * @param title title for the dialog
97      */

98     public InputDialog(JPanel JavaDoc panel, String JavaDoc title) {
99         this(panel, title, false, false);
100     }
101
102     /** Creates a new instance of modal InputDialog
103      * @param panel the panel from this dialog is opened
104      * @param title title for the dialog
105      * @param showRequiredNote set this if you want a '* denotes required field'
106      * message in the lower left hand corner.
107      */

108     public InputDialog(JPanel JavaDoc panel, String JavaDoc title, boolean showRequiredNote) {
109         this(panel, title, showRequiredNote, false);
110     }
111     
112     /** Creates a new instance of modal InputDialog
113      * @param panel the panel from this dialog is opened
114      * @param title title for the dialog
115      * @param showRequiredNote set this if you want a '* denotes required field'
116      * message in the lower left hand corner.
117      * @param resizeMsg set this to true if you want the error message panel to
118      * resize vertically if the user expands the dialog. Useful for dialogs that
119      * can have multiple warnings.
120      */

121     public InputDialog(JPanel JavaDoc panel, String JavaDoc title, boolean showRequiredNote, boolean resizeMsg) {
122         super(getFrame(panel), title, true);
123
124         addWindowListener(new WindowAdapter JavaDoc(){
125             public void windowClosing(WindowEvent JavaDoc e) {
126                 chosenOption = CANCEL_OPTION;
127             }
128         });
129
130         Object JavaDoc buttonPanelConstraints;
131         
132         if(!resizeMsg) {
133             // default to borderlayout...
134
getContentPane().setLayout(new BorderLayout JavaDoc());
135             buttonPanelConstraints = BorderLayout.SOUTH;
136         } else {
137             // for resizing button panels, we need gridbag.
138
getContentPane().setLayout(new GridBagLayout JavaDoc());
139             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
140             constraints.anchor = GridBagConstraints.SOUTH;
141             constraints.gridwidth = GridBagConstraints.REMAINDER;
142             constraints.fill = GridBagConstraints.HORIZONTAL;
143             constraints.weightx = 1.0;
144             buttonPanelConstraints = constraints;
145         }
146
147         // Create button panel -- using gridbaglayout now due to possible
148
// message label on left hand side (in addition to buttons on right
149
// hand side) and error text area above the buttons.
150
buttonPanel = new JPanel JavaDoc();
151         buttonPanel.setLayout(new GridBagLayout JavaDoc());
152
153         messagePanel = new JPanel JavaDoc();
154         messagePanel.setLayout(new GridBagLayout JavaDoc());
155         messagePanel.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ErrorTextArea")); // NOI18N
156
messagePanel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ErrorTextArea")); // NOI18N
157

158         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
159         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
160         gridBagConstraints.fill = GridBagConstraints.BOTH;
161         gridBagConstraints.insets = new Insets JavaDoc(6,12,5,11);
162         gridBagConstraints.weightx = 1.0;
163         buttonPanel.add(messagePanel, gridBagConstraints);
164
165         if(showRequiredNote) {
166             JLabel JavaDoc requiredNote = new JLabel JavaDoc();
167             requiredNote.setText(bundle.getString("LBL_RequiredMessage")); // NOI18N
168
gridBagConstraints = new GridBagConstraints JavaDoc();
169             gridBagConstraints.insets = new Insets JavaDoc(6,12,11,5);
170             gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
171             buttonPanel.add(requiredNote, gridBagConstraints);
172         }
173
174         okButton = new JButton JavaDoc(bundle.getString("LBL_OK")); // NOI18N
175
gridBagConstraints = new GridBagConstraints JavaDoc();
176         gridBagConstraints.insets = new Insets JavaDoc(6,6,11,5);
177         gridBagConstraints.anchor = GridBagConstraints.EAST;
178         gridBagConstraints.weightx = 1.0;
179         okButton.addActionListener(new ActionListener JavaDoc(){
180             public void actionPerformed(ActionEvent JavaDoc e){
181                 chosenOption = OK_OPTION;
182                 actionOk();
183             }
184         });
185         okButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_OK")); // NOI18N
186
okButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_OK")); // NOI18N
187
buttonPanel.add(okButton, gridBagConstraints);
188
189         JButton JavaDoc cancelButton = new JButton JavaDoc(bundle.getString("LBL_Cancel")); // NOI18N
190
gridBagConstraints = new GridBagConstraints JavaDoc();
191         gridBagConstraints.insets = new Insets JavaDoc(6,0,11,5);
192         gridBagConstraints.anchor = GridBagConstraints.EAST;
193         cancelButton.addActionListener(new ActionListener JavaDoc(){
194             public void actionPerformed(ActionEvent JavaDoc e){
195                 chosenOption = CANCEL_OPTION;
196                 actionCancel();
197             }
198         });
199         cancelButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_Cancel")); // NOI18N
200
cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Cancel")); // NOI18N
201
buttonPanel.add(cancelButton, gridBagConstraints);
202
203         helpButton = new JButton JavaDoc(bundle.getString("LBL_Help")); // NOI18N
204
helpButton.setMnemonic(bundle.getString("MNE_Help").charAt(0)); // NOI18N
205
gridBagConstraints = new GridBagConstraints JavaDoc();
206         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
207         gridBagConstraints.insets = new Insets JavaDoc(6,0,11,11);
208         gridBagConstraints.anchor = GridBagConstraints.EAST;
209         helpButton.addActionListener(new ActionListener JavaDoc(){
210             public void actionPerformed(ActionEvent JavaDoc e){
211                 chosenOption = HELP_OPTION;
212                 actionHelp();
213             }
214         });
215         helpButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_Help")); // NOI18N
216
helpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Help")); // NOI18N
217
buttonPanel.add(helpButton, gridBagConstraints);
218
219         getContentPane().add(buttonPanel, buttonPanelConstraints);
220         getRootPane().setDefaultButton(okButton);
221     }
222
223     
224     /** Displays the dialog.
225      * @return identifies whether the OK button or Cancel button was selected
226      */

227     public int display() {
228         this.setVisible(true);
229         return chosenOption;
230     }
231
232     protected void actionOk() {
233         super.dispose();
234     }
235
236     protected void actionCancel() {
237         super.dispose();
238     }
239
240     protected void actionHelp() {
241         Utils.invokeHelp(getHelpId());
242     }
243     
244     protected abstract String JavaDoc getHelpId();
245
246     /** Seeks for the nearest Frame containg this component.
247      */

248     public static Frame JavaDoc getFrame(Component JavaDoc component) {
249         while (!(component instanceof Frame JavaDoc)){
250             component = component.getParent();
251         }
252         return ((Frame JavaDoc) component);
253     }
254
255
256     /** Enables to place this dialog in the middle of the given panel.
257      * @param panel where the dialog should be placed
258      */

259     protected void setLocationInside(JPanel JavaDoc panel) {
260         java.awt.Rectangle JavaDoc rect = this.getBounds();
261         int width = rect.width;
262         int height = rect.height;
263         java.awt.Rectangle JavaDoc panelRect = panel.getBounds();
264         if (width > panelRect.width || height > panelRect.height) {
265             setLocationRelativeTo(panel);
266         } else {
267             java.awt.Point JavaDoc location = panel.getLocationOnScreen();
268             setLocation(location.x + (panelRect.width - width) / 2,
269                 location.y + (panelRect.height - height) / 2);
270         }
271     }
272
273     /** Simple enable/disable mechanism for the ok button - this should be improved
274      * to allow specification of which button, or a range of buttons, where the
275      * buttons are specified by enums (using typesafe enum pattern).
276      */

277     protected void setOkEnabled(boolean flag) {
278         okButton.setEnabled(flag);
279     }
280     
281     /** Shows the errors at the top of dialog panel.
282      * Set focus to the focused component.
283      */

284     public void showErrors() {
285         boolean hasErrors = false;
286         boolean hasWarnings = false;
287         
288         // clear existing errors first.
289
messagePanel.removeAll();
290         
291         if(warningList != null & warningList.size() > 0) {
292             hasWarnings = true;
293             
294             for(Iterator JavaDoc iter = warningList.iterator(); iter.hasNext();) {
295                 String JavaDoc message = iter.next().toString();
296                 
297                 // Add warning message
298
JLabel JavaDoc label = new JLabel JavaDoc();
299                 label.setIcon(BaseCustomizer.warningMessageIcon);
300                 label.setText("<html>" + message + "</html>"); // NOI18N
301
label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_WarningMessage")); // NOI18N
302
label.getAccessibleContext().setAccessibleDescription(message);
303                 label.setForeground(BaseCustomizer.getWarningForegroundColor());
304
305                 GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
306                 constraints.gridwidth = GridBagConstraints.REMAINDER;
307                 constraints.fill = GridBagConstraints.HORIZONTAL;
308                 constraints.weightx = 1.0;
309                 messagePanel.add(label, constraints);
310             }
311         }
312
313         if(errorList != null & errorList.size() > 0) {
314             hasErrors = true;
315             for(Iterator JavaDoc iter = errorList.iterator(); iter.hasNext();) {
316                 String JavaDoc message = iter.next().toString();
317                 
318                 // Add error message
319
JLabel JavaDoc label = new JLabel JavaDoc();
320                 label.setIcon(BaseCustomizer.errorMessageIcon);
321                 label.setText("<html>" + message + "</html>"); // NOI18N
322
label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_ErrorMessage")); // NOI18N
323
label.getAccessibleContext().setAccessibleDescription(message);
324                 label.setForeground(BaseCustomizer.getErrorForegroundColor());
325
326                 GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
327                 constraints.gridwidth = GridBagConstraints.REMAINDER;
328                 constraints.fill = GridBagConstraints.HORIZONTAL;
329                 constraints.weightx = 1.0;
330                 messagePanel.add(label, constraints);
331             }
332         }
333        
334         // Layout the dialog again to account for new or removed messages. validate()
335
// is not used here because the dialog may need to be resized to look nice.
336
pack();
337         
338         // Display errors and warnings (if any) and disable/enable the OkButton as appropriate
339
setOkEnabled(!hasErrors);
340     }
341
342     protected void setErrors(Collection JavaDoc errors, Collection JavaDoc warnings) {
343         warningList = new ArrayList JavaDoc(warnings);
344         errorList = new ArrayList JavaDoc(errors);
345         showErrors();
346     }
347     
348     /** Sets the existing error list to the collection of errors passed in.
349      * @param errors Collection of error messages.
350      */

351     protected void setErrors(Collection JavaDoc errors) {
352         setErrors(errors, Collections.EMPTY_LIST);
353     }
354
355     /** Adds an warning string to the warning list.
356      * @param warning error message
357      */

358     public void addWarning(String JavaDoc warning) {
359         if(warningList == null) {
360             warningList = new ArrayList JavaDoc();
361         }
362         
363         warningList.add(warning);
364         showErrors();
365     }
366     
367     /** Adds a collection of warnings to the warning list.
368      * @param warnings Collection of warning messages.
369      */

370     protected void addWarnings(Collection JavaDoc warnings) {
371         if(warningList == null) {
372             warningList = new ArrayList JavaDoc(warnings);
373         } else {
374             warningList.addAll(warnings);
375         }
376         
377         showErrors();
378     }
379     
380     /** Adds an error string to the error list.
381      * @param error error message
382      */

383     public void addError(String JavaDoc error) {
384         if(errorList == null) {
385             errorList = new ArrayList JavaDoc();
386         }
387         
388         errorList.add(error);
389         showErrors();
390     }
391     
392     /** Adds a collection of errors to the error list.
393      * @param errors Collection of error messages.
394      */

395     protected void addErrors(Collection JavaDoc errors) {
396         if(errorList == null) {
397             errorList = new ArrayList JavaDoc(errors);
398         } else {
399             errorList.addAll(errors);
400         }
401         
402         showErrors();
403     }
404     
405     
406     /** Clears out all error messages.
407      */

408     protected void clearErrors() {
409         warningList = null;
410         errorList = null;
411         showErrors();
412     }
413
414     
415     /** Test if the error list is filled or not.
416      * @return true if there are errors, false if not.
417      */

418     public boolean hasErrors() {
419         boolean result = false;
420         
421         if(errorList != null && errorList.size() > 0) {
422             result = true;
423         }
424         
425         return result;
426     }
427     
428     /** -----------------------------------------------------------------------
429      * Implementation of HelpCtx.Provider interface
430      */

431     public HelpCtx getHelpCtx() {
432         return new HelpCtx(getHelpId());
433     }
434
435
436         protected void setButtonPanelPreferredSize(Dimension JavaDoc dimension){
437             buttonPanel.setMinimumSize(dimension);
438             buttonPanel.setPreferredSize(dimension);
439         }
440 }
441
Popular Tags