1 19 20 package org.openide.explorer.propertysheet.editors; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.lang.reflect.Modifier ; 25 import java.util.Locale ; 26 import java.util.StringTokenizer ; 27 28 import javax.swing.JPanel ; 29 30 import org.openide.src.ElementProperties; 31 import org.openide.util.HelpCtx; 32 import org.openide.explorer.propertysheet.ExPropertyEditor; 33 import org.openide.explorer.propertysheet.PropertyEnv; 34 import org.openide.ErrorManager; 35 import org.openide.util.NbBundle; 36 37 41 public class ModifierEditor extends JPanel implements ExPropertyEditor { 42 43 44 private ModifierPanel panel; 45 46 47 static final long serialVersionUID = 6324048239020120791L; 48 49 50 private int mask; 51 52 53 private int modifier; 54 55 private PropertyEnv env; 56 57 59 public ModifierEditor() { 60 this(ModifierPanel.EDITABLE_MASK); 61 } 62 63 66 public ModifierEditor(int mask) { 67 modifier = 0; 68 setMask(mask & ModifierPanel.EDITABLE_MASK); 69 70 getAccessibleContext().setAccessibleDescription(getString("ACSD_ModifierPanel")); 71 } 72 73 public void addNotify() { 74 setLayout(new BorderLayout()); 75 panel = new ModifierPanel(this); 76 panel.setMnemonics(env); 77 add(panel, BorderLayout.CENTER); 78 79 super.addNotify(); 80 } 81 82 public void removeNotify() { 83 super.removeNotify(); 84 if (panel != null) { 85 remove(panel); 86 panel = null; 87 } 88 } 89 90 93 int getMask() { 94 return mask; 95 } 96 97 100 public void setMask(int mask) { 101 if (this.mask != mask) { 102 int oldMask = this.mask; 103 this.mask = mask & ModifierPanel.EDITABLE_MASK; 104 firePropertyChange (ModifierPanel.PROP_MASK, new Integer (oldMask), new Integer (mask)); 105 setModifier(modifier & mask); 106 } 107 } 108 109 112 int getModifier() { 113 return modifier; 114 } 115 116 119 void setModifier(int modifier) { 120 if (this.modifier != modifier) { 121 int oldModifier = this.modifier; 122 this.modifier = modifier; 123 firePropertyChange (ModifierPanel.PROP_MODIFIER, new Integer (oldModifier), new Integer (modifier)); 125 firePropertyChange(ElementProperties.PROP_MODIFIERS, new Integer (oldModifier), new Integer (modifier)); 127 } 128 } 129 130 131 public void setValue(Object object) throws IllegalArgumentException { 132 if (object == null) { 133 setModifier(0); 134 return; 135 } 136 if (object instanceof Integer ) { 137 setModifier(((Integer ) object).intValue()); 138 } 139 else { 140 throw new IllegalArgumentException (); 141 } 142 } 143 144 147 public String getJavaInitializationString() { 148 return new Integer (getModifier()).toString(); 149 } 150 151 152 public Object getValue() { 153 return new Integer (getModifier()); 154 } 155 156 157 public boolean isPaintable() { 158 return false; 159 } 160 161 162 public void paintValue(Graphics g, Rectangle rectangle) { 163 } 164 165 166 public String getAsText() { 167 return Modifier.toString(getModifier()); 168 } 169 170 171 public void setAsText(String string) throws IllegalArgumentException { 172 int newValue = 0; 173 int oldValue = modifier; 174 175 StringTokenizer tukac = new StringTokenizer (string, ", ", false); while (tukac.hasMoreTokens()) { 177 String token = tukac.nextToken(); 178 boolean known = false; 179 for (int i = 0; i < ModifierPanel.MODIFIER_COUNT; i++) { 180 if ((ModifierPanel.MODIFIER_VALUES[i] & mask) != 0) { 181 if (token.equals(ModifierPanel.MODIFIER_NAMES[i])) { 182 if (((ModifierPanel.MODIFIER_VALUES[i] == Modifier.FINAL) && ((newValue & Modifier.ABSTRACT) != 0)) || 183 ((ModifierPanel.MODIFIER_VALUES[i] == Modifier.ABSTRACT) && ((newValue & Modifier.FINAL) != 0))) 184 break; 185 newValue |= ModifierPanel.MODIFIER_VALUES[i]; 186 known = true; 187 break; 188 } 189 } 190 } 191 if ((newValue & ModifierPanel.ACCESS_MASK) == 0) { 192 for (int i = 1; i <= 3; i++) { 193 if ((ModifierPanel.ACCESS_VALUES[i] & mask) != 0) { 194 if (token.equals(ModifierPanel.ACCESS_NAMES[i])) { 195 newValue |= ModifierPanel.ACCESS_VALUES[i]; 196 known = true; 197 break; 198 } 199 } 200 } 201 } 202 if (!known) { 203 IllegalArgumentException x = new IllegalArgumentException ( 204 "Invalid modifier: " + token); String message = java.text.MessageFormat.format( 206 getString("MSG_IllegalModifierString"), new Object [] { token }); 208 ErrorManager.getDefault().annotate(x, 209 ErrorManager.USER, null, message, null, null); 210 throw x; 211 } 212 } 213 if (oldValue != newValue) { 214 modifier = newValue; 215 firePropertyChange(ModifierPanel.PROP_MODIFIER, 216 new Integer (oldValue), new Integer (modifier)); 217 } 218 } 219 220 221 public String [] getTags() { 222 return null; 223 } 224 225 226 public Component getCustomEditor() { 227 return this; 228 } 229 230 231 public boolean supportsCustomEditor() { 232 return true; 233 } 234 235 240 public Object getPropertyValue() throws IllegalStateException { 241 return getValue(); 242 } 243 244 248 public void attachEnv(PropertyEnv env) { 249 this.env = env; 250 } 251 252 private static String getString(String key) { 253 return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), ModifierEditor.class.getClassLoader()).getString(key); 254 } 255 } 256 | Popular Tags |