summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/GuiManager.java
blob: 28da082431bf117f925c7b5e9666881bd43d1589 (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
package org.openslx.dozmod.gui.helper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.thrifthelper.ThriftManager.ErrorCallback;

public abstract class GuiManager {

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

	private static Shell mainShell;
	private static Composite contentComposite;
	private static Display display;

	private static final int THRIFT_RECONNECT_MAX_TRIES = 3;
	private static final String THRIFT_CONNECTION_ERROR = "Lost connection to the masterserver.";
	private static final String THRIFT_CONNECTION_DEAD = "Could not re-establish connection to the masterserver after "
		+ THRIFT_RECONNECT_MAX_TRIES + " tries. Contact an administrator/developer. Exiting application.";

	/**
	 * Add a new composite with content to the main Shell
	 * 
	 * @param The composite to add, should be a GUI
	 */
	public static void addContent(Composite contentComposite) {
		removeContent();

		GuiManager.contentComposite = contentComposite;

		// sets the starting preferred size.
		GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
		gridData.widthHint = 800;
		gridData.heightHint = 600;
		contentComposite.setLayoutData(gridData);
		mainShell.setMinimumSize(850, 650);
		mainShell.layout();

	}

	/**
	 * Remove the current content of the main shell
	 */
	private static void removeContent() {
		if (contentComposite != null) {
			GuiManager.contentComposite.dispose();
		}
	}

	public static void openPopup(Class<?> clazz) {
		Shell dialogShell = new Shell(display);
		// populate dialogShell
		dialogShell.setLayout(new GridLayout(1, false));
		LOGGER.debug(clazz.getDeclaredClasses());
		Constructor<?> con = null;
		try {
			con = clazz.getConstructor(Shell.class);
		} catch (NoSuchMethodException | SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			con.newInstance(dialogShell);
		} catch (InstantiationException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dialogShell.layout();
		dialogShell.pack();
		dialogShell.open();
		while (!dialogShell.isDisposed()) {
		    if (!display.readAndDispatch()) {
		        display.sleep();
		    }
		}
	}

	/**
	 * @return The instance of SWT display currently in use
	 */
	public static Display getDisplay() {
		return display;
	}

	/**
	 * Initialises 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 initGui() {
		// init SWT stuffs
		display = new Display();
		mainShell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);

		// setup global thrift connection error handler before anything else
		// Set master server to use (TODO: make configurable via command line)
		ThriftManager.setMasterServerAddress("bwlp-masterserver.ruf.uni-freiburg.de");

		// Set up thrift error message displaying
		ThriftManager.setErrorCallback(new ErrorCallback() {

			@Override
			public boolean thriftError(int failCount, String method, Throwable t) {
				// first check if we failed 3 reconnects, if so just let the user know
				// that we are closing the application due to connection problems.
				if (failCount > THRIFT_RECONNECT_MAX_TRIES) {
					showMessageBox(mainShell, THRIFT_CONNECTION_DEAD, MessageType.ERROR, LOGGER, t);
					return false;
					// System.exit(1); ?
				} else {
					showMessageBox(mainShell, THRIFT_CONNECTION_ERROR, MessageType.ERROR, LOGGER, t);
					// TODO how to actualy reconnect?
					return true;
				}
			}
		});

		Menu menuBar = new Menu(mainShell, SWT.BAR);
		MenuItem cascadeFileMenu = new MenuItem(menuBar, SWT.CASCADE);
		cascadeFileMenu.setText("&File");

		Menu fileMenu = new Menu(mainShell, SWT.DROP_DOWN);
		cascadeFileMenu.setMenu(fileMenu);

		MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
		exitItem.setText("&Exit");
		exitItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				mainShell.getDisplay().dispose();
				System.exit(0);
			}
		});

		mainShell.setText("bwSuite");
		mainShell.setMenuBar(menuBar);

		// Set layout for the mainshell, items added to the shell should get a gridData
		mainShell.setLayout(new GridLayout(1, true));

		// Add LoginWindow as the first window to be shown
		addContent(new org.openslx.dozmod.gui.window.LoginWindow(mainShell));

		// center the window on the primary monitor
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = mainShell.getBounds();

		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;

		mainShell.setLocation(x, y);

		mainShell.pack();
		mainShell.open();

		LOGGER.info("GUI initialised.");

		while (!mainShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Generic helper to show a message box to the user, and optionally log the
	 * message to the log file.
	 * 
	 * @param parent parent shell this message box belongs to
	 * @param message Message to display. Can be multiline.
	 * @param messageType Type of message (warning, information)
	 * @param logger Logger instance to log to. Can be null.
	 * @param exception Exception related to this message. Can be null.
	 */
	public static void showMessageBox(Shell parent, String message, MessageType messageType, Logger logger,
			Throwable exception) {
		if (logger != null)
			logger.log(messageType.logPriority, message, exception);
		if (exception != null)
			message += "\n\n" + exception.getClass().getSimpleName() + " (Siehe Logdatei)";
		MessageBox box = new MessageBox(parent, messageType.style);
		box.setMessage(message);
		box.setText(messageType.title);
		box.open();
	}

}