KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > 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.struts.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     //public static final JButton HELP_OPTION = new JButton(NbBundle.getMessage(AddDialog.class,"LBL_Help"));
41
private JPanel JavaDoc panel;
42     
43     /** Creates a new instance of EditDialog */
44     public AddDialog(ValidatingPanel panel, String JavaDoc title, HelpCtx helpCtx) {
45         super (new InnerPanel((JPanel JavaDoc)panel),getTitle(title),true,
46               //new Object[]{ADD_OPTION, DialogDescriptor.CANCEL_OPTION, HELP_OPTION},
47
new Object JavaDoc[]{ADD_OPTION, DialogDescriptor.CANCEL_OPTION},
48               DialogDescriptor.OK_OPTION,
49               DialogDescriptor.BOTTOM_ALIGN,
50               helpCtx,
51               null);
52         this.panel=(JPanel JavaDoc)panel;
53         AbstractButton JavaDoc[] comp = panel.getStateChangeComponents();
54         if (comp!=null && comp.length>0) {
55             StateChangeListener list = new StateChangeListener(this);
56             for (int i=0;i<comp.length;i++) {
57                 comp[i].addItemListener(list);
58             }
59         }
60         JTextComponent JavaDoc[] textComp = panel.getDocumentChangeComponents();
61         if (textComp!=null && textComp.length>0) {
62             DocListener list = new DocListener(this);
63             for (int i=0;i<textComp.length;i++) {
64                 textComp[i].getDocument().addDocumentListener(list);
65             }
66         }
67         checkValues();
68     }
69
70     private static String JavaDoc getTitle(String JavaDoc title) {
71         return NbBundle.getMessage(AddDialog.class,"TTL_ADD",title);
72     }
73     
74     public void disableAdd() {
75        ((JButton JavaDoc)getOptions()[0]).setEnabled(false);
76     }
77     
78     public void enableAdd() {
79        ((JButton JavaDoc)getOptions()[0]).setEnabled(true);
80     }
81     
82     /** Returns the dialog panel
83     * @return dialog panel
84     */

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

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

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

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

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