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.Menu;
21: import java.awt.MenuBar;
22:
23: class AwtMenuBarPersistenceDelegate extends DefaultPersistenceDelegate {
24: @Override
25: @SuppressWarnings({"nls","boxing"})
26: protected void initialize(Class<?> type, Object oldInstance,
27: Object newInstance, Encoder enc) {
28: super .initialize(type, oldInstance, newInstance, enc);
29: if (type != oldInstance.getClass()) {
30: return;
31: }
32:
33: MenuBar bar = (MenuBar) oldInstance;
34: int count = bar.getMenuCount();
35: Expression getterExp = null;
36: for (int i = 0; i < count; i++) {
37: getterExp = new Expression(bar.getMenu(i), "getLabel", null);
38: try {
39: // Calculate the old value of the property
40: Object oldVal = getterExp.getValue();
41: // Write the getter expression to the encoder
42: enc.writeExpression(getterExp);
43: // Get the target value that exists in the new environment
44: Object targetVal = enc.get(oldVal);
45: // Get the current property value in the new environment
46: Object newVal = null;
47: try {
48: newVal = new Expression(((MenuBar) newInstance)
49: .getMenu(i), "getLabel", null).getValue();
50: } catch (IndexOutOfBoundsException ex) {
51: // The newInstance has no elements, so current property
52: // value remains null
53: }
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, "insert", new Object[] {
63: null, i });
64: enc.writeStatement(setterStm);
65: }
66: } else {
67: PersistenceDelegate pd = enc
68: .getPersistenceDelegate(targetVal
69: .getClass());
70: if (!pd.mutatesTo(targetVal, newVal)) {
71: Menu menu = new Menu((String) oldVal);
72: menu.setName(bar.getMenu(i).getName());
73: Statement setterStm = new Statement(
74: oldInstance, "add",
75: new Object[] { menu });
76: enc.writeStatement(setterStm);
77: }
78: }
79: } catch (Exception ex) {
80: enc.getExceptionListener().exceptionThrown(ex);
81: }
82: }
83: }
84: }
|