diff options
| author | Jonathan Bauer | 2015-03-24 14:31:01 +0100 |
|---|---|---|
| committer | Jonathan Bauer | 2015-03-24 14:31:01 +0100 |
| commit | 62ba9338439e4558d3556576dc6f27a8011f16a6 (patch) | |
| tree | ee7da1b68f0d0d1110d4f54e526ffa78a6b6dcde /dozentenmodul/src/main/java/util/GuiManager.java | |
| parent | [client] fix tabbing through nirvana on CreateImageAllgemein (diff) | |
| download | tutor-module-62ba9338439e4558d3556576dc6f27a8011f16a6.tar.gz tutor-module-62ba9338439e4558d3556576dc6f27a8011f16a6.tar.xz tutor-module-62ba9338439e4558d3556576dc6f27a8011f16a6.zip | |
[client] GuiManager class cleanup + bundled priv/pub functions...
Diffstat (limited to 'dozentenmodul/src/main/java/util/GuiManager.java')
| -rw-r--r-- | dozentenmodul/src/main/java/util/GuiManager.java | 269 |
1 files changed, 146 insertions, 123 deletions
diff --git a/dozentenmodul/src/main/java/util/GuiManager.java b/dozentenmodul/src/main/java/util/GuiManager.java index 1f7b47cc..e8daca66 100644 --- a/dozentenmodul/src/main/java/util/GuiManager.java +++ b/dozentenmodul/src/main/java/util/GuiManager.java @@ -28,71 +28,36 @@ 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. + * 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 + * Our main window object as a JFrame */ - private static Rectangle rect = null; + private static JFrame mainWindow = null; /** - * Gets the bounds of the primary display using - * AWT's GraphicsEnvironment class. + * Currently displayed internal frame (inside the main window) */ - 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; - } - } + private static JInternalFrame currentFrame = null; /** - * Centers the given Window within the bounds of the display - * @param gui The Window object to be centered. + * Previously displayed frame TODO: use this when going back in the GUI */ - 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. + * 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 @@ -106,31 +71,118 @@ public abstract class GuiManager { 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); + ((BasicInternalFrameUI) currentFrame.getUI()).setNorthPane(null); mainWindow.pack(); // size management mainWindow.setBounds(0, 0, 785, 430); - mainWindow.setLocation((int) (rect.getWidth() / 2 - mainWindow.getWidth() / 2), + 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() { @@ -154,7 +206,7 @@ public abstract class GuiManager { mntmOtrs.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { - OpenLinks.openWebpage("otrs"); + OpenLinks.openWebpage("otrs"); } }); helpMenu.add(mntmOtrs); @@ -168,22 +220,28 @@ public abstract class GuiManager { 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 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" + // 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)); + helpMessage = (String) (currentFrame.getClass().getDeclaredField( + "HELP_MESSAGE").get(currentFrame)); } catch (NoSuchFieldException e) { - // only this case if interesting for us, + // 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) { + } catch (IllegalArgumentException | IllegalAccessException + | SecurityException e) { LOGGER.error("Failed to check for 'HELP_MESSAGE' variable in '" + currentFrame.getClass() + "' class, see trace: " + e); return false; @@ -194,7 +252,8 @@ public abstract class GuiManager { mnNewMenu_Info.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { - JOptionPane.showMessageDialog(currentFrame, helpMessage != null ? helpMessage : "No help message.", + JOptionPane.showMessageDialog(currentFrame, + helpMessage != null ? helpMessage : "No help message.", "Hilfe zu dieser Oberfläche", JOptionPane.INFORMATION_MESSAGE); } }); @@ -203,68 +262,32 @@ public abstract class GuiManager { } /** - * Public function to show the given frame, replacing the current frame - * @param newFrame the new frame to show + * Gets the bounds of the primary display using AWT's GraphicsEnvironment + * class. */ - 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()); + 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; } - } - public static void openPopup(Component popup) { - if (!(popup instanceof JFrame)) { - LOGGER.error("Popup classes need to be JFrame, given a: " + popup.getClass().getName()); - return; + 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; } - 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); } } |
