1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.beans.*; 23 import java.awt.Image ; 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 27 import org.openide.util.NbBundle; 28 import org.openide.util.SharedClassObject; 29 30 import org.netbeans.editor.BaseCaret; 31 32 36 public class BaseOptionsBeanInfo extends SimpleBeanInfo { 37 38 39 private String iconPrefix; 40 41 42 private String bundlePrefix; 43 44 45 private Image icon; 46 private Image icon32; 47 48 private HashMap names2PD; 49 50 51 PropertyDescriptor[] descriptors; 52 53 private static final String [] EXPERT_PROP_NAMES = new String [] { 54 BaseOptions.CARET_BLINK_RATE_PROP, 55 BaseOptions.CARET_TYPE_INSERT_MODE_PROP, 56 BaseOptions.CARET_TYPE_OVERWRITE_MODE_PROP, 57 BaseOptions.CARET_COLOR_INSERT_MODE_PROP, 58 BaseOptions.CARET_COLOR_OVERWRITE_MODE_PROP, 59 BaseOptions.HIGHLIGHT_CARET_ROW_PROP, 60 BaseOptions.HIGHLIGHT_MATCHING_BRACKET_PROP, 61 BaseOptions.LINE_HEIGHT_CORRECTION_PROP, 62 BaseOptions.MARGIN_PROP, 63 BaseOptions.SCROLL_JUMP_INSETS_PROP, 64 BaseOptions.SCROLL_FIND_INSETS_PROP, 65 BaseOptions.STATUS_BAR_CARET_DELAY_PROP, 66 BaseOptions.STATUS_BAR_VISIBLE_PROP, 67 BaseOptions.TEXT_LIMIT_LINE_COLOR_PROP, 68 BaseOptions.TEXT_LIMIT_LINE_VISIBLE_PROP, 69 BaseOptions.TEXT_LIMIT_WIDTH_PROP, 70 BaseOptions.TEXT_ANTIALIASING_PROP 71 }; 72 73 74 public BaseOptionsBeanInfo() { 75 this("/org/netbeans/modules/editor/resources/baseOptions"); } 77 78 public BaseOptionsBeanInfo(String iconPrefix) { 79 this(iconPrefix, ""); } 81 82 public BaseOptionsBeanInfo(String iconPrefix, String bundlePrefix) { 83 this.iconPrefix = iconPrefix; 84 this.bundlePrefix = bundlePrefix; 85 } 86 87 91 public PropertyDescriptor[] getPropertyDescriptors () { 92 if (descriptors == null) { 93 String [] propNames = getPropNames(); 94 PropertyDescriptor[] pds = new PropertyDescriptor[propNames.length]; 95 96 for (int i = 0; i < propNames.length; i++) { 97 pds[i] = createPropertyDescriptor(propNames[i]); 98 pds[i].setDisplayName(getString("PROP_" + bundlePrefix + propNames[i])); pds[i].setShortDescription(getString("HINT_" + bundlePrefix + propNames[i])); } 102 103 descriptors = pds; 105 updatePropertyDescriptors(); 107 } 108 return descriptors; 109 } 110 111 112 protected PropertyDescriptor createPropertyDescriptor(String propName) { 113 PropertyDescriptor pd; 114 try { 115 pd = new PropertyDescriptor(propName, getBeanClass()); 116 117 } catch (IntrospectionException e) { 118 try { 119 pd = new PropertyDescriptor(propName, null, null); 121 } catch (IntrospectionException e2) { 122 throw new IllegalStateException ("Invalid property name=" + propName); } 124 125 String cap = capitalize(propName); 128 Method m = findMethod("get" + cap); if (m != null) { 130 try { 131 pd.setReadMethod(m); 132 } catch (IntrospectionException e2) { 133 } 134 } 135 m = findMethod("set" + cap); if (m != null) { 137 try { 138 pd.setWriteMethod(m); 139 } catch (IntrospectionException e2) { 140 } 141 } 142 } 143 144 return pd; 145 } 146 147 private Method findMethod(String name) { 148 try { 149 Method [] ma = getBeanClass().getDeclaredMethods(); 150 for (int i = 0; i < ma.length; i++) { 151 if (name.equals(ma[i].getName())) { 152 return ma[i]; 153 } 154 } 155 } catch (SecurityException e) { 156 } 157 return null; 158 } 159 160 private static String capitalize(String s) { 161 if (s.length() == 0) { 162 return s; 163 } 164 char chars[] = s.toCharArray(); 165 chars[0] = Character.toUpperCase(chars[0]); 166 return new String (chars); 167 } 168 169 170 protected void updatePropertyDescriptors() { 171 setPropertyEditor(BaseOptions.ABBREV_MAP_PROP, AbbrevsEditor.class, false); 172 setPropertyEditor(BaseOptions.CARET_TYPE_INSERT_MODE_PROP, CaretTypeEditor.class); 173 setPropertyEditor(BaseOptions.CARET_TYPE_OVERWRITE_MODE_PROP, CaretTypeEditor.class); 174 setPropertyEditor(BaseOptions.KEY_BINDING_LIST_PROP, KeyBindingsEditor.class, false); 175 setPropertyEditor(BaseOptions.COLORING_MAP_PROP, ColoringArrayEditor.class, false); 176 setPropertyEditor(BaseOptions.SCROLL_JUMP_INSETS_PROP, ScrollInsetsEditor.class); 177 setPropertyEditor(BaseOptions.SCROLL_FIND_INSETS_PROP, ScrollInsetsEditor.class); 178 setPropertyEditor(BaseOptions.MACRO_MAP_PROP, MacrosEditor.class, false); 179 180 setExpert(EXPERT_PROP_NAMES); 181 boolean usesNewOptions = usesNewOptions(); 182 183 String hidden[] = (usesNewOptions) ? 184 new String [] { 185 BaseOptions.EXPAND_TABS_PROP, 186 BaseOptions.SPACES_PER_TAB_PROP, 187 BaseOptions.OPTIONS_VERSION_PROP, 188 BaseOptions.CARET_ITALIC_INSERT_MODE_PROP, 189 BaseOptions.CARET_ITALIC_OVERWRITE_MODE_PROP, 190 BaseOptions.COLORING_MAP_PROP, 191 BaseOptions.FONT_SIZE_PROP, 192 BaseOptions.KEY_BINDING_LIST_PROP, 193 BaseOptions.TEXT_LIMIT_LINE_COLOR_PROP, 194 BaseOptions.CARET_COLOR_INSERT_MODE_PROP, 195 BaseOptions.CARET_COLOR_OVERWRITE_MODE_PROP, 196 } : 197 new String [] { 198 BaseOptions.EXPAND_TABS_PROP, 199 BaseOptions.SPACES_PER_TAB_PROP, 200 BaseOptions.OPTIONS_VERSION_PROP, 201 BaseOptions.CARET_ITALIC_INSERT_MODE_PROP, 202 BaseOptions.CARET_ITALIC_OVERWRITE_MODE_PROP 203 } ; 204 205 setHidden(hidden); 206 207 } 208 209 protected boolean usesNewOptions() { 210 211 boolean usesNewOptions = false; 212 BaseOptions base = (BaseOptions) SharedClassObject.findObject(getBeanClass()); 213 if (base != null){ 214 usesNewOptions = base.usesNewOptionsDialog(); 215 } 216 return usesNewOptions; 217 } 218 219 protected Class getBeanClass() { 220 return BaseOptions.class; 221 } 222 223 protected String [] getPropNames() { 224 return BaseOptions.BASE_PROP_NAMES; 225 } 226 227 protected synchronized PropertyDescriptor getPD(String propName) { 228 if (names2PD == null) { 229 names2PD = new HashMap (37); 230 PropertyDescriptor[] pds = getPropertyDescriptors(); 231 for (int i = pds.length - 1; i >= 0; i--) { 232 names2PD.put(pds[i].getName(), pds[i]); 233 } 234 } 235 return (PropertyDescriptor)names2PD.get(propName); 236 } 237 238 protected void setPropertyEditor(String propName, Class propEditor, boolean canEditAsText) { 239 PropertyDescriptor pd = getPD(propName); 240 if (pd != null) { 241 pd.setPropertyEditorClass(propEditor); 242 pd.setValue("canEditAsText", canEditAsText ? Boolean.TRUE : Boolean.FALSE); } 244 } 245 246 protected void setPropertyEditor(String propName, Class propEditor) { 247 setPropertyEditor(propName, propEditor, true); 248 } 249 250 protected void setExpert(String [] propNames) { 251 for (int i = 0; i < propNames.length; i++) { 252 PropertyDescriptor pd = getPD(propNames[i]); 253 if (pd != null) { 254 pd.setExpert(true); 255 } 256 } 257 } 258 259 protected void setHidden(String [] propNames) { 260 for (int i = 0; i < propNames.length; i++) { 261 PropertyDescriptor pd = getPD(propNames[i]); 262 if (pd != null) { 263 pd.setHidden(true); 264 } 265 } 266 } 267 268 271 public Image getIcon(final int type) { 272 if ((type == BeanInfo.ICON_COLOR_16x16) || (type == BeanInfo.ICON_MONO_16x16)) { 273 if (icon == null) 274 icon = loadImage(iconPrefix + ".gif"); return icon; 276 } 277 else { 278 if (icon32 == null) 279 icon32 = loadImage(iconPrefix + "32.gif"); return icon32; 281 } 282 } 283 284 294 protected String getString(String key) { 295 return NbBundle.getMessage(BaseOptionsBeanInfo.class, key); 296 } 297 298 300 public static class CaretTypeEditor extends PropertyEditorSupport { 301 302 private static String [] tags = new String [] { 303 BaseCaret.LINE_CARET, 304 BaseCaret.THIN_LINE_CARET, 305 BaseCaret.BLOCK_CARET 306 }; 307 308 private static String [] locTags = new String [] { 309 getString("LINE_CARET"), getString("THIN_LINE_CARET"), getString("BLOCK_CARET") }; 313 314 public String [] getTags() { 315 return locTags; 316 } 317 318 public void setAsText(String txt) { 319 for (int i = 0; i < locTags.length; i++) { 320 if (locTags[i].equals(txt)) { 321 setValue(tags[i]); 322 break; 323 } 324 } 325 } 326 327 public String getAsText() { 328 String val = (String ) getValue(); 329 for (int i = 0; i < tags.length; i++) { 330 if (tags[i].equals(val)) { 331 return locTags[i]; 332 } 333 } 334 throw new IllegalStateException (); 335 } 336 337 static String getString(String s) { 338 return NbBundle.getMessage(BaseOptionsBeanInfo.class, s); 339 } 340 341 } 342 } 343 | Popular Tags |