1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common; 26 27 import java.awt.Dimension ; 28 import java.awt.GridBagConstraints ; 29 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 32 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.Collections ; 36 import java.util.ResourceBundle ; 37 38 import javax.swing.JPanel ; 39 import javax.swing.DefaultComboBoxModel ; 40 import javax.swing.SwingUtilities ; 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 55 public class ChangeDocTypePanel extends javax.swing.JPanel { 56 57 59 protected static final ResourceBundle commonBundle = ResourceBundle.getBundle( 60 "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); 62 private final JPanel customizerPanel; 63 private final Base theBean; 64 private final SunONEDeploymentConfiguration config; 65 66 private ASDDVersion currentVersion; 67 private DefaultComboBoxModel versionModel; 68 69 70 public ChangeDocTypePanel(JPanel 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 93 private void initComponents() { 95 java.awt.GridBagConstraints gridBagConstraints; 96 97 jLblAppServerVersion = new javax.swing.JLabel (); 98 jCbxAppServerVersion = new javax.swing.JComboBox (); 99 jLblDocType = new javax.swing.JLabel (); 100 jScrollPane1 = new javax.swing.JScrollPane (); 101 jTxtDocType = new javax.swing.JTextArea (); 102 103 setLayout(new java.awt.GridBagLayout ()); 104 105 jLblAppServerVersion.setLabelFor(jCbxAppServerVersion); 106 jLblAppServerVersion.setText(commonBundle.getString("LBL_ServerVersion_1")); 107 gridBagConstraints = new java.awt.GridBagConstraints (); 108 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 109 gridBagConstraints.insets = new java.awt.Insets (12, 12, 11, 11); 110 add(jLblAppServerVersion, gridBagConstraints); 111 112 jCbxAppServerVersion.addItemListener(new java.awt.event.ItemListener () { 113 public void itemStateChanged(java.awt.event.ItemEvent evt) { 114 jCbxAppServerVersionItemStateChanged(evt); 115 } 116 }); 117 118 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (); 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 (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 (); 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 (0, 12, 11, 11); 151 add(jScrollPane1, gridBagConstraints); 152 153 } 155 private void jCbxAppServerVersionItemStateChanged(java.awt.event.ItemEvent evt) { currentVersion = (ASDDVersion) versionModel.getSelectedItem(); 157 setDocTypeText(getDocType()); 158 firePropertyChange(Constants.USER_DATA_CHANGED, null, null); 159 } 161 162 private javax.swing.JComboBox jCbxAppServerVersion; 164 private javax.swing.JLabel jLblAppServerVersion; 165 private javax.swing.JLabel jLblDocType; 166 private javax.swing.JScrollPane jScrollPane1; 167 private javax.swing.JTextArea jTxtDocType; 168 170 private void initFields() { 171 versionModel = new DefaultComboBoxModel (); 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 doctype) { 192 jTxtDocType.setText(doctype); 193 jTxtDocType.getAccessibleContext().setAccessibleDescription(doctype); 194 195 jTxtDocType.setRows(Math.min(10, Math.max(3, countLines(doctype)))); 199 } 200 201 private int countLines(String text) { 202 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 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 getErrors() { 224 return Collections.EMPTY_SET; 225 } 226 227 private Collection getWarnings() { 228 ArrayList warnings = new ArrayList (); 230 231 ASDDVersion newVersion = getCurrentVersion(); 232 ASDDVersion oldVersion = getOriginalVersion(); 233 234 236 if(hasUnsavedChanges()) { 237 warnings.add(commonBundle.getString("WARN_WillSaveChanges")); } 239 240 244 if(newVersion.compareTo(oldVersion) < 0) { 245 warnings.add(commonBundle.getString("WARN_PossibleDataLoss")); } 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 ConfigDataObject primaryDO = null; 264 ConfigurationStorage storage = config.getStorage(); 265 if(storage != null) { 266 primaryDO = storage.getPrimaryDataObject(); 267 } 268 269 if(primaryDO == null) { 270 return false; 272 } 273 274 if(!primaryDO.closeConfigEditors()) { 275 return false; 277 } 278 279 config.setAppServerVersion(newVersion); 281 try { 282 Thread.currentThread().sleep(200); 284 } catch (InterruptedException ex) { 285 } 287 288 final ConfigDataObject theDataObject = primaryDO; 289 SwingUtilities.invokeLater(new Runnable () { 290 public void run() { 291 OpenCookie opener = (OpenCookie) theDataObject.getCookie(OpenCookie.class); 293 if(opener != null) { 294 opener.open(); 295 } 296 } 297 }); 298 299 return true; 300 } 301 302 313 public static void editASVersion(JPanel parent, Base theBean) { 314 ChangeDocTypePanel docTypePanel = new ChangeDocTypePanel(parent, theBean); 315 docTypePanel.displayDialog(parent, commonBundle.getString("TITLE_ChangeDocType"), "AS_CFG_ChangeDocTypePanelHelp"); } 318 319 private void displayDialog(JPanel parent, String title, String 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 private static class BetterInputDialog extends InputDialog { 335 private final ChangeDocTypePanel dialogPanel; 336 private final String panelHelpId; 337 338 public BetterInputDialog(JPanel parent, String title, String helpId, ChangeDocTypePanel childPanel) { 339 super(parent, title, false, true); 340 341 dialogPanel = childPanel; 342 panelHelpId = helpId; 343 344 dialogPanel.setPreferredSize(new Dimension (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 constraints = new GridBagConstraints (); 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 () { 365 public void propertyChange(PropertyChangeEvent evt) { 366 handleErrorDisplay(); 367 } 368 }); 369 } 370 371 private void handleErrorDisplay() { 372 setErrors(dialogPanel.getErrors(), dialogPanel.getWarnings()); 373 } 374 375 protected String getHelpId() { 376 return panelHelpId; 377 } 378 } 379 } 380 | Popular Tags |