1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import org.netbeans.jmi.javamodel.JavaEnum; 23 import org.netbeans.jmi.javamodel.EnumConstant; 24 import org.openide.util.HelpCtx; 25 import org.openide.util.NbBundle; 26 import org.openide.util.Utilities; 27 import org.openide.ErrorManager; 28 29 import javax.jmi.reflect.JmiException; 30 31 35 final class EnumConstantCustomizer extends javax.swing.JPanel { 36 37 38 private final JavaEnum jclass; 39 40 private EnumConstant constant; 41 private boolean isOK = true; 42 43 44 public EnumConstantCustomizer(JavaEnum jclass, EnumConstant constant) { 45 this.jclass = jclass; 46 this.constant = constant; 47 initComponents(); 48 49 nameTextField.setText(constant.getName()); 50 HelpCtx.setHelpIDString (this, "java.enumconstant.customizer"); 52 initAccessibility(); 53 } 54 55 public boolean isOK() { 56 nameTextFieldFocusLost(null); 57 return isOK; 58 } 59 60 private void initAccessibility() { 61 nameTextField.getAccessibleContext().setAccessibleName(getString("ACSN_ConstantNameTextField")); nameTextField.getAccessibleContext().setAccessibleDescription(getString("ACSD_ConstantNameTextField")); this.getAccessibleContext().setAccessibleDescription("ACSD_ConstantCustomizerDialog"); } 65 66 67 private static String getString(String key) { 68 return NbBundle.getMessage(EnumConstantCustomizer.class, key); 69 } 70 71 76 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 78 79 nameLabel = new javax.swing.JLabel (); 80 nameTextField = new javax.swing.JTextField (); 81 82 setLayout(new java.awt.GridBagLayout ()); 83 84 nameLabel.setLabelFor(nameTextField); 85 nameLabel.setText(org.openide.util.NbBundle.getMessage(EnumConstantCustomizer.class, "CTL_Name")); 86 gridBagConstraints = new java.awt.GridBagConstraints (); 87 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 8); 88 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 89 add(nameLabel, gridBagConstraints); 90 91 nameTextField.setColumns(25); 92 nameTextField.addFocusListener(new java.awt.event.FocusAdapter () { 93 public void focusLost(java.awt.event.FocusEvent evt) { 94 nameTextFieldFocusLost(evt); 95 } 96 }); 97 98 gridBagConstraints = new java.awt.GridBagConstraints (); 99 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 100 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 101 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 11); 102 gridBagConstraints.weightx = 1.0; 103 add(nameTextField, gridBagConstraints); 104 105 } 107 private void nameTextFieldFocusLost(java.awt.event.FocusEvent evt) { if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) 109 return; 110 111 String newName = nameTextField.getText().trim(); 112 String oldName = constant.getName(); 113 boolean ok = false; 114 Exception x = null; 115 116 if (!Utilities.isJavaIdentifier(newName)) { 117 x = new IllegalArgumentException ("Invalid name"); ErrorManager.getDefault().annotate( 119 x, ErrorManager.USER, null, 120 NbBundle.getMessage(FieldCustomizer.class, "MSG_Not_Valid_Identifier"), null, null); 122 } else if (oldName.equals(newName)) { 123 return; } else if (SourceEditSupport.findConstant(this.jclass, newName) != null) { 125 x = new IllegalArgumentException ("Invalid name"); ErrorManager.getDefault().annotate( 127 x, ErrorManager.USER, null, 128 NbBundle.getMessage(FieldCustomizer.class, "MSG_Used_Identifier", newName), null, null); 130 } else { 131 try { 132 constant.setName(newName); 133 ok = true; 134 } catch (JmiException e) { 135 ErrorManager.getDefault().notify(e); 136 } 137 } 138 isOK = ok; 139 if (!ok) { 140 nameTextField.setText(oldName); 141 } 142 if (x != null) { 143 ErrorManager.getDefault().notify(x); 144 } 145 } 147 148 private javax.swing.JLabel nameLabel; 150 private javax.swing.JTextField nameTextField; 151 153 } 154 | Popular Tags |