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: import java.awt.Container;
21:
22: class AwtContainerPersistenceDelegate extends
23: AwtComponentPersistenceDelegate {
24: @Override
25: @SuppressWarnings({"nls","boxing"})
26: protected void initialize(Class<?> type, Object oldInstance,
27: Object newInstance, Encoder enc) {
28: // Call the initialization of the super type
29: super .initialize(type, oldInstance, newInstance, enc);
30:
31: Container container = (Container) oldInstance;
32: int count = container.getComponentCount();
33: Expression getterExp = null;
34: for (int i = 0; i < count; i++) {
35: getterExp = new Expression(container.getComponent(i),
36: container, "getComponent", new Object[] { i });
37: try {
38: // Calculate the old value of the property
39: Object oldVal = getterExp.getValue();
40: // Write the getter expression to the encoder
41: enc.writeExpression(getterExp);
42: // Get the target value that exists in the new environment
43: Object targetVal = enc.get(oldVal);
44: // Get the current property value in the new environment
45: Object newVal = null;
46: try {
47: newVal = new Expression(((Container) newInstance)
48: .getComponent(i), newInstance,
49: "getComponent", new Object[] { i })
50: .getValue();
51: } catch (ArrayIndexOutOfBoundsException ex) {
52: // Current Container does not have any component, newVal set
53: // to null
54: }
55: /*
56: * Make the target value and current property value equivalent
57: * in the new environment
58: */
59: if (null == targetVal) {
60: if (null != newVal) {
61: // Set to null
62: Statement setterStm = new Statement(
63: oldInstance, "add",
64: new Object[] { null });
65: enc.writeStatement(setterStm);
66: }
67: } else {
68: PersistenceDelegate pd = enc
69: .getPersistenceDelegate(targetVal
70: .getClass());
71: if (!pd.mutatesTo(targetVal, newVal)) {
72: Statement setterStm = new Statement(
73: oldInstance, "add",
74: new Object[] { oldVal });
75: enc.writeStatement(setterStm);
76: }
77: }
78: } catch (Exception ex) {
79: enc.getExceptionListener().exceptionThrown(ex);
80: }
81: }
82: }
83: }
|