1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.swing.smf; |
|
27 |
|
28 import java.awt.Component; |
|
29 import java.util.*; |
|
30 import javax.swing.event.*; |
|
31 import com.oracle.solaris.vp.panel.common.smf.BasicSmfMutableProperty; |
|
32 import com.oracle.solaris.vp.util.misc.property.*; |
|
33 |
|
34 /** |
|
35 * The {@code ComponentListPropertySynchronizer} class provides a base class to |
|
36 * synchronize a {@link MutableProperty} with a {@link ComponentList} so that |
|
37 * changes in one will automatically be reflected in the other. |
|
38 */ |
|
39 public abstract class ComponentListPropertySynchronizer<T, C extends Component, |
|
40 L extends ComponentList<C>> extends PropertySynchronizer<List<T>, L> { |
|
41 |
|
42 // |
|
43 // Instance data |
|
44 // |
|
45 |
|
46 private ChangeListener listener = |
|
47 new ChangeListener() { |
|
48 @Override |
|
49 public void stateChanged(ChangeEvent e) { |
|
50 objectChanged(); |
|
51 } |
|
52 }; |
|
53 |
|
54 // |
|
55 // Constructors |
|
56 // |
|
57 |
|
58 public ComponentListPropertySynchronizer( |
|
59 BasicSmfMutableProperty<T> property, L list) { |
|
60 |
|
61 super(property, list); |
|
62 list.addChangeListener(listener); |
|
63 } |
|
64 |
|
65 // |
|
66 // PropertySynchronizer methods |
|
67 // |
|
68 |
|
69 @Override |
|
70 public void desynchronize() { |
|
71 super.desynchronize(); |
|
72 getObject().removeChangeListener(listener); |
|
73 } |
|
74 |
|
75 @Override |
|
76 public List<T> getValue() { |
|
77 ComponentList<C> list = getObject(); |
|
78 int nComps = list.getComponentCount(); |
|
79 List<T> values = new ArrayList<T>(nComps); |
|
80 for (int i = 0; i < nComps; i++) { |
|
81 C comp = list.getListComponent(i); |
|
82 values.add(getValue(comp)); |
|
83 } |
|
84 return values; |
|
85 } |
|
86 |
|
87 @Override |
|
88 public void setValue(List<T> values) { |
|
89 ComponentList<C> list = getObject(); |
|
90 int nComps = list.getComponentCount(); |
|
91 int nValues = values == null ? 0 : values.size(); |
|
92 |
|
93 if (nValues > nComps) { |
|
94 for (int i = 0; i < nValues - nComps; i++) { |
|
95 list.add(); |
|
96 } |
|
97 } else if (nValues < nComps) { |
|
98 for (int i = 0; i < nComps - nValues; i++) { |
|
99 list.remove(0); |
|
100 } |
|
101 } |
|
102 |
|
103 nComps = list.getComponentCount(); |
|
104 assert nValues == nComps; |
|
105 |
|
106 for (int i = 0; i < nValues; i++) { |
|
107 C comp = comp = list.getListComponent(i); |
|
108 T value = values.get(i); |
|
109 setValue(comp, value); |
|
110 } |
|
111 } |
|
112 |
|
113 // |
|
114 // ComponentListPropertySynchronizer methods |
|
115 // |
|
116 |
|
117 protected abstract T getValue(C comp); |
|
118 |
|
119 protected abstract void setValue(C comp, T value); |
|
120 } |
|