diff options
| author | Jonathan Bauer | 2015-03-03 19:02:48 +0100 |
|---|---|---|
| committer | Jonathan Bauer | 2015-03-03 19:02:48 +0100 |
| commit | 0447841f3a08890bf746625d0f17976adada6ac8 (patch) | |
| tree | f63bd9f2ac8d77f4732b70cac8e5c0497f4d3a45 /dozentenmodul/src/main/java/util | |
| parent | warnings fix (diff) | |
| download | tutor-module-0447841f3a08890bf746625d0f17976adada6ac8.tar.gz tutor-module-0447841f3a08890bf746625d0f17976adada6ac8.tar.xz tutor-module-0447841f3a08890bf746625d0f17976adada6ac8.zip | |
bwIDM - Shibboleth login working for Freiburg's SP - more to come
rework GUI classes to work with GuiManager: use GuiManager.show(<GUI to show>) and GuiManager.openPopup(<popup like About_GUI or ListAllOtherUsers_GUI>) only!
static openlinks class (models/links.java deleted). There are keywords to open links, e.g. OpenLinks.openWebpage("faq"). Please see the class.
Diffstat (limited to 'dozentenmodul/src/main/java/util')
6 files changed, 367 insertions, 156 deletions
diff --git a/dozentenmodul/src/main/java/util/GuiManager.java b/dozentenmodul/src/main/java/util/GuiManager.java new file mode 100644 index 00000000..a2c4cd1f --- /dev/null +++ b/dozentenmodul/src/main/java/util/GuiManager.java @@ -0,0 +1,273 @@ +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.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)); + + // 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 + LOGGER.debug("No 'HELP_MESSAGE' defined in class: " + currentFrame.getClass().getName()); + return true; + } 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 + LOGGER.debug("HELP_MESSAGE of '" + currentFrame.getClass().getName() + "': " + test); + // 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); + } +} diff --git a/dozentenmodul/src/main/java/util/GuiOrganizer.java b/dozentenmodul/src/main/java/util/GuiOrganizer.java deleted file mode 100644 index 55cc2030..00000000 --- a/dozentenmodul/src/main/java/util/GuiOrganizer.java +++ /dev/null @@ -1,66 +0,0 @@ -package util; - -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 { - - /** - * 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 xPosition = (width / 2 - gui.getWidth() / 2); - double yPosition = (height / 2 - gui.getHeight() / 2); - gui.setLocation((int) xPosition, (int) yPosition); - } -} diff --git a/dozentenmodul/src/main/java/util/ListAllOtherUsers_GUI.java b/dozentenmodul/src/main/java/util/ListAllOtherUsers_GUI.java index 56adaf4f..37f30dc3 100644 --- a/dozentenmodul/src/main/java/util/ListAllOtherUsers_GUI.java +++ b/dozentenmodul/src/main/java/util/ListAllOtherUsers_GUI.java @@ -1,19 +1,13 @@ package util; -import gui.intro.About_GUI; - import java.awt.Component; import java.awt.FlowLayout; import java.awt.Font; import java.awt.SystemColor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -22,10 +16,6 @@ import javax.swing.DefaultListSelectionModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSeparator; @@ -38,7 +28,6 @@ import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; -import models.Links; import models.SessionData; import models.person; @@ -69,7 +58,7 @@ public class ListAllOtherUsers_GUI extends JFrame { List<String> user = new ArrayList<String>(); - private static final String HELP_MESSAGE = "<html><div align=\"center\">" + public static final String HELP_MESSAGE = "<html><div align=\"center\">" + "In der Übersicht sehen Sie primär alle Veranstaltungen, die Sie erzeugt haben.<br />" + "Sie können die Veranstaltungen hier löschen. Alternativ werden veraltete Einträge irgendwann automatisch gelöscht.<br />" + "Veraltet bedeutet, dass Veranstaltungen, die drei Monate lang nicht augerufen wurden, vorerst deaktiviert werden." @@ -90,7 +79,7 @@ public class ListAllOtherUsers_GUI extends JFrame { * Constructor */ - public ListAllOtherUsers_GUI(Component formerGUI, final JTable table, + public ListAllOtherUsers_GUI(final JTable table, final int userIDPos) { // get table model to work with @@ -101,11 +90,6 @@ public class ListAllOtherUsers_GUI extends JFrame { this.userIDPos=userIDPos; addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent arg0) { - // Beendet die Anwendung nach klick auf X - dispose(); - } @Override public void windowOpened(WindowEvent arg0) { @@ -133,7 +117,7 @@ public class ListAllOtherUsers_GUI extends JFrame { setTitle("bwLehrpool Suite - Benutzer hinzufügen"); // Zentriert das Fenster in die Bildmitte setBounds(0, 0, 531, 673); - setLocationRelativeTo(formerGUI); + getContentPane().setLayout(null); { @@ -270,68 +254,6 @@ public class ListAllOtherUsers_GUI extends JFrame { JSeparator separator_1 = new JSeparator(); separator_1.setBounds(0, 552, 836, 1); getContentPane().add(separator_1); - - JMenuBar menuBar = new JMenuBar(); - setJMenuBar(menuBar); - - JMenu mnNewMenu_1 = new JMenu("Hilfe"); - menuBar.add(mnNewMenu_1); - - JMenuItem mntmFaq = new JMenuItem("FAQ"); - mntmFaq.addMouseListener(new MouseAdapter() { - @Override - public void mousePressed(MouseEvent arg0) { - - URI windows; - try { - windows = new URI(Links.getFAQ()); - OpenLinks.openWebpage(windows); - } catch (URISyntaxException e) { - - e.printStackTrace(); - } - } - }); - mnNewMenu_1.add(mntmFaq); - - JMenuItem mntmOtrs = new JMenuItem("OTRS"); - mntmOtrs.addMouseListener(new MouseAdapter() { - @Override - public void mousePressed(MouseEvent arg0) { - - URI windows; - try { - windows = new URI(Links.getOTRS()); - OpenLinks.openWebpage(windows); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } - }); - mnNewMenu_1.add(mntmOtrs); - - JMenuItem mntmAbout = new JMenuItem("About"); - mntmAbout.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - About_GUI ab = new About_GUI(); - ab.setVisible(true); - } - }); - mnNewMenu_1.add(mntmAbout); - - JMenu mnNewMenu_Info = new JMenu("Info"); - mnNewMenu_Info.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent arg0) { - JOptionPane.showMessageDialog(c, HELP_MESSAGE, - "Hilfe zu dieser Oberfläche", - JOptionPane.INFORMATION_MESSAGE); - } - }); - menuBar.add(mnNewMenu_Info); - - c = this; - } diff --git a/dozentenmodul/src/main/java/util/OpenLinks.java b/dozentenmodul/src/main/java/util/OpenLinks.java index 2dd7b936..51695c96 100644 --- a/dozentenmodul/src/main/java/util/OpenLinks.java +++ b/dozentenmodul/src/main/java/util/OpenLinks.java @@ -2,17 +2,74 @@ package util; import java.awt.Desktop; import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import javax.swing.JOptionPane; +import org.apache.log4j.Logger; + public class OpenLinks { - public static void openWebpage(URI uri) { + /** + * Logger instance for this class + */ + private final static Logger LOGGER = Logger.getLogger(ShibbolethECP.class); + + /** + * Map containing the links + */ + @SuppressWarnings("serial") + private static Map<String, String> links = Collections.unmodifiableMap(new HashMap<String, String>(){{ + put("faq", "http://bwlehrpool.hs-offenburg.de"); + put("otrs", "http://bwlehrpool.hs-offenburg.de"); + put("vmware", "https://my.vmware.com/de/web/vmware/free#desktop_end_user_computing/vmware_player/6_0"); + put("intro", "http://www.hs-offenburg.de/fileadmin/Einrichtungen/hrz/Projekte/bwLehrpool/3_bwLehrpool_-_Image_einbinden_und_starten.pdf"); + + }}); + + /** + * Static URIs + */ + + private static Map<String, URI> uris; + static { + // temp map + Map<String, URI> tmpUris = new HashMap<String, URI>(); + for (String key : links.keySet()) { + URI tmp; + try { + tmp = new URI(links.get(key)); + } catch (URISyntaxException e) { + // should never happen! + LOGGER.error("Bad URI syntax of '" + key + "', see trace: ", e); + tmp = null; + } + tmpUris.put(key, tmp); + } + // check sizes of maps to be equal + if (links.size() != tmpUris.size()) { + LOGGER.error("Links and URIs have different sizes, this should not happen. Contact a developper."); + } + + // all good, save it to the actual 'uris' map + uris = Collections.unmodifiableMap(tmpUris); + } + + + public static void openWebpage(String key) { + // first check if we have the link for the request key + if (!uris.containsKey(key)) { + LOGGER.error("OpenLinks has to link to '" + key + "'. Check if the given key actually exists."); + return; + } Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { try { - desktop.browse(uri); + desktop.browse(uris.get(key)); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, diff --git a/dozentenmodul/src/main/java/util/ServiceProviderResponse.java b/dozentenmodul/src/main/java/util/ServiceProviderResponse.java index 99f120f1..37b93b33 100644 --- a/dozentenmodul/src/main/java/util/ServiceProviderResponse.java +++ b/dozentenmodul/src/main/java/util/ServiceProviderResponse.java @@ -1,7 +1,15 @@ package util; +import java.util.HashMap; + public class ServiceProviderResponse { public String status; - public String id; + public String firstName; + public String lastName; + public String mail; + public HashMap<String, String> satellites; + public String token; + public String sessionId; public String url; + public String error; } diff --git a/dozentenmodul/src/main/java/util/ShibbolethECP.java b/dozentenmodul/src/main/java/util/ShibbolethECP.java index b834b1a2..a3e13a38 100644 --- a/dozentenmodul/src/main/java/util/ShibbolethECP.java +++ b/dozentenmodul/src/main/java/util/ShibbolethECP.java @@ -33,6 +33,10 @@ public class ShibbolethECP { private static final Gson GSON = new GsonBuilder().create(); /** + * + */ + private static ServiceProviderResponse _lastResponse = null; + /** * URL for bwLehrpool registration */ private static URL _registrationUrl = null; @@ -71,7 +75,7 @@ public class ShibbolethECP { static { URI tmp; try { - tmp = new URI("https://bwlp-masterserver.ruf.uni-freiburg.de/secure-all/api.php"); + tmp = new URI("https://bwlp-masterserver.ruf.uni-freiburg.de/webif/shib/api.php"); } catch (URISyntaxException e) { // should never happen! LOGGER.error("Bad URI syntax of the service provider, see trace: ", e); @@ -80,6 +84,9 @@ public class ShibbolethECP { BWLP_SP = tmp; } + public static ServiceProviderResponse getResponse() { + return _lastResponse; + } /** * Fetches the resource * @@ -149,18 +156,18 @@ public class ShibbolethECP { LOGGER.error("I/O error, see trace: ", e); return ReturnCode.ERROR_OTHER; } - ServiceProviderResponse spr = null; + _lastResponse = null; try { - spr = GSON.fromJson(responseBody, ServiceProviderResponse.class); + _lastResponse = GSON.fromJson(responseBody, ServiceProviderResponse.class); } catch (JsonSyntaxException e) { LOGGER.error("Bad JSON syntax, see trace: ", e); return ReturnCode.ERROR_SP; } // TODO: here we will need to parse the answer accordingly. // no errors, meaning everything worked fine. - if (spr.status.equals("unregistered")) { + if (_lastResponse.status.equals("unregistered")) { try { - _registrationUrl = new URL(spr.url); + _registrationUrl = new URL(_lastResponse.url); } catch (MalformedURLException e) { LOGGER.error("URL returned by masterserver is malformed, see trace: " + e); return ReturnCode.ERROR_URL; @@ -168,7 +175,17 @@ public class ShibbolethECP { return ReturnCode.ERROR_UNREG; } // TODO the rest of the cases... - + if (_lastResponse.status.equals("error")) { + LOGGER.error("Server side error: " + _lastResponse.error); + return ReturnCode.ERROR_OTHER; + } + if (_lastResponse.status.equals("anonymous")) { + LOGGER.error("IdP did not forward user account information to SP. Contact developper."); + return ReturnCode.ERROR_OTHER; + } + if (_lastResponse.status.equals("ok")) { + return ReturnCode.NO_ERROR; + } // still here? then something else went wrong return ReturnCode.ERROR_OTHER; } |
