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