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