1 19 24 25 package org.netbeans.modules.j2ee.sun.ide.sunresources.wizards; 26 27 import org.openide.util.NbBundle; 28 29 import java.util.List ; 30 import java.util.ResourceBundle ; 31 import javax.swing.*; 32 import javax.swing.event.ChangeListener ; 33 34 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils; 36 37 import org.netbeans.modules.j2ee.sun.sunresources.beans.Field; 38 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldGroup; 39 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants; 40 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldHelper; 41 42 46 public class CommonAttributeVisualPanel extends javax.swing.JPanel implements ChangeListener , WizardConstants { 47 48 52 public static final String TYPE_JDBC_RESOURCE = "_DS"; public static final String TYPE_PERSISTENCE_MANAGER = "_PM"; public static final String TYPE_CP_POOL_SETTING = "_CP_SETTING"; 56 protected ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.Bundle"); 58 protected final CommonAttributePanel panel; 59 protected FieldGroup[] groups; 60 protected int fieldSize; 61 protected ResourceConfigHelper helper; 62 protected String panelType = TYPE_JDBC_RESOURCE; 63 protected boolean createNewResource = false; 64 65 protected javax.swing.JTextArea descriptionTextArea = null; 67 protected javax.swing.JPanel jPanel1[] = null; 68 protected javax.swing.JLabel jLabels[] = null; 69 protected javax.swing.JComponent jFields[] = null; 70 protected Field fields[] = null; 71 protected String resourceName; 72 protected boolean showGroupTitle = false; 73 74 private javax.swing.JRadioButton existingResourceRadioButton; 76 private javax.swing.JRadioButton newResourceRadioButton; 77 private javax.swing.JPanel jPanel0; 78 private javax.swing.JComboBox existingResourceComboBox; 79 private boolean firstTime = true; 80 private int y = 0; 81 82 83 public CommonAttributeVisualPanel(CommonAttributePanel panel, FieldGroup[] groups, String panelType, ResourceConfigHelper helper) { 84 this.firstTime = true; 85 this.panel = panel; 86 this.helper = helper; 87 this.resourceName = panel.getResourceName(); 88 setFieldGroups(groups); 89 90 setName(NbBundle.getMessage(CommonAttributeVisualPanel.class, "TITLE_" + groups[0].getName())); 93 if (groups.length > 1) 94 this.showGroupTitle = true; 95 96 setLayout(new java.awt.GridBagLayout ()); 97 if (!showGroupTitle) { 98 initDescriptionComponent(); 99 } 100 101 setPanelType(panelType); 102 if (getPanelType().equals(TYPE_JDBC_RESOURCE) || getPanelType().equals(TYPE_PERSISTENCE_MANAGER)) { 103 initChoiceComponents(); 104 initComponents(); 105 } else { 106 initComponents(); 107 } 108 getAccessibleContext().setAccessibleName(bundle.getString("TITLE_ConnPoolWizardPanel_optionalProps")); 109 getAccessibleContext().setAccessibleDescription(bundle.getString("TITLE_ConnPoolWizardPanel_optionalProps")); 110 refreshFields(); 111 this.firstTime = false; 112 } 113 114 public void setFieldGroups(FieldGroup[] groups) { 115 this.groups = groups; 116 fieldSize = 0; 117 for (int i = 0; i < groups.length; i++) { 118 fieldSize += groups[i].sizeField(); 119 } 120 } 121 122 127 private void initComponents() { 128 java.awt.GridBagConstraints gridBagConstraints; 129 130 int gap = 0; 131 int itemHeight = 28; 132 if (fieldSize < 6) { 133 itemHeight = 42; 134 gap = 7; 135 } 136 int i = 0; 137 jPanel1 = new JPanel[groups.length]; 138 fields = new Field[fieldSize]; 139 jLabels = new JLabel[fieldSize]; 140 jFields = new JComponent[fieldSize]; 141 142 for (int j = 0; j < groups.length; j++) { 143 if (showGroupTitle) { 144 JLabel label = new JLabel(); 145 label.setText(bundle.getString("LBL_GROUP_" + groups[j].getName())); label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_LBL_GROUP_" + groups[j].getName() + "A11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 148 gridBagConstraints.gridx = 0; 149 gridBagConstraints.gridy = this.y; this.y++; 150 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 151 gridBagConstraints.ipadx = 0; 152 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 11); 153 add(label, gridBagConstraints); 154 } 155 156 Field[] flds = groups[j].getField(); 157 int size = flds.length; 158 jPanel1[j] = new javax.swing.JPanel (); 159 jPanel1[j].setLayout(new java.awt.GridBagLayout ()); 160 161 jPanel1[j].setMaximumSize(new java.awt.Dimension (480, itemHeight * size)); 162 jPanel1[j].setMinimumSize(new java.awt.Dimension (480, itemHeight * size)); 163 jPanel1[j].setPreferredSize(new java.awt.Dimension (480, itemHeight * size)); 164 165 for (int k = 0; k < size; k++) { 166 fields[i] = flds[k]; 167 JLabel jLabel = new JLabel(); 168 jLabel.setText(bundle.getString("LBL_" + fields[i].getName())); jLabel.setDisplayedMnemonic(bundle.getString("LBL_" + fields[i].getName() + "_Mnemonic").charAt(0)); gridBagConstraints = new java.awt.GridBagConstraints (); 171 gridBagConstraints.gridx = 0; 172 gridBagConstraints.gridy = k; 173 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 174 gridBagConstraints.ipadx = 0; 175 gridBagConstraints.insets = new java.awt.Insets (2, 12, gap, 0); 176 jLabels[i] = jLabel; 177 jPanel1[j].add(jLabels[i], gridBagConstraints); 178 179 if (FieldHelper.isList(fields[i])) { 180 JComboBox jComboBox = new JComboBox(); 181 jLabel.setLabelFor(jComboBox); 182 jComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ToolTip_" + fields[i].getName())); String tags[] = FieldHelper.getTags(fields[i]); 184 for (int h = 0; h < tags.length; h++) { 185 jComboBox.addItem(tags[h]); 186 } 187 final int index = i; 188 jComboBox.addActionListener(new java.awt.event.ActionListener () { 189 public void actionPerformed(java.awt.event.ActionEvent evt) { 190 jComboBoxActionPerformed(evt, index); 191 } 192 }); 193 gridBagConstraints = new java.awt.GridBagConstraints (); 194 gridBagConstraints.gridx = 1; 195 gridBagConstraints.gridy = k; 196 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 197 gridBagConstraints.insets = new java.awt.Insets (2, 20, gap, 0); 198 gridBagConstraints.weightx = 1.0; 199 jFields[i] = jComboBox; 200 jPanel1[j].add(jComboBox, gridBagConstraints); 201 } else { 202 JTextField jTextField; 203 if(FieldHelper.isInt(fields[i])) 204 jTextField = new javax.swing.JTextField (Util.getNumericDocument(), null, 0); 205 else{ 206 jTextField = new JTextField(); 207 jTextField.setText("jTextField1"); } 209 jLabel.setLabelFor(jTextField); 210 jTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ToolTip_" + fields[i].getName())); jTextField.setToolTipText(bundle.getString("ToolTip_" + fields[i].getName())); jTextField.setMinimumSize(new java.awt.Dimension (340, 21)); 213 jTextField.setPreferredSize(new java.awt.Dimension (340, 21)); 214 final int index = i; 215 jTextField.addKeyListener(new java.awt.event.KeyAdapter () { 216 public void keyReleased(java.awt.event.KeyEvent evt) { 217 jTextField1KeyReleased(evt, index); 218 } 219 }); 220 gridBagConstraints = new java.awt.GridBagConstraints (); 221 gridBagConstraints.gridx = 1; 222 gridBagConstraints.gridy = k; 223 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 224 gridBagConstraints.insets = new java.awt.Insets (2, 20, gap, 0); 225 jFields[i] = jTextField; 226 jPanel1[j].add(jTextField, gridBagConstraints); 227 } 228 i++; 229 } 230 231 gridBagConstraints = new java.awt.GridBagConstraints (); 232 gridBagConstraints.gridx = 0; 233 gridBagConstraints.gridy = this.y; this.y++; 234 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 235 gridBagConstraints.insets = new java.awt.Insets (5, 0, 10, 11); 236 jPanel1[j].setBorder(new javax.swing.border.EtchedBorder ()); 237 add(jPanel1[j], gridBagConstraints); 238 } 239 } 240 241 private void initDescriptionComponent() { 242 java.awt.GridBagConstraints gridBagConstraints; 243 descriptionTextArea = new javax.swing.JTextArea (); 244 descriptionTextArea.setEditable(false); 245 descriptionTextArea.setFont(javax.swing.UIManager.getFont("Label.font")); descriptionTextArea.setText(bundle.getString(this.resourceName + "_Description")); descriptionTextArea.setDisabledTextColor(javax.swing.UIManager.getColor("Label.foreground")); descriptionTextArea.setRequestFocusEnabled(false); 249 descriptionTextArea.setEnabled(false); 250 descriptionTextArea.setOpaque(false); 251 gridBagConstraints = new java.awt.GridBagConstraints (); 252 gridBagConstraints.gridx = 0; 253 gridBagConstraints.gridy = this.y; this.y++; 254 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 255 gridBagConstraints.weightx = 1.0; 256 gridBagConstraints.weighty = 0.0; 257 gridBagConstraints.insets = new java.awt.Insets (12, 12, 20, 11); 258 add(descriptionTextArea, gridBagConstraints); 259 } 260 261 private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt, int index) { 262 ResourceConfigData data = this.helper.getData(); 263 String item = (String )((JComboBox)jFields[index]).getSelectedItem(); 264 String fieldName = fields[index].getName(); 265 String val = data.getString(fieldName); 266 if (!item.equals(val)) 267 data.setString(fieldName, item); 268 } 269 270 private void jTextField1KeyReleased(java.awt.event.KeyEvent evt, int index) { 271 ResourceConfigData data = this.helper.getData(); 272 String item = (String )((JTextField)jFields[index]).getText(); 273 String fieldName = fields[index].getName(); 274 String val = data.getString(fieldName); 275 286 287 String jLabel = (String )this.jLabels[index].getText(); 288 if (!item.equals(val)) { 289 data.setString(fieldName, item); 290 } 292 panel.fireChange(this); 293 } 294 295 private void initChoiceComponents() { 297 java.awt.GridBagConstraints gridBagConstraints; 298 299 jPanel0 = new javax.swing.JPanel (); 300 ButtonGroup bg = new ButtonGroup(); 301 existingResourceRadioButton = new javax.swing.JRadioButton (); 302 existingResourceComboBox = new javax.swing.JComboBox (); 303 newResourceRadioButton = new javax.swing.JRadioButton (); 304 305 jPanel0.setMaximumSize(new java.awt.Dimension (480, 150)); 306 jPanel0.setMinimumSize(new java.awt.Dimension (480, 150)); 307 jPanel0.setPreferredSize(new java.awt.Dimension (480, 150)); 308 jPanel0.setLayout(new java.awt.GridBagLayout ()); 309 310 jPanel0.setBorder(new javax.swing.border.EtchedBorder ()); 311 existingResourceRadioButton.setSelected(true); 312 existingResourceRadioButton.setText(bundle.getString("ExistingResource" + getPanelType())); existingResourceRadioButton.setToolTipText(bundle.getString("ACS_ExistingResourceA11yDesc" + getPanelType())); existingResourceRadioButton.setMnemonic(bundle.getString("ExistingResource_Default_Mnemonic").charAt(0)); gridBagConstraints = new java.awt.GridBagConstraints (); 316 gridBagConstraints.gridx = 0; 317 gridBagConstraints.gridy = 1; 318 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 319 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 320 jPanel0.add(existingResourceRadioButton, gridBagConstraints); 321 322 existingResourceComboBox.setToolTipText(bundle.getString("ACS_ExistingResourceComboBoxA11yDesc" + getPanelType())); 323 existingResourceComboBox.getAccessibleContext().setAccessibleName(bundle.getString("ExistingResource" + getPanelType())); existingResourceComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ExistingResourceComboBoxA11yDesc" + getPanelType())); 325 existingResourceComboBox.addActionListener(new java.awt.event.ActionListener () { 326 public void actionPerformed(java.awt.event.ActionEvent evt) { 327 existingResourceComboBoxActionPerformed(evt); 328 } 329 }); 330 331 gridBagConstraints = new java.awt.GridBagConstraints (); 332 gridBagConstraints.gridx = 0; 333 gridBagConstraints.gridy = 2; 334 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 335 gridBagConstraints.insets = new java.awt.Insets (12, 24, 5, 11); 336 jPanel0.add(existingResourceComboBox, gridBagConstraints); 337 338 newResourceRadioButton.setText(bundle.getString("NewResource" + getPanelType())); newResourceRadioButton.setToolTipText(bundle.getString("ACS_NewResourceA11yDesc" + getPanelType())); newResourceRadioButton.setMnemonic(bundle.getString("NewResource_Default_Mnemonic").charAt(0)); gridBagConstraints = new java.awt.GridBagConstraints (); 342 gridBagConstraints.gridx = 0; 343 gridBagConstraints.gridy = 3; 344 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 345 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 346 jPanel0.add(newResourceRadioButton, gridBagConstraints); 347 348 gridBagConstraints = new java.awt.GridBagConstraints (); 349 gridBagConstraints.gridx = 0; 350 gridBagConstraints.gridy = this.y; this.y++; 351 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 352 gridBagConstraints.weightx = 1.0; 353 gridBagConstraints.weighty = 0.0; 354 gridBagConstraints.insets = new java.awt.Insets (0, 0, 40, 0); 355 add(jPanel0, gridBagConstraints); 356 357 bg.add(existingResourceRadioButton); 358 bg.add(newResourceRadioButton); 359 bg.getSelection().addChangeListener(this); 360 361 List list = null; 362 if (getPanelType().equals(TYPE_JDBC_RESOURCE)) { 363 list = ResourceUtils.getRegisteredConnectionPools(this.helper.getData()); 364 }else if (getPanelType().equals(TYPE_PERSISTENCE_MANAGER)) { 365 list = ResourceUtils.getRegisteredJdbcResources(this.helper.getData()); 366 } 367 if(list != null){ 368 for (int i = 0; i < list.size(); i++) { 369 existingResourceComboBox.addItem(list.get(i)); 370 } 371 } 372 373 if (existingResourceComboBox.getItemCount() == 0) { 374 existingResourceComboBox.insertItemAt(bundle.getString("NoResource" + getPanelType()), 0); existingResourceRadioButton.setEnabled(true); 376 existingResourceComboBox.setEnabled(true); 377 } else { 378 existingResourceComboBox.insertItemAt(bundle.getString("SelectFromTheList"), 0); existingResourceRadioButton.setSelected(true); 380 newResourceRadioButton.setSelected(false); 381 } 382 existingResourceComboBox.setSelectedIndex(0); 383 } 384 385 public void stateChanged(final javax.swing.event.ChangeEvent p1) { 386 if (p1.getSource().getClass() == javax.swing.JToggleButton.ToggleButtonModel.class) { 387 if (existingResourceRadioButton.isSelected()) { 388 existingResourceComboBox.setEnabled(true); 389 setExistingResourceData(); 390 } else { 391 existingResourceComboBox.setEnabled(false); 392 setNewResourceData(); 393 } 394 395 if (!firstTime) { 396 panel.fireChange(this); 397 } 398 } 399 } 400 401 private void existingResourceComboBoxActionPerformed(java.awt.event.ActionEvent evt) { 402 setExistingResourceData(); 403 } 404 405 public boolean isNewResourceSelected() { 406 return createNewResource; 407 } 408 409 public void setExistingResourceData() { 410 ResourceConfigData data = this.helper.getData(); 411 if(existingResourceComboBox.getSelectedIndex() > 0) { 412 String name = existingResourceComboBox.getSelectedItem().toString(); 413 if (getPanelType().equals(TYPE_JDBC_RESOURCE)) { 414 data.setString(__PoolName, name); 415 }else if (getPanelType().equals(TYPE_PERSISTENCE_MANAGER)) { 416 data.setString(__JdbcResourceJndiName, name); 417 } 418 }else{ 419 if (!firstTime) { 420 if (getPanelType().equals(TYPE_JDBC_RESOURCE)) { 421 data.setString(__PoolName, ""); }else if (getPanelType().equals(TYPE_PERSISTENCE_MANAGER)) { 423 data.setString(__JdbcResourceJndiName, ""); } 425 } 426 } 427 if (!firstTime) { 428 panel.fireChange(this); 429 } 430 createNewResource = false; 431 } 432 433 public void setNewResourceData() { 434 createNewResource = true; 435 } 436 437 public void refreshFields() { 438 ResourceConfigData data = this.helper.getData(); 439 for (int i = 0; i < jFields.length; i++) { 440 String item; 441 if (FieldHelper.isList(fields[i])) { 442 item = (String )((JComboBox)jFields[i]).getSelectedItem(); 443 } else { 444 item = (String )((JTextField)jFields[i]).getText(); 445 } 446 String fieldName = fields[i].getName(); 447 Object value = data.get(fieldName); 448 if (value == null) { 449 value = FieldHelper.getDefaultValue(fields[i]); 450 if(fieldName.equals("jndi-name")){ String helperJndiName = data.getTargetFile(); 452 if(helperJndiName != null){ 453 if(getPanelType().equals(TYPE_JDBC_RESOURCE) || getPanelType().equals(TYPE_PERSISTENCE_MANAGER)){ 454 value = value + helperJndiName; 455 } 456 } 457 } 458 data.set(fieldName, value); 459 } 460 String val = (String )value; 461 if (!item.equals(val)) { 462 if (FieldHelper.isList(fields[i])) { 463 ((JComboBox)jFields[i]).setSelectedItem(val); 464 } else { 465 ((JTextField)jFields[i]).setText(val); 466 } 467 } 468 } 469 if (getPanelType().equals(TYPE_JDBC_RESOURCE)) { 470 Object selPool = (Object )data.getString(__PoolName); 471 existingResourceComboBox.setSelectedItem(selPool); 472 }else if (getPanelType().equals(TYPE_PERSISTENCE_MANAGER)) { 473 Object selJDBC = (Object )data.getString(__JdbcResourceJndiName); 474 existingResourceComboBox.setSelectedItem(selJDBC); 475 } 476 } 477 478 public CommonAttributeVisualPanel setFirstTime(boolean first) { 479 this.firstTime = first; 480 return this; 481 } 482 483 public void initData() { 484 refreshFields(); 485 } 486 487 protected String getPanelType() { 488 return panelType; 489 } 490 491 protected void setPanelType(String type) { 492 this.panelType = type; 493 } 494 495 public void setInitialFocus(){ 496 new setFocus(this.existingResourceRadioButton); 497 } 498 499 public void setPropInitialFocus(){ 500 new setFocus(jFields[0]); 501 } 502 503 public void setHelper(ResourceConfigHelper helper){ 504 this.helper = helper; 505 this.helper.getData().setString("jndi-name", helper.getData().getTargetFile()); refreshFields(); 507 } 508 } 509 | Popular Tags |