KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ChangeDocTypePanel.java
21  *
22  * Created on April 4, 2006, 3:45 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.awt.Dimension JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.DefaultComboBoxModel JavaDoc;
40 import javax.swing.SwingUtilities JavaDoc;
41
42 import org.netbeans.modules.j2ee.sun.share.Constants;
43 import org.netbeans.modules.j2ee.sun.share.config.ConfigDataObject;
44 import org.netbeans.modules.j2ee.sun.share.config.ConfigurationStorage;
45 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion;
46 import org.netbeans.modules.j2ee.sun.share.configbean.Base;
47 import org.netbeans.modules.j2ee.sun.share.configbean.BaseRoot;
48 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration;
49 import org.openide.cookies.OpenCookie;
50
51 /**
52  *
53  * @author Peter Williams
54  */

55 public class ChangeDocTypePanel extends javax.swing.JPanel JavaDoc {
56     
57     /** Reference to the resource bundle in customizers/common
58      */

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

62     private final JPanel JavaDoc customizerPanel;
63     private final Base theBean;
64     private final SunONEDeploymentConfiguration config;
65     
66     private ASDDVersion currentVersion;
67     private DefaultComboBoxModel JavaDoc versionModel;
68     
69     /** Creates new form ChangeDocTypePanel */
70     public ChangeDocTypePanel(JPanel JavaDoc cp, Base bean) {
71         customizerPanel = cp;
72         theBean = bean;
73         config = theBean.getConfig();
74         currentVersion = config.getAppServerVersion();
75
76         initComponents();
77         initFields();
78     }
79
80     public ASDDVersion getOriginalVersion() {
81         return config.getAppServerVersion();
82     }
83
84     public ASDDVersion getCurrentVersion() {
85         return currentVersion;
86     }
87     
88     /** This method is called from within the constructor to
89      * initialize the form.
90      * WARNING: Do NOT modify this code. The content of this method is
91      * always regenerated by the Form Editor.
92      */

93     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
94
private void initComponents() {
95         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
96
97         jLblAppServerVersion = new javax.swing.JLabel JavaDoc();
98         jCbxAppServerVersion = new javax.swing.JComboBox JavaDoc();
99         jLblDocType = new javax.swing.JLabel JavaDoc();
100         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
101         jTxtDocType = new javax.swing.JTextArea JavaDoc();
102
103         setLayout(new java.awt.GridBagLayout JavaDoc());
104
105         jLblAppServerVersion.setLabelFor(jCbxAppServerVersion);
106         jLblAppServerVersion.setText(commonBundle.getString("LBL_ServerVersion_1"));
107         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
108         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
109         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 11, 11);
110         add(jLblAppServerVersion, gridBagConstraints);
111
112         jCbxAppServerVersion.addItemListener(new java.awt.event.ItemListener JavaDoc() {
113             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
114                 jCbxAppServerVersionItemStateChanged(evt);
115             }
116         });
117
118         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
119         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
120         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
121         gridBagConstraints.weightx = 1.0;
122         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 11, 11);
123         add(jCbxAppServerVersion, gridBagConstraints);
124         jCbxAppServerVersion.getAccessibleContext().setAccessibleName(commonBundle.getString("ACSN_ServerVersion"));
125         jCbxAppServerVersion.getAccessibleContext().setAccessibleDescription(commonBundle.getString("ACSD_ServerVersion"));
126
127         jLblDocType.setLabelFor(jTxtDocType);
128         jLblDocType.setText(commonBundle.getString("LBL_DocTypeTag_1"));
129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
131         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
133         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 1, 5);
134         add(jLblDocType, gridBagConstraints);
135         jLblDocType.getAccessibleContext().setAccessibleName(commonBundle.getString("ACSN_DocTypeTag"));
136
137         jTxtDocType.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.inactiveBackground"));
138         jTxtDocType.setColumns(20);
139         jTxtDocType.setEditable(false);
140         jTxtDocType.setRows(3);
141         jTxtDocType.setTabSize(4);
142         jScrollPane1.setViewportView(jTxtDocType);
143         jTxtDocType.getAccessibleContext().setAccessibleName(commonBundle.getString("ACSN_DocTypeTag"));
144
145         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
146         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
147         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
148         gridBagConstraints.weightx = 1.0;
149         gridBagConstraints.weighty = 1.0;
150         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 11, 11);
151         add(jScrollPane1, gridBagConstraints);
152
153     }// </editor-fold>//GEN-END:initComponents
154

155     private void jCbxAppServerVersionItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_jCbxAppServerVersionItemStateChanged
156
currentVersion = (ASDDVersion) versionModel.getSelectedItem();
157         setDocTypeText(getDocType());
158         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
159     }//GEN-LAST:event_jCbxAppServerVersionItemStateChanged
160

161     
162     // Variables declaration - do not modify//GEN-BEGIN:variables
163
private javax.swing.JComboBox JavaDoc jCbxAppServerVersion;
164     private javax.swing.JLabel JavaDoc jLblAppServerVersion;
165     private javax.swing.JLabel JavaDoc jLblDocType;
166     private javax.swing.JScrollPane JavaDoc jScrollPane1;
167     private javax.swing.JTextArea JavaDoc jTxtDocType;
168     // End of variables declaration//GEN-END:variables
169

170     private void initFields() {
171         versionModel = new DefaultComboBoxModel JavaDoc();
172         ASDDVersion minVersion = config.getMinASVersion();
173         ASDDVersion maxVersion = config.getMaxASVersion();
174         
175         for(int i = 0; i < ASDDVersion.asDDVersions.length; i++) {
176             if(ASDDVersion.asDDVersions[i].compareTo(minVersion) < 0) {
177                 continue;
178             } else if(ASDDVersion.asDDVersions[i].compareTo(maxVersion) > 0) {
179                 break;
180             } else {
181                 versionModel.addElement(ASDDVersion.asDDVersions[i]);
182             }
183         }
184         
185         jCbxAppServerVersion.setModel(versionModel);
186         jCbxAppServerVersion.setSelectedItem(currentVersion);
187         
188         setDocTypeText(getDocType());
189     }
190     
191     private void setDocTypeText(String JavaDoc doctype) {
192         jTxtDocType.setText(doctype);
193         jTxtDocType.getAccessibleContext().setAccessibleDescription(doctype);
194         
195         // set the line count of the text area to reflect how large the doctype
196
// statement is. This is to look nice if the doctype also includes the
197
// sun-cmp-mappings doctype.
198
jTxtDocType.setRows(Math.min(10, Math.max(3, countLines(doctype))));
199     }
200     
201     private int countLines(String JavaDoc text) {
202         // start count at one, so we always return one greater than actual number of lines.
203
int result = 1, max = text.length();
204         for(int i = 0; i < max; i++) {
205             if(text.charAt(i) == '\n') {
206                 result++;
207             }
208         }
209         return result;
210     }
211     
212     private String JavaDoc getDocType() {
213         Base bean = theBean;
214         
215         while(bean.getParent() != null) {
216             bean = bean.getParent();
217         }
218         
219         BaseRoot rootBean = (BaseRoot) bean;
220         return rootBean.generateDocType(currentVersion);
221     }
222     
223     private Collection JavaDoc getErrors() {
224         return Collections.EMPTY_SET;
225     }
226     
227     private Collection JavaDoc getWarnings() {
228         // Validate what the user typed in as a valid principal/group name
229
ArrayList JavaDoc warnings = new ArrayList JavaDoc();
230         
231         ASDDVersion newVersion = getCurrentVersion();
232         ASDDVersion oldVersion = getOriginalVersion();
233
234         /** Print warning if there are unsaved changes in configuration.
235          */

236         if(hasUnsavedChanges()) {
237             warnings.add(commonBundle.getString("WARN_WillSaveChanges")); // NOI18N
238
}
239
240         /** If new verion is < old version, warn about possible data loss of any
241          * new fields. Note we don't confirm if any of the newer fields are actually
242          * used, but we could implement such a check if we had the devtime.
243          */

244         if(newVersion.compareTo(oldVersion) < 0) {
245             warnings.add(commonBundle.getString("WARN_PossibleDataLoss")); // NOI18N
246
}
247         
248         return warnings;
249     }
250     
251     private boolean hasUnsavedChanges() {
252         boolean result = false;
253         ConfigurationStorage storage = config.getStorage();
254         if(storage != null) {
255             ConfigDataObject primaryDO = storage.getPrimaryDataObject();
256             result = (primaryDO != null && primaryDO.areModified());
257         }
258         return result;
259     }
260
261     private boolean changeVersion(ASDDVersion newVersion) {
262         // first get primary dataobject
263
ConfigDataObject primaryDO = null;
264         ConfigurationStorage storage = config.getStorage();
265         if(storage != null) {
266             primaryDO = storage.getPrimaryDataObject();
267         }
268         
269         if(primaryDO == null) {
270 // System.out.println("Can't find primary dataobject for this configuration.");
271
return false;
272         }
273         
274         if(!primaryDO.closeConfigEditors()) {
275 // System.out.println("Failed to close editor(s).");
276
return false;
277         }
278         
279         // change version via config api
280
config.setAppServerVersion(newVersion);
281         try {
282             // hack - wait for autosave() to post it's event.
283
Thread.currentThread().sleep(200);
284         } catch (InterruptedException JavaDoc ex) {
285             // shouldn't happen, but just ignore it if it does.
286
}
287
288         final ConfigDataObject theDataObject = primaryDO;
289         SwingUtilities.invokeLater(new Runnable JavaDoc() {
290             public void run() {
291                 // reopen editor
292
OpenCookie opener = (OpenCookie) theDataObject.getCookie(OpenCookie.class);
293                 if(opener != null) {
294                     opener.open();
295                 }
296             }
297         });
298         
299         return true;
300     }
301
302     /** Puts up an 'Edit...' dialog, doing validation against the supplied model,
303      * and ultimately updating the data model if the user hits <OK> and clears
304      * any errors.
305      *
306      * @param parent JPanel that is the parent of this popup - used for centering and sizing.
307      * @param entryValue The existing entry. This will be prefilled into the edit field.
308      * @param resourceBase The base name for the resources required for this instantiation
309      * of the panel.
310      * @param helpId The help ID for the dialog.
311      * @param theModel The particular Security model instance we're updating.
312      */

313     public static void editASVersion(JPanel JavaDoc parent, Base theBean) {
314         ChangeDocTypePanel docTypePanel = new ChangeDocTypePanel(parent, theBean);
315         docTypePanel.displayDialog(parent, commonBundle.getString("TITLE_ChangeDocType"), // NOI18N
316
"AS_CFG_ChangeDocTypePanelHelp"); // NOI18N
317
}
318     
319     private void displayDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId) {
320         BetterInputDialog dialog = new BetterInputDialog(parent, title, helpId, this);
321
322         int dialogChoice = dialog.display();
323         if(dialogChoice == dialog.OK_OPTION) {
324             ASDDVersion newVersion = getCurrentVersion();
325             ASDDVersion oldVersion = getOriginalVersion();
326
327             if(newVersion.compareTo(oldVersion) != 0) {
328                 changeVersion(newVersion);
329             }
330         }
331     }
332     
333     // !PW Should refactor this to handle any JPanel + error interface combo
334
private static class BetterInputDialog extends InputDialog {
335         private final ChangeDocTypePanel dialogPanel;
336         private final String JavaDoc panelHelpId;
337         
338         public BetterInputDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId, ChangeDocTypePanel childPanel) {
339             super(parent, title, false, true);
340             
341             dialogPanel = childPanel;
342             panelHelpId = helpId;
343             
344             dialogPanel.setPreferredSize(new Dimension JavaDoc(parent.getWidth()*3/4,
345                 dialogPanel.getPreferredSize().height));
346             
347             this.getAccessibleContext().setAccessibleName(dialogPanel.getAccessibleContext().getAccessibleName());
348             this.getAccessibleContext().setAccessibleDescription(dialogPanel.getAccessibleContext().getAccessibleDescription());
349
350             GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
351             constraints.gridwidth = GridBagConstraints.REMAINDER;
352             constraints.fill = GridBagConstraints.BOTH;
353             constraints.weightx = 1.0;
354             constraints.weighty = 1.0;
355             getContentPane().add(childPanel, constraints, 0);
356
357             addListeners();
358             pack();
359             setLocationInside(parent);
360             handleErrorDisplay();
361         }
362         
363         private void addListeners() {
364             dialogPanel.addPropertyChangeListener(Constants.USER_DATA_CHANGED, new PropertyChangeListener JavaDoc() {
365                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
366                     handleErrorDisplay();
367                 }
368             });
369         }
370
371         private void handleErrorDisplay() {
372             setErrors(dialogPanel.getErrors(), dialogPanel.getWarnings());
373         }
374         
375         protected String JavaDoc getHelpId() {
376             return panelHelpId;
377         }
378     }
379 }
380
Popular Tags