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

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

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.window.layout.LectureListWindowLayout;
import org.openslx.dozmod.thrift.LectureCache;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.UserCache;
import org.openslx.dozmod.util.FormatHelper;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

public class LectureListWindow extends LectureListWindowLayout {

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

	public final LectureListWindow me = this;

	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();
			}
		});


		lectureTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					LectureSummary lecture = lectureTable.getSelectedItem();
					if (lecture == null)
						return;
					LectureDetailsWindow.open((JFrame)SwingUtilities.getWindowAncestor(me), lecture.getLectureId());
				}
			}
		});


		lectureTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					final LectureSummary lecture = lectureTable.getSelectedItem();
					if (lecture == null)
						return;
					/* TODO for lecture
					ImageDetailsWindow popup = new ImageDetailsWindow(SwingUtilities.windowForComponent(me));
					if (popup != null)
						popup.setImage(image.getImageBaseId());
					 */
				}
			}
		});

		newButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				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);
				}
			}
		});

		editButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			}
		});

		// delete lecture
		deleteButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			}
		});

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

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

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

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

	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 = FormatHelper.userName(UserCache.find(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);
		}
	}

}