-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path115.java
More file actions
257 lines (225 loc) · 8.49 KB
/
Copy path115.java
File metadata and controls
257 lines (225 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* DataCleaner (community edition)
* Copyright (C) 2014 Free Software Foundation, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.datacleaner.widgets.properties;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import org.apache.commons.lang.ArrayUtils;
import org.datacleaner.descriptors.ConfiguredPropertyDescriptor;
import org.datacleaner.job.builder.ComponentBuilder;
import org.datacleaner.panels.DCPanel;
import org.datacleaner.util.WidgetFactory;
import org.datacleaner.widgets.DCCheckBox;
import org.datacleaner.widgets.DCCheckBox.Listener;
import org.jdesktop.swingx.HorizontalLayout;
import org.jdesktop.swingx.VerticalLayout;
/**
* Abstract implementation of the {@link PropertyWidget} interface, for array
* properties that are represented using a list of checkboxes.
*
* @param <E>
*/
public abstract class AbstractMultipleCheckboxesPropertyWidget<E> extends AbstractPropertyWidget<E[]> {
private final Listener<E> _changeListener = (item, selected) -> fireValueChanged();
private final Map<String, DCCheckBox<E>> _checkBoxes;
private final ActionListener _selectAllListener = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
for (final JCheckBox cb : _checkBoxes.values()) {
if (cb.isEnabled()) {
cb.setSelected(true);
}
}
fireValueChanged();
}
};
private final ActionListener _selectNoneListener = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
for (final JCheckBox cb : _checkBoxes.values()) {
cb.setSelected(false);
}
fireValueChanged();
}
};
private final Class<E> _itemClass;
private final DCPanel _buttonPanel;
private final DCCheckBox<E> _notAvailableCheckBox;
public AbstractMultipleCheckboxesPropertyWidget(final ComponentBuilder componentBuilder,
final ConfiguredPropertyDescriptor propertyDescriptor, final Class<E> itemClass) {
super(componentBuilder, propertyDescriptor);
_itemClass = itemClass;
_checkBoxes = new LinkedHashMap<>();
setLayout(new VerticalLayout(2));
_notAvailableCheckBox = new DCCheckBox<>(getNotAvailableText(), false);
_notAvailableCheckBox.setOpaque(false);
_notAvailableCheckBox.setEnabled(false);
_buttonPanel = createButtonPanel();
add(_buttonPanel);
}
private void updateVisibility() {
_buttonPanel.setVisible(_checkBoxes.size() > 3);
if (_checkBoxes.isEmpty()) {
add(_notAvailableCheckBox);
} else {
remove(_notAvailableCheckBox);
}
}
protected DCPanel createButtonPanel() {
final DCPanel buttonPanel = new DCPanel();
buttonPanel.setLayout(new HorizontalLayout(2));
final JButton selectAllButton = WidgetFactory.createDefaultButton("Select all");
selectAllButton.addActionListener(_selectAllListener);
buttonPanel.add(selectAllButton);
final JButton selectNoneButton = WidgetFactory.createDefaultButton("Select none");
selectNoneButton.addActionListener(_selectNoneListener);
buttonPanel.add(selectNoneButton);
return buttonPanel;
}
public void initialize(final E[] values) {
if (values != null) {
// add all registered values
for (final E item : values) {
addCheckBox(item, true);
}
}
// add all available checkboxes
final E[] availableValues = getAvailableValues();
for (final E item : availableValues) {
addCheckBox(item, isEnabled(item, values));
}
updateVisibility();
}
protected JCheckBox[] getCheckBoxes() {
return _checkBoxes.values().toArray(new JCheckBox[_checkBoxes.size()]);
}
protected abstract E[] getAvailableValues();
/**
* Gets the text for an optional disabled checkbox in case no items are
* available.
*
* @return
*/
protected abstract String getNotAvailableText();
protected JCheckBox addCheckBox(final E item, final boolean checked) {
final String name = getName(item);
DCCheckBox<E> checkBox = _checkBoxes.get(name);
if (checkBox != null) {
checkBox.setSelected(checked);
return checkBox;
}
checkBox = new DCCheckBox<>(name, checked);
checkBox.setValue(item);
checkBox.setOpaque(false);
checkBox.addListener(_changeListener);
_checkBoxes.put(name, checkBox);
add(checkBox);
updateVisibility();
updateUI();
return checkBox;
}
protected void editCheckBox(final E oldvalue, final E newValue) {
final String name = getName(oldvalue);
final DCCheckBox<E> checkBox = _checkBoxes.get(name);
if (checkBox != null) {
_checkBoxes.remove(name);
checkBox.addListener(_changeListener);
final boolean isSelected = checkBox.isSelected();
checkBox.setValue(newValue);
checkBox.setSelected(isSelected, true);
_checkBoxes.put(name, checkBox);
add(checkBox);
// 'fireValueChanged' changes the value in the component builder. Otherwise the listener
// is activated at the later point(when the window is opened again) *
fireValueChanged();
} else {
addCheckBox(newValue, true);
}
updateVisibility();
updateUI();
}
protected void removeCheckBox(final E item) {
final DCCheckBox<E> checkBox = _checkBoxes.remove(getName(item));
if (checkBox != null) {
remove(checkBox);
}
updateVisibility();
}
private boolean isEnabled(final E value, final E[] enabledValues) {
if (enabledValues == null || enabledValues.length == 0) {
return false;
}
for (final E currentValue : enabledValues) {
if (currentValue.equals(value)) {
return true;
}
}
return false;
}
@Override
public boolean isSet() {
for (final JCheckBox checkBox : getCheckBoxes()) {
if (checkBox.isSelected()) {
return true;
}
}
return false;
}
@Override
public E[] getValue() {
final List<E> result = new ArrayList<>();
final Collection<DCCheckBox<E>> checkBoxes = _checkBoxes.values();
for (final DCCheckBox<E> cb : checkBoxes) {
if (cb.isSelected()) {
result.add(cb.getValue());
}
}
@SuppressWarnings("unchecked") final E[] array = (E[]) Array.newInstance(_itemClass, result.size());
return result.toArray(array);
}
@Override
protected void setValue(final E[] values) {
// if checkBoxes is empty it means that the value is being set before
// initializing the widget. This can occur in subclasses and automatic
// creating of checkboxes should be done.
if (_checkBoxes.isEmpty() && values != null) {
for (final E value : values) {
addCheckBox(value, true);
}
}
// update selections in checkboxes
for (final DCCheckBox<E> cb : _checkBoxes.values()) {
if (ArrayUtils.contains(values, cb.getValue())) {
cb.setSelected(true);
} else {
cb.setSelected(false);
}
}
updateVisibility();
}
protected abstract String getName(E item);
}