summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RoomSelector.java
blob: ae1afc5e4202b4210590f0f3ac09ea1a2e200cfe (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
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.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
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;

public class RoomSelector extends QLabel {
	private final static Logger LOGGER = Logger.getLogger(RoomSelector.class);

	private DefaultListModel<Location> roomModel = new DefaultListModel<Location>();
	private DefaultListModel<Location> selectedRoomModel = new DefaultListModel<Location>();

	private JButton addAllButton = new JButton(">>");
	private JButton addButton = new JButton(">");
	private JButton removeButton = new JButton("<");
	private JButton removeAllButton = new JButton("<<");

	private boolean initDone = false;
	private List<Integer> preselection;

	public RoomSelector() {
		GridManager grid = new GridManager(this, 3);
		LocationRenderer locationRenderer = new LocationRenderer();

		// the available list of rooms
		final JList<Location> roomList = new JList<Location>();
		roomList.setCellRenderer(locationRenderer);
		roomList.setModel(roomModel);

		// the selected list of rooms
		final JList<Location> selectedRoomList = new JList<Location>();
		selectedRoomList.setCellRenderer(locationRenderer);
		selectedRoomList.setModel(selectedRoomModel);

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

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

		addAllButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				moveAll(roomModel, selectedRoomModel);
			}
		});
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				addToSelection(roomList.getSelectedValuesList());

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

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

		grid.add(new QLabel("Verfügbare Räume"));
		grid.skip();
		grid.add(new QLabel("Ausgewählte Räume"));
		grid.nextRow();
		grid.add(new JScrollPane(roomList)).fill(true, true).expand(true, true);
		// Vertical BoxLayout for buttons
		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);
		grid.add(new JScrollPane(selectedRoomList)).fill(true, true)
				.expand(true, true);
		grid.nextRow();
		grid.finish(true);

		// initialise the data
		init();
	}

	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 = fillRoomList(locsList);
						// check if preselection was set before we were done initialising
						if (initDone && preselection != null) {
							setSelectionInternal(preselection);
						}
					}
				});
			}
		});
	}

	public boolean fillRoomList(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());
				roomModel.addElement(loc);
			} else {
				LOGGER.error("One location returned from the server was null");
			}
		}
		return true;
	}

	private void moveAll(DefaultListModel<Location> from,
			DefaultListModel<Location> to) {
		for (Enumeration<Location> rooms = from.elements(); rooms
				.hasMoreElements();) {
			Location current = rooms.nextElement();
			to.addElement(current);
		}
		from.clear();
		sortLists();
	}

	// convenience
	private void addToSelection(Location location) {
		if (location == null) {
			return;
		}
		List<Location> newLocations = new ArrayList<Location>();
		newLocations.add(location);
		addToSelection(newLocations);
	}

	private void addToSelection(List<Location> locations) {
		if (locations == null)
			return;
		// LOGGER.debug("AddToSelection: " + locations);
		for (Location location : locations) {
			selectedRoomModel.addElement(location);
			roomModel.removeElement(location);
		}
		sortLists();
	}

	private void removeFromSelection(List<Location> locations) {
		if (locations == null)
			return;
		// LOGGER.debug("RemoveFromSelection: " + locations);
		for (Location location : locations) {
			selectedRoomModel.removeElement(location);
			roomModel.addElement(location);
		}
		sortLists();
	}

	private void sortLists() {
		// sort the available room list
		List<Location> list = Collections.list(roomModel.elements());
		Collections.sort(list);
		roomModel.clear();
		for (Location loc : list) {
			roomModel.addElement(loc);
		}
		// sort the selected room list
		list = Collections.list(selectedRoomModel.elements());
		Collections.sort(list);
		selectedRoomModel.clear();
		for (Location loc : list) {
			selectedRoomModel.addElement(loc);
		}
	}

	public DefaultListModel<Location> getRoomModel() {
		return roomModel;
	}

	public DefaultListModel<Location> getSelectedRoomModel() {
		return selectedRoomModel;
	}

	public void setSelectedRoomsAsIds(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);
	}
	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(selectedRoomModel, roomModel);

		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);
	}
}