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.openide.util.HelpCtx; 34 import org.netbeans.jmi.javamodel.JavaClass; 35 import org.netbeans.jmi.javamodel.JavaEnum; 36 import org.netbeans.jmi.javamodel.Resource; 37 38 42 final class EnumCustomizer extends JPanel { 43 44 private final JavaEnum element; 45 48 private final JavaClass jclass; 49 50 private final Resource resource; 51 52 private boolean isOK = true; 53 54 58 public EnumCustomizer(JavaClass jclass, JavaEnum element) { 59 this(null, jclass, element); 60 } 61 62 66 public EnumCustomizer(Resource resource, JavaEnum element) { 67 this(resource, null, element); 68 } 69 70 private EnumCustomizer(Resource resource, JavaClass jclass, JavaEnum element) { 71 this.resource = resource; 72 this.jclass = jclass; 73 this.element = element; 74 initComponents (); 75 76 interfacesPanel.setBorder (new TitledBorder(NbBundle.getMessage(EnumCustomizer.class, "CTL_Interfaces"))); 79 int mask = Modifier.ABSTRACT; 81 if (resource == null) { mask |= Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC; 83 } 84 if (!element.isInterface()) { 85 mask |= Modifier.FINAL; 86 } 87 accessPanel.add(SourceEditSupport.createAccessModifiersPanel(element, mask), BorderLayout.CENTER); 88 modifierPanel.add(SourceEditSupport.createOtherModifiersPanel(element, mask), BorderLayout.CENTER); 89 90 nameTextField.setText(element.getSimpleName()); 92 93 interfacesPanel.add( 95 new PropertyPanel(ClassNode.createInterfacesProperty(element, true), PropertyPanel.PREF_CUSTOM_EDITOR), 96 BorderLayout.CENTER 97 ); 98 99 HelpCtx.setHelpIDString (this, "java.enum.customizer"); 101 jLabel1.setDisplayedMnemonic(NbBundle.getMessage(EnumCustomizer.class, "CTL_Name_Mnemonic").charAt(0)); 104 initAccessibility(); 105 } 106 107 public void addNotify() { 108 super.addNotify(); 109 110 int l = nameTextField.getText().length(); 112 nameTextField.setCaretPosition(0); 113 nameTextField.moveCaretPosition(l); 114 SwingUtilities.invokeLater(new Runnable () { 115 public void run() { 116 nameTextField.requestFocus(); 117 } 118 }); 119 } 120 121 126 private void initComponents() { 128 java.awt.GridBagConstraints gridBagConstraints; 129 130 jLabel1 = new javax.swing.JLabel (); 131 nameTextField = new javax.swing.JTextField (); 132 jLabel2 = new javax.swing.JLabel (); 133 accessPanel = new javax.swing.JPanel (); 134 jLabel3 = new javax.swing.JLabel (); 135 modifierPanel = new javax.swing.JPanel (); 136 interfacesPanel = new javax.swing.JPanel (); 137 138 setLayout(new java.awt.GridBagLayout ()); 139 140 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 6, 6, 6))); 141 jLabel1.setLabelFor(nameTextField); 142 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(EnumCustomizer.class, "CTL_Name")); 143 gridBagConstraints = new java.awt.GridBagConstraints (); 144 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 145 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 146 add(jLabel1, gridBagConstraints); 147 148 nameTextField.addFocusListener(new java.awt.event.FocusAdapter () { 149 public void focusLost(java.awt.event.FocusEvent evt) { 150 nameTextFieldFocusLost(evt); 151 } 152 }); 153 154 gridBagConstraints = new java.awt.GridBagConstraints (); 155 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 156 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 158 gridBagConstraints.weightx = 1.0; 159 gridBagConstraints.insets = new java.awt.Insets (12, 8, 0, 0); 160 add(nameTextField, gridBagConstraints); 161 162 jLabel2.setLabelFor(accessPanel); 163 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(EnumCustomizer.class, "CTL_AccessRights")); 164 gridBagConstraints = new java.awt.GridBagConstraints (); 165 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 166 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 167 add(jLabel2, gridBagConstraints); 168 169 accessPanel.setLayout(new java.awt.BorderLayout ()); 170 171 gridBagConstraints = new java.awt.GridBagConstraints (); 172 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 173 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 174 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 175 add(accessPanel, gridBagConstraints); 176 177 jLabel3.setLabelFor(modifierPanel); 178 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(EnumCustomizer.class, "CTL_Modifiers")); 179 gridBagConstraints = new java.awt.GridBagConstraints (); 180 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 181 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 182 gridBagConstraints.insets = new java.awt.Insets (8, 0, 0, 0); 183 add(jLabel3, gridBagConstraints); 184 185 modifierPanel.setLayout(new java.awt.BorderLayout ()); 186 187 gridBagConstraints = new java.awt.GridBagConstraints (); 188 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 189 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 190 gridBagConstraints.insets = new java.awt.Insets (8, 8, 0, 0); 191 add(modifierPanel, gridBagConstraints); 192 193 interfacesPanel.setLayout(new java.awt.BorderLayout ()); 194 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 197 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 198 gridBagConstraints.weightx = 1.0; 199 gridBagConstraints.weighty = 1.0; 200 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 201 add(interfacesPanel, gridBagConstraints); 202 203 } 204 206 private void nameTextFieldFocusLost (java.awt.event.FocusEvent evt) { if (evt != null && (evt.isTemporary() || !this.isAncestorOf(evt.getOppositeComponent()))) 208 return; 209 210 String newName = nameTextField.getText().trim(); 211 String oldName = element.getSimpleName(); 212 boolean ok = false; 213 214 if (!Utilities.isJavaIdentifier(newName)) { 215 IllegalArgumentException x = new IllegalArgumentException ("Invalid name: " + newName); ErrorManager.getDefault().annotate( 217 x, ErrorManager.USER, null, 218 NbBundle.getMessage(EnumCustomizer.class, "MSG_Not_Valid_Identifier"), null, null); 220 ErrorManager.getDefault().notify (x); 221 } else if (oldName.equals(newName)) { 222 return; } else if (this.resource == null && jclass.getInnerClass(newName, true) != null) { 224 IllegalArgumentException x = new IllegalArgumentException ("Invalid name: " + newName); ErrorManager.getDefault().annotate( 226 x, ErrorManager.USER, null, 227 NbBundle.getMessage(EnumCustomizer.class, "MSG_Not_Valid_Identifier"), null, null); 229 ErrorManager.getDefault().notify (x); 230 } else if (this.jclass == null && SourceEditSupport.findTopLevelClass(this.resource, newName) != null) { 231 IllegalArgumentException x = new IllegalArgumentException ("Invalid name: " + newName); ErrorManager.getDefault().annotate( 233 x, ErrorManager.USER, null, 234 NbBundle.getMessage(EnumCustomizer.class, "MSG_Not_Valid_Identifier"), null, null); 236 ErrorManager.getDefault().notify (x); 237 } else { 238 try { 239 element.setSimpleName(newName); 240 ok = true; 241 } catch (JmiException e) { 242 ErrorManager.getDefault().notify(e); 243 } 244 } 245 isOK = ok; 246 if (!ok) { 247 nameTextField.setText(oldName); 248 } 249 } 251 252 private javax.swing.JPanel accessPanel; 254 private javax.swing.JPanel interfacesPanel; 255 private javax.swing.JLabel jLabel1; 256 private javax.swing.JLabel jLabel2; 257 private javax.swing.JLabel jLabel3; 258 private javax.swing.JPanel modifierPanel; 259 private javax.swing.JTextField nameTextField; 260 262 263 private void initAccessibility() { 264 nameTextField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(EnumCustomizer.class, "ACS_EnumNameTextField")); nameTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(EnumCustomizer.class, "ACS_EnumNameTextField")); this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(EnumCustomizer.class, "ACSD_EnumCustomizerDialog")); } 268 269 public boolean isOK() { 270 nameTextFieldFocusLost(null); 271 return isOK; 272 } 273 } 274 | Popular Tags |