summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LocationSelectionWindow.java
blob: bf818cf5554b62442f903252c72bcfabe99fe7f0 (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
package org.openslx.dozmod.gui.window;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.Gui.GuiCallable;
import org.openslx.dozmod.gui.control.JCheckBoxTree.CheckChangeEvent;
import org.openslx.dozmod.gui.control.JCheckBoxTree.CheckChangeEventListener;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.LocationSelectionWindowLayout;
import org.openslx.dozmod.thrift.Session;

/**
 * Minimal window containing a LocationSelector widget
 * 
 * @author Jonathan Bauer
 * 
 */
public class LocationSelectionWindow extends LocationSelectionWindowLayout implements UiFeedback {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5898656786160024212L;
	private boolean apply = false;

	/**
	 * @param modalParent
	 *            parent to this window
	 * @param list
	 *            of locations to preselect in the LocationSelector
	 */
	public LocationSelectionWindow(Window modalParent, List<Integer> locations, boolean limitToLocations) {
		super(modalParent, locations, limitToLocations);

		ctlLocationSelector.setSelectedLocationsAsIds(locations);
		ctlLocationSelector.setOnlyInSelection(limitToLocations);

		// listeners for the buttons
		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO safe close? (unsaved changes -> user must confirm)
				dispose();
			}
		});
		// disable "Save" button at first since nothing changed
		btnSaveChanges.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				List<Integer> tempIntList = ctlLocationSelector.getSelectedLocationsAsIds();
				if (tempIntList != null
						&& tempIntList.size() > Session.getSatelliteConfig().maxLocationsPerLecture) {
					ctlLocationSelector.setSelectedLocationsAsIds(tempIntList);
					Gui.showMessageBox("Bitten reduzieren Sie die Anzahl gewählter Orte",
							MessageType.WARNING, null, null);
					return;
				}
				apply = true;
				dispose();
			}
		});

		// addCheckChangeEventListener
		ctlLocationSelector.addCheckChangeEventListener(new CheckChangeEventListener() {
			@Override
			public void checkStateChanged(CheckChangeEvent event) {
				List<Integer> tempIntList = ctlLocationSelector.getSelectedLocationsAsIds();
				if (tempIntList != null) {
					if (tempIntList.size() > Session.getSatelliteConfig().maxLocationsPerLecture) {
						// add error
						lblError.setText("Zu viele Orte ausgewählt!");
					} else {
						lblError.setText("");
					}
				}
			}
		});

		Gui.centerShellOverShell(modalParent, this);
	}

	/**
	 * @return list of ids of the selected locations. Will be either a new list
	 *         if the user clicked apply or the list received from the sat.
	 */
	public LocationInfo runAndReturn() {
		setVisible(true);
		if (!apply)
			return null;
		return new LocationInfo(ctlLocationSelector.getSelectedLocationsAsIds(),
				ctlLocationSelector.getOnlyInSelection());
	}

	/**
	 * Static method to open this window with
	 * 
	 * @param modalParent
	 *            parent to this window
	 * @param locations
	 *            locations to preselect in the LocationSelector
	 * @return forwards the return value of the window itself: list of ids of
	 *         the selected locations
	 */
	public static LocationInfo open(final Window modalParent, final List<Integer> locations,
			final boolean limitToLocations) {
		return Gui.syncExec(new GuiCallable<LocationInfo>() {
			@Override
			public LocationInfo run() {
				return new LocationSelectionWindow(modalParent, locations, limitToLocations).runAndReturn();
			}
		});
	}

	@Override
	public boolean wantConfirmQuit() {
		return false;
	}

	@Override
	public void escapePressed() {
		dispose();
	}
}

class LocationInfo {
	public List<Integer> locationList;
	public boolean limitToLocations;

	public LocationInfo(List<Integer> locs, boolean limited) {
		this.locationList = locs;
		this.limitToLocations = limited;
	}
}