KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BeanInputDialog.java
21  *
22  * Created on October 4, 2003, 8:42 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.ResourceBundle JavaDoc;
30
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33
34 import java.awt.BorderLayout JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.Insets JavaDoc;
37
38 import javax.swing.border.EmptyBorder JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40
41
42 import org.netbeans.modules.j2ee.sun.share.Constants;
43 import org.netbeans.modules.j2ee.sun.validation.ValidationManager;
44 import org.netbeans.modules.j2ee.sun.validation.ValidationManagerFactory;
45
46 /**
47  *
48  * @author Rajeshwar Patil
49  * @version %I%, %G%
50  */

51
52 public abstract class BeanInputDialog extends InputDialog {
53
54     /** This is the property that child dialog panels should indicate has changed
55      * when they want the parent dialog (this object) to update the error status.
56      */

57     public static final String JavaDoc USER_DATA_CHANGED = Constants.USER_DATA_CHANGED; // NOI18N
58

59     private static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
60         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); //NOI18N
61

62     // Owner & child panels
63
private JPanel JavaDoc parentPanel;
64     private JPanel JavaDoc dialogPanel;
65     private boolean editmode;
66     
67     // Validation support
68
protected ValidationSupport validationSupport;;
69
70     /** Creates a new instance of BeanInputDialog */
71     public BeanInputDialog(JPanel JavaDoc parent, String JavaDoc title, Object JavaDoc[] values) {
72         this(parent, title, false, values);
73     }
74
75     public BeanInputDialog(JPanel JavaDoc parent, String JavaDoc title, boolean showRequiredNote, Object JavaDoc[] values) {
76         super(parent, title, showRequiredNote);
77
78         editmode = true;
79         parentPanel = parent;
80         dialogPanel = getDialogPanel(values);
81         validationSupport = new ValidationSupport();
82         
83         initComponents();
84     }
85
86     /** Creates a new instance of BeanInputDialog */
87     public BeanInputDialog(JPanel JavaDoc parent, String JavaDoc title) {
88         this(parent, title, false);
89     }
90
91     public BeanInputDialog(JPanel JavaDoc parent, String JavaDoc title, boolean showRequiredNote) {
92         super(parent, title, showRequiredNote);
93         editmode = false;
94         parentPanel = parent;
95         dialogPanel = getDialogPanel();
96         validationSupport = new ValidationSupport();
97         
98         initComponents();
99     }
100
101     private void initComponents() {
102             
103         // Add the content panel
104
getContentPane().add(dialogPanel, BorderLayout.CENTER);
105         
106         // add user data listener
107
addListeners();
108
109         //adjust size of dialog based on parent panel
110
adjustSize();
111
112         // reorder controls
113
pack();
114         
115         // set proper location
116
setLocationInside(parentPanel);
117         
118         // Display any initial errors (mostly likely related to empty fields with
119
// no defaults or possible duplicate entry based on defaults that conflict
120
// with an existing item.
121
handleErrorDisplay();
122     }
123
124     //this method adjust the size of the size of this dialog based on parent
125
//panel width and no of elements in this dialog
126
private void adjustSize(){
127         int preferredWidth = parentPanel.getWidth()*3/4;
128         int noOfFields = getNOofFields();
129         if(-1 != noOfFields){
130             ///dialogPanel.setBorder(new EmptyBorder(new Insets(5, 5, 0, 5)));
131
//dialogPanel.setPreferredSize(new Dimension(preferredWidth,
132
//(int)dialogPanel.getPreferredSize().getHeight()));
133
Dimension JavaDoc dm = getContentPane().getPreferredSize();
134             int preferredHeight = (int)dialogPanel.getPreferredSize().getHeight();
135
136             if(preferredWidth < dm.getWidth()) {
137                 preferredWidth = (int)dm.getWidth();
138             }
139             if(preferredHeight < dm.getHeight()) {
140                 preferredHeight = (int)dm.getHeight();
141             }
142         }
143     };
144
145     // method to get the number of elements in this dialog
146
protected int getNOofFields() {
147         return -1;
148     };
149
150     protected abstract Object JavaDoc[] getValues();
151
152     protected abstract Collection JavaDoc getErrors();
153
154     protected abstract JPanel JavaDoc getDialogPanel();
155
156     protected abstract JPanel JavaDoc getDialogPanel(Object JavaDoc[] values);
157     
158     abstract protected String JavaDoc getHelpId();
159
160     // Handle change events from child panel
161
private void addListeners() {
162         dialogPanel.addPropertyChangeListener(USER_DATA_CHANGED, new PropertyChangeListener JavaDoc() {
163             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
164                 handleErrorDisplay();
165             }
166         });
167     }
168
169     private void handleErrorDisplay() {
170         // Check for duplicate entry, then check for other errors
171
ArrayList JavaDoc errors = new ArrayList JavaDoc();
172         
173         if(hasDuplicateEntry()) {
174             errors.add(bundle.getString("ERR_ObjectIsDuplicate"));
175         } else {
176             errors.addAll(getErrors());
177         }
178         
179         setErrors(errors);
180     }
181     
182     protected boolean hasDuplicateEntry() {
183         boolean result = false;
184         
185         if(parentPanel instanceof BeanTablePanel) {
186             BeanTablePanel beanPanel = (BeanTablePanel) parentPanel;
187             BeanTableModel model = beanPanel.getModel();
188             Object JavaDoc [] newValues = getValues();
189             
190             if(!editmode) {
191                 // New item mode is easy
192
if(model.alreadyExists(newValues)) {
193                     result = true;
194                 }
195             } else {
196                 // Edit mode is more difficult. The item is allowed to match
197
// the original item. But it is not allowed to match any other
198
// item. What is a clean way to do this? Only the model knows
199
// which are the key fields.
200
// if(model.alreadyExists(newValues) && !original) {
201
// result = true;
202
// }
203
}
204         }
205     
206         return result;
207     }
208
209     protected BeanTableModel getModel(){
210         BeanTableModel model = null;
211         if(parentPanel instanceof BeanTablePanel) {
212             BeanTablePanel beanPanel = (BeanTablePanel) parentPanel;
213             model = beanPanel.getModel();
214         }
215         return model;
216     }
217 }
218
Popular Tags