Skip to main content
summaryrefslogtreecommitdiffstats
blob: 60668c889b8a6b267515567fba75e891ad655752 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ui.databinding;

import java.text.MessageFormat;
import java.util.function.BiFunction;

import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.fx.core.databinding.AdapterFactory;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;

/**
 * Utility to setup {@link ListView} and {@link ComboBox}
 */
public class ListUtil {
	/**
	 * Setup a list where cell text is bound to the given property
	 *
	 * @param listView
	 *            the list view
	 * @param property
	 *            the text property
	 * @param <T>
	 *            the list element type
	 * @see PropertyListCellFaytory#textFactory(IValueProperty)
	 */
	public static <T> void setupList(@NonNull ListView<T> listView, @NonNull IValueProperty property) {
		listView.setCellFactory(PropertyListCellFaytory.<T> textFactory(property));
	}

	/**
	 * Setup a list where the template is used to format the property values
	 * using {@link MessageFormat}
	 *
	 * @param listView
	 *            the list view
	 * @param template
	 *            the template to use
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the list element type
	 * @see PropertyListCellFaytory#textCell(String, IValueProperty...)
	 */
	public static <T> void setupList(@NonNull ListView<T> listView, @NonNull String template,
			@NonNull IValueProperty... properties) {
		listView.setCellFactory(PropertyListCellFaytory.<T> textFactory(template, properties));
	}

	/**
	 * Setup a list where the template is used to format the property values
	 * using {@link MessageFormat}
	 *
	 * @param listView
	 *            the list view
	 * @param template
	 *            the template to use
	 * @param converter
	 *            the converter
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the list element type
	 * @see PropertyListCellFaytory#textCell(String, IValueProperty...)
	 * @since 2.2.0
	 */
	public static <T> void setupList(@NonNull ListView<T> listView, @NonNull String template,
			@NonNull BiFunction<@NonNull IValueProperty, @Nullable Object, @Nullable Object> converter,
			@NonNull IValueProperty... properties) {
		listView.setCellFactory(PropertyListCellFaytory.<T> textFactory(template, converter, properties));
	}

	/**
	 * Setup a list where the cell text is bound to a given property
	 *
	 * @param listView
	 *            the list view
	 * @param list
	 *            the list to set as the input
	 * @param property
	 *            the property
	 * @param <T>
	 *            the list element type
	 * @see #setupList(ListView, IValueProperty)
	 */
	public static <T> void setupList(@NonNull ListView<T> listView, @NonNull IObservableList list,
			@NonNull IValueProperty property) {
		setupList(listView, property);
		listView.setItems(AdapterFactory.<T> adapt(list));
	}

	/**
	 * Setup a list where the template is used to format the property values
	 * using {@link MessageFormat}
	 *
	 * @param listView
	 *            the list view
	 * @param list
	 *            the list to set as the input
	 * @param template
	 *            the template
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the list element type
	 * @see #setupList(ListView, String, IValueProperty...)
	 */
	public static <T> void setupList(@NonNull ListView<T> listView, @NonNull IObservableList list,
			@NonNull String template, @NonNull IValueProperty... properties) {
		setupList(listView, template, properties);
		listView.setItems(AdapterFactory.<T> adapt(list));
	}

	/**
	 * Setup a list where the template is used to format the property values
	 * using {@link MessageFormat}
	 *
	 * @param listView
	 *            the list view
	 * @param list
	 *            the list to set as the input
	 * @param converter
	 *            the converter
	 * @param template
	 *            the template
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the list element type
	 * @see #setupList(ListView, String, IValueProperty...)
	 * @since 2.2.0
	 */
	public static <T> void setupList(@NonNull ListView<T> listView, @NonNull IObservableList list,
			@NonNull BiFunction<@NonNull IValueProperty, @Nullable Object, @Nullable Object> converter,
			@NonNull String template, @NonNull IValueProperty... properties) {
		setupList(listView, template, converter, properties);
		listView.setItems(AdapterFactory.<T> adapt(list));
	}

	/**
	 * Setup a combo box where the cell text is bound to the given property
	 *
	 * @param comboBox
	 *            the combo box
	 * @param property
	 *            the property
	 * @param <T>
	 *            the combobox element type
	 * @see PropertyListCellFaytory#textFactory(IValueProperty)
	 * @see PropertyListCellFaytory#textCell(IValueProperty)
	 */
	public static <T> void setupComboBox(@NonNull ComboBox<T> comboBox, @NonNull IValueProperty property) {
		comboBox.setCellFactory(PropertyListCellFaytory.<T> textFactory(property));
		comboBox.setButtonCell(PropertyListCellFaytory.<T> textCell(property));
	}

	/**
	 * Setup a combo box where the template is used to format the property
	 * values using {@link MessageFormat}
	 *
	 * @param comboBox
	 *            the combo box
	 * @param template
	 *            the template to use
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the combobox element type
	 * @see PropertyListCellFaytory#textFactory(String, IValueProperty...)
	 * @see PropertyListCellFaytory#textCell(String, IValueProperty...)
	 */
	public static <T> void setupComboBox(@NonNull ComboBox<T> comboBox, @NonNull String template,
			@NonNull IValueProperty... properties) {
		comboBox.setCellFactory(PropertyListCellFaytory.<T> textFactory(template, properties));
		comboBox.setButtonCell(PropertyListCellFaytory.<T> textCell(template, properties));
	}

	/**
	 * Setup a combo box where the template is used to format the property
	 * values using {@link MessageFormat}
	 *
	 * @param comboBox
	 *            the combo box
	 * @param template
	 *            the template to use
	 * @param converter
	 *            the converter
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the combobox element type
	 * @see PropertyListCellFaytory#textFactory(String, IValueProperty...)
	 * @see PropertyListCellFaytory#textCell(String, IValueProperty...)
	 * @since 2.2.0
	 */
	public static <T> void setupComboBox(@NonNull ComboBox<T> comboBox, @NonNull String template,
			@NonNull BiFunction<@NonNull IValueProperty, @Nullable Object, @Nullable Object> converter,
			@NonNull IValueProperty... properties) {
		comboBox.setCellFactory(PropertyListCellFaytory.<T> textFactory(template, converter, properties));
		comboBox.setButtonCell(PropertyListCellFaytory.<T> textCell(template, converter, properties));
	}

	/**
	 * Setup a combo box where the cell text is bound to the given property
	 *
	 * @param list
	 *            the list to set as the input
	 * @param comboBox
	 *            the combo box
	 * @param property
	 *            the property
	 * @param <T>
	 *            the combobox element type
	 * @see #setupComboBox(ComboBox, IValueProperty)
	 */
	public static <T> void setupComboBox(@NonNull IObservableList list, @NonNull ComboBox<T> comboBox,
			@NonNull IValueProperty property) {
		setupComboBox(comboBox, property);
		comboBox.setItems(AdapterFactory.<T> adapt(list));
	}

	/**
	 * Setup a combo box where the template is used to format the property
	 * values using {@link MessageFormat}
	 *
	 * @param comboBox
	 *            the combo box
	 * @param list
	 *            the list to set as the input
	 * @param template
	 *            the template to use
	 * @param properties
	 *            the properties to display
	 * @param <T>
	 *            the combobox element type
	 * @see #setupComboBox(ComboBox, String, IValueProperty...)
	 */
	public static <T> void setupComboBox(@NonNull ComboBox<T> comboBox, @NonNull IObservableList list,
			@NonNull String template, @NonNull IValueProperty... properties) {
		setupComboBox(comboBox, template, properties);
		comboBox.setItems(AdapterFactory.<T> adapt(list));
	}
}

Back to the top