summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/LocationSelector.java
blob: 0079ece23bfa923f0445e92ee33c6476d157875f (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package org.openslx.dozmod.gui.control;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.Location;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

/**
 * 
 * Widget to select the locations of a lecture. The UI is simply two JLists and
 * a button panel to move the elements from one list to the other. Use the
 * getters to read the values of the lists/checkbox: get
 * 
 * @author Jonathan Bauer
 * 
 */
public class LocationSelector extends JPanel {
	private final static Logger LOGGER = Logger
			.getLogger(LocationSelector.class);

	private DefaultListModel<Location> availableLocationModel = new DefaultListModel<Location>();
	private DefaultListModel<Location> selectedLocationModel = new DefaultListModel<Location>();

	private JButton addAllButton = new JButton(">>");
	private JButton addButton = new JButton(">");
	private JButton removeButton = new JButton("<");
	private JButton removeAllButton = new JButton("<<");
	private JRadioButton exclusivelyButton = new JRadioButton("Veranstaltung ausschließlich in den ausgewählten Räumen anzeigen");
	private JRadioButton prioritizedButton = new JRadioButton("Veranstaltung mit höherer Priorität in den ausgewählten Räumen anzeigen");

	/**
	 * Flag for the initialization state
	 */
	private boolean initDone = false;

	/**
	 * List of ID's of locations to set the selection to when we finished initializing
	 */
	private List<Integer> preselection;

	/**
	 * Getter for the available location model
	 * 
	 * @return the locationModel of the "available location" list
	 */
	public DefaultListModel<Location> getLocationModel() {
		return availableLocationModel;
	}

	/**
	 * Getter for the selected location model
	 * 
	 * @return the locationModel of the "selected location" list
	 */
	public DefaultListModel<Location> getSelectedLocationModel() {
		return selectedLocationModel;
	}

	/**
	 * Getter for the "show only in selection" checkbox
	 * 
	 * @return true if checked, false otherwise
	 */
	public boolean getOnlyInSelection() {
		return exclusivelyButton.isSelected();
	}

	/**
	 * Sets the selection to the locations corresponding to the given list of
	 * id's
	 * 
	 * @param list
	 *            of integers to set the locations to
	 */
	public void setSelectedLocationsAsIds(List<Integer> list) {
		if (list == null) {
			LOGGER.error("No list given!");
			return;
		}
		preselection = list;
		// if we are initialised, we need to explicitly set the selection
		if (initDone)
			setSelectionInternal(preselection);
	}

	/**
	 * Sets the list of available locations. Should be called with the list
	 * given by the server
	 * 
	 * @param list
	 *            of locations to set the available list to
	 * @return true if setting the list worked, false otherwise
	 */
	public boolean fillLocationsList(final List<Location> locations) {
		if (locations == null) {
			LOGGER.error("No locations returned from the metadata cache.");
			return false;
		}
		for (Location loc : locations) {
			if (loc != null) {
				LOGGER.debug("Adding: " + loc.getLocationName());
				availableLocationModel.addElement(loc);
			} else {
				LOGGER.error("One location returned from the server was null");
			}
		}
		return true;
	}

	/**
	 * Constructor. Initializes the grid layout, the location lists and
	 * initializes the data
	 */
	public LocationSelector() {
		GridManager grid = new GridManager(this, 3);
		LocationRenderer locationRenderer = new LocationRenderer();

		// the available list of locations
		final JList<Location> availableLocationList = new JList<Location>();
		availableLocationList.setCellRenderer(locationRenderer);
		availableLocationList.setModel(availableLocationModel);

		// the selected list of locations
		final JList<Location> selectedLocationList = new JList<Location>();
		selectedLocationList.setCellRenderer(locationRenderer);
		selectedLocationList.setModel(selectedLocationModel);

		// the listeners for lists
		selectedLocationList.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					Location selection = selectedLocationList.getSelectedValue();
					if (selection != null) {
						// LOGGER.debug("Removing: " + selection);
						if (selectedLocationModel.contains(selection)) {
							selectedLocationModel.removeElement(selection);
							if (!availableLocationModel.contains(selection)) {
								availableLocationModel.addElement(selection);
							}
							sortLists();
						}
					}
				}
			}
		});

		availableLocationList.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					Location selection = availableLocationList.getSelectedValue();
					if (selection != null) {
						// LOGGER.debug("Adding: " + selection);
						if (!selectedLocationModel.contains(selection)) {
							selectedLocationModel.addElement(selection);
							availableLocationModel.removeElement(selection);
						}
						sortLists();
					}
				}
			}
		});

		// the listeners for the button panel in the middle
		addAllButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				moveAll(availableLocationModel, selectedLocationModel);
			}
		});
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				addToSelection(availableLocationList.getSelectedValuesList());

			}
		});
		removeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				removeFromSelection(selectedLocationList.getSelectedValuesList());

			}
		});
		removeAllButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				moveAll(selectedLocationModel, availableLocationModel);
			}
		});

		// the text header
		grid.add(new QLabel("Verfügbare Räume"));
		grid.skip();
		grid.add(new QLabel("Ausgewählte Räume"));
		grid.nextRow();

		// the available location panel
		JScrollPane availableScrollPane = new JScrollPane(availableLocationList);
		availableScrollPane.setPreferredSize(Gui.getScaledDimension(200, 150));
		grid.add(availableScrollPane).fill(true, true).expand(true, true);

		// the middle button panel
		JPanel buttonPanel = new JPanel();
		GridManager buttonGrid = new GridManager(buttonPanel, 1);
		buttonGrid.add(addAllButton).fill(true, false);
		buttonGrid.nextRow();
		buttonGrid.add(addButton).fill(true, false);
		buttonGrid.nextRow();
		buttonGrid.add(removeButton).fill(true, false);
		buttonGrid.nextRow();
		buttonGrid.add(removeAllButton).fill(true, false);
		buttonGrid.nextRow();
		buttonGrid.finish(true);
		grid.add(buttonPanel).fill(false, false).expand(false, false);

		// the selection location panel
		JScrollPane selectionScrollPane = new JScrollPane(selectedLocationList);
		selectionScrollPane.setPreferredSize(Gui.getScaledDimension(200, 150));
		grid.add(selectionScrollPane).fill(true, true).expand(true, true);
		grid.nextRow();

		// the radio buttons, default is to show the lecture exclusively in the selected locations
		exclusivelyButton.setSelected(true);
		ButtonGroup group = new ButtonGroup();
		group.add(exclusivelyButton);
		group.add(prioritizedButton);
		JPanel radioPanel = new JPanel();
		radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.PAGE_AXIS));
		radioPanel.add(exclusivelyButton);
		radioPanel.add(prioritizedButton);
		grid.add(radioPanel, 3);
		grid.finish(true);

		// initialise the data
		init();
	}

	/**
	 * Async fetching of the list of the locations from the server
	 */
	public void init() {
		QuickTimer.scheduleOnce(new Task() {
			List<Location> locsList = null;

			@Override
			public void fire() {
				locsList = MetaDataCache.getLocations();
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						initDone = fillLocationsList(locsList);
						// check if preselection was set before we were done
						// initialising
						if (initDone && preselection != null) {
							setSelectionInternal(preselection);
						}
					}
				});
			}
		});
	}

	/**
	 * Moves all elements from the available to the selected list
	 * 
	 * @param model
	 *            to move from
	 * @param model
	 *            to move to
	 */
	private void moveAll(DefaultListModel<Location> from,
			DefaultListModel<Location> to) {
		for (Enumeration<Location> location = from.elements(); location
				.hasMoreElements();) {
			Location current = location.nextElement();
			to.addElement(current);
		}
		from.clear();
		sortLists();
	}

	/**
	 * Adds the given location to the selection
	 * 
	 * @param location
	 *            to add to selection
	 */
	private void addToSelection(Location location) {
		if (location == null) {
			return;
		}
		List<Location> newLocations = new ArrayList<Location>();
		newLocations.add(location);
		addToSelection(newLocations);
	}

	/**
	 * Adds the given list of locations to the selection
	 * 
	 * @param list
	 *            of locations to add to the selection
	 */
	private void addToSelection(List<Location> locations) {
		if (locations == null)
			return;
		// LOGGER.debug("AddToSelection: " + locations);
		for (Location location : locations) {
			selectedLocationModel.addElement(location);
			availableLocationModel.removeElement(location);
		}
		sortLists();
	}

	/**
	 * Removes the given list of locations from the selections
	 * 
	 * @param locations
	 *            to remove from the selection
	 */
	private void removeFromSelection(List<Location> locations) {
		if (locations == null)
			return;
		// LOGGER.debug("RemoveFromSelection: " + locations);
		for (Location location : locations) {
			selectedLocationModel.removeElement(location);
			availableLocationModel.addElement(location);
		}
		sortLists();
	}

	/**
	 * Helper to sort both lists when changes occur. TODO: doing this via model
	 * change listeners?
	 */
	private void sortLists() {
		// sort the available location list
		List<Location> list = Collections.list(availableLocationModel.elements());
		Collections.sort(list);
		availableLocationModel.clear();
		for (Location loc : list) {
			availableLocationModel.addElement(loc);
		}
		// sort the selected location list
		list = Collections.list(selectedLocationModel.elements());
		Collections.sort(list);
		selectedLocationModel.clear();
		for (Location loc : list) {
			selectedLocationModel.addElement(loc);
		}
	}

	/**
	 * Internal helper to set the selection of the list of location
	 * corresponding the the given list of ids
	 *
	 * @param list of id's to set the selected locations to
	 */
	private void setSelectionInternal(List<Integer> list) {
		if (list == null || list.isEmpty()) {
			LOGGER.error("No list given for preselection!");
			return;
		}
		LOGGER.debug("Setting to: " + preselection);
		moveAll(selectedLocationModel, availableLocationModel);

		for (Integer id : preselection) {
			LOGGER.debug("Searching: " + id);
			Location loc = MetaDataCache.getLocationById(id);
			LOGGER.debug("Found: " + loc);
			if (loc != null) {
				addToSelection(loc);
				LOGGER.debug("Added: " + loc);
			}
		}
	}
}

@SuppressWarnings("serial")
class LocationRenderer extends DefaultListCellRenderer {
	@Override
	public Component getListCellRendererComponent(JList<? extends Object> list,
			Object value, int index, boolean isSelected, boolean cellHasFocus) {
		if (value instanceof Location) {
			return super.getListCellRendererComponent(list,
					((Location) value).getLocationName(), index, isSelected,
					cellHasFocus);
		}
		return super.getListCellRendererComponent(list, value, index,
				isSelected, cellHasFocus);
	}
}