diff options
Diffstat (limited to 'dozentenmodul/src/main/java/util/GuiManager.java')
| -rw-r--r-- | dozentenmodul/src/main/java/util/GuiManager.java | 290 |
1 files changed, 0 insertions, 290 deletions
diff --git a/dozentenmodul/src/main/java/util/GuiManager.java b/dozentenmodul/src/main/java/util/GuiManager.java deleted file mode 100644 index 67671fcb..00000000 --- a/dozentenmodul/src/main/java/util/GuiManager.java +++ /dev/null @@ -1,290 +0,0 @@ -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); - } -} |
