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