diff options
12 files changed, 303 insertions, 111 deletions
diff --git a/dozentenmodul/pom.xml b/dozentenmodul/pom.xml index 051523c4..47a51005 100644 --- a/dozentenmodul/pom.xml +++ b/dozentenmodul/pom.xml @@ -47,7 +47,7 @@ <configuration> <archive> <manifest> - <mainClass>gui.intro.Login_GUI</mainClass> + <mainClass>App</mainClass> </manifest> <manifestEntries> <Version-Timestamp>${maven.build.timestamp}</Version-Timestamp> @@ -81,7 +81,7 @@ <minimizeJar>true</minimizeJar> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> - <mainClass>gui.intro.Login_GUI</mainClass> + <mainClass>App</mainClass> <manifestEntries> <Version-Timestamp>${maven.build.timestamp}</Version-Timestamp> </manifestEntries> diff --git a/dozentenmodul/src/main/java/App.java b/dozentenmodul/src/main/java/App.java new file mode 100644 index 00000000..10b9a460 --- /dev/null +++ b/dozentenmodul/src/main/java/App.java @@ -0,0 +1,40 @@ +import gui.intro.Login_GUI; + +import java.awt.EventQueue; +import java.io.IOException; + +import javax.swing.JOptionPane; + +import config.Config; + + +public class App { + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + // Pruefe und Erzeuge gegebenfalls Config + try { + Config.init(); + } catch (IOException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, e.getMessage(), + "Fehler", JOptionPane.ERROR_MESSAGE); + return; + } + + // Aufruf und Anzeige des Login Fensters + Login_GUI frame = new Login_GUI(); + frame.setVisible(true); + + } catch (Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, e.getStackTrace(), + "Message", JOptionPane.ERROR_MESSAGE); + } + } + + }); + } +} diff --git a/dozentenmodul/src/main/java/config/Config.java b/dozentenmodul/src/main/java/config/Config.java index 13bdf830..95547b5f 100644 --- a/dozentenmodul/src/main/java/config/Config.java +++ b/dozentenmodul/src/main/java/config/Config.java @@ -5,19 +5,39 @@ import java.io.IOException; import org.ini4j.Wini; +/** + * Represents the configuration of the client + * + * @author Jonathan Bauer + */ + public class Config { - - // Nur die Wini als member + + /** + * The main configuration object is of type Wini + * It contains the content of the config.ini as + * determined in the init() function. + */ private static Wini ini = null; - - // Konstruktor ermittelt der Pfad zur Konfigurationsdatei. - // Unterscheidet dabei Windows/Linux + + /** + * Initializes the class by determining the path + * to the config.ini on the system and setting the + * private creating the ini member from that file. + * + * This function will make a distinction between + * Linux and Windows OS's, as the standard paths + * for configuration files obviously differ. + * + * @throws IOException + */ public static void init() throws IOException { - - // Hauptvariablen nur lokal notwendig + + // Variables only needed locally String configPath = null; File configFile = null; - // Unterscheide zwischen Windows/Unix + + // Determine OS String OSName = System.getProperty("os.name").toLowerCase(); System.out.println("Machine's OS: " + OSName); if (OSName.contains("windows")) { @@ -36,36 +56,36 @@ public class Config { } else if (OSName.contains("linux")) { configPath=System.getProperty("user.home") + "/.config/bwSuite/config.ini"; } else { - // Nicht Windows oder Linux, TODO MacOS Support? + // Not Windows nor Linux, TODO MacOS Support? configPath = null; } - //File fuer den Ordner erzeugen + // Check if we got a path if (!(configPath.isEmpty()||configPath == null)) { configFile = new File(configPath); } else { throw new IOException("Konnte kein Pfad für die Konfigurationsdatei ermitteln."); } - // Checke, ob der Ordner existiert. + // Check if the directory exists. if (!configFile.getParentFile().exists()) { System.out.println("Ordner " + configFile.getParentFile() + " exisitiert nicht - lege ihn jetzt an."); - // Wenn nicht, erzeuge Ordner + // Does not, create it if (!configFile.getParentFile().mkdirs()) { throw new IOException("Konnte '" + configFile.getParentFile() + "' nicht erstellen."); } } - // Pruefen, ob Datei schon existiert + // Check if the file already exists if (!configFile.exists()) { - // Erzeuge eine neue Datei + // Does not, create it configFile.createNewFile(); - // Wenn in die Datei geschrieben werden kann + // Check if file is writeable if(configFile.canWrite()) { ini = new Wini(configFile); System.out.println("Erzeuge '" + configFile + "'..."); - //Schreibe Config + // write default configuration options and values ini.put("main", "BillOfRights", false); ini.put("main", "vmware", false); ini.put("main", "Benutzername speichern", false); @@ -81,16 +101,120 @@ public class Config { System.out.println("'" + configFile + "' existiert bereits - keine weitere Aktion."); ini = new Wini(configFile); } + } // end constructor. + + /** + * Query the value of 'BillOfRights' from the configuration file. + * @return true if the user already accepted bill of rights, false otherwise. + */ + public static boolean getBillOfRights() { + return getBoolean("main", "BillOfRights", false); + } + /** + * Query the value of 'vmware' from the configuration file. + * @return true if the user already accepted vmware license, false otherwise. + */ + public static boolean getVmwareLicense() { + return getBoolean("main", "vmware", false); + } + /** + * Query the value of 'Benutzername speichern' from the configuration file. + * @return true if the username should be saved, false otherwise. + */ + public static boolean getSaveUsername() { + return getBoolean("main", "Benutzername speichern", false); + } + /** + * Query the value of 'Benutzername' from the configuration file. + * @return username if saved, an empty string otherwise. + */ + public static String getUsername() { + return getString("main", "Benutzername", ""); + } + /** + * Query the value of 'Letzter Downloadpfad' from the configuration file. + * @return last download path if saved, the path to the user's home otherwise. + */ + public static String getLastDownloadPath() { + return getString("main", "Letzter Downloadpfad", System.getProperty("user.home")); + } + /** + * Query the value of 'Letzter Uploadpfad' from the configuration file. + * @return last upload path if saved, the path to the user's home otherwise. + */ + public static String getLastUploadPath() { + return getString("main", "Letzter Uploadpfad", System.getProperty("user.home")); + } + + /** + * Sets the value of 'BillOfRights' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setBillOfRights(boolean value) { + return setBoolean("main", "BillOfRights", value); + } + /** + * Sets the value of 'vmware' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setVmwareLicense(boolean value) { + return setBoolean("main", "vmware", value); + } + /** + * Sets the value of 'Benutzername speichern' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setSaveUsername(boolean value) { + return setBoolean("main", "Benutzername speichern", value); + } + /** + * Sets the value of 'Benutzername' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setUsername(String value) { + return setString("main", "Benutzername", value); + } + /** + * Sets the value of 'Letzter Downloadpfad' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setLastDownloadPath(String value) { + return setString("main", "Letzter Downloadpfad", value); + } + /** + * Sets the value of "Letzter Uploadpfad" in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setLastUploadPath(String value) { + return setString("main", "Letzter Uploadpfad", value); + } + + /** + * Save the changes to the ini file to actual file on the disk. + * + * @return true if succeeded, false otherwise + */ + public static boolean store() { + try { + ini.store(); + return true; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } } - // Ende Konstruktor. - // Public funktion zur Abfrage der Boolean-Werte - // Gibt Wert von Attribut 'key' in Sektion 'section', falls es existiert. - // Sonst gibt es 'defaultValue' zurück. - // - // Usage: getBoolean(<section>, <key>, <defaultValue>) - // Ex.: getBoolean("main", "Benutzername", "NichtGesetzt"); - public static boolean getBoolean(String section, String key, Boolean defaultValue) { + /** + * Gets the boolean from the given 'key' in the given 'section'. + * If nothing is found, return the given 'defaultValue' + * + * @param section section to search the key in + * @param key key to query in that section + * @param defaultValue default value to be returned, if none is found. + * @return + */ + private static boolean getBoolean(String section, String key, Boolean defaultValue) { if (ini.containsKey(section) && ini.get(section).containsKey(key)) { return ini.get(section, key, Boolean.class); } else { @@ -98,32 +222,45 @@ public class Config { } } - // Public funktion zur Abfrage der String-Werte - // Usage: getBoolean(<key>) - public static String getString(String section, String key, String defaultValue) { + /** + * Gets the string from the given 'key' in the given 'section' + * If nothing is found, return the given 'defaultValue' + * + * @param section section of the configuration file to search for + * @param key key to lookup in the section + * @param defaultValue default value to return if none is found in the file + * @return value of 'key' in 'section' if it exists, 'defaultValue' otherwise + */ + private static String getString(String section, String key, String defaultValue) { if (ini.containsKey(section) && ini.get(section).containsKey(key)) { return ini.get(section, key); } else { return defaultValue; } } - - public static boolean setBoolean(String section, String key, Boolean value) { + /** + * Sets the given 'key' in the given 'section' to 'value'. + * Restricted to boolean. + * + * @param section section of the configuration file + * @param key key to set + * @param value value to assign to key + * @return true if it succeeded, false otherwise + */ + private static boolean setBoolean(String section, String key, boolean value) { return ini.put(section, key, value) != null; } - public static boolean setString(String section, String key, String value) { + /** + * Sets the given 'key' in the given 'section' to 'value'. + * Restricted to string. + * + * @param section section of the configuration file + * @param key key to set + * @param value value to assign to key + * @return true if it succeeded, false otherwise + */ + private static boolean setString(String section, String key, String value) { return ini.put(section, key, value) != null; } - - public static boolean store() { - try { - ini.store(); - return true; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } - } } diff --git a/dozentenmodul/src/main/java/ftp/FTPUtility.java b/dozentenmodul/src/main/java/ftp/FTPUtility.java index beae3f34..fffc5503 100644 --- a/dozentenmodul/src/main/java/ftp/FTPUtility.java +++ b/dozentenmodul/src/main/java/ftp/FTPUtility.java @@ -55,7 +55,7 @@ public class FTPUtility { ftpClient.connect(host, port); replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { - throw new FTPException("FTP serve refused connection."); + throw new FTPException("FTP server refused connection."); } boolean logged = ftpClient.login(username, password); diff --git a/dozentenmodul/src/main/java/gui/image/FTPCreateUploader_GUI.java b/dozentenmodul/src/main/java/gui/image/FTPCreateUploader_GUI.java index 9b026a59..ded9098b 100644 --- a/dozentenmodul/src/main/java/gui/image/FTPCreateUploader_GUI.java +++ b/dozentenmodul/src/main/java/gui/image/FTPCreateUploader_GUI.java @@ -163,7 +163,7 @@ public class FTPCreateUploader_GUI extends JFrame implements btnSpeicherortAuswhlen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - fc = new JFileChooser(Config.getString("main", "Letzter Uploadpfad", System.getProperty("user.home"))); + fc = new JFileChooser(Config.getLastUploadPath()); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.showOpenDialog(getParent()); @@ -186,7 +186,7 @@ public class FTPCreateUploader_GUI extends JFrame implements contentPanel.add(btnSpeicherortAuswhlen); } lblPath = new JLabel(""); - lblPath.setText(Config.getString("main", "Letzter Uploadpfad", System.getProperty("user.home"))); + lblPath.setText(Config.getLastUploadPath()); lblPath.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent arg0) { @@ -346,11 +346,11 @@ public class FTPCreateUploader_GUI extends JFrame implements File f = new File(lblPath.getText()); if (f.isDirectory()) { // shouldn't be a directory, but let's check that case anyway - Config.setString("main", "Letzter Uploadpfad", f.toString()); + Config.setLastUploadPath(f.toString()); } else if (f.getParentFile().isDirectory()) { - Config.setString("main", "Letzter Uploadpfad", f.getParentFile().toString()); + Config.setLastUploadPath(f.getParentFile().toString()); } - // save config. + // save configuration Config.store(); dispose(); diff --git a/dozentenmodul/src/main/java/gui/image/FTPEditDownloader_GUI.java b/dozentenmodul/src/main/java/gui/image/FTPEditDownloader_GUI.java index eb864d91..08f1b223 100644 --- a/dozentenmodul/src/main/java/gui/image/FTPEditDownloader_GUI.java +++ b/dozentenmodul/src/main/java/gui/image/FTPEditDownloader_GUI.java @@ -154,7 +154,7 @@ public class FTPEditDownloader_GUI extends JFrame implements btnSpeicherortAuswhlen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - fc = new JFileChooser(Config.getString("main", "Letzter Downloadpfad", System.getProperty("user.home"))); + fc = new JFileChooser(Config.getLastDownloadPath()); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.showOpenDialog(getParent()); @@ -178,7 +178,7 @@ public class FTPEditDownloader_GUI extends JFrame implements } lblPath = new JLabel(""); - lblPath.setText(Config.getString("main", "Letzter Downloadpfad", System.getProperty("user.home"))); + lblPath.setText(Config.getLastDownloadPath()); lblPath.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent arg0) { @@ -437,9 +437,9 @@ public class FTPEditDownloader_GUI extends JFrame implements // always save download path File f = new File(lblPath.getText()); if (f.isDirectory()) { - Config.setString("main", "Letzter Downloadpfad", f.toString()); + Config.setLastDownloadPath(f.toString()); } else if (f.getParentFile().isDirectory()) { - Config.setString("main", "Letzter Downloadpfad", f.getParentFile().toString()); + Config.setLastDownloadPath(f.getParentFile().toString()); } Config.store(); diff --git a/dozentenmodul/src/main/java/gui/image/FTPEditUploader_GUI.java b/dozentenmodul/src/main/java/gui/image/FTPEditUploader_GUI.java index 6d84f247..3b856881 100644 --- a/dozentenmodul/src/main/java/gui/image/FTPEditUploader_GUI.java +++ b/dozentenmodul/src/main/java/gui/image/FTPEditUploader_GUI.java @@ -160,7 +160,7 @@ public class FTPEditUploader_GUI extends JFrame implements btnSpeicherortAuswhlen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - fc = new JFileChooser(Config.getString("main", "Letzter Uploadpfad", System.getProperty("user.home"))); + fc = new JFileChooser(Config.getLastUploadPath()); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.showOpenDialog(getParent()); @@ -184,7 +184,7 @@ public class FTPEditUploader_GUI extends JFrame implements } lblPath = new JLabel(""); - lblPath.setText(Config.getString("main", "Letzter Uploadpfad", System.getProperty("user.home"))); + lblPath.setText(Config.getLastUploadPath()); lblPath.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent arg0) { @@ -337,9 +337,9 @@ public class FTPEditUploader_GUI extends JFrame implements updateData(); File f = new File(lblPath.getText()); if (f.isDirectory()) { - Config.setString("main", "Letzter Downloadpfad", f.toString()); + Config.setLastUploadPath(f.toString()); } else if (f.getParentFile().isDirectory()) { - Config.setString("main", "Letzter Downloadpfad", f.getParentFile().toString()); + Config.setLastUploadPath(f.getParentFile().toString()); } Config.store(); diff --git a/dozentenmodul/src/main/java/gui/image/FTPSearchDownloader_GUI.java b/dozentenmodul/src/main/java/gui/image/FTPSearchDownloader_GUI.java index db80bc35..34e0cf80 100644 --- a/dozentenmodul/src/main/java/gui/image/FTPSearchDownloader_GUI.java +++ b/dozentenmodul/src/main/java/gui/image/FTPSearchDownloader_GUI.java @@ -155,7 +155,7 @@ public class FTPSearchDownloader_GUI extends JFrame implements "Speicherort auswählen"); btnSpeicherortAuswhlen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - fc = new JFileChooser(Config.getString("main", "Letzter Downloadpfad", System.getProperty("user.home"))); + fc = new JFileChooser(Config.getLastDownloadPath()); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.showOpenDialog(getParent()); @@ -180,7 +180,7 @@ public class FTPSearchDownloader_GUI extends JFrame implements // read last download path from config lblPath = new JLabel(""); - lblPath.setText(Config.getString("main", "Letzter Downloadpfad", System.getProperty("user.home"))); + lblPath.setText(Config.getLastDownloadPath()); lblPath.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent arg0) { @@ -445,7 +445,7 @@ public class FTPSearchDownloader_GUI extends JFrame implements dispose(); }// end else // save config in any case - Config.setString("main", "Letzter Downloadpfad", lblPath.getText()); + Config.setLastDownloadPath(lblPath.getText()); Config.store(); }//end action }); @@ -455,7 +455,7 @@ public class FTPSearchDownloader_GUI extends JFrame implements btnMainMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - Config.setString("main", "Letzter Downloadpfad", lblPath.getText()); + Config.setLastDownloadPath(lblPath.getText()); Config.store(); MainMenue_GUI mm = new MainMenue_GUI(); diff --git a/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java b/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java index 8e943c8a..1ae73608 100644 --- a/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java +++ b/dozentenmodul/src/main/java/gui/intro/BillOfRights_GUI.java @@ -129,7 +129,7 @@ public class BillOfRights_GUI extends JFrame { fwdButton = new JButton("Weiter"); fwdButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - Config.setBoolean("main", "BillOfRights", true); + Config.setBillOfRights(true); Config.store(); VmWareLink_GUI ac=new VmWareLink_GUI(); ac.setVisible(true); diff --git a/dozentenmodul/src/main/java/gui/intro/Login_GUI.java b/dozentenmodul/src/main/java/gui/intro/Login_GUI.java index 4465ec3a..ebfaf9a3 100644 --- a/dozentenmodul/src/main/java/gui/intro/Login_GUI.java +++ b/dozentenmodul/src/main/java/gui/intro/Login_GUI.java @@ -1,14 +1,12 @@ package gui.intro; import java.awt.Color; -import java.awt.EventQueue; import java.awt.Image; import java.awt.SystemColor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.io.IOException; import java.util.Arrays; import javax.swing.ImageIcon; @@ -49,36 +47,6 @@ public class Login_GUI extends JFrame { JCheckBox chckbxBenutzernameSpeichern; /** - * Launch the application. - */ - public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - // Pruefe und Erzeuge gegebenfalls Config - try { - Config.init(); - } catch (IOException e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(null, e.getMessage(), - "Fehler", JOptionPane.ERROR_MESSAGE); - return; - } - - // Aufruf und Anzeige des Login Fensters - Login_GUI frame = new Login_GUI(); - frame.setVisible(true); - - } catch (Exception e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(null, e.getStackTrace(), - "Message", JOptionPane.ERROR_MESSAGE); - } - } - }); - } - - /** * Create the frame. */ public Login_GUI() { @@ -158,9 +126,9 @@ public class Login_GUI extends JFrame { .setToolTipText("Bitte geben Sie Ihren bwIDM-Benutzernamen ein."); // Lese Information aus der Konfigurationsdatei - lblusername.setText(Config.getString("main", "Benutzername", "")); - chckbxBenutzernameSpeichern.setSelected(Config.getBoolean("main", - "Benutzername speichern", false)); + System.out.println(Config.getUsername()); + lblusername.setText(Config.getUsername()); + chckbxBenutzernameSpeichern.setSelected(Config.getSaveUsername()); lblusername.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { @@ -278,18 +246,18 @@ public class Login_GUI extends JFrame { // person.verantwortlicher.setRole("GetToTheChopper!"); if (chckbxBenutzernameSpeichern.isSelected()) { - Config.setString("main", "Benutzername", lblusername.getText()); - Config.setBoolean("main", "Benutzername speichern", true); + Config.setUsername(lblusername.getText()); + Config.setSaveUsername(true); } else { - Config.setString("main", "Benutzername", ""); - Config.setBoolean("main", "Benutzername speichern", false); + Config.setUsername(""); + Config.setSaveUsername(false); } // speichern Config.store(); // Pruefe ob Bills Of Rights schon akzeptiert wurden, // wenn nicht zeige diese an - if (!Config.getBoolean("main", "BillOfRights", false)) { + if (!Config.getBillOfRights()) { // Erstellen einer Instanz der Aktionsauswahl BillOfRights_GUI re = new BillOfRights_GUI(); re.setVisible(true); @@ -298,7 +266,7 @@ public class Login_GUI extends JFrame { } else { // Pruefe ob die Links zu vmware angezeigt werden // sollen - if (!Config.getBoolean("main", "vmware", false)) { + if (!Config.getVmwareLicense()) { VmWareLink_GUI vm = new VmWareLink_GUI(); vm.setVisible(true); setVisible(false); diff --git a/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java b/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java index 3e927378..57d6be66 100644 --- a/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java +++ b/dozentenmodul/src/main/java/gui/intro/VmWareLink_GUI.java @@ -113,7 +113,7 @@ public class VmWareLink_GUI extends JFrame { chckbxNewCheckBox.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent arg0) { if(chckbxNewCheckBox.isSelected()) { - Config.setBoolean("main", "vmware", true); + Config.setVmwareLicense(true); Config.store(); } } diff --git a/dozentenmodul/src/main/java/util/GuiOrganizer.java b/dozentenmodul/src/main/java/util/GuiOrganizer.java index 5d6b2de8..55cc2030 100644 --- a/dozentenmodul/src/main/java/util/GuiOrganizer.java +++ b/dozentenmodul/src/main/java/util/GuiOrganizer.java @@ -1,19 +1,66 @@ package util; -import java.awt.Dimension; -import java.awt.Toolkit; +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 javax.swing.JOptionPane; + +/** + * An abstract class to organize the GUI. + * Currently only provide a method for centering Window-objects. + */ public abstract class GuiOrganizer { - /* receive GUI, set it to center of the screen */ + /** + * 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) { - Dimension dm = Toolkit.getDefaultToolkit().getScreenSize(); - double width = dm.getWidth(); - double height = dm.getHeight(); + + if (rect == null) getDisplayBounds(); + + double width = rect.getWidth(); + double height = rect.getHeight(); double xPosition = (width / 2 - gui.getWidth() / 2); double yPosition = (height / 2 - gui.getHeight() / 2); gui.setLocation((int) xPosition, (int) yPosition); } - } |