KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > dialogs > AddDialog


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 package org.netbeans.modules.web.jsf.dialogs;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.event.ItemListener JavaDoc;
24 import javax.swing.AbstractButton JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import org.openide.DialogDescriptor;
29 import org.openide.util.HelpCtx;
30 import org.openide.util.NbBundle;
31
32 /** AddDialog.java
33  *
34  * Created on November 28, 2004, 7:18 PM
35  * @author mkuchtiak
36  */

37 public class AddDialog extends DialogDescriptor {
38     private static Color JavaDoc errorLabelColor = javax.swing.UIManager.getDefaults().getColor("ToolBar.dockingForeground"); //NOI18N
39
public static final JButton JavaDoc ADD_OPTION = new JButton JavaDoc(NbBundle.getMessage(AddDialog.class,"LBL_Add"));
40     private JPanel JavaDoc panel;
41     
42     /** Creates a new instance of EditDialog */
43     public AddDialog(ValidatingPanel panel, String JavaDoc title, HelpCtx helpCtx) {
44         super (new InnerPanel((JPanel JavaDoc)panel),getTitle(title),true,
45               new Object JavaDoc[]{ADD_OPTION, DialogDescriptor.CANCEL_OPTION},
46               DialogDescriptor.OK_OPTION,
47               DialogDescriptor.BOTTOM_ALIGN,
48               helpCtx,
49               null);
50         this.panel=(JPanel JavaDoc)panel;
51         AbstractButton JavaDoc[] comp = panel.getStateChangeComponents();
52         if (comp!=null && comp.length>0) {
53             StateChangeListener list = new StateChangeListener(this);
54             for (int i=0;i<comp.length;i++) {
55                 comp[i].addItemListener(list);
56             }
57         }
58         JTextComponent JavaDoc[] textComp = panel.getDocumentChangeComponents();
59         if (textComp!=null && textComp.length>0) {
60             DocListener list = new DocListener(this);
61             for (int i=0;i<textComp.length;i++) {
62                 textComp[i].getDocument().addDocumentListener(list);
63             }
64         }
65         checkValues();
66     }
67
68     private static String JavaDoc getTitle(String JavaDoc title) {
69         return NbBundle.getMessage(AddDialog.class,"TTL_ADD",title);
70     }
71     
72     public void disableAdd() {
73        ((JButton JavaDoc)getOptions()[0]).setEnabled(false);
74     }
75     
76     public void enableAdd() {
77        ((JButton JavaDoc)getOptions()[0]).setEnabled(true);
78     }
79     
80     /** Returns the dialog panel
81     * @return dialog panel
82     */

83     public final javax.swing.JPanel JavaDoc getDialogPanel() {
84         return panel;
85     }
86     
87     /** Calls validation of panel components, displays or removes the error message
88     * Should be called from listeners listening to component changes.
89     */

90     protected final void checkValues() {
91         String JavaDoc errorMessage = validate();
92         if (errorMessage==null) {
93             enableAdd();
94         } else {
95             disableAdd();
96         }
97         javax.swing.JLabel JavaDoc errorLabel = ((InnerPanel)getMessage()).getErrorLabel();
98         errorLabel.setText(errorMessage==null?" ":errorMessage);
99     }
100     
101     /** Provides validation for panel components */
102     protected String JavaDoc validate() {
103         return ((ValidatingPanel)panel).validatePanel();
104     }
105     
106     private static class InnerPanel extends javax.swing.JPanel JavaDoc {
107         javax.swing.JLabel JavaDoc errorLabel;
108         InnerPanel(JPanel JavaDoc panel) {
109             super(new java.awt.BorderLayout JavaDoc());
110             errorLabel = new javax.swing.JLabel JavaDoc(" ");
111             errorLabel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(12,12,0,0));
112             errorLabel.setForeground(errorLabelColor);
113             add(panel, java.awt.BorderLayout.CENTER);
114             add(errorLabel, java.awt.BorderLayout.SOUTH);
115             this.getAccessibleContext().setAccessibleDescription(panel.getAccessibleContext().getAccessibleDescription());
116             this.getAccessibleContext().setAccessibleName(panel.getAccessibleContext().getAccessibleName());
117         }
118         
119         void setErrorMessage(String JavaDoc message) {
120             errorLabel.setText(message);
121         }
122         
123         javax.swing.JLabel JavaDoc getErrorLabel() {
124             return errorLabel;
125         }
126     }
127     
128     /** Useful DocumentListener class that can be added to the panel's text compoents */
129     private class DocListener implements javax.swing.event.DocumentListener JavaDoc {
130         AddDialog dialog;
131         
132         DocListener(AddDialog dialog) {
133             this.dialog=dialog;
134         }
135         /**
136          * Method from DocumentListener
137          */

138         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
139             dialog.checkValues();
140         }
141
142         /**
143          * Method from DocumentListener
144          */

145         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
146             dialog.checkValues();
147         }
148
149         /**
150          * Method from DocumentListener
151          */

152         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
153             dialog.checkValues();
154         }
155     }
156     
157     private class StateChangeListener implements java.awt.event.ItemListener JavaDoc {
158         AddDialog dialog;
159         StateChangeListener (AddDialog dialog) {
160             this.dialog=dialog;
161         }
162         public void itemStateChanged(java.awt.event.ItemEvent JavaDoc e) {
163             dialog.checkValues();
164         }
165     }
166 }
167
Popular Tags