01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.beans;
19:
20: class AwtComponentPersistenceDelegate extends
21: DefaultPersistenceDelegate {
22:
23: @Override
24: @SuppressWarnings("nls")
25: protected void initialize(Class<?> type, Object oldInstance,
26: Object newInstance, Encoder enc) {
27: // Call the initialization of the super type
28: super .initialize(type, oldInstance, newInstance, enc);
29:
30: // background
31: writeProperty(oldInstance, newInstance, enc, "Background");
32:
33: // foreground
34: writeProperty(oldInstance, newInstance, enc, "Foreground");
35:
36: // font
37: writeProperty(oldInstance, newInstance, enc, "Font");
38:
39: // bounds
40: writeProperty(oldInstance, newInstance, enc, "Bounds");
41:
42: // name
43: writeProperty(oldInstance, newInstance, enc, "Name");
44:
45: }
46:
47: @SuppressWarnings("nls")
48: static void writeProperty(Object oldInstance, Object newInstance,
49: Encoder enc, String property) {
50: StringBuilder builder = new StringBuilder();
51: Expression getterExp = new Expression(oldInstance, builder
52: .append("get").append(property).toString(), null);
53: try {
54: // Calculate the old value of the property
55: Object oldVal = getterExp.getValue();
56: // Write the getter expression to the encoder
57: enc.writeExpression(getterExp);
58: // Get the target value that exists in the new environment
59: Object targetVal = enc.get(oldVal);
60: // Get the current property value in the new environment
61: builder.delete(0, builder.capacity());
62: Object newVal = new Expression(newInstance, builder.append(
63: "get").append(property).toString(), null)
64: .getValue();
65: /*
66: * Make the target value and current property value equivalent in
67: * the new environment
68: */
69: if (null == targetVal) {
70: if (null != newVal) {
71: // Set to null
72: builder.delete(0, builder.capacity());
73: Statement setterStm = new Statement(oldInstance,
74: builder.append("set").append(property)
75: .toString(), new Object[] { null });
76: enc.writeStatement(setterStm);
77: }
78: } else {
79: PersistenceDelegate pd = enc
80: .getPersistenceDelegate(targetVal.getClass());
81: if (!pd.mutatesTo(targetVal, newVal)) {
82: builder.delete(0, builder.capacity());
83: Statement setterStm = new Statement(oldInstance,
84: builder.append("set").append(property)
85: .toString(),
86: new Object[] { oldVal });
87: enc.writeStatement(setterStm);
88: }
89: }
90: } catch (Exception ex) {
91: enc.getExceptionListener().exceptionThrown(ex);
92: }
93: }
94: }
|