summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureCreationPage.java
blob: e9eeb3bf168d306fb0be8fbf69b415d2ae2c8abb (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
package org.openslx.dozmod.gui.wizard.page;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.apache.log4j.Logger;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.LectureCreationPageLayout;
import org.openslx.dozmod.state.LectureWizardState;

@SuppressWarnings("serial")
public class LectureCreationPage extends LectureCreationPageLayout {

	private final static Logger LOGGER = Logger.getLogger(LectureCreationPage.class);

	private LectureWizardState state = null;

	/**
	 * Page for creating lectures
	 * 
	 * @param editExistingLecture whether to edit existing lecture or create new
	 *            one
	 */
	public LectureCreationPage(Wizard wizard, LectureWizardState state) {
		super(wizard);
		this.state = state;
		
		// listener for the text fields
		final DocumentListener docListener = new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				changedUpdate(e);
			}
			@Override
			public void insertUpdate(DocumentEvent e) {
				changedUpdate(e);
			}
			@Override
			public void changedUpdate(DocumentEvent e) {
				reactToUserInput();
			}
		};
		
		lectureNameTextField.getDocument().addDocumentListener(docListener);
		descriptionText.getDocument().addDocumentListener(docListener);

		// listeners for the date pickers
		ActionListener dateListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				reactToUserInput();
			}
		};
		// listeners for the time pickers
		ChangeListener timeListener = new ChangeListener() {	
			@Override
			public void stateChanged(ChangeEvent e) {
				reactToUserInput();
			}
		};
		startTime.addChangeListener(timeListener);
		endTime.addChangeListener(timeListener);
		startDate.addActionListener(dateListener);
		endDate.addActionListener(dateListener);
	}
	/**
	 * Called by event listeners. This will set guidance message or error
	 * message and call setPageComplete(bool) accordingly.
	 * The state will be updated if the fields are valid.
	 */
	private void reactToUserInput() {
		if (lectureNameTextField.getText().isEmpty()) {
			setWarningMessage("Geben Sie einen Veranstaltungsnamen ein.");
			setPageComplete(false);
			return;
		}  else {
			state.name = lectureNameTextField.getText();
		}
		if (descriptionText.getText().isEmpty()) {
			setWarningMessage("Fügen Sie eine Beschreibung hinzu.");
			setPageComplete(false);
			return;
		} else {
			state.description = descriptionText.getText();
		}
		Date now = new Date();
		Date start = getStartDate();
		Date end = getEndDate();
		if (start.after(end)) {
			setWarningMessage("Startzeit is nach Endzeit!");
			setPageComplete(false);
			return;				
		} else if (now.after(end)) { 
			setWarningMessage("Endzeit liegt in die Vergangenheit!");
			setPageComplete(false);
			return;	
		} else { // all good, save them both
			state.start = start;
			state.end = end;
		}
		setWarningMessage("Sie können jetzt fortfahren.");
		setPageComplete(true);
	}

	/**
	 * @return the Date object representing the user's input for the start date
	 */
	private Date getStartDate() {
		// start date from the DatePicker
		int years = startDate.getModel().getYear();
		int months = startDate.getModel().getMonth() + 1;
		int days = startDate.getModel().getDay();
		// start time from the Spinner
		Date starttime = (Date) startTime.getValue();
		Calendar cal = Calendar.getInstance();
		cal.setTime(starttime);
		int hours = cal.get(Calendar.HOUR_OF_DAY);
		int minutes = cal.get(Calendar.MINUTE);
		// build the time from the single values
		DateFormat format = new SimpleDateFormat("dd-MM-yyyy kk:mm");
		Date date = null;
		try {
			date = format.parse(days + "-" + months + "-" + years + " " + hours + ":" + minutes);
		} catch (ParseException e) {
			LOGGER.error("Parsing error while converting date: ", e);
		}
		return date;
	}
	/**
	 * @return the Date object representing the user's input for the end date
	 */
	private Date getEndDate() {
		// end date from the DatePicker
		int years = endDate.getModel().getYear();
		int months = endDate.getModel().getMonth() + 1;
		int days = endDate.getModel().getDay();
		// end time from the Spinner
		Date endtime = (Date) endTime.getValue();
		Calendar cal = Calendar.getInstance();
		cal.setTime(endtime);
		int hours = cal.get(Calendar.HOUR_OF_DAY);
		int minutes = cal.get(Calendar.MINUTE);
		// build the time from the single values
		DateFormat format = new SimpleDateFormat("dd-MM-yyyy kk:mm");
		Date date = null;
		try {
			date = format.parse(days + "-" + months + "-" + years + " " + hours + ":" + minutes);
		} catch (ParseException e) {
			LOGGER.error("Parsing error while converting date: ", e);
		}
		return date;
	}
}