KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > IndexedPropertySupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.nodes;
21
22 import org.openide.util.Utilities;
23
24 import java.beans.Beans JavaDoc;
25
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 /** Support for indexed properties.
30 *
31 * @author Jan Jancura
32 */

33 public class IndexedPropertySupport<T,E> extends Node.IndexedProperty<T,E> {
34     /** Instance of the bean. */
35     protected T instance;
36
37     /** setter method */
38     private Method JavaDoc setter;
39
40     /** getter method */
41     private Method JavaDoc getter;
42
43     /** indexed setter method */
44     private Method JavaDoc indexedSetter;
45
46     /** indexed getter method */
47     private Method JavaDoc indexedGetter;
48
49     /** Constructor.
50     * @param instance the bean for which these properties exist
51     * @param valueType type of the entire property
52     * @param elementType type of one element of the property
53     * @param getter get method for the entire property
54     * @param setter set method for the entire property
55     * @param indexedGetter get method for one element
56     * @param indexedSetter set method for one element
57     */

58     public IndexedPropertySupport(
59         T instance, Class JavaDoc<T> valueType, Class JavaDoc<E> elementType, Method JavaDoc getter, Method JavaDoc setter, Method JavaDoc indexedGetter,
60         Method JavaDoc 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     /* Setter for display name.
71     * @param s the string
72     */

73     public final void setDisplayName(String JavaDoc s) {
74         super.setDisplayName(s);
75     }
76
77     /* Setter for name.
78     * @param s the string
79     */

80     public final void setName(String JavaDoc s) {
81         super.setName(s);
82     }
83
84     /* Setter for short description.
85     * @param s the string
86     */

87     public final void setShortDescription(String JavaDoc s) {
88         super.setShortDescription(s);
89     }
90
91     /* Can read the value of the property.
92     * @return <CODE>true</CODE> if the read of the value is supported
93     */

94     public boolean canRead() {
95         return getter != null;
96     }
97
98     /* Getter for the value.
99     * @return the value of the property
100     * @exception IllegalAccessException cannot access the called method
101     * @exception IllegalArgumentException wrong argument
102     * @exception InvocationTargetException an exception during invocation
103     */

104     public T getValue() throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
105         if (!canRead()) {
106             throw new IllegalAccessException JavaDoc();
107         }
108
109         Object JavaDoc validInstance = Beans.getInstanceOf(instance, getter.getDeclaringClass());
110
111         return PropertySupport.cast(getValueType(), getter.invoke(validInstance));
112     }
113
114     /* Can write the value of the property.
115     * @return <CODE>true</CODE> if the read of the value is supported
116     */

117     public boolean canWrite() {
118         return setter != null;
119     }
120
121     /* Setter for the value.
122     * @param val the value of the property
123     * @exception IllegalAccessException cannot access the called method
124     * @exception IllegalArgumentException wrong argument
125     * @exception InvocationTargetException an exception during invocation
126     */

127     public void setValue(T val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
128         if (!canWrite()) {
129             throw new IllegalAccessException JavaDoc();
130         }
131
132         Object JavaDoc validInstance = Beans.getInstanceOf(instance, setter.getDeclaringClass());
133
134         Object JavaDoc value = val;
135         if (
136             (val != null) && (setter.getParameterTypes()[0].getComponentType().isPrimitive()) &&
137                 (!val.getClass().getComponentType().isPrimitive())
138         ) {
139             value = Utilities.toPrimitiveArray((Object JavaDoc[]) val);
140         }
141
142         setter.invoke(validInstance, value);
143     }
144
145     /* Can read the indexed value of the property.
146     * @return <CODE>true</CODE> if the read of the value is supported
147     */

148     public boolean canIndexedRead() {
149         return indexedGetter != null;
150     }
151
152     /* Getter for the indexed value.
153     * @return the value of the property
154     * @exception IllegalAccessException cannot access the called method
155     * @exception IllegalArgumentException wrong argument
156     * @exception InvocationTargetException an exception during invocation
157     */

158     public E getIndexedValue(int index)
159     throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
160         if (!canIndexedRead()) {
161             throw new IllegalAccessException JavaDoc();
162         }
163
164         Object JavaDoc validInstance = Beans.getInstanceOf(instance, indexedGetter.getDeclaringClass());
165
166         return PropertySupport.cast(getElementType(), indexedGetter.invoke(validInstance, index));
167     }
168
169     /* Can write the indexed value of the property.
170     * @return <CODE>true</CODE> if the read of the value is supported
171     */

172     public boolean canIndexedWrite() {
173         return indexedSetter != null;
174     }
175
176     /* Setter for the indexed value.
177     * @param val the value of the property
178     * @exception IllegalAccessException cannot access the called method
179     * @exception IllegalArgumentException wrong argument
180     * @exception InvocationTargetException an exception during invocation
181     */

182     public void setIndexedValue(int index, E val)
183     throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
184         if (!canIndexedWrite()) {
185             throw new IllegalAccessException JavaDoc();
186         }
187
188         Object JavaDoc validInstance = Beans.getInstanceOf(instance, indexedSetter.getDeclaringClass());
189         indexedSetter.invoke(validInstance, new Object JavaDoc[] { new Integer JavaDoc(index), val });
190     }
191 }
192
Popular Tags