KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > LinkProperties


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * LinkFields.java
26  *
27  * Created on March 14, 2001, 11:33 AM
28  */

29
30 package com.sun.enterprise.tools.common;
31
32 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
33 import com.sun.enterprise.tools.common.util.diagnostics.StackTrace;
34
35 import java.lang.reflect.Method JavaDoc;
36 import java.beans.PropertyDescriptor JavaDoc;
37
38 /**
39  *
40  * @author vkraemer
41  * @version
42  */

43 public class LinkProperties extends Object JavaDoc implements java.beans.PropertyChangeListener JavaDoc {
44
45     protected Object JavaDoc target;
46     
47     protected String JavaDoc sourceFieldName;
48     
49     protected Method JavaDoc writer = null;
50 // protected Method reader = null;
51

52     protected Object JavaDoc args[] = { target };
53     
54     /** Creates new LinkFields */
55     public LinkProperties(Object JavaDoc target, String JavaDoc commonName) throws java.beans.IntrospectionException JavaDoc {
56         this.target = target;
57         sourceFieldName = commonName;
58         writer = PropertyUtils.getWriter(target,commonName);
59 // reader = PropertyUtils.getWriter(target,commonName);
60
}
61
62     /** Creates new LinkFields */
63     public LinkProperties(Object JavaDoc target, String JavaDoc srcName, String JavaDoc destName)
64         throws java.beans.IntrospectionException JavaDoc {
65         this.target = target;
66         sourceFieldName = srcName;
67         writer = PropertyUtils.getWriter(target,destName);
68 // reader = PropertyUtils.getWriter(target,destName);
69
}
70         
71     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc pce) {
72         Reporter.info(pce); //NOI18N
73
String JavaDoc changedProperty = pce.getPropertyName();
74         Reporter.info(changedProperty); //NOI18N
75
Reporter.info(pce.getOldValue()); //NOI18N
76
Reporter.info(pce.getNewValue()); //NOI18N
77
Reporter.info(sourceFieldName); //NOI18N
78
try {
79 // Object targetValue = reader.invoke(target, null);
80

81             if (changedProperty.equals(sourceFieldName)) {
82                 Reporter.info("case one");//NOI18N
83
Reporter.verbose(pce); //NOI18N
84
args[0] = pce.getNewValue();
85                 writer.invoke(target, args);
86             }
87             // this is a hack for forte which has really strange property names...
88
// for example:
89
// the /WebApp/ResourceRef.51/Description is the description
90
// of a WebStandardData.ResourceRefData.
91
/*else if (changedProperty.endsWith(sourceFieldName.substring(1))) {
92                 Reporter.info("case two");//NOI18N
93                 args[0] = pce.getNewValue();
94                 writer.invoke(target, args);
95             }*/

96         }
97         catch (Throwable JavaDoc t) {
98             try {
99                 args[0] = pce.getNewValue().toString();
100                 writer.invoke(target, args);
101             }
102             catch (Throwable JavaDoc tt) {
103                 Reporter.critical(new StackTrace(t)); //NOI18N
104
}
105         }
106     }
107     
108     public static void main(String JavaDoc args[]) {
109         Reporter.setSeverityLevel(0); //NOI18N
110
TestObject a = new TestObject("foo");//NOI18N
111
TestObject b = new TestObject("bar");//NOI18N
112

113         java.beans.PropertyChangeSupport JavaDoc propWrap = new java.beans.PropertyChangeSupport JavaDoc(a);
114         
115         System.out.println(a); //NOI18N
116
System.out.println(b); //NOI18N
117
try {
118         propWrap.addPropertyChangeListener(new LinkProperties(b,"fOne"));//NOI18N
119
propWrap.addPropertyChangeListener(new LinkProperties(b,"fOne", "fTwo"));//NOI18N
120

121         a.setFOne("baz");//NOI18N
122
a.setFTwo("Blah");//NOI18N
123
propWrap.firePropertyChange("fOne","foo","baz");//NOI18N
124
propWrap.firePropertyChange("fTwo", "foo", "Blah");//NOI18N
125

126         System.out.println(a); //NOI18N
127
System.out.println(b); //NOI18N
128
}
129         catch(Throwable JavaDoc t) {
130             t.printStackTrace();
131         }
132     }
133     
134     static class TestObject {
135         private String JavaDoc fOne;
136         private String JavaDoc fTwo;
137         
138         public TestObject(String JavaDoc arg) {
139             fOne = arg;
140             fTwo = arg;
141         }
142         
143         public void setFOne(String JavaDoc newVal) {
144             fOne = newVal;
145         }
146         
147         public String JavaDoc getFOne() {
148             return fOne;
149         }
150
151         public void setFTwo(String JavaDoc newVal) {
152             fTwo = newVal;
153         }
154         
155         public String JavaDoc getFTwo() {
156             return fTwo;
157         }
158         
159         public String JavaDoc toString() {
160             return "My values are " + fOne + " and " + fTwo;//NOI18N
161
}
162     }
163 }
164
Popular Tags