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

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImageVersionDetails;
import org.openslx.bwlp.thrift.iface.LectureRead;
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.LectureDetailsWindowLayout;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.UserCache;
import org.openslx.dozmod.util.FormatHelper;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

@SuppressWarnings("serial")
public class LectureDetailsWindow extends LectureDetailsWindowLayout {

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

	private final LectureDetailsWindow me = this;

	private LectureRead lecture = null;
	private ImageDetailsRead image = null;

	public LectureDetailsWindow(Frame modalParent) {
		super(modalParent);

		// Close button closes window
		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		// ESC closes this window
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					dispose();
				}
			}
		});
		setFocusable(true);

		btnAutoUpdate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				cboVersions.setEnabled(!btnAutoUpdate.isSelected());
			}
		});
	}

	/**
	 * @param lectureId the id of the lecture to be displayed
	 */
	public void setLecture(final String lectureId) {
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				Exception error = null;
				try {
					synchronized (me) {
						if (lecture != null)
							return;
						lecture = ThriftManager.getSatClient().getLectureDetails(Session.getSatelliteToken(), lectureId);
					}
				} catch (Exception e) {
					error = e;
				}
				// getting imageDetailsRead for the versions
				if (lecture != null) {
					try {
						synchronized (me) {
							if (image != null)
								return;
							image = ThriftManager.getSatClient().getImageDetails(Session.getSatelliteToken(), lecture.getImage().getImageBaseId());
						}
					} catch (Exception e) {

						error = e;
					}
				}
				final Exception e = error;
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						if (e != null || lecture == null || image == null) {
							Gui.showMessageBox(null, "Konnte Daten der Vorlesung nicht abrufen",
									MessageType.ERROR, LOGGER, e);
							dispose();
						} else {
							fill();
						}
					}
				});
			}
		});
	}

	/**
	 * callback function when we received the lecture's details from the server
	 */
	private void fill() {
		if (lecture == null || image == null)
			return;
		txtTitle.setText(lecture.getLectureName());
		txtDescription.setText(lecture.getDescription());
		txtImageName.setText(image.getImageName());
		lblOwner.setUser(UserCache.find(lecture.getOwnerId()));
		lblUpdater.setUser(UserCache.find(lecture.getUpdaterId()));
		lblCreateTime.setText(FormatHelper.longDate(lecture.getCreateTime()));
		lblUpdateTime.setText(FormatHelper.longDate(lecture.getUpdateTime()));
		lblStartTime.setText(FormatHelper.longDate(lecture.getStartTime()));
		lblEndTime.setText(FormatHelper.longDate(lecture.getEndTime()));
		txtId.setText(lecture.getLectureId());

		btnIsExam.setSelected(lecture.isExam);
		btnAutoUpdate.setSelected(lecture.autoUpdate);
		cboVersions.setEnabled(!lecture.autoUpdate);

		lblUseCount.setText(Integer.toString(lecture.useCount));

		// version combo
		List<ImageVersionDetails> versions = image.getVersions();
		cboVersions.setModel(new DefaultComboBoxModel<ImageVersionDetails>(versions.toArray(new ImageVersionDetails[versions.size()])));
		cboVersions.setRenderer(new DefaultListCellRenderer() {
			@Override
			public Component getListCellRendererComponent(JList<?> list, Object value, int index,
					boolean isSelected, boolean cellHasFocus) {
				super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
				if (value instanceof ImageVersionDetails) {
					ImageVersionDetails version = (ImageVersionDetails) value;
					setText(FormatHelper.longDate(version.getCreateTime()));
				}
				return this;
			}
		});


		// TODO grey out non editable components, lecture image has no permissions yet
		//makeEditable(LecturePerms.canEdit(lecture));
		pack();
		MainWindow.centerShell(this);
		setVisible(true);
	}

	/**
	 * Enables/disables the editable fields based on 'editable'
	 * 
	 * @param editable true to make fields editable, false otherwise.
	 */
	private void makeEditable(boolean editable) {
		txtTitle.setEnabled(editable);
		txtDescription.setEditable(editable);
		txtId.setEditable(editable);
		
		// TODO functionality for changing dates still missing 
		//	lblCreateTime.setText(FormatHelper.longDate(lecture.getCreateTime()));
		//	lblUpdateTime.setText(FormatHelper.longDate(lecture.getUpdateTime()));
		//	lblStartTime.setText(FormatHelper.longDate(lecture.getStartTime()));
		//	lblEndTime.setText(FormatHelper.longDate(lecture.getEndTime()));

		btnLinkImage.setEnabled(editable);
		btnIsExam.setEnabled(editable);
		btnAutoUpdate.setEnabled(editable);
		cboVersions.setEnabled(editable);
		btnSaveChanges.setEnabled(editable);
	}

	/**
	 * Opens a new LectureDetailsWindow showing the details of the
	 * lecture with ID = lectureId
	 * 
	 * @param modalParent parent of this window
	 * @param lectureId id of the lecture to set the details of
	 */
	public static void open(Frame modalParent, String lectureId) {
		LectureDetailsWindow win = new LectureDetailsWindow(modalParent);
		win.setLecture(lectureId);
	}
}