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

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.io.IOException;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Action;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;

import org.apache.log4j.Logger;
import org.openslx.dozmod.gui.helper.DateTimeHelper;
import org.openslx.dozmod.gui.helper.TextChangeListener;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.LectureCreationPageLayout;
import org.openslx.dozmod.state.LectureWizardState;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.util.FormatHelper;

/**
 * First page for creating a new lecture.
 * Used to set name, description and start- and endtime
 */
@SuppressWarnings("serial")
public class LectureCreationPage extends LectureCreationPageLayout {

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

	private LectureWizardState state = null;

	/**
	 * Page for creating lectures
	 */
	public LectureCreationPage(Wizard wizard, LectureWizardState state) {
		super(wizard, state.image != null ? state.image.imageName : "Neu");
		this.state = state;

		// listener for the text fields
		final TextChangeListener docListener = new TextChangeListener() {
			@Override
			public void changed() {
				reactToUserInput();
			}
		};

		txtName.getDocument().addDocumentListener(docListener);
		txtDescription.getDocument().addDocumentListener(docListener);

		// listeners for the date pickers
		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				reactToUserInput();
			}
		};
		// listeners for the time pickers
		final ChangeListener changeListener = new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				reactToUserInput();
			}
		};
		spnStartTime.addChangeListener(changeListener);
		spnEndTime.addChangeListener(changeListener);
		dtpStartDate.addActionListener(actionListener);
		dtpEndDate.addActionListener(actionListener);
		calculateDatePeriod();

		cbTxtSize.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int size = Integer.parseInt((String) cbTxtSize.getSelectedItem());
				Action act = new StyledEditorKit.FontSizeAction(String.valueOf(size),size);
				act.actionPerformed(new ActionEvent(act,ActionEvent.ACTION_PERFORMED, 
					(String)act.getValue(Action.ACTION_COMMAND_KEY)));
			}
		});

		cbTxtColor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String color = (String) cbTxtColor.getSelectedItem();
				Action act = null;

				switch(color) {
					case "Black":
						act = new StyledEditorKit.ForegroundAction("Black", Color.black);
						break;
					case "Blue":
						act = new StyledEditorKit.ForegroundAction("Blue", Color.blue);
						break;
					case "Yellow":
						act = new StyledEditorKit.ForegroundAction("Yellow", Color.yellow);
						break;
					case "Red":
						act = new StyledEditorKit.ForegroundAction("Red", Color.red);
						break;
					case "Green":
						act = new StyledEditorKit.ForegroundAction("Green", Color.green);
						break;
				}

				act.actionPerformed(new ActionEvent(act,ActionEvent.ACTION_PERFORMED,
						(String)act.getValue(Action.ACTION_COMMAND_KEY)));
			}
		});


		btnWysiwyg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String tmp = txtDescription.getText();
				if (txtDescription.getContentType().equals("text/html")) {
					txtDescription.setContentType("text/plain");
					txtDescription.setText(tmp);
					btnWysiwyg.setText("Wysiwyg");

					btnBold.setEnabled(false);
					btnUnderline.setEnabled(false);
					btnItalic.setEnabled(false);
					cbTxtColor.setEnabled(false);
					cbTxtSize.setEnabled(false);
				} else {
					txtDescription.setContentType("text/html");
					txtDescription.setText(tmp);
					btnWysiwyg.setText("Html");

					btnBold.setEnabled(true);
					btnUnderline.setEnabled(true);
					btnItalic.setEnabled(true);
					cbTxtColor.setEnabled(true);
					cbTxtSize.setEnabled(true);
				}
			}
		});

		txtDescription.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
			}

			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER && txtDescription.getContentType().equals("text/html")) {
					try {
						kit.insertHTML((HTMLDocument) txtDescription.getDocument(), txtDescription.getCaretPosition(),
								"<br>", 0, 0, HTML.Tag.BR);
						txtDescription.setCaretPosition(txtDescription.getCaretPosition()); // This moves caret to next
																							// line
					} catch (BadLocationException | IOException ex) {
						ex.printStackTrace();
					}
				}
			}
		});
	}

	

	@Override
	protected boolean wantNextOrFinish() {
		return reactToUserInput();
	}

	/**
	 * 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 boolean reactToUserInput() {
		boolean b = isPageValid();
		calculateDatePeriod();
		setPageComplete(b);
		return b;
	}

	/**
	 * Calculate the days between the selected dates and update the
	 * corresponding label.
	 */
	private void calculateDatePeriod() {
		Date start = DateTimeHelper.getDateFrom(dtpStartDate, spnStartTime);
		Date end = DateTimeHelper.getDateFrom(dtpEndDate, spnEndTime);
		if (end.before(start)) {
			lblCalcPeriod.setText("Endzeitpunkt ist vor Startzeitpunkt!");
		} else {
			int numberOfDays = DateTimeHelper.calculatePeriodInDays(start, end);
			lblCalcPeriod.setText(numberOfDays + "Tag(e)");
			lblCalcPeriod.setForeground(numberOfDays < 7 ? Color.RED : null);
		}
	}

	private boolean isPageValid() {
		if (txtName.getText().isEmpty()) {
			setWarningMessage("Geben Sie einen Veranstaltungsnamen ein.");
			return false;
		} else {
			state.name = txtName.getText();
		}
		if (txtDescription.getText().isEmpty()) {
			setWarningMessage("Fügen Sie eine Beschreibung hinzu.");
			return false;
		} else {
			state.description = txtDescription.getText();
		}
		final Date now = new Date();
		final Date start = DateTimeHelper.getDateFrom(dtpStartDate, spnStartTime);
		final Date end = DateTimeHelper.getDateFrom(dtpEndDate, spnEndTime);

		if (start.after(end)) {
			setWarningMessage("Startzeit ist nach Endzeit!");
			return false;
		} else if (now.after(end)) {
			setWarningMessage("Endzeit liegt in die Vergangenheit!");
			return false;
		} else {
			int validityPeriod = Session.getSatelliteConfig().getMaxLectureValidityDays();
			Date validityPeriodEnd = DateTimeHelper.addDaysTo(start, validityPeriod); // all good, save them both
			if (end.after(validityPeriodEnd)) {
				setWarningMessage("Endzeit liegt nach dem spätest möglichen Datum: "
						+ FormatHelper.shortDate(validityPeriodEnd.getTime() / 1000L));
				return false;
			} else {
				state.start = start;
				state.end = end;
			}
		}
		String nextText;
		nextText = state.image == null ? ", um eine Virtuelle Maschine auszuwählen"
				: " für Berechtigungen oder 'Fertigstellen'.";
		setDescription("Klicken Sie auf 'Weiter' " + nextText);
		return true;
	}
}