KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > EnsmerManager


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer;
17
18 import java.io.*;
19 import com.buchuki.ensmer.prevayler.*;
20 import org.prevayler.*;
21 import java.util.*;
22
23 /**
24  * Class to manage the overall functions of Ensmer. Responsible for keeping
25  * references to the submanagers (<code>InterfaceManager</code>, <code>UserManager</code>
26  * , etc), and other <q>global-level</q> properties. Also keeps a reference
27  * to the <code>Prevayler</code> that manages object storage. <code>EnsmerManager</code>
28  * is a singleton class. Call the <code>getInstance()</code> static method to
29  * get the singleton instance of the <code>EnsmerManager</code> class.
30  *
31  * @author Dusty Phillips [dusty@buchuki.com]
32  */

33 public class EnsmerManager {
34
35     /**
36      * Construct the <code>EnsmerManager</code> object. This method is private
37      * to keep the class singleton. Call <code>getInstance()</code> to get a
38      * reference to a <code>EnsmerManager</code> object.
39      *
40      * @throws Exception if the prevayler cannot be instantiated
41      */

42     private EnsmerManager() {
43     }
44
45     /**
46      * Retrieve a reference to the <code>InterfaceManager</code> used to set up
47      * the frame and scenegraph.
48      *
49      * @return Ensmer's InterfaceManager
50      */

51     public InterfaceManager getInterfaceManager() {
52         return interfaceManager;
53     }
54
55     /**
56      * Retrieve a reference to the AreaManager used to manage the distinct areas
57      * of objects
58      *
59      * @return The areaManager value
60      */

61     public AreaManager getAreaManager() {
62         return areaManager;
63     }
64
65     /**
66      * Retrieve a reference to the UserManager that manages user input and
67      * positioning data
68      *
69      * @return Ensmer's UserManager
70      */

71     public UserManager getUserManager() {
72         return userManager;
73     }
74
75     /**
76      * Retrieves a reference to the Backhoe that manages loading of objects and
77      * object typse.
78      *
79      * @return Ensmer's Backhoe
80      */

81     public Backhoe getBackhoe() {
82         return backhoe;
83     }
84
85     /**
86      * Retrieve a reference to the SelectionManager that manages selected
87      * objects for the current area
88      *
89      * @return Ensmer's SelectionManager
90      */

91     public SelectionManager getSelectionManager() {
92         return selectionManager;
93     }
94     
95     /**
96      * Access Ensmer's SpecialAreaManager that manages named areas and creates
97      * built in special areas.
98      *
99      * @return Ensmer's SpecialAreaManager
100      */

101     public SpecialAreaManager getSpecialAreaManager() {
102         return specialAreaManager;
103     }
104     
105     /**
106      * Access Ensmer's ConfigManager to retrieve a reference to the settings
107      * directory and other configuration information
108      *
109      * @return Ensmers ConfigManager
110      */

111     public ConfigManager getConfigManager() {
112         return configManager;
113     }
114     
115     /**
116      * Retrieve a reference to the Prevayler used to serialize Ensmer data
117      *
118      * @return Ensmer's prevayler, which accesses a <code>EnsmerPrevayler</code>
119      * object
120      */

121     Prevayler getPrevayler() {
122         return prevayler;
123     }
124
125     /**
126      * Callback function called when the Ensmer Window is closed, or possibly if
127      * a special exit function is called. Snapshots prevayler and exits the
128      * system.
129      */

130     void closeEnsmer() {
131         try {
132             prevayler.takeSnapshot();
133         } catch (IOException e) {
134             //will be recovered from transaction logs
135
}
136         System.exit(0);
137     }
138
139     /**
140      * Initialize a newly created EnsmerManager. This method is called instead
141      * of putting the elements in the constructor so that the managers created
142      * in it can access each other by calling
143      * EnsmerManager.getInstance().getXYZManager(). The order of the creations
144      * in this method are important, as the later managers tend to access the
145      * earlier ones.
146      *
147      * @param props a list of properties used to initialize the manager.
148      * Some specific properties are used in initializing the manager. Specifically,
149      * the <code>settingsDir</code> property can be used to use a specific
150      * config directory and the <code>world</code> property can be used to set
151      * a specific PrevalanceBase.
152      *
153      * @exception Exception Description of the Exception
154      */

155     private void initEnsmerManager(Properties props) throws Exception JavaDoc {
156         String JavaDoc settingsDir = props.getProperty("settingsDir");
157         String JavaDoc worldDir = props.getProperty("world", "defaultWorld");
158         configManager = settingsDir == null ? new ConfigManager() :
159             new ConfigManager(new File(settingsDir));
160         File world = new File(worldDir);
161         String JavaDoc prevalenceBase = world.isAbsolute() ? worldDir :
162             new File(configManager.getSettingsDirectory(), worldDir).getAbsolutePath();
163         System.out.println("Settings Directory: " + configManager.getSettingsDirectory());
164         System.out.println("World Directory: " + prevalenceBase);
165         prevayler = PrevaylerFactory.createPrevayler(new EnsmerPrevayler(), prevalenceBase);
166         interfaceManager = new InterfaceManager();
167         selectionManager = new SelectionManager();
168         backhoe = new Backhoe();
169         areaManager = new AreaManager();
170         specialAreaManager = new SpecialAreaManager();
171         userManager = new UserManager();
172         areaManager.loadAreas();
173         interfaceManager.displayArea(areaManager.getCurrentArea());
174     }
175
176     /**
177      * Get the singleton instance of the <code>EnsmerManager</code>. If there
178      * is no instance (there is little sense calling this method if there is
179      * one), it is initted with the given properties.
180      *
181      * @param props the properties file to init it with. The init method
182      * will check this file.
183      */

184     public static EnsmerManager instance(Properties props) {
185         if (instance == null) {
186             try {
187                 instance = new EnsmerManager();
188                 instance.initEnsmerManager(props);
189             } catch (Exception JavaDoc e) {
190                 System.err.println("There was a problem running Ensmer. Please request support, providing the following output:\n");
191                 e.printStackTrace();
192                 System.exit(1);
193             }
194         }
195         return instance;
196     }
197     /**
198      * Get the singleton instance of the <code>EnsmerManager</code>. If no
199      * instance has been created yet, create one. Generally, however, the instance
200      * is created by the single argument version of this method.
201      *
202      * @return the singleton instance of <code>EnsmerManager</code> that
203      * overviews the entire application.
204      */

205     public static EnsmerManager instance() {
206         return instance(new Properties());
207     }
208     
209     /**
210      * Ensmer's <code>InterfaceManager</code> that manages the user interface
211      * and portions of the scenegraph.
212      */

213     private InterfaceManager interfaceManager;
214
215     /**
216      * Ensmer's <code>Backhoe</code> that manages loading and serializing
217      * objects and types of objects
218      */

219     private Backhoe backhoe;
220
221     /**
222      * Ensmer's AreaManager. This object manages all of the areas under Ensmer
223      */

224     private AreaManager areaManager;
225     
226     /**
227      * Ensmer's SpecialAreaManager. This object manages mapping of names
228      * to special (special defined by the object that decided it needed a name)
229      * areas and contains factories for creating and managing some of the
230      * built in areas.
231      */

232     private SpecialAreaManager specialAreaManager;
233
234     /**
235      * Ensmer's <code>UserManager</code> that manages tasks related to the user,
236      * specifically positioning and input.
237      */

238     private UserManager userManager;
239
240     /**
241      * The <code>SelectionManager</code> that manages tasks related to the
242      * selected objects in the current area
243      */

244     private SelectionManager selectionManager;
245     
246     /**
247      * The ConfigManager that is used to retrieve the settings directory and
248      * possibly other configuration info
249      */

250     private ConfigManager configManager;
251
252     /**
253      * The prevayler that is used across the application for storing data. The
254      * prevayler manipulates the <q>EnsmerPrevayler</q> object
255      */

256     private Prevayler prevayler;
257
258     /**
259      * The singleton instance of the <code>EnsmerManager</code> class. Null only
260      * if no manager has yet been constructed. Once constructed, it doesn't
261      * change.
262      */

263     private static EnsmerManager instance;
264 }
265
266
Popular Tags