1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * PropertyNode.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.experiment; |
---|
24 | |
---|
25 | import weka.core.RevisionHandler; |
---|
26 | import weka.core.RevisionUtils; |
---|
27 | |
---|
28 | import java.beans.IntrospectionException; |
---|
29 | import java.beans.PropertyDescriptor; |
---|
30 | import java.io.IOException; |
---|
31 | import java.io.Serializable; |
---|
32 | |
---|
33 | /** |
---|
34 | * Stores information on a property of an object: the class of the |
---|
35 | * object with the property; the property descriptor, and the current |
---|
36 | * value. |
---|
37 | * |
---|
38 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
39 | * @version $Revision: 1.7 $ |
---|
40 | */ |
---|
41 | public class PropertyNode |
---|
42 | implements Serializable, RevisionHandler { |
---|
43 | |
---|
44 | /** for serialization */ |
---|
45 | private static final long serialVersionUID = -8718165742572631384L; |
---|
46 | |
---|
47 | /** The current property value */ |
---|
48 | public Object value; |
---|
49 | |
---|
50 | /** The class of the object with this property */ |
---|
51 | public Class parentClass; |
---|
52 | |
---|
53 | /** Other info about the property */ |
---|
54 | public PropertyDescriptor property; |
---|
55 | |
---|
56 | /** |
---|
57 | * Creates a mostly empty property. |
---|
58 | * |
---|
59 | * @param pValue a property value. |
---|
60 | */ |
---|
61 | public PropertyNode(Object pValue) { |
---|
62 | |
---|
63 | this(pValue, null, null); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Creates a fully specified property node. |
---|
68 | * |
---|
69 | * @param pValue the current property value. |
---|
70 | * @param prop the PropertyDescriptor. |
---|
71 | * @param pClass the Class of the object with this property. |
---|
72 | */ |
---|
73 | public PropertyNode(Object pValue, PropertyDescriptor prop, Class pClass) { |
---|
74 | |
---|
75 | value = pValue; |
---|
76 | property = prop; |
---|
77 | parentClass = pClass; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Returns a string description of this property. |
---|
82 | * |
---|
83 | * @return a value of type 'String' |
---|
84 | */ |
---|
85 | public String toString() { |
---|
86 | |
---|
87 | if (property == null) { |
---|
88 | return "Available properties"; |
---|
89 | } |
---|
90 | return property.getDisplayName(); |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * Handle serialization ourselves since PropertyDescriptor isn't |
---|
95 | * serializable |
---|
96 | */ |
---|
97 | private void writeObject(java.io.ObjectOutputStream out) throws IOException { |
---|
98 | |
---|
99 | try { |
---|
100 | out.writeObject(value); |
---|
101 | } catch (Exception ex) { |
---|
102 | throw new IOException("Can't serialize object: " + ex.getMessage()); |
---|
103 | } |
---|
104 | out.writeObject(parentClass); |
---|
105 | out.writeObject(property.getDisplayName()); |
---|
106 | out.writeObject(property.getReadMethod().getName()); |
---|
107 | out.writeObject(property.getWriteMethod().getName()); |
---|
108 | } |
---|
109 | private void readObject(java.io.ObjectInputStream in) |
---|
110 | throws IOException, ClassNotFoundException { |
---|
111 | |
---|
112 | value = in.readObject(); |
---|
113 | parentClass = (Class) in.readObject(); |
---|
114 | String name = (String) in.readObject(); |
---|
115 | String getter = (String) in.readObject(); |
---|
116 | String setter = (String) in.readObject(); |
---|
117 | /* |
---|
118 | System.err.println("Loading property descriptor:\n" |
---|
119 | + "\tparentClass: " + parentClass.getName() |
---|
120 | + "\tname: " + name |
---|
121 | + "\tgetter: " + getter |
---|
122 | + "\tsetter: " + setter); |
---|
123 | */ |
---|
124 | try { |
---|
125 | property = new PropertyDescriptor(name, parentClass, getter, setter); |
---|
126 | } catch (IntrospectionException ex) { |
---|
127 | throw new ClassNotFoundException("Couldn't create property descriptor: " |
---|
128 | + parentClass.getName() + "::" |
---|
129 | + name); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Returns the revision string. |
---|
135 | * |
---|
136 | * @return the revision |
---|
137 | */ |
---|
138 | public String getRevision() { |
---|
139 | return RevisionUtils.extract("$Revision: 1.7 $"); |
---|
140 | } |
---|
141 | } // PropertyNode |
---|