package com.onaro.sanscreen.client.login; import java.awt.Point; import java.io.IOException; import javax.security.auth.callback.Callback; import javax.security.auth.callback.UnsupportedCallbackException; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import com.onaro.sanscreen.client.SANscreenClientPlugin; /** * Displays a border less window with an image and a progress indicator. Used * to pass the time while the application initializes. */ public class SplashScreen extends JFrame implements LoginHandlerDialog { /** * Initializes a splash screen and displays it in the center of the screen. */ public SplashScreen() { /* * The following statement is needed just for Mac OS X transparency. It doesn't * affect Linux or Windows. * * Note: This statement stopped having the desired effect at some point due to * an Oracle bug (JDK-8013450). Oracle says the bug will be fixed in Java 9. * Meanwhile, a gray box is displayed behind the splash screen on Mac OS. * Not too bad, so will leave it as is and hope Oracle gets it working again. */ getRootPane().putClientProperty ("Window.alpha", new Float(0.0F)); //$NON-NLS-1$ // Create frame. Only seen by user in OS task bar and task switcher. setTitle (SANscreenClientPlugin.getDefault().getBranding().getProductName()); setIconImage (SANscreenClientPlugin.getDefault().getBranding().getWindowIcon()); setResizable (false); setUndecorated (true); setDefaultCloseOperation (EXIT_ON_CLOSE); // Create window that user sees. window = new SplashWindow (this); // Must call setVisible to make frame visible in OS task list, though we won't see frame. setVisible (true); // Repaint needed to force display of initial image, which is now the only image with a shadow. window.repaint(); } @Override public CallbackHandlerPane getCallbackHandlerPane() { return getLoginDialog().getCallbackHandlerPane(); } @Override public JButton getOkButton() { return getLoginDialog().getOkButton(); } @Override public JButton getCancelButton() { return getLoginDialog().getCancelButton(); } @Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { getLoginDialog().handle (callbacks); } @Override public void setLoginEnabled(boolean enabled) { getLoginDialog().setLoginEnabled (enabled); } @Override public void requestLogin() { // Make sure splash window is visible. Made invisible after login failure. window.showLoginBackground(); window.setVisible (true); window.toFront(); getLoginDialog().requestLogin(); } @Override public void failLogin() { // Hide splash window while showing About box that's displayed when login fails. hideMessage(); window.setVisible (false); getLoginDialog().failLogin(); // LoginHandler calls requestLogin() after failure. } @Override public void endLogin() { hideMessage(); if (loginDialog != null) { loginDialog.endLogin(); } window.toFront(); } /** * Show a message indicating that login is in process */ public void showMessageLoggingIn() { window.showMessageLoggingIn(); } /** * Show a message indicating that initialization is in process */ public void showMessageInitializing() { window.showMessageInitializing(); } /** * If a message is currently being displayed, change the message icon * @param icon message icon or {@code null} to display no icon * @see #showMessage */ public void setMessage (Icon icon) { window.setMessage (icon); } /** * If a message is currently being displayed, change the status text * @param text status text or {@code null} to display no text * @see #showMessage */ public void setStatus (String text) { window.setStatus (text); } /** * Hide any message that is displayed */ public void hideMessage() { window.hideMessage(); } /** * Hides and disposes the splash window and its owner frame. */ public void done() { setVisible (false); window.done(); dispose(); } private LoginHandlerDialog getLoginDialog() { if (loginDialog == null) { // Align login dialog to splash window. Point location = window.getLocationOnScreen(); location.x += LOGIN_DIALOG_X_OFFSET; location.y += LOGIN_DIALOG_Y_OFFSET; // Create login dialog box. CallbackHandlerDialog dialog = new CallbackHandlerDialog(this); dialog.setBounds (location.x, location.y, LOGIN_DIALOG_WIDTH, LOGIN_DIALOG_HEIGHT); loginDialog = dialog; } return loginDialog; } private static final long serialVersionUID = 1L; private static final int LOGIN_DIALOG_X_OFFSET = 205; private static final int LOGIN_DIALOG_Y_OFFSET = 80; private static final int LOGIN_DIALOG_WIDTH = 260; private static final int LOGIN_DIALOG_HEIGHT = 160; private final SplashWindow window; private LoginHandlerDialog loginDialog = null; }