View Javadoc

1   /***
2    * ApplicationWrapper.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing;
27  
28  import java.awt.Frame;
29  import java.awt.event.WindowAdapter;
30  import java.awt.event.WindowEvent;
31  import java.util.prefs.BackingStoreException;
32  import java.util.prefs.Preferences;
33  
34  import org.jcreme.state.MementoException;
35  import org.jcreme.swing.state.FrameMemento;
36  
37  /***
38   * 
39   * @author $Author: dbregeon $
40   * @version $Revision: 1.2 $
41   */
42  public abstract class AbstractApplicationWrapper {
43  	private final WindowAdapter windowListener = new WindowAdapter() {
44  		public void windowClosing(WindowEvent evt) {
45  			System.exit(0);
46  		}
47  	};
48  
49  	private FrameMemento applicationMemento = null;
50  
51  	private Preferences prefs = null;
52  
53  	/***
54  	 * 
55  	 * @return the name of the application.
56  	 */
57  	public abstract String getApplicationName();
58  
59  	/***
60  	 * 
61  	 * @return
62  	 * @throws Exception
63  	 */
64  	public abstract Frame buildApplication() throws Exception;
65  
66  	/***
67  	 * 
68  	 * @return the Memento that will enable to save the Frame's state.
69  	 */
70  	public abstract FrameMemento buildApplicationMemento();
71  
72  	/***
73  	 * 
74  	 *  
75  	 */
76  	public void exit() {
77  		if (this.prefs != null) {
78  			try {
79  				if (this.applicationMemento != null) {
80  					this.applicationMemento.store(this.prefs);
81  				}
82  
83  				this.prefs.flush();
84  			} catch (BackingStoreException e) {
85  				e.printStackTrace();
86  			} catch (MementoException e) {
87  				e.printStackTrace();
88  			}
89  		}
90  	}
91  
92  	/***
93  	 * 
94  	 * @param args
95  	 */
96  	public void start(final String args[]) {
97  		try {
98  			this.prefs = Preferences.userRoot().node(getApplicationName());
99  			this.applicationMemento = buildApplicationMemento();
100 			if (this.applicationMemento != null) {
101 				this.applicationMemento.load(this.prefs);
102 			}
103 			Frame app = buildApplication();
104 			Runtime.getRuntime().addShutdownHook(new Thread() {
105 				public void run() {
106 					exit();
107 				}
108 			});
109 			app.addWindowListener(this.windowListener);
110 			app.setVisible(true);
111 		} catch (Exception e) {
112 			e.printStackTrace();
113 		}
114 	}
115 
116 	protected FrameMemento getApplicationMemento() {
117 		return this.applicationMemento;
118 	}
119 }