1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.lang.reflect.Modifier ; 23 import java.awt.*; 24 25 import javax.swing.*; 26 import javax.jmi.reflect.JmiException; 27 28 import org.openide.*; 29 import org.openide.util.Utilities; 30 import org.openide.util.NbBundle; 31 32 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 33 import org.netbeans.modules.java.ui.nodes.editors.TypeEditor; 34 import org.netbeans.jmi.javamodel.*; 35 36 40 public final class FieldCustomizer extends JPanel { 41 42 private final Field element; 43 private final JavaClass jclass; 44 45 48 private static final String [] COMMON_TYPES = TypeEditor.LVALUE_TYPES; 49 50 private boolean isOK = true; 51 52 55 public FieldCustomizer(JavaClass jclass, Field element) { 56 this.element = element; 57 this.jclass = jclass; 58 59 initComponents (); 60 61 int mask; 63 if (jclass.isInterface()) { 64 mask = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL; 65 } else { 66 mask = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 67 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | 68 Modifier.VOLATILE; 69 } 70 accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER); 71 modifierPanel.add(SourceEditSupport.createOtherModifiersPanel(element, mask), BorderLayout.CENTER); 72 73 nameTextField.setText(element.getName()); 75 76 typeCombo.setSelectedItem(ElementFormat.elementName(element.getType())); 78 79 initValueEditor.setText(element.getInitialValueText()); 81 82 jLabel1.setDisplayedMnemonic(NbBundle.getMessage(FieldCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); jLabel2.setDisplayedMnemonic(NbBundle.getMessage(FieldCustomizer.class, "CTL_Type_Mnemonic").charAt(0)); jLabel5.setDisplayedMnemonic(NbBundle.getMessage(FieldCustomizer.class, "CTL_InitValue_Mnemonic").charAt(0)); initAccessibility(); 87 } 88 89 public void addNotify() { 90 super.addNotify(); 91 92 int l = nameTextField.getText().length(); 94 nameTextField.setCaretPosition(0); 95 nameTextField.moveCaretPosition(l); 96 nameTextField.requestFocus(); 97 SwingUtilities.invokeLater(new Runnable () { 98 public void run() { 99 nameTextField.requestFocus(); 100 } 101 }); 102 } 103 104 private JavaModelPackage getJModel() { 105 return JavaMetamodel.getManager().getJavaExtent(this.jclass); 106 } 107 108 113 private void initComponents() { 115 java.awt.GridBagConstraints gridBagConstraints; 116 117 jLabel1 = new javax.swing.JLabel (); 118 nameTextField = new javax.swing.JTextField (); 119 jLabel2 = new javax.swing.JLabel (); 120 typeCombo = new javax.swing.JComboBox (COMMON_TYPES); 121 jLabel3 = new javax.swing.JLabel (); 122 accessPanel = new javax.swing.JPanel (); 123 jLabel4 = new javax.swing.JLabel (); 124 modifierPanel = new javax.swing.JPanel (); 125 jLabel5 = new javax.swing.JLabel (); 126 initValuePanel = new javax.swing.JPanel (); 127 jScrollPane1 = new javax.swing.JScrollPane (); 128 initValueEditor = new javax.swing.JEditorPane (); 129 130 setLayout(new java.awt.GridBagLayout ()); 131 132 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 6, 6, 6))); 133 jLabel1.setLabelFor(nameTextField); 134 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_Name")); 135 gridBagConstraints = new java.awt.GridBagConstraints (); 136 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 137 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 138 add(jLabel1, gridBagConstraints); 139 140 nameTextField.addFocusListener(new java.awt.event.FocusAdapter () { 141 public void focusLost(java.awt.event.FocusEvent evt) { 142 nameTextFieldFocusLost(evt); 143 } 144 }); 145 146 gridBagConstraints = new java.awt.GridBagConstraints (); 147 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 148 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 149 gridBagConstraints.weightx = 1.0; 150 gridBagConstraints.insets = new java.awt.Insets (12, 8, 0, 0); 151 add(nameTextField, gridBagConstraints); 152 nameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "ACSD_FieldNameTextField")); 153 154 jLabel2.setLabelFor(typeCombo); 155 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_Type")); 156 gridBagConstraints = new java.awt.GridBagConstraints (); 157 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 158 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 159 add(jLabel2, gridBagConstraints); 160 161 typeCombo.setEditable(true); 162 typeCombo.addActionListener(new java.awt.event.ActionListener () { 163 public void actionPerformed(java.awt.event.ActionEvent evt) { 164 jComboBox1ActionPerformed(evt); 165 } 166 }); 167 168 gridBagConstraints = new java.awt.GridBagConstraints (); 169 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 170 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 171 gridBagConstraints.weightx = 1.0; 172 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 173 add(typeCombo, gridBagConstraints); 174 typeCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "ACSD_FieldType")); 175 176 jLabel3.setLabelFor(accessPanel); 177 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_AccessRights")); 178 gridBagConstraints = new java.awt.GridBagConstraints (); 179 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 180 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 181 add(jLabel3, gridBagConstraints); 182 183 accessPanel.setLayout(new java.awt.BorderLayout ()); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 187 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 188 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 189 add(accessPanel, gridBagConstraints); 190 191 jLabel4.setLabelFor(modifierPanel); 192 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_Modifiers")); 193 gridBagConstraints = new java.awt.GridBagConstraints (); 194 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 195 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 196 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 197 add(jLabel4, gridBagConstraints); 198 199 modifierPanel.setLayout(new java.awt.BorderLayout ()); 200 201 gridBagConstraints = new java.awt.GridBagConstraints (); 202 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 203 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 204 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 205 add(modifierPanel, gridBagConstraints); 206 207 jLabel5.setLabelFor(initValueEditor); 208 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(FieldCustomizer.class, "CTL_InitValue")); 209 gridBagConstraints = new java.awt.GridBagConstraints (); 210 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 211 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 212 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 213 add(jLabel5, gridBagConstraints); 214 215 initValuePanel.setLayout(new java.awt.BorderLayout ()); 216 217 initValueEditor.setContentType("text/x-java"); 218 initValueEditor.setPreferredSize(new java.awt.Dimension (302, 200)); 219 initValueEditor.addFocusListener(new java.awt.event.FocusAdapter () { 220 public void focusLost(java.awt.event.FocusEvent evt) { 221 initValueEditorFocusLost(evt); 222 } 223 }); 224 225 jScrollPane1.setViewportView(initValueEditor); 226 227 initValuePanel.add(jScrollPane1, java.awt.BorderLayout.CENTER); 228 229 gridBagConstraints = new java.awt.GridBagConstraints (); 230 gridBagConstraints.gridwidth = 2; 231 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 232 gridBagConstraints.weightx = 1.0; 233 gridBagConstraints.weighty = 1.0; 234 gridBagConstraints.insets = new java.awt.Insets (2, 0, 0, 0); 235 add(initValuePanel, gridBagConstraints); 236 237 } 238 240 private void jComboBox1ActionPerformed (java.awt.event.ActionEvent evt) { Object selItem = typeCombo.getSelectedItem(); 242 if (selItem == null) 243 return; 244 245 String selItemTxt = selItem.toString().trim(); 246 Type oldValue = element.getType(); 247 boolean ok = false; 248 249 try { 250 final Type newValue = getJModel().getType().resolve(selItemTxt); 251 if (oldValue.equals(newValue)) { 252 return; } else if (!TypeEditor.isValidTypeSyntax(selItemTxt)) { 254 notifyUserWarning("MSG_Not_Valid_Type", "invalid type"); } else if (newValue instanceof PrimitiveType && 256 PrimitiveTypeKindEnum.VOID.equals(((PrimitiveType) newValue).getKind())) { 257 notifyUserWarning("MSG_Not_Valid_Type", "invalid type"); } else { 259 element.setType(newValue); 260 ok = true; 261 } 262 } catch (JmiException e) { 263 ErrorManager.getDefault().notify(e); 264 } 265 isOK = ok; 266 if (!ok) 267 typeCombo.setSelectedItem(ElementFormat.elementName(oldValue)); 268 } 270 private void initValueEditorFocusLost (java.awt.event.FocusEvent evt) { if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) 272 return; 273 274 String newText = initValueEditor.getText(); 275 String oldText = element.getInitialValueText(); 276 boolean ok = false; 277 if (!newText.equals(oldText)) { 278 try { 279 element.setInitialValueText(newText); 280 ok = true; 281 } catch (JmiException e) { 282 ErrorManager.getDefault().notify(e); 283 } 284 } else 285 return; 286 isOK = ok; 287 if (!ok) 288 initValueEditor.setText(oldText); 289 } 291 private void nameTextFieldFocusLost (java.awt.event.FocusEvent evt) { if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) 293 return; 294 295 String newName = nameTextField.getText().trim(); 296 String oldName = element.getName(); 297 boolean ok = false; 298 299 try { 300 if (!Utilities.isJavaIdentifier(newName)) { 301 notifyUserWarning("MSG_Not_Valid_Identifier", "invalid name"); } else if (oldName.equals(newName)) { 303 return; } else if (this.jclass.getField(newName, true) != null) { 305 notifyUserWarning("MSG_Used_Identifier", newName, "invalid name"); } else { 307 element.setName(newName); 308 ok = true; 309 } 310 } catch (JmiException e) { 311 ErrorManager.getDefault().notify(e); 312 } 313 isOK = ok; 314 if (!ok) { 315 nameTextField.setText(oldName); 316 } 317 } 319 320 private javax.swing.JPanel accessPanel; 322 private javax.swing.JEditorPane initValueEditor; 323 private javax.swing.JPanel initValuePanel; 324 private javax.swing.JLabel jLabel1; 325 private javax.swing.JLabel jLabel2; 326 private javax.swing.JLabel jLabel3; 327 private javax.swing.JLabel jLabel4; 328 private javax.swing.JLabel jLabel5; 329 private javax.swing.JScrollPane jScrollPane1; 330 private javax.swing.JPanel modifierPanel; 331 private javax.swing.JTextField nameTextField; 332 private javax.swing.JComboBox typeCombo; 333 335 private void initAccessibility() { 336 nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FieldCustomizer.class, "ACS_FieldNameTextField")); nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FieldCustomizer.class, "ACS_FieldNameTextField")); initValueEditor.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FieldCustomizer.class, "ACSN_InitValueEditorPane")); initValueEditor.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FieldCustomizer.class, "ACSD_InitValueEditorPane")); this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FieldCustomizer.class, "ACSD_FieldCustomizerDialog")); } 342 343 public boolean isOK() { 344 nameTextFieldFocusLost(null); 345 if (isOK) { 346 initValueEditorFocusLost(null); 347 } 348 if (isOK) { 349 jComboBox1ActionPerformed(null); 350 } 351 return isOK; 352 } 353 354 private static void notifyUserWarning(String bundleKey, String msg) { 355 IllegalArgumentException e = new IllegalArgumentException (msg); ErrorManager.getDefault().annotate( 357 e, ErrorManager.USER, null, 358 NbBundle.getMessage(FieldCustomizer.class, bundleKey), 359 null, null); 360 ErrorManager.getDefault().notify(e); 361 362 } 363 364 private static void notifyUserWarning(String bundleKey, String param, String msg) { 365 IllegalArgumentException e = new IllegalArgumentException (msg); 366 ErrorManager.getDefault().annotate( 367 e, ErrorManager.USER, null, 368 NbBundle.getMessage(FieldCustomizer.class, bundleKey, param), 369 null, null); 370 ErrorManager.getDefault().notify(e); 371 } 372 373 } 374 | Popular Tags |