Revision: 1241
Author:   peterdb
Date:     2006-07-26 05:21:04 -0700 (Wed, 26 Jul 2006)
ViewCVS:  http://svn.sourceforge.net/spring-rich-c/?rev=1241&view=rev

Log Message:
-----------
checkin of code for RCP-112

Added Paths:
-----------
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashApplicationLauncher.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashScreen.java
Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashApplicationLauncher.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashApplicationLauncher.java
                              (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashApplicationLauncher.java
      2006-07-26 12:21:04 UTC (rev 1241)
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.application.splash;
+
+import java.awt.EventQueue;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.richclient.application.Application;
+import org.springframework.richclient.application.ApplicationException;
+import org.springframework.richclient.application.SplashScreen;
+import org.springframework.util.Assert;
+
+/**
+ * Custom application launcher for showing the 
<code>ProgressSplashScreen</code>.
+ * <p>
+ * 
+ * @author Peter De Bruycker
+ */
+public class ProgressSplashApplicationLauncher {
+    public static final String SPLASH_SCREEN_BEAN_ID = "splashScreen";
+
+    public static final String APPLICATION_BEAN_ID = "application";
+
+    private final Log logger = LogFactory.getLog( getClass() );
+
+    private ProgressSplashScreen splashScreen;
+
+    private ApplicationContext rootApplicationContext;
+
+    /**
+     * Launch the application using the spring application context at the 
provided path
+     * for configuration.
+     * 
+     * @param contextPath the classpath application context path
+     */
+    public ProgressSplashApplicationLauncher( String contextPath ) {
+        this( new String[] { contextPath } );
+    }
+
+    /**
+     * Launch the application using the spring application context at the 
provided paths
+     * for configuration.
+     * 
+     * @param contextPath the classpath application context paths
+     */
+    public ProgressSplashApplicationLauncher( String[] contextPath ) {
+        Assert.notEmpty( contextPath, "One or more root rich client 
application context paths must be provided" );
+        this.rootApplicationContext = loadRootApplicationContext( contextPath 
);
+        launchMyRichClient();
+    }
+
+    private ApplicationContext loadRootApplicationContext( String[] 
contextPaths ) {
+        try {
+            return new ClassPathXmlApplicationContext( contextPaths );
+        } catch( Exception e ) {
+            logger.warn( "Exception occured initializing application startup 
context.", e );
+            throw new ApplicationException( "Unable to start rich client 
application", e );
+        }
+    }
+
+    /**
+     * Launch this rich client application; with the startup context loading 
first, built
+     * from the <code>startupContextPath</code> location in the classpath.
+     * <p>
+     * It is recommended that the startup context contain contain a splash 
screen
+     * definition for quick loading & display.
+     * <p>
+     * Once the splash screen is displayed, the main application context is 
then
+     * initialized, built from the <code>contextPaths</code> location(s) in the
+     * classpath. The root application bean is retrieved and the startup 
lifecycle begins.
+     */
+    private void launchMyRichClient() {
+        displaySplashScreen( rootApplicationContext );
+
+        try {
+            Application application = (Application) 
rootApplicationContext.getBean( APPLICATION_BEAN_ID,
+                    Application.class );
+            application.start();
+        } catch( NoSuchBeanDefinitionException e ) {
+            logger.error( "A single org.springframework.richclient.Application 
bean definition must be defined "
+                    + "in the main application context", e );
+            throw e;
+        } catch( RuntimeException e ) {
+            logger.error( "Exception occured initializing Application bean", e 
);
+            throw new ApplicationException( "Unable to start rich client 
application", e );
+        } finally {
+            destroySplashScreen();
+            logger.debug( "Launcher thread exiting..." );
+        }
+    }
+
+    private void displaySplashScreen( BeanFactory beanFactory ) {
+        try {
+            if( beanFactory.containsBean( SPLASH_SCREEN_BEAN_ID ) ) {
+                this.splashScreen = (ProgressSplashScreen) 
beanFactory.getBean( SPLASH_SCREEN_BEAN_ID,
+                        ProgressSplashScreen.class );
+                logger.info( "Displaying application splash screen..." );
+            } else {
+                logger.info( "No splash screen bean found to 
display--continuing..." );
+            }
+        } catch( Exception e ) {
+            logger.warn( "Unable to load and display startup splash screen.", 
e );
+        }
+    }
+
+    private void destroySplashScreen() {
+        if( splashScreen != null ) {
+            logger.debug( "Closing splash screen..." );
+            new SplashScreenCloser( splashScreen );
+        }
+    }
+
+    /**
+     * Closes the splash screen in the event dispatching (GUI) thread.
+     * 
+     * @author Keith Donald
+     * @see SplashScreen
+     */
+    public static class SplashScreenCloser {
+
+        /**
+         * Closes the currently-displayed, non-null splash screen.
+         * 
+         * @param splashScreen
+         */
+        public SplashScreenCloser( final ProgressSplashScreen splashScreen ) {
+
+            /*
+             * Removes the splash screen.
+             * 
+             * Invoke this <code> Runnable </code> using <code> 
EventQueue.invokeLater
+             * </code> , in order to remove the splash screen in a thread-safe 
manner.
+             */
+            EventQueue.invokeLater( new Runnable() {
+                public void run() {
+                    splashScreen.dispose();
+                }
+            } );
+        }
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashScreen.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashScreen.java
                           (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/application/splash/ProgressSplashScreen.java
   2006-07-26 12:21:04 UTC (rev 1241)
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.application.splash;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.Image;
+import java.awt.MediaTracker;
+import java.awt.Rectangle;
+import java.awt.Toolkit;
+import java.net.URL;
+import java.util.logging.Logger;
+
+import javax.swing.ImageIcon;
+import javax.swing.JLabel;
+import javax.swing.JProgressBar;
+import javax.swing.JWindow;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
+
+/**
+ * Splash screen implementation that shows the progress of the Spring 
application context loading.
+ * <p>
+ * TODO i18n of progressbar messages
+ * TODO define SplashScreen interface for easier pluggable splash screens
+ * 
+ * @author Peter De Bruycker
+ */
+public class ProgressSplashScreen implements ApplicationContextAware, 
BeanPostProcessor, InitializingBean {
+
+    private JWindow window;
+
+    private Image image;
+
+    private String imageResourcePath;
+
+    private static final Logger logger = Logger.getLogger( 
ProgressSplashScreen.class.getPackage().getName() );
+    private ApplicationContext context;
+    private JProgressBar progressBar;
+    private int progress = 0;
+    private boolean showProgressLabel;
+
+    public ProgressSplashScreen() {
+        progressBar = new JProgressBar();
+    }
+
+    /**
+     * Show the splash screen.
+     */
+    public void splash() {
+        window = new JWindow();
+        if( image == null ) {
+            image = loadImage( imageResourcePath );
+            if( image == null ) {
+                return;
+            }
+        }
+        MediaTracker mediaTracker = new MediaTracker( window );
+        mediaTracker.addImage( image, 0 );
+        try {
+            mediaTracker.waitForID( 0 );
+        } catch( InterruptedException e ) {
+            logger.warning( "Interrupted while waiting for splash image to 
load." );
+        }
+
+        window.getContentPane().add( new JLabel( new ImageIcon( image ) ) );
+        window.getContentPane().add( progressBar, BorderLayout.SOUTH );
+        window.pack();
+        center( window );
+
+        window.setVisible( true );
+    }
+
+    public void setImageResourcePath( String path ) {
+        Assert.hasText( path, "The splash screen image resource path is 
required" );
+        this.imageResourcePath = path;
+    }
+
+    private void center( JWindow window ) {
+        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
+        Rectangle r = window.getBounds();
+        window.setLocation( (screen.width - r.width) / 2, (screen.height - 
r.height) / 2 );
+    }
+
+    /**
+     * Dispose of the the splash screen. Once disposed, the same splash screen 
instance
+     * may not be shown again.
+     */
+    public void dispose() {
+        window.dispose();
+        window = null;
+    }
+
+    private Image loadImage( String path ) {
+        URL url = this.getClass().getResource( path );
+        if( url == null ) {
+            logger.warning( "Unable to locate splash screen in classpath at: " 
+ path );
+            return null;
+        }
+        return Toolkit.getDefaultToolkit().createImage( url );
+    }
+
+    /**
+     * @see 
org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
+     */
+    public void setApplicationContext( ApplicationContext context ) throws 
BeansException {
+        this.context = context;
+    }
+
+    /**
+     * @see 
org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object,
+     *      java.lang.String)
+     */
+    public Object postProcessBeforeInitialization( Object bean, String name ) 
throws BeansException {
+        // if (context.containsBeanDefinition(name)) {
+        progressBar.setValue( progress++ );
+        if( showProgressLabel ) {
+            progressBar.setString( "Loading bean " + name );
+        }
+        // }
+        return bean;
+    }
+
+    /**
+     * @see 
org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object,
+     *      java.lang.String)
+     */
+    public Object postProcessAfterInitialization( Object bean, String name ) 
throws BeansException {
+        return bean;
+    }
+
+    /**
+     * @see 
org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
+     */
+    public void afterPropertiesSet() throws Exception {
+        if( context.containsBean( "lookAndFeelConfigurer" ) ) {
+            context.getBean( "lookAndFeelConfigurer" );
+            progressBar = new JProgressBar();
+        }
+
+        Assert.state( StringUtils.hasText( imageResourcePath ), "The splash 
screen image resource path is required" );
+
+        if( showProgressLabel ) {
+            progressBar.setStringPainted( true );
+            progressBar.setString( "Loading context" );
+        }
+        progressBar.setMinimum( 0 );
+        progressBar.setMaximum( calculateMaximum() );
+        splash();
+    }
+
+    private int calculateMaximum() {
+        int maximum = 0;
+        String[] beanNames = context.getBeanDefinitionNames();
+        for( int i = 0; i < beanNames.length; i++ ) {
+            if( context.isSingleton( beanNames[i] ) )
+                maximum++;
+        }
+        return maximum;
+    }
+
+    public boolean getShowProgressLabel() {
+        return showProgressLabel;
+    }
+
+    public void setShowProgressLabel( boolean showProgressLabel ) {
+        this.showProgressLabel = showProgressLabel;
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to