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