1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.awt.BorderLayout ; 23 import java.lang.reflect.Modifier ; 24 25 import javax.swing.*; 26 import javax.swing.border.*; 27 import javax.jmi.reflect.JmiException; 28 29 import org.openide.*; 30 import org.openide.explorer.propertysheet.PropertyPanel; 31 import org.openide.util.Utilities; 32 import org.openide.util.NbBundle; 33 import org.netbeans.jmi.javamodel.JavaClass; 34 import org.netbeans.jmi.javamodel.JavaModelPackage; 35 import org.netbeans.jmi.javamodel.MultipartId; 36 import org.netbeans.jmi.javamodel.Resource; 37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 38 39 43 final class ClassCustomizer extends JPanel { 44 45 private static final String [] COMMON_TYPES = { 46 "java.lang.Object", "java.awt.Component", "javax.swing.JComponent", "javax.swing.JPanel" }; 51 52 53 private final JavaClass element; 54 57 private final JavaClass jclass; 58 59 private final Resource resource; 60 61 private boolean isOK = true; 62 63 67 public ClassCustomizer(JavaClass jclass, JavaClass element) { 68 this(null, jclass, element); 69 } 70 71 75 public ClassCustomizer(Resource resource, JavaClass element) { 76 this(resource, null, element); 77 } 78 79 private ClassCustomizer(Resource resource, JavaClass jclass, JavaClass element) { 80 this.resource = resource; 81 this.jclass = jclass; 82 this.element = element; 83 initComponents (); 84 85 interfacesPanel.setBorder(new TitledBorder(NbBundle.getMessage(ClassCustomizer.class, "CTL_Interfaces"))); 88 int mask = Modifier.PUBLIC | Modifier.ABSTRACT; 90 if (resource == null) { mask |= Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC; 92 } 93 if (!element.isInterface()) { 94 mask |= Modifier.FINAL; 95 } 96 accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER); 97 modifierPanel.add(SourceEditSupport.createOtherModifiersPanel(element, mask), BorderLayout.CENTER); 98 99 if (element.isInterface()) { 100 superClassCombo.setEnabled(false); 101 jLabel2.setVisible(false); 102 superClassCombo.setVisible(false); 103 } else { 104 JavaClass superclassId = element.getSuperClass(); 105 superClassCombo.setSelectedItem( 106 (superclassId == null) ? "" : superclassId.getName()); 108 } 109 110 nameTextField.setText(element.getSimpleName()); 112 113 interfacesPanel.add( 115 new PropertyPanel(ClassNode.createInterfacesProperty(element, true), PropertyPanel.PREF_CUSTOM_EDITOR), 116 BorderLayout.CENTER 117 ); 118 119 jLabel1.setDisplayedMnemonic(NbBundle.getMessage(ClassCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); jLabel2.setDisplayedMnemonic(NbBundle.getMessage(ClassCustomizer.class, "CTL_Type_Mnemonic").charAt(0)); 123 jLabel3.setVisible(false); 125 typeTextField.setVisible(false); 126 127 initAccessibility(); 128 } 129 130 public void addNotify() { 131 super.addNotify(); 132 133 int l = nameTextField.getText().length(); 135 nameTextField.setCaretPosition(0); 136 nameTextField.moveCaretPosition(l); 137 SwingUtilities.invokeLater(new Runnable () { 138 public void run() { 139 nameTextField.requestFocus(); 140 } 141 }); 142 } 143 144 149 private void initComponents() { 151 java.awt.GridBagConstraints gridBagConstraints; 152 153 jLabel1 = new javax.swing.JLabel (); 154 nameTextField = new javax.swing.JTextField (); 155 jLabel3 = new javax.swing.JLabel (); 156 typeTextField = new javax.swing.JTextField (); 157 jLabel2 = new javax.swing.JLabel (); 158 superClassCombo = new javax.swing.JComboBox (COMMON_TYPES); 159 jLabel4 = new javax.swing.JLabel (); 160 accessPanel = new javax.swing.JPanel (); 161 jLabel5 = new javax.swing.JLabel (); 162 modifierPanel = new javax.swing.JPanel (); 163 interfacesPanel = new javax.swing.JPanel (); 164 165 setLayout(new java.awt.GridBagLayout ()); 166 167 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 6, 6, 6))); 168 jLabel1.setLabelFor(nameTextField); 169 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Name")); 170 gridBagConstraints = new java.awt.GridBagConstraints (); 171 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 172 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 173 add(jLabel1, gridBagConstraints); 174 175 nameTextField.addFocusListener(new java.awt.event.FocusAdapter () { 176 public void focusLost(java.awt.event.FocusEvent evt) { 177 nameTextFieldFocusLost(evt); 178 } 179 }); 180 181 gridBagConstraints = new java.awt.GridBagConstraints (); 182 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 183 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 184 gridBagConstraints.weightx = 1.0; 185 gridBagConstraints.insets = new java.awt.Insets (12, 8, 0, 0); 186 add(nameTextField, gridBagConstraints); 187 nameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "ACSD_ClassNameTextField")); 188 189 jLabel3.setLabelFor(typeTextField); 190 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Generic_Type")); 191 gridBagConstraints = new java.awt.GridBagConstraints (); 192 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 193 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 194 add(jLabel3, gridBagConstraints); 195 196 gridBagConstraints = new java.awt.GridBagConstraints (); 197 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 198 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 199 gridBagConstraints.weightx = 1.0; 200 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 201 add(typeTextField, gridBagConstraints); 202 typeTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "ACSD_ClassType")); 203 204 jLabel2.setLabelFor(superClassCombo); 205 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Superclass")); 206 gridBagConstraints = new java.awt.GridBagConstraints (); 207 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 208 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 209 add(jLabel2, gridBagConstraints); 210 211 superClassCombo.setEditable(true); 212 superClassCombo.addActionListener(new java.awt.event.ActionListener () { 213 public void actionPerformed(java.awt.event.ActionEvent evt) { 214 superClassComboActionPerformed(evt); 215 } 216 }); 217 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 220 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 221 gridBagConstraints.weightx = 1.0; 222 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 223 add(superClassCombo, gridBagConstraints); 224 superClassCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "ACSD_SuperClass")); 225 226 jLabel4.setLabelFor(accessPanel); 227 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_AccessRights")); 228 gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 230 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 231 add(jLabel4, gridBagConstraints); 232 233 accessPanel.setLayout(new java.awt.BorderLayout ()); 234 235 gridBagConstraints = new java.awt.GridBagConstraints (); 236 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 237 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 238 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 239 add(accessPanel, gridBagConstraints); 240 241 jLabel5.setLabelFor(modifierPanel); 242 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(ClassCustomizer.class, "CTL_Modifiers")); 243 gridBagConstraints = new java.awt.GridBagConstraints (); 244 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 245 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 246 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 247 add(jLabel5, gridBagConstraints); 248 249 modifierPanel.setLayout(new java.awt.BorderLayout ()); 250 251 gridBagConstraints = new java.awt.GridBagConstraints (); 252 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 253 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 254 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 255 add(modifierPanel, gridBagConstraints); 256 257 interfacesPanel.setLayout(new java.awt.BorderLayout ()); 258 259 gridBagConstraints = new java.awt.GridBagConstraints (); 260 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 261 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 262 gridBagConstraints.weightx = 1.0; 263 gridBagConstraints.weighty = 1.0; 264 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 265 add(interfacesPanel, gridBagConstraints); 266 267 } 268 270 private void superClassComboActionPerformed (java.awt.event.ActionEvent evt) { if (!superClassCombo.isEnabled()) return; 272 273 JavaClass oldValue = element.getSuperClass(); 274 String oldValueStr = (oldValue == null) ? "" : oldValue.getName(); Object item = superClassCombo.getSelectedItem(); 276 277 if (item == null) { 278 superClassCombo.setSelectedItem(oldValueStr); 280 return; 281 } 282 String newValueStr = item.toString().trim(); 283 boolean ok = false; 284 if (!oldValueStr.equals(newValueStr)) { 285 try { 286 MultipartId mid = getJModel().getMultipartId().createMultipartId(newValueStr, null, null); 287 element.setSuperClassName(mid); 288 ok = true; 289 } catch (JmiException e) { 290 ErrorManager.getDefault().notify(e); 291 } 292 } else { 293 return; } 295 isOK = ok; 296 if (!ok) 297 superClassCombo.setSelectedItem(oldValueStr); 298 } 300 private void nameTextFieldFocusLost (java.awt.event.FocusEvent evt) { if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) 302 return; 303 304 String newName = nameTextField.getText().trim(); 305 String oldName = element.getSimpleName(); 306 boolean ok = false; 307 308 if (!Utilities.isJavaIdentifier(newName)) { 309 IllegalArgumentException x = new IllegalArgumentException ("Invalid name: " + newName); ErrorManager.getDefault().annotate( 311 x, ErrorManager.USER, null, 312 NbBundle.getMessage(ClassCustomizer.class, "MSG_Not_Valid_Identifier"), null, null); 314 ErrorManager.getDefault().notify (x); 315 } else if (oldName.equals(newName)) { 316 return; } else if (this.resource == null && jclass.getInnerClass(newName, true) != null) { 318 IllegalArgumentException x = new IllegalArgumentException ("Invalid name: " + newName); ErrorManager.getDefault().annotate( 320 x, ErrorManager.USER, null, 321 NbBundle.getMessage(ClassCustomizer.class, "MSG_Used_Identifier"), null, null); 323 ErrorManager.getDefault().notify (x); 324 } else if (this.jclass == null && SourceEditSupport.findTopLevelClass(this.resource, newName) != null) { 325 IllegalArgumentException x = new IllegalArgumentException ("Invalid name: " + newName); ErrorManager.getDefault().annotate( 327 x, ErrorManager.USER, null, 328 NbBundle.getMessage(ClassCustomizer.class, "MSG_Used_Identifier"), null, null); 330 ErrorManager.getDefault().notify (x); 331 } else { 332 try { 333 element.setSimpleName(newName); 334 ok = true; 335 } catch (JmiException e) { 336 ErrorManager.getDefault().notify(e); 337 } 338 } 339 isOK = ok; 340 if (!ok) { 341 nameTextField.setText(oldName); 342 } 343 } 345 346 private javax.swing.JPanel accessPanel; 348 private javax.swing.JPanel interfacesPanel; 349 private javax.swing.JLabel jLabel1; 350 private javax.swing.JLabel jLabel2; 351 private javax.swing.JLabel jLabel3; 352 private javax.swing.JLabel jLabel4; 353 private javax.swing.JLabel jLabel5; 354 private javax.swing.JPanel modifierPanel; 355 private javax.swing.JTextField nameTextField; 356 private javax.swing.JComboBox superClassCombo; 357 private javax.swing.JTextField typeTextField; 358 360 361 private void initAccessibility() { 362 nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ClassCustomizer.class, "ACS_ClassNameTextField")); nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ClassCustomizer.class, "ACS_ClassNameTextField")); this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ClassCustomizer.class, "ACSD_ClassCustomizerDialog")); } 366 367 public boolean isOK() { 368 superClassComboActionPerformed(null); 369 if (isOK) { 370 nameTextFieldFocusLost(null); 371 } 372 return isOK; 373 } 374 375 private JavaModelPackage getJModel() { 376 return jclass != null? 377 JavaMetamodel.getManager().getJavaExtent(jclass): 378 JavaMetamodel.getManager().getJavaExtent(resource); 379 } 380 } 381 | Popular Tags |