1 19 20 package org.netbeans.modules.form.editors; 21 22 import java.awt.event.*; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.FeatureDescriptor ; 26 import java.lang.reflect.Modifier ; 27 28 import javax.swing.*; 29 30 import org.openide.util.NbBundle; 31 import org.openide.util.WeakListeners; 32 33 40 final class ModifierPanel { 41 42 44 45 public static final String PROP_MASK = "mask"; 47 48 public static final String PROP_MODIFIER = "modifier"; 50 private static final int CHECK_ABSTRACT = 0; 51 private static final int CHECK_FINAL = 1; 52 private static final int CHECK_STATIC = 2; 53 private static final int CHECK_SYNCHRONIZED = 3; 54 private static final int CHECK_TRANSIENT = 4; 55 private static final int CHECK_VOLATILE = 5; 56 private static final int CHECK_NATIVE = 6; 57 58 59 static final String MODIFIER_NAMES[] = { 60 "abstract", "final", "static", "synchronized", "transient", "volatile", "native" }; 62 63 private static final String [] MODIFIER_DESCRIPTION_KEYS = { 64 "ACSD_ModifierPanel_Modifier_Abstract", "ACSD_ModifierPanel_Modifier_Final", "ACSD_ModifierPanel_Modifier_Static", "ACSD_ModifierPanel_Modifier_Synchronized", "ACSD_ModifierPanel_Modifier_Transient", "ACSD_ModifierPanel_Modifier_Volatile", "ACSD_ModifierPanel_Modifier_Native" }; 72 73 private static final String [] MODIFIER_MNEMONICS_KEYS = { 74 "ModifierPanel_Modifier_Abstract_Mnemonic", "ModifierPanel_Modifier_Final_Mnemonic", "ModifierPanel_Modifier_Static_Mnemonic", "ModifierPanel_Modifier_Synchronized_Mnemonic", "ModifierPanel_Modifier_Transient_Mnemonic", "ModifierPanel_Modifier_Volatile_Mnemonic", "ModifierPanel_Modifier_Native_Mnemonic" }; 82 83 84 static final int MODIFIER_VALUES[] = { 85 Modifier.ABSTRACT, Modifier.FINAL, Modifier.STATIC, Modifier.SYNCHRONIZED, 86 Modifier.TRANSIENT, Modifier.VOLATILE, Modifier.NATIVE 87 }; 88 89 90 static final int MODIFIER_COUNT = MODIFIER_VALUES.length; 91 92 93 static final String ACCESS_NAMES[] = { 94 "<default>", "private", "protected", "public" }; 96 97 98 static final int ACCESS_VALUES[] = { 99 0, Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC 100 }; 101 102 103 static final int ACCESS_MASK = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC; 104 105 106 private static final int OTHERS_MASK = Modifier.ABSTRACT | 107 Modifier.FINAL | Modifier.STATIC | Modifier.SYNCHRONIZED | 108 Modifier.TRANSIENT | Modifier.VOLATILE | Modifier.NATIVE | Modifier.STRICT; 109 110 111 static final int EDITABLE_MASK = ACCESS_MASK | OTHERS_MASK; 112 113 115 116 private ModifierEditor myEditor; 117 118 119 private int currentAccessValues[]; 120 121 122 private String currentAccessNames[]; 123 124 125 private JCheckBox[] checks; 126 127 128 private ActionListener listener; 129 130 131 private boolean ignored = false; 132 133 134 private PropertyChangeListener editorListener; 135 136 137 public ModifierPanel(ModifierEditor ed) { 138 myEditor = ed; 139 currentAccessValues = ACCESS_VALUES; 140 currentAccessNames = ACCESS_NAMES; 141 142 editorListener = new PropertyChangeListener () { 143 public void propertyChange(PropertyChangeEvent evt) { 144 if (PROP_MODIFIER.equals(evt.getPropertyName()) || 145 PROP_MASK.equals(evt.getPropertyName())) { 146 updateAccess(); 147 ignored = true; 148 updateComponents(); 149 ignored = false; 150 } 151 } 152 }; 153 154 myEditor.addPropertyChangeListener(WeakListeners.propertyChange(editorListener, myEditor)); 155 156 listener = new ActionListener() { 157 158 public void actionPerformed(ActionEvent evt) { 159 161 int selIndex = accessCombo.getSelectedIndex(); 162 if (evt.getSource() == accessCombo && selIndex < 0) { 164 return; 166 } 167 if (checks[CHECK_ABSTRACT].isSelected() && 168 ((myEditor.getModifier() & MODIFIER_VALUES[CHECK_ABSTRACT]) == 0) && 169 ((myEditor.getModifier() & Modifier.PRIVATE) > 0)) { 170 checks[CHECK_ABSTRACT].setSelected(false); 171 } 172 if (selIndex >= 0 && 173 (currentAccessValues[accessCombo.getSelectedIndex()] & Modifier.PRIVATE) > 0 && 174 (myEditor.getModifier() & Modifier.PRIVATE) == 0) { 175 checks[CHECK_ABSTRACT].setSelected(false); 176 } 177 178 excludeChecks(CHECK_ABSTRACT, CHECK_FINAL); 179 excludeChecks(CHECK_ABSTRACT, CHECK_NATIVE); 180 excludeChecks(CHECK_ABSTRACT, CHECK_STATIC); 181 excludeChecks(CHECK_ABSTRACT, CHECK_SYNCHRONIZED); 182 excludeChecks(CHECK_VOLATILE, CHECK_FINAL); 183 184 if (!ignored) 185 updateValue(); 186 187 } 188 }; 189 190 ignored = true; 191 initBasicComponents(); 192 193 updateAccess(); 194 updateModifiers(); 195 updateComponents(); 196 ignored = false; 197 } 198 199 202 private void excludeChecks(int check1, int check2) { 203 if (checks[check1].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check1]) == 0)) 204 checks[check2].setSelected(false); 205 else if (checks[check2].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check2]) == 0)) 206 checks[check1].setSelected(false); 207 } 208 209 private void initBasicComponents() { 210 accessCombo = new JComboBox(); 211 accessCombo.addActionListener(listener); 212 accessCombo.getAccessibleContext().setAccessibleName("ACS_AccessRights"); accessCombo.getAccessibleContext().setAccessibleDescription("ACSD_AccessRights"); 215 modifPanel = new JPanel(); 216 modifPanel.setLayout(new java.awt.GridLayout (4, 2, 4, 4)); 217 modifPanel.getAccessibleContext().setAccessibleName("ACSN_OtherModifiers"); modifPanel.getAccessibleContext().setAccessibleDescription("ACSD_OtherModifiers"); 220 checks = new JCheckBox[MODIFIER_COUNT]; 221 for (int i = 0; i < MODIFIER_COUNT; i++) { 222 checks[i] = new JCheckBox(MODIFIER_NAMES[i]); 223 checks[i].setMnemonic(getModifierMnemonics(i)); 224 checks[i].getAccessibleContext().setAccessibleDescription(getModifierDescription(i)); 225 modifPanel.add(checks[i]); 226 checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0); 227 checks[i].addActionListener(listener); 228 } 229 } 230 231 private void initComponents() { 232 jLabel1 = new JLabel(); 233 jLabel1.setText(getString("LAB_AccessRights")); jLabel1.setLabelFor(accessCombo); 235 jLabel1.setDisplayedMnemonic(getString("LAB_AccessRights_Mnemonic").charAt(0)); 237 jPanel2 = new JPanel(); 238 jPanel2.setLayout(new java.awt.BorderLayout (8, 8)); 239 jPanel2.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5))); 240 jPanel2.add(jLabel1, java.awt.BorderLayout.WEST); 241 jPanel2.add(accessCombo, java.awt.BorderLayout.CENTER); 242 modifPanel.setBorder (new javax.swing.border.CompoundBorder ( 243 new javax.swing.border.TitledBorder (getString("LAB_Modifiers")), new javax.swing.border.EmptyBorder (new java.awt.Insets (3, 3, 3, 3)) 245 )); 246 247 commpactP = new JPanel(); 248 commpactP.setLayout(new java.awt.BorderLayout ()); 249 commpactP.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 7, 6, 7))); 250 commpactP.add(modifPanel, java.awt.BorderLayout.CENTER); 251 commpactP.add(jPanel2, java.awt.BorderLayout.NORTH); 252 commpactP.getAccessibleContext().setAccessibleName("ACSN_ModifierPanel"); commpactP.getAccessibleContext().setAccessibleDescription(getString("ACSD_ModifierPanel")); } 255 256 257 private JComboBox accessCombo; 258 private JLabel jLabel1; 259 private JPanel jPanel2; 260 private JPanel modifPanel; 261 private JPanel commpactP; 262 private boolean isCompact = false; 263 private boolean isPartial = false; 264 265 public JComboBox getAccessComponent() { 266 if (isCompact) 267 throw new IllegalStateException ("cannot use both getAccessComponent/getCompactComponent"); isPartial = true; 269 return accessCombo; 270 } 271 272 public JComponent getModifiersComponent() { 273 if (isCompact) 274 throw new IllegalStateException ("cannot use both getModifiersComponent/getCompactComponent"); isPartial = true; 276 return modifPanel; 277 } 278 279 public JPanel getCompactComponent() { 280 if (isPartial) 281 throw new IllegalStateException ( 282 "cannot use getAccessComponent/getModifiersComponent/getCompactComponent together"); isCompact = true; 284 if (commpactP == null) { 285 initComponents(); 286 } 287 return commpactP; 288 } 289 290 292 private void updateAccess() { 293 int selValue = myEditor.getModifier() & ACCESS_MASK; 294 int selIndex = -1; 295 296 int counter = 1; 297 for (int i = 1; i < ACCESS_VALUES.length; i++) { 298 if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0) 299 counter++; 300 } 301 currentAccessValues = new int[counter]; 302 currentAccessNames = new String [counter]; 303 304 currentAccessValues[0] = ACCESS_VALUES[0]; 305 currentAccessNames[0] = ACCESS_NAMES[0]; 306 counter = 1; 307 308 for (int i = 1; i < ACCESS_VALUES.length; i++) { 309 if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0) { 310 currentAccessValues[counter] = ACCESS_VALUES[i]; 311 currentAccessNames[counter] = ACCESS_NAMES[i]; 312 if (ACCESS_VALUES[i] == selValue) { 313 selIndex = counter; 314 } 315 counter++; 316 } 317 } 318 if (selIndex == -1 && selValue == 0) 319 selIndex = 0; 320 321 ignored = true; 322 accessCombo.setModel(new DefaultComboBoxModel(currentAccessNames)); 323 accessCombo.setSelectedIndex(selIndex); 324 ignored = false; 325 } 326 327 329 private void updateModifiers() { 330 for (int i = 0; i < MODIFIER_COUNT; i++) { 331 checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0); 332 } 333 } 334 335 338 private void updateComponents() { 339 updateAccessCombo(); 340 updateModifiers(); 341 for (int i = 0; i < MODIFIER_COUNT; i++) { 342 checks[i].setSelected((myEditor.getModifier() & MODIFIER_VALUES[i]) != 0); 343 } 344 } 345 346 private void updateAccessCombo() { 347 int selIndex = -1; 348 if (myEditor.getModifier() == 0) { 349 selIndex = 0; 350 } else { 351 for (int i = 1; i < currentAccessValues.length; i++) { 352 if ((currentAccessValues[i] & myEditor.getModifier()) != 0) { 353 if (selIndex != -1) { 354 selIndex = -1; 355 break; 356 } 357 selIndex = i; 358 } 359 } 360 } 361 if (accessCombo.getSelectedIndex() != selIndex) { 362 accessCombo.setSelectedIndex(selIndex); 363 } 364 } 365 366 367 private void updateValue() { 368 int newValue = 0; 369 int comboIndex = accessCombo.getSelectedIndex(); 370 Object type = myEditor.getType(); 371 int mask = 0; 372 if (ModifierEditor.FULL_CUSTOM_EDITOR == type || ModifierEditor.ACCESS_MODIFIERS_CUSTOM_EDITOR == type) { 373 mask |= ACCESS_MASK; 374 if (comboIndex == -1) { 375 newValue = myEditor.getModifier() & ACCESS_MASK; 376 } else { 377 newValue |= currentAccessValues[comboIndex]; 378 } 379 } 380 if (ModifierEditor.FULL_CUSTOM_EDITOR == type || ModifierEditor.OTHERS_MODIFIERS_CUSTOM_EDITOR == type) { 381 mask |= OTHERS_MASK; 382 for (int i = 0; i < MODIFIER_COUNT; i++) { 383 if (checks[i].isSelected() & checks[i].isEnabled()) 384 newValue |= MODIFIER_VALUES[i]; 385 } 386 } 387 388 int oldValue = myEditor.getModifier(); 389 if ((oldValue & mask) != newValue) { 390 if (ModifierEditor.ACCESS_MODIFIERS_CUSTOM_EDITOR == type) { 391 newValue |= (oldValue & ~ACCESS_MASK); 392 } else if (ModifierEditor.OTHERS_MODIFIERS_CUSTOM_EDITOR == type) { 393 newValue |= (oldValue & ~OTHERS_MASK); 394 } 395 myEditor.setModifier(newValue); 396 } 397 } 398 399 private static String getString(String key) { 400 return NbBundle.getMessage(ModifierPanel.class, key); 401 } 402 403 static String getModifierDescription(int i) { 404 return getString(MODIFIER_DESCRIPTION_KEYS[i]); 405 } 406 407 static char getModifierMnemonics(int i) { 408 return getString(MODIFIER_MNEMONICS_KEYS[i]).charAt(0); 409 } 410 } 411 | Popular Tags |