summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/util/GuiManager.java
blob: 67671fcb52ecb70602f64d3daa4f741581eb4916 (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
package util;

import gui.intro.About_GUI;
import gui.intro.Login_GUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicInternalFrameUI;

import org.apache.log4j.Logger;

/**
 * An abstract class to organize the GUI. Currently only provide a method for
 * centering Window-objects.
 */
public abstract class GuiManager {

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

	/**
	 * Our main window object as a JFrame
	 */
	private static JFrame mainWindow = null;

	/**
	 * Currently displayed internal frame (inside the main window)
	 */
	private static JInternalFrame currentFrame = null;

	/**
	 * Previously displayed frame TODO: use this when going back in the GUI
	 */
	private static JInternalFrame formerFrame = null;

	/**
	 * The rectangle representing the bounds of the primary display
	 */
	private static Rectangle rect = null;

	/**
	 * Initializes the GUI by creating the main window and showing the Login_GUI
	 * as the first frame.
	 */
	public static void initGui() {
		// get the screen size
		getDisplayBounds();
		if (rect == null) {
			LOGGER.error("Could not get display size. Contact developper.");
			System.exit(1);
		}

		// create main window
		mainWindow = new JFrame("DozMod");
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainWindow.setResizable(false);

		// create login frame
		currentFrame = new Login_GUI();
		mainWindow.getContentPane().add(currentFrame, BorderLayout.CENTER);
		((BasicInternalFrameUI) currentFrame.getUI()).setNorthPane(null);
		mainWindow.pack();

		// size management
		mainWindow.setBounds(0, 0, 785, 430);
		mainWindow.setLocation((int) (rect.getWidth() / 2 - mainWindow.getWidth() / 2),
				(int) (rect.getHeight() / 2 - mainWindow.getHeight() / 2));

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// Non-critical - applying the look failed, but app will still work
		}

		// finally let's see the frames
		currentFrame.setVisible(true);
		mainWindow.setVisible(true);
	}

	/**
	 * Public function to show the given frame, replacing the current frame
	 * 
	 * @param newFrame
	 *            the new frame to show
	 */
	public static void show(JInternalFrame newFrame) {
		// first remove the current component
		currentFrame.setVisible(false);
		mainWindow.getContentPane().remove(currentFrame);
		// save it as formerFrame in case we need it
		formerFrame = currentFrame;
		// from now on currentFrame is newFrame !!!
		currentFrame = newFrame;
		currentFrame.setBorder(null);

		// show the menu bar for everything but the Login_GUI
		if (!(currentFrame instanceof Login_GUI)) {
			if (mainWindow.getMenuBar() == null) {
				if (!addMenuBar()) {
					LOGGER.error("Failed to add menu to main window. See logs.");
				}
			}
			// add help if needed
			if (!addHelp()) {
				LOGGER.error("Failed to add help to main window's menu. See logs.");
			}
		}
		// prepare the switch
		if (currentFrame.getUI() instanceof BasicInternalFrameUI) {
			BasicInternalFrameUI bar = ((BasicInternalFrameUI) currentFrame.getUI());
			if (bar.getNorthPane() != null)
				bar.setNorthPane(null);
		}
		// TODO else case

		mainWindow.setTitle(newFrame.getTitle() != null ? newFrame.getTitle() : "bwLehrpool Suite");
		mainWindow.getContentPane().add(currentFrame, BorderLayout.CENTER);
		mainWindow.setBounds((int) mainWindow.getLocationOnScreen().getX(),
				(int) mainWindow.getLocationOnScreen().getY(), currentFrame.getWidth(),
				currentFrame.getHeight());
		currentFrame.setVisible(true);

	}

	/**
	 * Public function to show the given frame, replacing the current frame
	 * 
	 * @param newFrame
	 *            the new frame to show
	 * @param center
	 *            true if the main window is to be centered, false otherwise
	 */
	public static void show(JInternalFrame newFrame, boolean center) {
		show(newFrame);
		if (center) {
			double xCenter = (rect.getWidth() / 2 - newFrame.getWidth() / 2);
			double yCenter = (rect.getHeight() / 2 - newFrame.getHeight() / 2);
			mainWindow.setBounds((int) xCenter, (int) yCenter, newFrame.getWidth(), newFrame.getHeight());
		}
	}

	/**
	 * Public function to open the given JFrame as a popup and center it on the
	 * main window
	 * 
	 * @param popup
	 *            The component to open as popup. Note this must be a JFrame.
	 */
	public static void openPopup(Component popup) {
		if (!(popup instanceof JFrame)) {
			LOGGER.error("Popup classes need to be JFrame, given a: " + popup.getClass().getName());
			return;
		}
		int xPopup = (int) (mainWindow.getX() + (mainWindow.getWidth() / 2) - (popup.getWidth() / 2));
		int yPopup = (int) (mainWindow.getY() + (mainWindow.getHeight() / 2) - (popup.getHeight() / 2));
		((JFrame) popup).setLocation(xPopup, yPopup);
		((JFrame) popup).setVisible(true);
	}

	/**
	 * Private function to add the menu bar to the main window.
	 * 
	 * @return true if adding the menu bar worked, false otherwise
	 */
	private static boolean addMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		mainWindow.setJMenuBar(menuBar);

		JMenu helpMenu = new JMenu("Hilfe");
		menuBar.add(helpMenu);

		// FAQ Field
		JMenuItem mntmFaq = new JMenuItem("FAQ");
		mntmFaq.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				OpenLinks.openWebpage("faq");
			}
		});
		helpMenu.add(mntmFaq);
		// About Field
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GuiManager.openPopup(new About_GUI());
			}
		});
		helpMenu.add(mntmAbout);
		return true;
	}

	/**
	 * Private function to determine whether the currentFrame has a
	 * 'HELP_MESSAGE'
	 * defined and to add it to the menu bar if found one.
	 * 
	 * @return true if setting the help button to the menu bar worked, false
	 *         otherwise
	 */
	private static boolean addHelp() {
		// let's see if we have a HELP_MESSAGE variable defined in
		// the class of the currentFrame, if so we need to show it by pressing
		// "Hilfe"
		final String helpMessage;
		try {
			helpMessage = (String) (currentFrame.getClass().getDeclaredField("HELP_MESSAGE").get(currentFrame));
		} catch (NoSuchFieldException e) {
			// only this case if interesting for us,
			// since we now we don't have a help message to show
			return false;
		} catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
			LOGGER.error("Failed to check for 'HELP_MESSAGE' variable in '" + currentFrame.getClass()
					+ "' class, see trace: " + e);
			return false;
		}
		// print it for debugging purposes
		// still here? means we have a HELP_MESSAGE to display
		JMenu mnNewMenu_Info = new JMenu("Info");
		mnNewMenu_Info.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				JOptionPane.showMessageDialog(currentFrame, helpMessage != null ? helpMessage
						: "No help message.", "Hilfe zu dieser Oberfläche", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		mainWindow.getJMenuBar().add(mnNewMenu_Info);
		return true;
	}

	/**
	 * Gets the bounds of the primary display using AWT's GraphicsEnvironment
	 * class.
	 */
	private static boolean getDisplayBounds() {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = null;
		try {
			gd = ge.getDefaultScreenDevice();
		} catch (HeadlessException he) {
			he.printStackTrace();
			JOptionPane.showMessageDialog(null, "Konnte kein Display ermittelt werden.", "Fehler",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}

		GraphicsConfiguration gc = gd.getDefaultConfiguration();
		rect = gc.getBounds();

		if (rect == null) {
			JOptionPane.showMessageDialog(null, "Konnte die Resolution des Bildschirms nicht ermitteln!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Generic helper to show a message box to the user, and optionally log the message to the log file.
	 *
	 * @param message Message to display. Can be multi line.
	 * @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(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();
		JOptionPane.showMessageDialog(mainWindow, message, messageType.title, messageType.optionPaneId);
	}
}