summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureListWindow.java
blob: ce455b5a5f05a8d109e38d67d09b1b94dbbaa681 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package org.openslx.dozmod.gui.window;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.PopupMenu;
import org.openslx.dozmod.gui.window.LectureDetailsWindow.LectureUpdatedCallback;
import org.openslx.dozmod.gui.window.layout.LectureListWindowLayout;
import org.openslx.dozmod.gui.wizard.LectureWizard;
import org.openslx.dozmod.permissions.LecturePerms;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftActions;
import org.openslx.dozmod.thrift.ThriftActions.DeleteLectureCallback;
import org.openslx.dozmod.thrift.cache.LectureCache;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

@SuppressWarnings("serial")
public class LectureListWindow extends LectureListWindowLayout {

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

	public final LectureListWindow me = this;

	/**
	 * Popup menu items
	 */
	private JMenuItem popupItemNew = new JMenuItem("Neu");
	private JMenuItem popupItemEdit = new JMenuItem("Bearbeiten");
	private JMenuItem popupItemDelete = new JMenuItem("Löschen");

	public LectureListWindow() {
		super();

		// filter the objects in the table depending on the search field
		searchTextField.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				changedUpdate(e);
			}
			@Override
			public void insertUpdate(DocumentEvent e) {
				changedUpdate(e);
			}
			@Override
			public void changedUpdate(DocumentEvent e) {
				// stuff
				applyFilterOnTable();
			}
		});

		filterCbo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				applyFilterOnTable();
			}
		});

		newButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new LectureWizard(SwingUtilities.getWindowAncestor(me), null, null).setVisible(true);
			}
		});

		editButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				openLectureDetails(lectureTable.getSelectedItem());
			}
		});

		// delete lecture
		deleteButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteLecture(lectureTable.getSelectedItem());
			}
		});

		// return to mainMenu
		switchViewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainWindow.showPage(ImageListWindow.class);
			}
		});

		/**
		 * Popup menu for the version table on the right side
		 */
		final PopupMenu pop = new PopupMenu(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LectureSummary lecture = lectureTable.getSelectedItem();
				if (lecture == null)
					return;
				if (e.getSource().equals(popupItemNew)) {
					if (Gui.showMessageBox(me, "Um eine Veranstaltung zu erstellen, müssen Sie ein Image auswählen. Zur Imageauswahl wechseln?",
							MessageType.QUESTION_YESNO, LOGGER, null)) {
						MainWindow.showPage(ImageListWindow.class);
					}
				}
				if (e.getSource().equals(popupItemEdit)) {
					openLectureDetails(lecture);
				}
				if (e.getSource().equals(popupItemDelete)) {
					deleteLecture(lecture);
				}

			}
		});
		pop.addMenuItem(popupItemNew);
		pop.addMenuItem(popupItemEdit);
		pop.addSeparator();
		pop.addMenuItem(popupItemDelete);

		// finally the table listeners
		lectureTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
			@Override
			public void mousePressed(MouseEvent e) {
				processClick(e);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				processClick(e);
			}
			private void processClick(MouseEvent e) {
				// left double click => open details
				if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
					openLectureDetails(lectureTable.getSelectedItem());
					return;
				}
				// else, check if we are a popup trigger
				int r = lectureTable.rowAtPoint(e.getPoint());
				if (r >= 0 && r < lectureTable.getRowCount()) {
					// highlight the row and popup the menu
					lectureTable.setRowSelectionInterval(r, r);
					if (e.isPopupTrigger()) {
						pop.show(e.getComponent(), e.getX(), e.getY());
					}
				} else {
					lectureTable.clearSelection();
				}
			}
		});

		lectureTable.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_F5) {
					refreshList(true);
				}
			}
		});

		lectureTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				LectureSummary item = lectureTable.getSelectedItem();
				updateAvailableOptions(item);
			}
		});
		updateAvailableOptions(null);
	}
	/**
	 * Updates the buttons/popup menu items according to the user's permissions
	 * 
	 * @param item the image to check the user's permissions for
	 */
	private void updateAvailableOptions(LectureSummary lecture) {
		boolean edit = LecturePerms.canEdit(lecture);
		boolean admin = LecturePerms.canAdmin(lecture);
		editButton.setEnabled(edit);
		deleteButton.setEnabled(admin);
		popupItemEdit.setEnabled(edit);
		popupItemDelete.setEnabled(admin);
	}
	/**
	 * Opens the given lecture's details as a popup window
	 */
	private void openLectureDetails(final LectureSummary lecture) {
		if (lecture == null)
			return;
		LectureDetailsWindow.open((JFrame)SwingUtilities.getWindowAncestor(me), lecture.getLectureId(), new LectureUpdatedCallback() {
			@Override
			public void updated(boolean success) {
				refreshList(success);
			}
		});
	}

	/**
	 * Deletes the given lecture
	 */
	private void deleteLecture(final LectureSummary lecture) {
		if (lecture == null)
			return;
		if (!Gui.showMessageBox(this, "Wollen Sie diese Veranstaltung wirklich löschen?", MessageType.QUESTION_YESNO, LOGGER, null))
			return;
		ThriftActions.deleteLecture(JOptionPane.getFrameForComponent(me),
				lecture.getLectureId(), new DeleteLectureCallback() {
					@Override
					public void deleted(boolean success) {
						refreshList(success);
					}
				});
	}
	/**
	 * Applies the filter entered in the search field to the table
	 */
	private void applyFilterOnTable() {
		try {
			// List for filters
			List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(3);

			// string for text in filter text field
			String filterField = searchTextField.getText();
			filters.add(RowFilter.regexFilter("(?i)" + filterField, 0, 1, 2));
			// filter for user (only show own)
			switch (filterCbo.getSelectedIndex()) {
			case 0:
				// no additional filters
				break;
			case 1:
				// filter for owner
				String userFilter = Session.getUserId();
				filters.add(RowFilter.regexFilter("(?i)" + userFilter, 1));
				break;
			case 2:
				// filter for editable
				RowFilter<Object, Object> editFilter = new RowFilter<Object, Object>() {
					public boolean include(Entry entry) {
						return lectureTable.getModelRow((Integer) entry.getIdentifier()).getDefaultPermissions().edit;
					}
				};
				filters.add(editFilter);
				break;
			case 3:
				// filter for soon ending lectures
				RowFilter<Object, Object> expireFilter = new RowFilter<Object, Object>() {
					public boolean include(Entry entry) {
						long endTime = lectureTable.getModelRow((Integer) entry.getIdentifier()).getEndTime() * 1000l;
						Calendar cal = Calendar.getInstance();
						cal.setTime(new Date());
						cal.add(Calendar.DATE, 7);
						return cal.getTimeInMillis() > endTime ? true: false;
					}
				};
				filters.add(expireFilter);
				break;
			default:
				break;
			}
			// for using multiple filters
			RowFilter<Object, Object> andFilters = RowFilter.andFilter(filters);
			lectureTable.getRowSorter().setRowFilter(andFilters);

			searchTextField.setForeground(UIManager.getColor("TextField.foreground"));
		} catch (IllegalArgumentException ex) {
			// TODO set background color of search field to something redish
			searchTextField.setForeground(Color.RED);
		}
	}

	private void refreshList(final boolean force) {
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				final List<LectureSummary> lectureList = LectureCache.get(force);
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						lectureTable.setData(lectureList, true);
					}
				});
			}
		});
	}

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

	@Override
	public void requestShow() {
		refreshList(false);
	}

}