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.List;
21:
22: class AwtListPersistenceDelegate extends DefaultPersistenceDelegate {
23: @Override
24: @SuppressWarnings({"nls","boxing"})
25: protected void initialize(Class<?> type, Object oldInstance,
26: Object newInstance, Encoder enc) {
27: super .initialize(type, oldInstance, newInstance, enc);
28:
29: List list = (List) oldInstance;
30: Statement setterStm = new Statement(oldInstance, "setSize",
31: new Object[] { list.getSize() });
32: enc.writeStatement(setterStm);
33:
34: int count = list.getItemCount();
35: Expression getterExp = null;
36: for (int i = 0; i < count; i++) {
37: getterExp = new Expression(list, "getItem",
38: new Object[] { i });
39: try {
40: // Calculate the old value of the property
41: Object oldVal = getterExp.getValue();
42: // Write the getter expression to the encoder
43: enc.writeExpression(getterExp);
44: // Get the target value that exists in the new environment
45: Object targetVal = enc.get(oldVal);
46: // Get the current property value in the new environment
47: Object newVal = null;
48: try {
49: newVal = new Expression(newInstance, "getItem",
50: new Object[] { i }).getValue();
51: } catch (IndexOutOfBoundsException ex) {
52: // The newInstance has no elements, so current property
53: // value remains 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: setterStm = new Statement(oldInstance, "add",
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: setterStm = new Statement(oldInstance, "add",
72: new Object[] { oldVal });
73: enc.writeStatement(setterStm);
74: }
75: }
76: } catch (Exception ex) {
77: enc.getExceptionListener().exceptionThrown(ex);
78: }
79: }
80: }
81: }
|