1 23 24 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 ; 36 import java.beans.PropertyDescriptor ; 37 38 43 public class LinkProperties extends Object implements java.beans.PropertyChangeListener { 44 45 protected Object target; 46 47 protected String sourceFieldName; 48 49 protected Method writer = null; 50 52 protected Object args[] = { target }; 53 54 55 public LinkProperties(Object target, String commonName) throws java.beans.IntrospectionException { 56 this.target = target; 57 sourceFieldName = commonName; 58 writer = PropertyUtils.getWriter(target,commonName); 59 } 61 62 63 public LinkProperties(Object target, String srcName, String destName) 64 throws java.beans.IntrospectionException { 65 this.target = target; 66 sourceFieldName = srcName; 67 writer = PropertyUtils.getWriter(target,destName); 68 } 70 71 public void propertyChange(java.beans.PropertyChangeEvent pce) { 72 Reporter.info(pce); String changedProperty = pce.getPropertyName(); 74 Reporter.info(changedProperty); Reporter.info(pce.getOldValue()); Reporter.info(pce.getNewValue()); Reporter.info(sourceFieldName); try { 79 81 if (changedProperty.equals(sourceFieldName)) { 82 Reporter.info("case one"); Reporter.verbose(pce); args[0] = pce.getNewValue(); 85 writer.invoke(target, args); 86 } 87 96 } 97 catch (Throwable t) { 98 try { 99 args[0] = pce.getNewValue().toString(); 100 writer.invoke(target, args); 101 } 102 catch (Throwable tt) { 103 Reporter.critical(new StackTrace(t)); } 105 } 106 } 107 108 public static void main(String args[]) { 109 Reporter.setSeverityLevel(0); TestObject a = new TestObject("foo"); TestObject b = new TestObject("bar"); 113 java.beans.PropertyChangeSupport propWrap = new java.beans.PropertyChangeSupport (a); 114 115 System.out.println(a); System.out.println(b); try { 118 propWrap.addPropertyChangeListener(new LinkProperties(b,"fOne")); propWrap.addPropertyChangeListener(new LinkProperties(b,"fOne", "fTwo")); 121 a.setFOne("baz"); a.setFTwo("Blah"); propWrap.firePropertyChange("fOne","foo","baz"); propWrap.firePropertyChange("fTwo", "foo", "Blah"); 126 System.out.println(a); System.out.println(b); } 129 catch(Throwable t) { 130 t.printStackTrace(); 131 } 132 } 133 134 static class TestObject { 135 private String fOne; 136 private String fTwo; 137 138 public TestObject(String arg) { 139 fOne = arg; 140 fTwo = arg; 141 } 142 143 public void setFOne(String newVal) { 144 fOne = newVal; 145 } 146 147 public String getFOne() { 148 return fOne; 149 } 150 151 public void setFTwo(String newVal) { 152 fTwo = newVal; 153 } 154 155 public String getFTwo() { 156 return fTwo; 157 } 158 159 public String toString() { 160 return "My values are " + fOne + " and " + fTwo; } 162 } 163 } 164 | Popular Tags |