1 19 20 package org.netbeans.modules.java.ui.nodes.editors; 21 22 import java.awt.*; 23 import java.lang.reflect.Modifier ; 24 import java.util.StringTokenizer ; 25 26 import javax.swing.*; 27 28 import org.openide.src.ElementProperties; 29 import org.openide.explorer.propertysheet.ExPropertyEditor; 30 import org.openide.explorer.propertysheet.PropertyEnv; 31 import org.openide.ErrorManager; 32 import org.openide.util.NbBundle; 33 34 38 public final class ModifierEditor extends JPanel implements ExPropertyEditor { 39 40 45 public static final String CUSTOM_EDITOR_TYPE = "customEditorType"; 49 public static final Integer ACCESS_MODIFIERS_CUSTOM_EDITOR = new Integer (0); 50 53 public static final Integer OTHERS_MODIFIERS_CUSTOM_EDITOR = new Integer (1); 54 57 public static final Integer FULL_CUSTOM_EDITOR = new Integer (2); 58 59 60 private ModifierPanel panel; 61 62 63 private int mask; 64 65 66 private int modifier; 67 68 private PropertyEnv env; 69 70 73 private Object type; 74 75 77 public ModifierEditor() { 78 this(ModifierPanel.EDITABLE_MASK); 79 } 80 81 84 public ModifierEditor(int mask) { 85 modifier = 0; 86 setMask(mask & ModifierPanel.EDITABLE_MASK); 87 } 88 89 private Component customComponent; 90 91 public void addNotify() { 92 setLayout(new BorderLayout()); 93 panel = new ModifierPanel(this); 94 Object type = getType(); 95 if (ACCESS_MODIFIERS_CUSTOM_EDITOR == type) { 96 customComponent = panel.getAccessComponent(); 97 } else if (OTHERS_MODIFIERS_CUSTOM_EDITOR == type) { 98 customComponent = panel.getModifiersComponent(); 99 } else { 100 customComponent = panel.getCompactComponent(); 101 } 102 add(customComponent, BorderLayout.CENTER); 103 104 super.addNotify(); 105 } 106 107 public void removeNotify() { 108 super.removeNotify(); 109 if (panel != null) { 110 remove(customComponent); 111 panel = null; 112 } 113 } 114 115 118 int getMask() { 119 return mask; 120 } 121 122 125 public void setMask(int mask) { 126 if (this.mask != mask) { 127 int oldMask = this.mask; 128 this.mask = mask & ModifierPanel.EDITABLE_MASK; 129 firePropertyChange (ModifierPanel.PROP_MASK, new Integer (oldMask), new Integer (mask)); 130 setModifier(modifier & mask); 131 } 132 } 133 134 137 int getModifier() { 138 return modifier; 139 } 140 141 144 void setModifier(int modifier) { 145 if (this.modifier != modifier) { 146 int oldModifier = this.modifier; 147 this.modifier = modifier; 148 firePropertyChange (ModifierPanel.PROP_MODIFIER, new Integer (oldModifier), new Integer (modifier)); 150 firePropertyChange(ElementProperties.PROP_MODIFIERS, new Integer (oldModifier), new Integer (modifier)); 152 } 153 } 154 155 161 Object getType() { 162 return type; 163 } 164 165 166 public void setValue(Object object) throws IllegalArgumentException { 167 if (object == null) { 168 setModifier(0); 169 return; 170 } 171 if (object instanceof Integer ) { 172 setModifier(((Integer ) object).intValue()); 173 } 174 else { 175 throw new IllegalArgumentException (); 176 } 177 } 178 179 182 public String getJavaInitializationString() { 183 return new Integer (getModifier()).toString(); 184 } 185 186 187 public Object getValue() { 188 return new Integer (getModifier()); 189 } 190 191 192 public boolean isPaintable() { 193 return false; 194 } 195 196 197 public void paintValue(Graphics g, Rectangle rectangle) { 198 } 199 200 201 public String getAsText() { 202 return Modifier.toString(getModifier()); 203 } 204 205 206 public void setAsText(String string) throws IllegalArgumentException { 207 int newValue = 0; 208 int oldValue = modifier; 209 210 StringTokenizer tukac = new StringTokenizer (string, ", ", false); while (tukac.hasMoreTokens()) { 212 String token = tukac.nextToken(); 213 boolean known = false; 214 for (int i = 0; i < ModifierPanel.MODIFIER_COUNT; i++) { 215 if ((ModifierPanel.MODIFIER_VALUES[i] & mask) != 0) { 216 if (token.equals(ModifierPanel.MODIFIER_NAMES[i])) { 217 if (((ModifierPanel.MODIFIER_VALUES[i] == Modifier.FINAL) && ((newValue & Modifier.ABSTRACT) != 0)) || 218 ((ModifierPanel.MODIFIER_VALUES[i] == Modifier.ABSTRACT) && ((newValue & Modifier.FINAL) != 0))) 219 break; 220 newValue |= ModifierPanel.MODIFIER_VALUES[i]; 221 known = true; 222 break; 223 } 224 } 225 } 226 if ((newValue & ModifierPanel.ACCESS_MASK) == 0) { 227 for (int i = 1; i <= 3; i++) { 228 if ((ModifierPanel.ACCESS_VALUES[i] & mask) != 0) { 229 if (token.equals(ModifierPanel.ACCESS_NAMES[i])) { 230 newValue |= ModifierPanel.ACCESS_VALUES[i]; 231 known = true; 232 break; 233 } 234 } 235 } 236 } 237 if (!known) { 238 IllegalArgumentException x = new IllegalArgumentException ( 239 "Invalid modifier: " + token); String message = java.text.MessageFormat.format( 241 getString("MSG_IllegalModifierString"), new Object [] { token }); 243 ErrorManager.getDefault().annotate(x, 244 ErrorManager.USER, null, message, null, null); 245 throw x; 246 } 247 } 248 if (oldValue != newValue) { 249 modifier = newValue; 250 firePropertyChange(ModifierPanel.PROP_MODIFIER, 251 new Integer (oldValue), new Integer (modifier)); 252 } 253 } 254 255 256 public String [] getTags() { 257 return null; 258 } 259 260 261 public Component getCustomEditor() { 262 return this; 263 } 264 265 266 public boolean supportsCustomEditor() { 267 return true; 268 } 269 270 275 public Object getPropertyValue() throws IllegalStateException { 276 return getValue(); 277 } 278 279 283 public void attachEnv(PropertyEnv env) { 284 this.env = env; 285 type = env.getFeatureDescriptor().getValue(CUSTOM_EDITOR_TYPE); 286 if (type == null) { 287 type = FULL_CUSTOM_EDITOR; 288 } else if (ACCESS_MODIFIERS_CUSTOM_EDITOR.equals(type)) { 289 type = ACCESS_MODIFIERS_CUSTOM_EDITOR; 290 } else if (OTHERS_MODIFIERS_CUSTOM_EDITOR.equals(type)) { 291 type = OTHERS_MODIFIERS_CUSTOM_EDITOR; 292 } else { 293 type = FULL_CUSTOM_EDITOR; 294 } 295 296 } 297 298 private static String getString(String key) { 299 return NbBundle.getMessage(ModifierEditor.class, key); 300 } 301 } 302 | Popular Tags |