1 19 20 package org.openide.explorer.propertysheet.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 import java.util.Locale ; 28 29 import javax.swing.*; 30 31 import org.openide.util.NbBundle; 32 import org.openide.util.WeakListeners; 33 import org.openide.explorer.propertysheet.PropertyEnv; 34 35 42 class ModifierPanel extends javax.swing.JPanel { 43 44 46 47 public static final String PROP_MASK = "mask"; 49 50 public static final String PROP_MODIFIER = "modifier"; 52 private static final int CHECK_ABSTRACT = 0; 53 private static final int CHECK_FINAL = 1; 54 private static final int CHECK_STATIC = 2; 55 private static final int CHECK_SYNCHRONIZED = 3; 56 private static final int CHECK_TRANSIENT = 4; 57 private static final int CHECK_VOLATILE = 5; 58 private static final int CHECK_NATIVE = 6; 59 60 61 static final String MODIFIER_NAMES[] = { 62 "abstract", "final", "static", "synchronized", "transient", "volatile", "native" }; 64 65 private static final String [] MODIFIER_DESCRIPTION_KEYS = { 66 "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" }; 74 75 private static final String [] MODIFIER_MNEMONICS_KEYS = { 76 "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" }; 84 85 86 static final int MODIFIER_VALUES[] = { 87 Modifier.ABSTRACT, Modifier.FINAL, Modifier.STATIC, Modifier.SYNCHRONIZED, 88 Modifier.TRANSIENT, Modifier.VOLATILE, Modifier.NATIVE 89 }; 90 91 92 static final int MODIFIER_COUNT = MODIFIER_VALUES.length; 93 94 95 static final String ACCESS_NAMES[] = { 96 "<default>", "private", "protected", "public" }; 98 99 100 static final int ACCESS_VALUES[] = { 101 0, Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC 102 }; 103 104 105 static final int ACCESS_MASK = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC; 106 107 108 static final int EDITABLE_MASK = ACCESS_MASK | Modifier.ABSTRACT | 109 Modifier.FINAL | Modifier.STATIC | Modifier.SYNCHRONIZED | 110 Modifier.TRANSIENT | Modifier.VOLATILE | Modifier.NATIVE; 111 112 114 115 private ModifierEditor myEditor; 116 117 118 private int currentAccessValues[]; 119 120 121 private String currentAccessNames[]; 122 123 124 private JCheckBox[] checks; 125 126 127 private ActionListener listener; 128 129 130 private boolean ignored = false; 131 132 133 static final long serialVersionUID =6884758007403225916L; 134 135 public ModifierPanel(ModifierEditor ed) { 136 myEditor = ed; 137 currentAccessValues = ACCESS_VALUES; 138 currentAccessNames = ACCESS_NAMES; 139 140 myEditor.addPropertyChangeListener( 141 WeakListeners.propertyChange( 142 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 myEditor 154 ) 155 ); 156 157 listener = new ActionListener() { 158 public void actionPerformed(ActionEvent evt) { 159 int selIndex = accessCombo.getSelectedIndex(); 161 if (evt.getSource() == accessCombo && selIndex < 0) { 163 return; 165 } 166 if (checks[CHECK_ABSTRACT].isSelected() && 167 ((myEditor.getModifier() & MODIFIER_VALUES[CHECK_ABSTRACT]) == 0) && 168 ((myEditor.getModifier() & Modifier.PRIVATE) > 0)) { 169 checks[CHECK_ABSTRACT].setSelected(false); 170 } 171 if (selIndex >= 0 && 172 (currentAccessValues[accessCombo.getSelectedIndex()] & Modifier.PRIVATE) > 0 && 173 (myEditor.getModifier() & Modifier.PRIVATE) == 0) 174 checks[CHECK_ABSTRACT].setSelected(false); 175 176 excludeChecks(CHECK_ABSTRACT, CHECK_FINAL); 177 excludeChecks(CHECK_ABSTRACT, CHECK_NATIVE); 178 excludeChecks(CHECK_ABSTRACT, CHECK_STATIC); 179 excludeChecks(CHECK_ABSTRACT, CHECK_SYNCHRONIZED); 180 excludeChecks(CHECK_VOLATILE, CHECK_FINAL); 181 182 if (!ignored) 183 updateValue(); 184 } 185 }; 186 187 ignored = true; 188 initComponents(); 189 190 modifPanel.setBorder (new javax.swing.border.CompoundBorder ( 191 new javax.swing.border.TitledBorder (getString("LAB_Modifiers")), 192 new javax.swing.border.EmptyBorder (new java.awt.Insets (3, 3, 3, 3)) 193 )); 194 195 updateAccess(); 196 updateModifiers(); 197 updateComponents(); 198 ignored = false; 199 200 jLabel1.setDisplayedMnemonic(getString("LAB_AccessRights_Mnemonic").charAt(0)); 201 202 jLabel1.getAccessibleContext().setAccessibleDescription(getString("ACSD_AccessRights")); 203 } 204 205 208 private void excludeChecks(int check1, int check2) { 209 if (checks[check1].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check1]) == 0)) 210 checks[check2].setSelected(false); 211 else if (checks[check2].isSelected() && ((myEditor.getModifier() & MODIFIER_VALUES[check2]) == 0)) 212 checks[check1].setSelected(false); 213 } 214 215 220 private void initComponents() { modifPanel = new javax.swing.JPanel (); 222 jPanel2 = new javax.swing.JPanel (); 223 jLabel1 = new javax.swing.JLabel (); 224 accessCombo = new javax.swing.JComboBox (); 225 226 setLayout(new java.awt.BorderLayout ()); 227 228 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 7, 6, 7))); 229 modifPanel.setLayout(new java.awt.GridLayout (4, 2, 4, 4)); 230 231 checks = new JCheckBox[MODIFIER_COUNT]; 232 for (int i = 0; i < MODIFIER_COUNT; i++) { 233 checks[i] = new JCheckBox(MODIFIER_NAMES[i]); 234 checks[i].getAccessibleContext().setAccessibleDescription(getModifierDescription(i)); 235 modifPanel.add(checks[i]); 236 checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0); 237 checks[i].addActionListener(listener); 238 } 239 240 add(modifPanel, java.awt.BorderLayout.CENTER); 241 242 jPanel2.setLayout(new java.awt.BorderLayout (8, 8)); 243 244 jPanel2.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5))); 245 jLabel1.setText(getString("LAB_AccessRights")); 246 jLabel1.setLabelFor(accessCombo); 247 jPanel2.add(jLabel1, java.awt.BorderLayout.WEST); 248 249 accessCombo.addActionListener(listener); 250 jPanel2.add(accessCombo, java.awt.BorderLayout.CENTER); 251 252 add(jPanel2, java.awt.BorderLayout.NORTH); 253 254 } 256 257 private javax.swing.JComboBox accessCombo; 259 private javax.swing.JLabel jLabel1; 260 private javax.swing.JPanel jPanel2; 261 private javax.swing.JPanel modifPanel; 262 264 266 private void updateAccess() { 267 int selValue = myEditor.getModifier() & ACCESS_MASK; 268 int selIndex = -1; 269 270 int counter = 1; 271 for (int i = 1; i < ACCESS_VALUES.length; i++) { 272 if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0) 273 counter++; 274 } 275 currentAccessValues = new int[counter]; 276 currentAccessNames = new String [counter]; 277 278 currentAccessValues[0] = ACCESS_VALUES[0]; 279 currentAccessNames[0] = ACCESS_NAMES[0]; 280 counter = 1; 281 282 for (int i = 1; i < ACCESS_VALUES.length; i++) { 283 if ((ACCESS_VALUES[i] & myEditor.getMask()) != 0) { 284 currentAccessValues[counter] = ACCESS_VALUES[i]; 285 currentAccessNames[counter] = ACCESS_NAMES[i]; 286 if (ACCESS_VALUES[i] == selValue) { 287 selIndex = counter; 288 } 289 counter++; 290 } 291 } 292 if (selIndex == -1 && selValue == 0) 293 selIndex = 0; 294 295 ignored = true; 296 accessCombo.setModel(new DefaultComboBoxModel(currentAccessNames)); 297 accessCombo.setSelectedIndex(selIndex); 298 ignored = false; 299 } 300 301 303 private void updateModifiers() { 304 for (int i = 0; i < MODIFIER_COUNT; i++) { 305 checks[i].setEnabled((myEditor.getMask() & MODIFIER_VALUES[i]) != 0); 306 } 307 } 308 309 312 private void updateComponents() { 313 updateAccessCombo(); 314 updateModifiers(); 315 for (int i = 0; i < MODIFIER_COUNT; i++) { 316 checks[i].setSelected((myEditor.getModifier() & MODIFIER_VALUES[i]) != 0); 317 } 318 } 319 320 private void updateAccessCombo() { 321 int selIndex = -1; 322 if (myEditor.getModifier() == 0) { 323 selIndex = 0; 324 } else { 325 for (int i = 1; i < currentAccessValues.length; i++) { 326 if ((currentAccessValues[i] & myEditor.getModifier()) != 0) { 327 if (selIndex != -1) { 328 selIndex = -1; 329 break; 330 } 331 selIndex = i; 332 } 333 } 334 } 335 if (accessCombo.getSelectedIndex() != selIndex) { 336 accessCombo.setSelectedIndex(selIndex); 337 } 338 } 339 340 341 private void updateValue() { 342 int newValue = 0; 343 int comboIndex = accessCombo.getSelectedIndex(); 344 345 if (comboIndex == -1) { 346 newValue = myEditor.getModifier() & ACCESS_MASK; 347 } else { 348 newValue |= currentAccessValues[comboIndex]; 349 } 350 351 for (int i = 0; i < MODIFIER_COUNT; i++) { 352 if (checks[i].isSelected() & checks[i].isEnabled()) 353 newValue |= MODIFIER_VALUES[i]; 354 } 355 if (myEditor.getModifier() != newValue) { 356 myEditor.setModifier(newValue); 357 } 358 } 359 360 void setMnemonics(PropertyEnv env) { 361 FeatureDescriptor desc = env.getFeatureDescriptor(); 362 363 for (int i = 0; i < MODIFIER_COUNT; i++) { 364 checks[i].setMnemonic(getModifierMnemonics(i)); 365 366 Object o = desc.getValue(MODIFIER_MNEMONICS_KEYS[i]); 367 if (o instanceof String ) { 368 checks[i].setMnemonic(((String )o).charAt(0)); 369 } else { 370 checks[i].setMnemonic(getModifierMnemonics(i)); 371 } 372 } 373 } 374 375 private static String getString(String key) { 376 return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), ModifierPanel.class.getClassLoader()).getString(key); 377 } 378 379 static String getModifierDescription(int i) { 380 return getString(MODIFIER_DESCRIPTION_KEYS[i]); 381 } 382 383 static char getModifierMnemonics(int i) { 384 return getString(MODIFIER_MNEMONICS_KEYS[i]).charAt(0); 385 } 386 } 387 | Popular Tags |