001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application.config;
017    
018    import org.springframework.beans.factory.InitializingBean;
019    import org.springframework.richclient.application.Application;
020    import org.springframework.richclient.application.ApplicationServices;
021    import org.springframework.richclient.application.ApplicationServicesLocator;
022    import org.springframework.richclient.application.ApplicationWindow;
023    import org.springframework.richclient.application.session.ApplicationSessionInitializer;
024    import org.springframework.richclient.application.statusbar.StatusBar;
025    import org.springframework.richclient.application.statusbar.support.DefaultStatusBar;
026    import org.springframework.richclient.application.support.ApplicationWindowCommandManager;
027    import org.springframework.richclient.command.CommandGroup;
028    import org.springframework.richclient.exceptionhandling.DefaultRegisterableExceptionHandler;
029    import org.springframework.richclient.exceptionhandling.RegisterableExceptionHandler;
030    
031    /**
032     * <p>
033     * Advisor for the Application. This class provides the startingPageId, an
034     * exceptionHandler for uncaught exceptions, application wide window
035     * structures(menu/toolbar/statusbar) and a number of hook methods that are
036     * called in the startup/closing process.
037     * </p>
038     *
039     * <p>
040     * The sequence in which the hooks are called is as follows:
041     * </p>
042     *
043     * <pre>
044     * Application Creation
045     *              {@link ApplicationLifecycleAdvisor#setApplication(Application)}
046     *              {@link ApplicationLifecycleAdvisor#onPreInitialize(Application)}
047     *
048     * Application Start
049     *              {@link ApplicationLifecycleAdvisor#onPreStartup()}
050     *
051     *              ApplicationWindow Creation
052     *                      {@link ApplicationLifecycleAdvisor#setOpeningWindow(ApplicationWindow)}
053     *                      {@link ApplicationLifecycleAdvisor#onPreWindowOpen(ApplicationWindowConfigurer)}
054     *                      {@link ApplicationLifecycleAdvisor#createWindowCommandManager()}
055     *                      {@link ApplicationLifecycleAdvisor#getMenuBarCommandGroup()}
056     *                      {@link ApplicationLifecycleAdvisor#getToolBarCommandGroup()}
057     *                      {@link ApplicationLifecycleAdvisor#getStatusBar()}
058     *                      {@link ApplicationLifecycleAdvisor#onCommandsCreated(ApplicationWindow)}
059     *
060     *              ApplicationWindow Creating the JFrame
061     *                      {@link ApplicationLifecycleAdvisor#onWindowCreated(ApplicationWindow)}
062     *                      ApplicationWindow Shows JFrame (setVisible(true))
063     *                      {@link ApplicationLifecycleAdvisor#onWindowOpened(ApplicationWindow)}
064     *
065     *              {@link ApplicationLifecycleAdvisor#onPostStartup()}
066     * </pre>
067     *
068     * <p>
069     * The remaining hook is called when the ApplicationWindow is closed:
070     * {@link ApplicationLifecycleAdvisor#onPreWindowClose(ApplicationWindow)}.
071     * </p>
072     *
073     * @author Keith Donald
074     * @author Jim Moore
075     */
076    public abstract class ApplicationLifecycleAdvisor implements InitializingBean {
077    
078            /** Application to work with. */
079            private Application application;
080    
081            /** The applicationWindow. */
082            private ApplicationWindow openingWindow;
083    
084            /** Initial page to show. */
085            private String startingPageId;
086    
087        private ApplicationSessionInitializer applicationSessionInitializer;
088    
089            /** ExceptionHandler to catch all uncaught exceptions. */
090            private RegisterableExceptionHandler registerableExceptionHandler;
091    
092            /**
093             * This is used to allow the ViewDescriptor to be lazily created when the
094             * ApplicationWindow is opened. Useful when the ApplicationAdvisor needs to
095             * do things before ViewDescriptor should be created, such as setting up a
096             * security context.
097             *
098             * @param pageDescriptorId id of the pageDescriptor bean to show on startup.
099             *
100             * @see #getStartingPageId()
101             */
102            public void setStartingPageId(String pageDescriptorId) {
103                    this.startingPageId = pageDescriptorId;
104            }
105    
106            /**
107             * Sets the exception handler which will be registered upon initialization
108             * to handle uncaught throwables.
109             *
110             * By default this is a DefaultRegisterableExceptionHandler, which is
111             * inferior to a well configured DelegatingExceptionHandler (java 1.5 only).
112             *
113             * @param registerableExceptionHandler the exception handler which will
114             * handle uncaught throwables
115             */
116            public void setRegisterableExceptionHandler(RegisterableExceptionHandler registerableExceptionHandler) {
117                    this.registerableExceptionHandler = registerableExceptionHandler;
118            }
119    
120            /**
121             * After properties are set, register the exceptionHandler.
122             */
123            public void afterPropertiesSet() throws Exception {
124                    getRegisterableExceptionHandler().registerExceptionHandler();
125            }
126    
127            /**
128             * @return the id of the starting Page.
129             */
130            public String getStartingPageId() {
131                    return startingPageId;
132            }
133    
134            /**
135             * @return Application.
136             */
137            protected Application getApplication() {
138                    return application;
139            }
140    
141            /**
142             * @return ApplicationServices.
143             */
144            protected ApplicationServices getApplicationServices() {
145                    return ApplicationServicesLocator.services();
146            }
147    
148            /**
149             * Hook called right after the application has been created.
150             *
151             * @param application the application.
152             */
153            public void onPreInitialize(Application application) {
154    
155            }
156    
157            /**
158             * Hook called right before the applicationWindow is created.
159             */
160            public void onPreStartup() {
161    
162            }
163    
164            /**
165             * Hook called right after the applicationWindow is created.
166             */
167            public void onPostStartup() {
168    
169            }
170    
171            /**
172             * Hook called right afther the application is closed.
173             */
174            public void onShutdown() {
175    
176            }
177    
178            /**
179             * @param window the openingWindow.
180             */
181            public void setOpeningWindow(ApplicationWindow window) {
182                    this.openingWindow = window;
183            }
184    
185            /**
186             * Hook called right before the application opens a window.
187             *
188             * @param configurer
189             */
190            public void onPreWindowOpen(ApplicationWindowConfigurer configurer) {
191                    configurer.setTitle(getApplication().getName());
192                    configurer.setImage(getApplication().getImage());
193            }
194    
195            /**
196             * @return the openingWindow.
197             */
198            protected final ApplicationWindow getOpeningWindow() {
199                    return openingWindow;
200            }
201    
202            /**
203             * Create a {@link ApplicationWindowCommandManager} for the application.
204             *
205             * @return applicationWindowCommandManager.
206             */
207            public ApplicationWindowCommandManager createWindowCommandManager() {
208                    return new ApplicationWindowCommandManager();
209            }
210    
211            /**
212             * Create the menuBar for the application.
213             *
214             * @return a CommandGroup.
215             */
216            public CommandGroup getMenuBarCommandGroup() {
217                    return new CommandGroup();
218            }
219    
220            /**
221             * Create the toolBar for the application.
222             *
223             * @return a CommandGroup.
224             */
225            public CommandGroup getToolBarCommandGroup() {
226                    return new CommandGroup();
227            }
228    
229            /**
230             * Create the statusBar for the application.
231             *
232             * @return a statusBar.
233             */
234            public StatusBar getStatusBar() {
235                    return new DefaultStatusBar();
236            }
237    
238            /**
239             * Hook called right after commands are initialized. Typically the next step
240             * after the get*CommandGroup() methods are called.
241             *
242             * @param window applicationWindow.
243             */
244            public void onCommandsCreated(ApplicationWindow window) {
245    
246            }
247    
248            /**
249             * Hook called right after the window (JFrame) of the application is
250             * created.
251             *
252             * @param window applicationWindow.
253             */
254            public void onWindowCreated(ApplicationWindow window) {
255    
256            }
257    
258            /**
259             * Hook called right after the window (JFrame) of the application is shown
260             * (setVisible(true)).
261             *
262             * @param window applicationWindow.
263             */
264            public void onWindowOpened(ApplicationWindow window) {
265    
266            }
267    
268            /**
269             * Check if the ApplicationWindow can close.
270             *
271             * @param window the applicationWindow that should be closed.
272             * @return <code>true</code> if the window may close.
273             */
274            public boolean onPreWindowClose(ApplicationWindow window) {
275                    return true;
276            }
277    
278            /**
279             * @return the ExceptionHandler to be registered as
280             * uncaughtExceptionHandler.
281             */
282            public RegisterableExceptionHandler getRegisterableExceptionHandler() {
283                    if (registerableExceptionHandler == null) {
284                            this.registerableExceptionHandler = new DefaultRegisterableExceptionHandler();
285                    }
286                    return registerableExceptionHandler;
287            }
288    
289            /**
290             * @param application set the current application.
291             */
292            public void setApplication(Application application) {
293                    this.application = application;
294            }
295    
296        public ApplicationSessionInitializer getApplicationSessionInitializer()
297        {
298            return applicationSessionInitializer;
299        }
300    
301        public void setApplicationSessionInitializer(ApplicationSessionInitializer applicationSessionInitializer)
302        {
303            this.applicationSessionInitializer = applicationSessionInitializer;
304        }
305    }