KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ErrorSupport.java
21  *
22  * Created on November 14, 2003, 3:56 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.awt.Container JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29 import java.awt.GridBagLayout JavaDoc;
30 import java.awt.Insets JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35
36 /**
37  *
38  * @author Rajeshwar Patil
39  * @version %I%, %G%
40  */

41 public class ErrorSupport implements ErrorSupportClient{
42
43     private static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
44         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); // NOI18N
45

46     private java.util.List JavaDoc errorList;
47     private javax.swing.JPanel JavaDoc errorPanel;
48     private javax.swing.JComponent JavaDoc focusedComponent;
49
50     private ErrorSupportClient client;
51
52     /** Creates a new instance of ErrorSupport */
53     public ErrorSupport() {
54         initialize();
55         this.client = (ErrorSupportClient) this;
56     }
57
58
59     public ErrorSupport(ErrorSupportClient client) {
60         initialize();
61         this.client = client;
62     }
63
64
65     private void initialize(){
66         errorList = new java.util.ArrayList JavaDoc();
67         errorPanel = null;
68         focusedComponent = null;
69     }
70
71
72     /** Shows the errors Panel.
73      * Set focus to the focusedComponent.
74      */

75     public void showErrors(){
76         //Should be called Called on the following --
77
// on <enter> or item selection of each field
78
// on focus gain of each field
79
removeAllErrors();
80         java.util.Collection JavaDoc errors = client.getErrors();
81         if(errors != null){
82             java.util.Iterator JavaDoc iterator = errors.iterator();
83             while(iterator.hasNext()){
84                 Object JavaDoc object = iterator.next();
85                 if(object instanceof java.lang.String JavaDoc){
86                     addError((String JavaDoc)object);
87                 }
88             }
89         }
90
91         Container JavaDoc parentPanel = client.getErrorPanelParent();
92         if(parentPanel != null){
93             if(errorList.size() != 0){
94                 if(errorPanel == null) {
95                     errorPanel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
96                     parentPanel.add(errorPanel, client.getErrorPanelConstraints());
97                 } else {
98                     errorPanel.removeAll();
99                 }
100               
101                 for(Iterator JavaDoc iter = errorList.iterator(); iter.hasNext(); ) {
102                     String JavaDoc message = (String JavaDoc) iter.next();
103
104                     // Add error message
105
JLabel JavaDoc label = new JLabel JavaDoc();
106                     label.setIcon(BaseCustomizer.errorMessageIcon);
107                     label.setText(message);
108                     label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_ErrorMessage")); // NOI18N
109
label.getAccessibleContext().setAccessibleDescription(message);
110                     label.setForeground(getMessageForegroundColor());
111
112                     GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
113                     constraints.gridwidth = GridBagConstraints.REMAINDER;
114                     constraints.fill = GridBagConstraints.HORIZONTAL;
115                     constraints.weightx = 1.0;
116                     errorPanel.add(label, constraints);
117                 }
118                 
119 // if (focusedComponent != null) {
120
// focusedComponent.requestFocus();
121
// }
122
} else {
123                 if(errorPanel != null) {
124                     parentPanel.remove(errorPanel);
125                     errorPanel = null;
126                 }
127             }
128             
129             parentPanel.validate();
130         }
131     }
132
133
134     /** Test if the error list is filled or not.
135     * @return true if list has errors, false if not.
136     */

137     public boolean hasErrors(){
138         return (errorList.size()==0?false:true);
139     }
140
141
142     /** Adds an error string to the error list.
143     * @param error error message
144     */

145     public void addError(String JavaDoc error){
146         errorList.add(error);
147     }
148
149
150     /** Adds an error string to the error list and sets the component
151     * that should gain the focus.
152     * @param error error message
153     * @param focusedComponent component(JTextField) where the error should be fixed
154     */

155     public void addError(javax.swing.JComponent JavaDoc focusedComponent, String JavaDoc error){
156         setFocusedComponent(focusedComponent);
157         errorList.add(error);
158     }
159
160
161     /** Setting focus to the selected component when showing the dialog.
162     * Using in the showErrors() method.
163     * @param comp component that need to be focused
164     */

165     public void setFocusedComponent(javax.swing.JComponent JavaDoc comp){
166         focusedComponent = comp;
167     }
168
169
170     /**
171     * @return the focused component
172     */

173     public javax.swing.JComponent JavaDoc getFocusedComponent(){
174         return focusedComponent;
175     }
176
177
178     /**
179     * Removes all the errors
180     */

181     public void removeAllErrors(){
182         errorList.clear();
183         focusedComponent = null;
184     }
185
186
187     public java.util.Collection JavaDoc getErrors(){
188         return null;
189     }
190
191
192     public java.awt.Container JavaDoc getErrorPanelParent(){
193         return null;
194     }
195
196
197     public java.awt.GridBagConstraints JavaDoc getErrorPanelConstraints(){
198         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
199         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
200         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
201         gridBagConstraints.weightx = 1.0;
202         gridBagConstraints.insets = new Insets JavaDoc(6,12,11,11);
203         return gridBagConstraints;
204     }
205
206     public java.awt.Color JavaDoc getMessageForegroundColor() {
207         return BaseCustomizer.getErrorForegroundColor();
208     }
209 }
210
Popular Tags