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

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollBar;

import org.apache.log4j.Logger;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.GenericNoticeWindowLayout;

/**
 * Window for showing the notice.
 */
@SuppressWarnings("serial")
public abstract class GenericNoticeWindow extends GenericNoticeWindowLayout implements UiFeedback {

	final GenericNoticeWindow me = this;

	private final static Logger LOGGER = Logger.getLogger(GenericNoticeWindow.class);
	private boolean shouldBeShown = true;

	public GenericNoticeWindow(Frame modalParent, boolean shouldBeShown) {
		super(modalParent);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		this.shouldBeShown = shouldBeShown;

		// agree box toggles the "Continue" button
		chkAgreeBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnContinue.setEnabled(chkAgreeBox.isSelected());
			}
		});

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				closeWindow();
			}
		});
		if (!shouldBeShown) {
			chkAgreeBox.setVisible(false);
			btnContinue.setText("Schließen");
			btnContinue.setEnabled(true);
		} else {
			disclaimerPanel.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
				@Override
				public void adjustmentValueChanged(AdjustmentEvent e) {
					JScrollBar scrollBar = (JScrollBar) e.getAdjustable();
					int extent = scrollBar.getModel().getExtent();

					int value = e.getValue() + extent;
					int max = e.getAdjustable().getMaximum();

					if (value >= max) {
						chkAgreeBox.setEnabled(true);
					}
				}
			});
		}
	}

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

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

	/**
	 * Check, whether to ask for confirmation, when notice hasn't been
	 * accepted or just close.
	 */
	private void closeWindow() {
		if (shouldBeShown) {
			if (Gui.showMessageBox(me,
					"Wenn diesen rechtlichen Hinweis nicht akzeptieren, können Sie die Software nicht verwenden! "
							+ "Sind Sie sicher, dass sie abbrechen wollen?", MessageType.QUESTION_YESNO,
					LOGGER, null)) {
				System.exit(ABORT);
			}
		} else {
			me.dispose();
		}
	}

}