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.Window; 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.UnsupportedLookAndFeelException; 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); /** * The rectangle representing the bounds of the primary display */ private static Rectangle rect = null; /** * 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; } } /** * Centers the given Window within the bounds of the display * @param gui The Window object to be centered. */ public static void centerGUI(Window gui) { if (rect == null) getDisplayBounds(); double width = rect.getWidth(); double height = rect.getHeight(); double xCenter = (width / 2 - gui.getWidth() / 2); double yCenter = (height / 2 - gui.getHeight() / 2); gui.setLocation((int) xCenter, (int) yCenter); } private static JFrame mainWindow = null; public static JFrame getMainWindow() { return mainWindow; } private static JInternalFrame currentFrame = null; // TODO use this formerFrame when going "back" in the gui private static JInternalFrame formerFrame = null; /** * Starts 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); } /** * 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); // OTRS Field JMenuItem mntmOtrs = new JMenuItem("OTRS"); mntmOtrs.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { OpenLinks.openWebpage("otrs"); } }); helpMenu.add(mntmOtrs); // 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" String test = ""; try { test = (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); // just do nothing } // 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) { String helpMessage = null; try { helpMessage = (String) currentFrame.getClass().getField("HELP_MESSAGE").get(currentFrame); } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { LOGGER.error("Failed to check for 'HELP_MESSAGE' variable in '" + currentFrame.getClass() + "' class, see trace: " + e); } JOptionPane.showMessageDialog(currentFrame, helpMessage != null ? helpMessage : "No help message.", "Hilfe zu dieser Oberfläche", JOptionPane.INFORMATION_MESSAGE); } }); mainWindow.getJMenuBar().add(mnNewMenu_Info); return 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 static void openPopup(Component popup) { if (!(popup instanceof JFrame)) { LOGGER.error("Popup classes need to be JFrame, given a: " + popup.getClass().getName()); return; } ((JFrame)popup).setLocation((int)(rect.getWidth() / 2 - popup.getWidth() / 2), (int)(rect.getHeight() / 2 - popup.getHeight() / 2)); ((JFrame)popup).setVisible(true); } }