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

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.NetRule;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.Gui.GuiCallable;
import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.LectureNetrulesWindowLayout;

public class LectureNetrulesWindow extends LectureNetrulesWindowLayout
		implements UiFeedback {

	private static final long serialVersionUID = -1970717955867180231L;
	private static final Logger LOGGER = Logger
			.getLogger(LectureNetrulesWindow.class);
	private boolean apply = false;
	private List<NetRule> newRules = null;

	public LectureNetrulesWindow(Window modalParent,
			List<NetRule> netrules, DialogChangeMonitor changeMonitor) {
		super(modalParent);

		btnSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				newRules = ctlNetrulesConfigurator.getState(false);
				if (newRules == null) {
					return;
				}
				apply = true;
				dispose();
			}
		});
		// listeners for the buttons
		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				safeClose();
			}
		});
		ctlNetrulesConfigurator.setState(netrules);
		changeMonitor.add(this).reset();
	}

	/**
	 * @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 List<NetRule> runAndReturn() {
		setVisible(true);
		if (!apply)
			return null;
		return newRules != null ? newRules : new ArrayList<NetRule>();
	}

	/**
	 * Static method to open this window with
	 * 
	 * @param modalParent
	 *            parent to this window
	 * @param changeMonitor If you want to track changes...
	 * @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 List<NetRule> open(final Window modalParent,
			final List<NetRule> netrules, final DialogChangeMonitor changeMonitor) {
		return Gui.syncExec(new GuiCallable<List<NetRule>>() {
			@Override
			public List<NetRule> run() {
				return new LectureNetrulesWindow(modalParent, netrules, changeMonitor)
						.runAndReturn();
			}
		});
	}

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

	@Override
	public void escapePressed() {
		safeClose();
	}
	private void safeClose() {
		if (newRules != null) {
			if (Gui.showMessageBox("Änderungen werden verworfen. Wollen Sie wirklich abbrechen?", MessageType.ERROR_RETRY, LOGGER, null)) {
				dispose();						
			}
		} else {
			dispose();
		}
	}

	@Override
	public List<NetRule> getState() {
		return ctlNetrulesConfigurator.getState(true);
	}

	@Override
	public void addChangeListener(final ChangeListener l) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				l.stateChanged(null);
			}
		});
	}

}