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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.openslx.dozmod.App;
import org.openslx.dozmod.Config;
import org.openslx.dozmod.gui.Gui.GuiCallable;
import org.openslx.dozmod.gui.helper.CompositePage;
import org.openslx.dozmod.gui.helper.DebugWindow;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.window.DisclaimerWindow;
import org.openslx.dozmod.gui.window.ImageListWindow;
import org.openslx.dozmod.gui.window.LectureListWindow;
import org.openslx.dozmod.gui.window.LoginWindow;
import org.openslx.dozmod.gui.window.MainMenuWindow;
import org.openslx.dozmod.gui.window.VirtualizerNoticeWindow;
import org.openslx.dozmod.thrift.Session;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.thrifthelper.ThriftManager.ErrorCallback;

public abstract class MainWindow {

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

	private static final JFrame mainWindow;
	private static final JPanel mainContainer = new JPanel();

	private static CompositePage currentPage;

	private static boolean isQuitQuestionOpen = false;

	private static final Map<Class<? extends CompositePage>, CompositePage> pages = new ConcurrentHashMap<>();

	private static final String THRIFT_CONNECTION_ERROR = "Lost connection to the masterserver. Do you want to retry?";

	/**
	 * Set the visible page of the main window.
	 * 
	 * @param clazz
	 */
	public static void showPage(Class<? extends CompositePage> clazz) {
		if (currentPage != null) {
			if (!currentPage.requestHide()) {
				return; // Canceled by currently shown page
			}
			currentPage.setVisible(false);
		}

		currentPage = pages.get(clazz);
		if (currentPage == null) {
			Gui.showMessageBox(mainWindow, "Tried to show unknown page " + clazz.getSimpleName(),
					MessageType.ERROR, LOGGER, null);
			Gui.exit(1);
			return;
		}

		// sets the starting preferred size.
		currentPage.requestShow();
		currentPage.setVisible(true);
		mainWindow.validate();
	}

	public static void centerShell(Window shell) {
		Gui.centerShellOverShell(mainWindow, shell);
	}

	static {
		JFrame ret = Gui.syncExec(new GuiCallable<JFrame>() {
			@Override
			public JFrame run() {
				return new JFrame("bwLehrstuhl");
			}
		});
		mainWindow = ret;
	}

	/**
	 * Initializes the GUI by creating the main window, adding the menu and
	 * creating the login mask as the first content window.
	 * Further sets up the global thrift error callback to catch any
	 * connection errors during the communication with the servers.
	 */

	public static void open() {
		// init SWT stuff
		mainWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		// Catch the close button (X)
		mainWindow.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				MainWindow.askApplicationQuit();
			}
		});

		// Set up thrift error message displaying
		ThriftManager.setErrorCallback(new ErrorCallback() {
			@Override
			public boolean thriftError(int failCount, String method, final Throwable t) {
				// if it's the first fail, retry immediately
				if (failCount == 1)
					return true;
				// Otherwise, ask user if we should retry
				return Gui.syncExec(new GuiCallable<Boolean>() {
					@Override
					public Boolean run() {
						return Gui.showMessageBox(mainWindow, THRIFT_CONNECTION_ERROR,
								MessageType.ERROR_RETRY, LOGGER, t);
					}
				});
			}
		});

		// Same for config errors
		Config.setErrorCallback(new Config.ErrorCallback() {
			@Override
			public void writeError(final Throwable t) {
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						Gui.showMessageBox(mainWindow, "Konnte Programmeinstellungen nicht speichern",
								MessageType.WARNING, LOGGER, t);
					}
				});
			}
		});

		// Global key listener
		KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
			@Override
			public boolean dispatchKeyEvent(KeyEvent event) {
				if (event.getKeyChar() == 17) { // Ctrl-Q = Quit
					if (!isQuitQuestionOpen) {
						isQuitQuestionOpen = true;
						askApplicationQuit();
					}
					event.consume();
				}
				return event.isConsumed();
			}
		});

		createMenu();

		// Set layout for the mainshell, items added to the shell should get a gridData
		mainContainer.setLayout(new BoxLayout(mainContainer, BoxLayout.PAGE_AXIS));
		mainWindow.setMinimumSize(new Dimension(850, 650));

		// register all pages of the main window
		registerPage(new MainMenuWindow());
		registerPage(new ImageListWindow());
		registerPage(new LectureListWindow());

		// Debug?
		if (System.getProperty("log") != null) {
			DebugWindow win = new DebugWindow();
			win.setMinimumSize(new Dimension(0, 250));
			win.setPreferredSize(win.getMinimumSize());
			mainWindow.getContentPane().add(win, BorderLayout.PAGE_START);
		}

		// center the window on the primary monitor
		mainWindow.getContentPane().add(mainContainer, BorderLayout.CENTER);
		mainWindow.setVisible(true);

		// here we can check for Session information
		if (Session.getSatelliteToken() != null) {
			// Wait for proxy server init
			App.waitForInit();
			// TODO: Try to resume session
		}

		// Session resume probably failed, show login window
		if (Session.getSatelliteToken() == null) {
			// User did not login, show the login mask
			LoginWindow.open(mainWindow);
		}

		// Show main menu by default
		showPage(MainMenuWindow.class);
	}

	/**
	 * Request application quit. Will show a message box asking the user for
	 * confirmation.
	 */
	protected static void askApplicationQuit() {
		// TODO: Only ask if an upload or download is running,, wizard is open etc..
		if (Gui.showMessageBox(mainWindow, "Are you sure you want to quit?", MessageType.QUESTION_YESNO,
				null, null)) {
			Gui.exit(0);
		}
		isQuitQuestionOpen = false;
	}

	/**
	 * Register a page that can be displayed in the main window.
	 * 
	 * @param window
	 */
	private static synchronized void registerPage(CompositePage window) {
		Class<? extends CompositePage> clazz = window.getClass();
		if (pages.containsKey(clazz))
			throw new IllegalArgumentException("Page " + clazz.getSimpleName() + " already registered!");
		pages.put(clazz, window);
		mainContainer.add(window);
		window.setVisible(false);
	}

	private static void createMenu() {
		// the File menu button
		JMenuBar menuBar = new JMenuBar();
		mainWindow.setJMenuBar(menuBar);

		JMenu cascadeFileMenu = new JMenu("File");
		menuBar.add(cascadeFileMenu);

		JMenuItem exitItem = new JMenuItem("Exit");
		cascadeFileMenu.add(exitItem);

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

		// the About menu button
		JMenu cascadeAboutMenu = new JMenu("About");
		menuBar.add(cascadeAboutMenu);

		JMenuItem disclaimerItem = new JMenuItem("Disclaimer");
		JMenuItem virtualizerNoticeItem = new JMenuItem("Virtualizer");
		cascadeAboutMenu.add(disclaimerItem);
		cascadeAboutMenu.add(virtualizerNoticeItem);

		disclaimerItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DisclaimerWindow.open(mainWindow);
			}
		});

		virtualizerNoticeItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				VirtualizerNoticeWindow.open(mainWindow);
			}
		});
	}

}