1 19 20 package org.netbeans.modules.editor; 21 22 import java.beans.*; 23 import java.awt.Image ; 24 import java.lang.reflect.Method ; 25 import org.netbeans.editor.LocaleSupport; 26 import org.openide.util.Utilities; 27 28 32 public abstract class FormatterIndentEngineBeanInfo extends SimpleBeanInfo { 33 34 35 private String iconPrefix; 36 37 38 private Image icon; 39 private Image icon32; 40 41 private PropertyDescriptor[] propertyDescriptors; 42 43 private String [] propertyNames; 44 45 public FormatterIndentEngineBeanInfo() { 46 this(null); 47 } 48 49 public FormatterIndentEngineBeanInfo(String iconPrefix) { 50 this.iconPrefix = iconPrefix; 51 } 52 53 public PropertyDescriptor[] getPropertyDescriptors() { 54 if (propertyDescriptors == null) { 55 String [] propNames = getPropertyNames(); 56 PropertyDescriptor[] pds = new PropertyDescriptor[propNames.length]; 57 58 for (int i = 0; i < propNames.length; i++) { 59 pds[i] = createPropertyDescriptor(propNames[i]); 60 pds[i].setDisplayName(getString("PROP_indentEngine_" + propNames[i])); pds[i].setShortDescription(getString("HINT_indentEngine_" + propNames[i])); 64 } 65 66 propertyDescriptors = pds; 68 updatePropertyDescriptors(); 70 } 71 72 return propertyDescriptors; 73 } 74 75 76 protected PropertyDescriptor createPropertyDescriptor(String propName) { 77 PropertyDescriptor pd; 78 try { 79 pd = new PropertyDescriptor(propName, getBeanClass()); 80 81 } catch (IntrospectionException e) { 82 try { 83 pd = new PropertyDescriptor(propName, null, null); 85 } catch (IntrospectionException e2) { 86 throw new IllegalStateException ("Invalid property name=" + propName); } 88 89 String cap = capitalize(propName); 92 Method m = findMethod("get" + cap); if (m != null) { 94 try { 95 pd.setReadMethod(m); 96 } catch (IntrospectionException e2) { 97 } 98 } 99 m = findMethod("set" + cap); if (m != null) { 101 try { 102 pd.setWriteMethod(m); 103 } catch (IntrospectionException e2) { 104 } 105 } 106 } 107 108 return pd; 109 } 110 111 protected void updatePropertyDescriptors() { 112 } 113 114 private Method findMethod(String name) { 115 try { 116 Method [] ma = getBeanClass().getDeclaredMethods(); 117 for (int i = 0; i < ma.length; i++) { 118 if (name.equals(ma[i].getName())) { 119 return ma[i]; 120 } 121 } 122 } catch (SecurityException e) { 123 } 124 return null; 125 } 126 127 private static String capitalize(String s) { 128 if (s.length() == 0) { 129 return s; 130 } 131 char chars[] = s.toCharArray(); 132 chars[0] = Character.toUpperCase(chars[0]); 133 return new String (chars); 134 } 135 136 protected abstract Class getBeanClass(); 137 138 protected String [] getPropertyNames() { 139 if (propertyNames == null) { 140 propertyNames = createPropertyNames(); 141 } 142 return propertyNames; 143 } 144 145 protected String [] createPropertyNames() { 146 return new String [] { 147 FormatterIndentEngine.EXPAND_TABS_PROP, 148 FormatterIndentEngine.SPACES_PER_TAB_PROP 149 }; 150 } 151 152 protected PropertyDescriptor getPropertyDescriptor(String propertyName) { 153 String [] propNames = getPropertyNames(); 154 for (int i = 0; i < propNames.length; i++) { 155 if (propertyName.equals(propNames[i])) { 156 return getPropertyDescriptors()[i]; 157 } 158 } 159 return null; 160 } 161 162 protected void setPropertyEditor(String propertyName, Class propertyEditor) { 163 PropertyDescriptor pd = getPropertyDescriptor(propertyName); 164 if (pd != null) { 165 pd.setPropertyEditorClass(propertyEditor); 166 } 167 } 168 169 protected void setExpert(String [] expertPropertyNames) { 170 for (int i = 0; i < expertPropertyNames.length; i++) { 171 PropertyDescriptor pd = getPropertyDescriptor(expertPropertyNames[i]); 172 if (pd != null) { 173 pd.setExpert(true); 174 } 175 } 176 } 177 178 protected void setHidden(String [] hiddenPropertyNames) { 179 for (int i = 0; i < hiddenPropertyNames.length; i++) { 180 PropertyDescriptor pd = getPropertyDescriptor(hiddenPropertyNames[i]); 181 if (pd != null) { 182 pd.setHidden(true); 183 } 184 } 185 } 186 187 private String getValidIconPrefix() { 188 return (iconPrefix != null) ? iconPrefix 189 : "org/netbeans/modules/editor/resources/indentEngine"; } 191 192 private Image getDefaultIcon(String iconResource){ 193 return Utilities.loadImage(iconResource); 194 } 195 196 public Image getIcon(int type) { 197 if ((type == BeanInfo.ICON_COLOR_16x16) || (type == BeanInfo.ICON_MONO_16x16)) { 198 if (icon == null) { 199 icon = loadImage(getValidIconPrefix() + ".gif"); } 201 return (icon != null) ? icon : getDefaultIcon(getValidIconPrefix() + ".gif"); 203 } else { 204 if (icon32 == null) { 205 icon32 = loadImage(getValidIconPrefix() + "32.gif"); } 207 return (icon32 != null) ? icon32 : getDefaultIcon(getValidIconPrefix() + "32.gif"); } 209 } 210 211 218 protected String getString(String key) { 219 return LocaleSupport.getString( key ); 220 } 221 222 } 223 224 | Popular Tags |