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.Box;
21:
22: class SwingBoxPersistenceDelegate extends PersistenceDelegate {
23: @Override
24: protected Expression instantiate(Object oldInstance, Encoder enc) {
25: return new Expression(oldInstance, oldInstance.getClass(),
26: "createVerticalBox", null); //$NON-NLS-1$
27: }
28:
29: @Override
30: @SuppressWarnings({"nls","boxing"})
31: protected void initialize(Class<?> type, Object oldInstance,
32: Object newInstance, Encoder enc) {
33: Box box = (Box) oldInstance;
34: Expression getterExp = new Expression(box.getAlignmentX(), box,
35: "getAlignmentX", null);
36: try {
37: // Calculate the old value of the property
38: Object oldVal = getterExp.getValue();
39: // Write the getter expression to the encoder
40: enc.writeExpression(getterExp);
41: // Get the target value that exists in the new environment
42: Object targetVal = enc.get(oldVal);
43: // Get the current property value in the new environment
44: Object newVal = null;
45: Box newBox = (Box) newInstance;
46: newVal = new Expression(newBox.getAlignmentX(), newBox,
47: "AlignmentX", null).getValue();
48: /*
49: * Make the target value and current property value equivalent in
50: * the new environment
51: */
52: if (null == targetVal) {
53: if (null != newVal) {
54: // Set to null
55: Statement setterStm = new Statement(oldInstance,
56: "setAlignmentX", new Object[] { null });
57: enc.writeStatement(setterStm);
58: }
59: } else {
60: PersistenceDelegate pd = enc
61: .getPersistenceDelegate(targetVal.getClass());
62: if (!pd.mutatesTo(targetVal, newVal)) {
63: Statement setterStm = new Statement(oldInstance,
64: "setAlignmentX", new Object[] { oldVal });
65: enc.writeStatement(setterStm);
66: }
67: }
68: } catch (Exception ex) {
69: enc.getExceptionListener().exceptionThrown(ex);
70: }
71: }
72: }
|