1 19 20 package org.openide.nodes; 21 22 import org.openide.util.Utilities; 23 24 import java.beans.Beans ; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 29 33 public class IndexedPropertySupport<T,E> extends Node.IndexedProperty<T,E> { 34 35 protected T instance; 36 37 38 private Method setter; 39 40 41 private Method getter; 42 43 44 private Method indexedSetter; 45 46 47 private Method indexedGetter; 48 49 58 public IndexedPropertySupport( 59 T instance, Class <T> valueType, Class <E> elementType, Method getter, Method setter, Method indexedGetter, 60 Method indexedSetter 61 ) { 62 super(valueType, elementType); 63 this.instance = instance; 64 this.setter = setter; 65 this.getter = getter; 66 this.indexedSetter = indexedSetter; 67 this.indexedGetter = indexedGetter; 68 } 69 70 73 public final void setDisplayName(String s) { 74 super.setDisplayName(s); 75 } 76 77 80 public final void setName(String s) { 81 super.setName(s); 82 } 83 84 87 public final void setShortDescription(String s) { 88 super.setShortDescription(s); 89 } 90 91 94 public boolean canRead() { 95 return getter != null; 96 } 97 98 104 public T getValue() throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 105 if (!canRead()) { 106 throw new IllegalAccessException (); 107 } 108 109 Object validInstance = Beans.getInstanceOf(instance, getter.getDeclaringClass()); 110 111 return PropertySupport.cast(getValueType(), getter.invoke(validInstance)); 112 } 113 114 117 public boolean canWrite() { 118 return setter != null; 119 } 120 121 127 public void setValue(T val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 128 if (!canWrite()) { 129 throw new IllegalAccessException (); 130 } 131 132 Object validInstance = Beans.getInstanceOf(instance, setter.getDeclaringClass()); 133 134 Object value = val; 135 if ( 136 (val != null) && (setter.getParameterTypes()[0].getComponentType().isPrimitive()) && 137 (!val.getClass().getComponentType().isPrimitive()) 138 ) { 139 value = Utilities.toPrimitiveArray((Object []) val); 140 } 141 142 setter.invoke(validInstance, value); 143 } 144 145 148 public boolean canIndexedRead() { 149 return indexedGetter != null; 150 } 151 152 158 public E getIndexedValue(int index) 159 throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 160 if (!canIndexedRead()) { 161 throw new IllegalAccessException (); 162 } 163 164 Object validInstance = Beans.getInstanceOf(instance, indexedGetter.getDeclaringClass()); 165 166 return PropertySupport.cast(getElementType(), indexedGetter.invoke(validInstance, index)); 167 } 168 169 172 public boolean canIndexedWrite() { 173 return indexedSetter != null; 174 } 175 176 182 public void setIndexedValue(int index, E val) 183 throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 184 if (!canIndexedWrite()) { 185 throw new IllegalAccessException (); 186 } 187 188 Object validInstance = Beans.getInstanceOf(instance, indexedSetter.getDeclaringClass()); 189 indexedSetter.invoke(validInstance, new Object [] { new Integer (index), val }); 190 } 191 } 192 | Popular Tags |