563N/A/*
563N/A * CDDL HEADER START
563N/A *
563N/A * The contents of this file are subject to the terms of the
563N/A * Common Development and Distribution License, Version 1.0 only
563N/A * (the "License"). You may not use this file except in compliance
563N/A * with the License.
563N/A *
563N/A * You can obtain a copy of the license at
563N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
563N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
563N/A * See the License for the specific language governing permissions
563N/A * and limitations under the License.
563N/A *
563N/A * When distributing Covered Code, include this CDDL HEADER in each
563N/A * file and include the License file at
563N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
563N/A * add the following below this CDDL HEADER, with the fields enclosed
873N/A * by brackets "[]" replaced with your own identifying information:
563N/A * Portions Copyright [yyyy] [name of copyright owner]
563N/A *
563N/A * CDDL HEADER END
563N/A *
563N/A *
3215N/A * Copyright 2006-2008 Sun Microsystems, Inc.
563N/A */
563N/A
563N/Apackage org.opends.quicksetup;
563N/A
2086N/Aimport static org.opends.messages.QuickSetupMessages.*;
2086N/A
563N/Aimport java.awt.Dimension;
563N/Aimport java.awt.Frame;
563N/Aimport java.awt.Graphics;
563N/Aimport java.awt.Image;
563N/Aimport java.awt.MediaTracker;
563N/Aimport java.awt.Toolkit;
563N/Aimport java.awt.Window;
563N/A
563N/Aimport javax.swing.SwingUtilities;
563N/A
1824N/Aimport org.opends.quicksetup.ui.Utilities;
563N/A
563N/A/**
662N/A * This is the class that displays a splash screen and in the background it will
662N/A * create QuickSetup object.
662N/A *
662N/A * The main method of this class is directly called by the Java Web Start
662N/A * mechanism to launch the JWS setup.
563N/A *
563N/A * This class tries to minimize the time to be displayed. So it does the loading
563N/A * of the setup class in runtime once we already have displayed the splash
563N/A * screen. This is why the quickSetup variable is of type Object.
662N/A *
662N/A * This class can be reused by simply overwriting the methods
662N/A * constructApplication() and displayApplication().
563N/A */
563N/Apublic class SplashScreen extends Window
563N/A{
563N/A private static final long serialVersionUID = 8918803902867388766L;
563N/A
563N/A private Image image;
563N/A
662N/A private Object quickSetup;
563N/A
662N/A private Class<?> quickSetupClass;
563N/A
563N/A // Constant for the display of the splash screen
563N/A private static final int MIN_SPLASH_DISPLAY = 3000;
563N/A
563N/A /**
563N/A * The main method for this class.
563N/A * It can be called from the event thread and outside the event thread.
563N/A * @param args arguments to be passed to the method QuickSetup.initialize
563N/A */
563N/A public static void main(String[] args)
563N/A {
662N/A SplashScreen screen = new SplashScreen();
662N/A screen.display(args);
563N/A }
563N/A
563N/A /**
563N/A * {@inheritDoc}
563N/A */
563N/A public void update(Graphics g)
563N/A {
563N/A paint(g);
563N/A }
563N/A
563N/A /**
563N/A * {@inheritDoc}
563N/A */
563N/A public void paint(Graphics g)
563N/A {
563N/A g.drawImage(image, 0, 0, this);
563N/A }
563N/A
563N/A /**
662N/A * Protected constructor to force to use the main method.
563N/A *
563N/A */
662N/A protected SplashScreen()
563N/A {
563N/A super(new Frame());
563N/A try
563N/A {
563N/A image = getSplashImage();
563N/A MediaTracker mt = new MediaTracker(this);
563N/A mt.addImage(image, 0);
563N/A mt.waitForID(0);
563N/A
563N/A int width = image.getWidth(this);
563N/A int height = image.getHeight(this);
563N/A setPreferredSize(new Dimension(width, height));
563N/A setSize(width, height);
1824N/A Utilities.centerOnScreen(this);
563N/A
563N/A } catch (Exception ex)
563N/A {
563N/A ex.printStackTrace(); // Bug
563N/A }
563N/A }
563N/A
563N/A /**
662N/A * The method used to display the splash screen. It will also call create
662N/A * the application associated with this SplashScreen and display it.
662N/A * It can be called from the event thread and outside the event thread.
662N/A * @param args arguments to be passed to the method QuickSetup.initialize
662N/A */
662N/A protected void display(String[] args)
662N/A {
662N/A if (SwingUtilities.isEventDispatchThread())
662N/A {
662N/A final String[] fArgs = args;
662N/A Thread t = new Thread(new Runnable()
662N/A {
662N/A public void run()
662N/A {
662N/A mainOutsideEventThread(fArgs);
662N/A }
662N/A });
662N/A t.start();
662N/A } else
662N/A {
662N/A mainOutsideEventThread(args);
662N/A }
662N/A }
662N/A
662N/A /**
563N/A * This method creates the image directly instead of using UIFactory to reduce
563N/A * class loading.
563N/A * @return the splash image.
563N/A */
563N/A private Image getSplashImage()
563N/A {
2086N/A String resource = INFO_SPLASH_ICON.get().toString();
563N/A resource = "org/opends/quicksetup/" + resource;
563N/A return Toolkit.getDefaultToolkit().createImage(
563N/A this.getClass().getClassLoader().getResource(resource));
563N/A }
563N/A
563N/A /**
563N/A * This is basically the method that is execute in SplashScreen.main but it
563N/A * it assumes that is being called outside the event thread.
563N/A *
563N/A * @param args arguments to be passed to the method QuickSetup.initialize.
563N/A */
662N/A private void mainOutsideEventThread(String[] args)
563N/A {
563N/A displaySplashScreen();
563N/A long splashDisplayStartTime = System.currentTimeMillis();
662N/A constructApplication(args);
563N/A sleepIfNecessary(splashDisplayStartTime);
563N/A disposeSplashScreen();
662N/A displayApplication();
563N/A }
563N/A
563N/A /**
563N/A * This methods displays the splash screen.
563N/A * This method assumes that is being called outside the event thread.
563N/A */
662N/A private void displaySplashScreen()
563N/A {
563N/A try
563N/A {
563N/A SwingUtilities.invokeAndWait(new Runnable()
563N/A {
563N/A public void run()
563N/A {
662N/A setVisible(true);
563N/A }
563N/A });
563N/A } catch (Exception ex)
563N/A {
563N/A ex.printStackTrace();
563N/A }
563N/A }
563N/A
563N/A /**
662N/A * This methods constructs the objects before displaying them.
563N/A * This method assumes that is being called outside the event thread.
662N/A * This method can be overwritten by subclasses to construct other objects
662N/A * different than the Quick Setup.
662N/A * @param args arguments passed in the main of this class.
563N/A */
662N/A protected void constructApplication(String[] args)
563N/A {
563N/A try
563N/A {
1126N/A quickSetupClass = Class.forName("org.opends.quicksetup.ui.QuickSetup");
563N/A quickSetup = quickSetupClass.newInstance();
563N/A quickSetupClass.getMethod("initialize", new Class[]
563N/A { String[].class }).invoke(quickSetup, new Object[]
563N/A { args });
563N/A } catch (Exception e)
563N/A {
563N/A InternalError error =
563N/A new InternalError("Failed to invoke initialize method");
563N/A error.initCause(e);
563N/A throw error;
563N/A }
563N/A }
563N/A
563N/A /**
563N/A * This method displays the QuickSetup dialog.
1126N/A * @see org.opends.quicksetup.ui.QuickSetup#display
563N/A * This method assumes that is being called outside the event thread.
662N/A * This method can be overwritten by subclasses to construct other objects
662N/A * different than the Quick Setup.
563N/A */
662N/A protected void displayApplication()
563N/A {
563N/A try
563N/A {
563N/A SwingUtilities.invokeAndWait(new Runnable()
563N/A {
563N/A public void run()
563N/A {
563N/A try
563N/A {
563N/A quickSetupClass.getMethod("display").invoke(quickSetup);
563N/A } catch (Exception e)
563N/A {
563N/A InternalError error =
563N/A new InternalError("Failed to invoke display method");
563N/A error.initCause(e);
563N/A throw error;
563N/A }
563N/A }
563N/A });
563N/A } catch (Exception ex)
563N/A {
1502N/A // do nothing;
563N/A }
563N/A }
563N/A
563N/A /**
563N/A * Disposes the splash screen.
563N/A * This method assumes that is being called outside the event thread.
563N/A */
662N/A private void disposeSplashScreen()
563N/A {
563N/A try
563N/A {
563N/A SwingUtilities.invokeAndWait(new Runnable()
563N/A {
563N/A public void run()
563N/A {
662N/A setVisible(false);
662N/A dispose();
563N/A }
563N/A });
563N/A } catch (Exception ex)
563N/A {
1502N/A // do nothing;
563N/A }
563N/A }
563N/A
563N/A /**
563N/A * This method just executes an sleep depending on how long the splash
563N/A * screen has been displayed. The idea of calling this method is to have the
563N/A * splash screen displayed a minimum time (specified by
563N/A * MIN_SPLASH_DISPLAY).
563N/A * @param splashDisplayStartTime the time in milliseconds when the splash
563N/A * screen started displaying.
563N/A */
662N/A private void sleepIfNecessary(long splashDisplayStartTime)
563N/A {
563N/A long t2 = System.currentTimeMillis();
563N/A
563N/A long sleepTime = MIN_SPLASH_DISPLAY - (t2 - splashDisplayStartTime);
563N/A
563N/A if (sleepTime > 0)
563N/A {
563N/A try
563N/A {
563N/A Thread.sleep(sleepTime);
563N/A } catch (Exception ex)
563N/A {
1502N/A // do nothing;
563N/A }
563N/A }
563N/A }
563N/A}