source: trunk/src/org/apollo/ApolloSystem.java@ 1554

Last change on this file since 1554 was 1554, checked in by davidb, 3 years ago

Minor tweaks

File size: 7.7 KB
Line 
1package org.apollo;
2
3import java.util.Collection;
4import java.util.HashSet;
5import java.util.Set;
6
7import javax.swing.UIManager;
8
9import org.apollo.actions.ApolloActions;
10import org.apollo.agents.MelodySearch;
11import org.apollo.audio.ApolloPlaybackMixer;
12import org.apollo.audio.RecordManager;
13import org.apollo.audio.SampledAudioManager;
14import org.apollo.audio.util.MultiTrackPlaybackController;
15import org.apollo.audio.util.PlaybackClock;
16import org.apollo.audio.util.SoundDesk;
17import org.apollo.gui.FrameLayoutDaemon;
18import org.apollo.gui.FramePlaybackBarRenderer;
19import org.apollo.gui.FrameRenderPasses;
20import org.apollo.io.SampledAudioFileImporter;
21import org.apollo.util.ApolloSystemLog;
22import org.apollo.util.Mutable;
23import org.apollo.widgets.FramePlayer;
24import org.expeditee.actions.Actions;
25import org.expeditee.core.BlockingRunnable;
26import org.expeditee.gio.EcosystemManager;
27import org.expeditee.gio.EcosystemManager.Ecosystem;
28import org.expeditee.gio.GraphicsManager;
29import org.expeditee.gio.InputManager.WindowEventListener;
30import org.expeditee.gio.InputManager.WindowEventType;
31import org.expeditee.gui.Browser;
32import org.expeditee.gui.DisplayController;
33import org.expeditee.gui.Frame;
34import org.expeditee.gui.FrameIO;
35import org.expeditee.items.Item;
36import org.expeditee.items.Text;
37import org.expeditee.settings.UserSettings;
38
39/**
40 * Provides initialization and shutdown services for the Apollo system.
41 *
42 * @author Brook Novak
43 * @author David Bainbridge
44
45 *
46 */
47public final class ApolloSystem {
48
49 // It has been decidedthat Apollo nolonger needs its own icon
50 //private static final String APOLLO_ICON = "org/apollo/icons/mainicon.png";
51
52 // TODO: Create actual frames
53 public static final String SYSTEM_FRAMESET_NAME = "apollosystem";
54 public static final String HELP_TOP_FRAMENAME = SYSTEM_FRAMESET_NAME + 2;
55
56 // TODO: How to get good results: collection (motif) and query
57 // TODO: How to omit indexing on tracks
58 public static final String HELP_MELODYSEARCH_FRAMENAME = SYSTEM_FRAMESET_NAME + 3;
59
60 public static final String SETTINGS_NAME_TIMELINE_RMARGIN = "timelinerightmargin";
61 public static final String SETTINGS_NAME_TIMELINE_LMARGIN = "timelineleftmargin";
62
63 public static boolean useQualityGraphics = true;
64
65 private static boolean hasInitialized = false;
66
67 private ApolloSystem()
68 {
69 }
70
71 /**
72 * Initializes Apollo mod for expeditee - prepares all subsystems.
73 */
74 public static void initialize()
75 {
76 if (hasInitialized) return;
77
78 ApolloSystemLog.println("Initializing...");
79
80 // Ensure that resources are released before the application is closed.
81 EcosystemManager.getInputManager().addWindowEventListener(new WindowEventListener() {
82 @Override
83 public void onWindowEvent(WindowEventType type)
84 {
85 if (type != WindowEventType.WINDOW_CLOSED) return;
86 ApolloSystem.shutdown();
87 }
88 });
89
90 EcosystemManager.getInputManager().registerGestureListener(ApolloGestureActions.getInstance());
91 EcosystemManager.getInputManager().addInputEventToGestureTranslator(new ApolloKBMGestureTranslator());
92
93 // Set title
94 //Browser._theBrowser.setTitle("Apollo");
95
96 loadSettings();
97
98 ApolloSystemLog.println(" Preparing sub-systems...");
99
100 SampledAudioManager.getInstance();
101
102 RecordManager.getInstance();
103
104 ApolloPlaybackMixer.getInstance();
105
106 FrameLayoutDaemon.getInstance();
107
108 FrameRenderPasses.getInstance();
109
110 PlaybackClock.getInstance();
111
112 FramePlaybackBarRenderer.getInstance();
113
114 // Setup for importing audio
115 EcosystemManager.getDragAndDropManager().addCustomFileImporter(new SampledAudioFileImporter());
116
117 ApolloSystemLog.println(" Loading actions and agents...");
118
119 // Add apollo actions
120 Actions.LoadMethods(ApolloActions.class);
121
122 Set<String> agents = new HashSet<String>();
123 agents.add(MelodySearch.class.getName());
124
125 Collection<String> omitted = Actions.addAgents(agents);
126
127 for (String agent : omitted) {
128
129 if (agent == null || agent.length() == 0) continue;
130
131 String name = agent;
132
133 int index = agent.lastIndexOf('.');
134 if (index > 0 && agent.length() > (index - 1)) {
135 name = agent.substring(index);
136 }
137 ApolloSystemLog.println(" WARNING: Failed to add agent \"" + name + "\"");
138
139 }
140
141 ApolloSystemLog.println(" Loading banks...");
142 SoundDesk.getInstance(); // loads upon creation
143
144 ApolloSystemLog.println("Initialized");
145
146 hasInitialized = true;
147 }
148
149 /**
150 * TODO: This is temporary and should be integrated with expeditee's settings system.. once it is designed
151 * to support plugins...
152 *
153 * When invoked, the apollo setting frame is loaded and parsed... setting apollo-specific settings.
154 */
155 public static void loadSettings()
156 {
157 // Load apollo settings frame from the default profile
158 Frame profile = FrameIO.LoadProfile(UserSettings.DEFAULT_PROFILE_NAME);
159 if (profile == null) {
160 try {
161 profile = FrameIO.CreateNewDefaultProfile();
162 } catch (Exception e) {
163 e.printStackTrace();
164 return;
165 }
166 }
167 assert(profile != null);
168
169 for (Item i : profile.getSortedItems()) {
170 if (i instanceof Text) {
171 Text textItem = (Text)i;
172
173 if (textItem.getText().toLowerCase().trim().startsWith(SETTINGS_NAME_TIMELINE_LMARGIN)) {
174 Mutable.Integer val = stripNameValueStringInteger(textItem.getText());
175 if (val != null) {
176 FrameLayoutDaemon.getInstance().setTimelineMargins(
177 val.value,
178 FrameLayoutDaemon.getInstance().getRightMargin());
179 }
180
181 } else if (textItem.getText().toLowerCase().trim().startsWith(SETTINGS_NAME_TIMELINE_RMARGIN)) {
182 Mutable.Integer val = stripNameValueStringInteger(textItem.getText());
183 if (val != null) {
184 FrameLayoutDaemon.getInstance().setTimelineMargins(
185 FrameLayoutDaemon.getInstance().getLeftMargin(),
186 val.value);
187 }
188 }
189 }
190 }
191
192 }
193
194 private static Mutable.Integer stripNameValueStringInteger(String namevalue)
195 {
196 assert (namevalue != null);
197 int valueIndex = namevalue.indexOf(':') + 1;
198 if (valueIndex == 0 || valueIndex >= (namevalue.length() - 1)) return null;
199
200 try {
201 int value = Integer.parseInt(namevalue.substring(valueIndex));
202 return Mutable.createMutableInteger(value);
203 } catch (NumberFormatException e) { /* Consume*/ }
204
205 return null;
206 }
207
208
209
210 /**
211 * Releases all resources currently used by the SampledAudioManager.
212 */
213 public static void shutdown()
214 {
215 ApolloSystemLog.println("Saving banks...");
216 SoundDesk.getInstance().saveMasterMix();
217 SoundDesk.getInstance().saveMixes();
218
219 FramePlayer.saveTypedFrames();
220
221 ApolloSystemLog.println("Releasing resources...");
222
223 ApolloPlaybackMixer.getInstance().releaseResources();
224 RecordManager.getInstance().releaseResources(); // blocking
225 MultiTrackPlaybackController.getInstance().releaseResources();
226
227 ApolloSystemLog.println("Subsystems shutdown");
228 }
229
230 /**
231 * @return
232 * True if has initialized.
233 */
234 public static boolean isInitialized()
235 {
236 return hasInitialized;
237 }
238
239
240
241 /**
242 * The apollo main just ensures that apollo is initialized during startup.
243 *
244 * This may eventually become redundant once Expeditee implements a plugin system
245 * that allows a plugin / mod to initialize itself at started...
246 *
247 * @param args
248 */
249 public static void main(String[] args)
250 {
251 if (Browser.ECOSYSTEM_TYPE == Ecosystem.Swing) {
252 try {
253 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
254 } catch (Exception e) {
255 e.printStackTrace();
256 return;
257 }
258 }
259
260 // Window icon must be set before initialisation
261 // It has been decided that Apollo no longer needs its own icon
262 // => run with the one set in Browser.main()
263 //GraphicsManager.setWindowIcon(APOLLO_ICON);
264
265 // Run expeditee
266 Browser.main(args);
267
268 EcosystemManager.getMiscManager().runOnGIOThread(new BlockingRunnable() {
269 public void execute() {
270 ApolloSystem.initialize();
271 }
272 });
273 }
274
275}
Note: See TracBrowser for help on using the repository browser.