1 | package org.apollo;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.Set;
|
---|
6 |
|
---|
7 | import javax.swing.UIManager;
|
---|
8 |
|
---|
9 | import org.apollo.actions.ApolloActions;
|
---|
10 | import org.apollo.agents.MelodySearch;
|
---|
11 | import org.apollo.audio.ApolloPlaybackMixer;
|
---|
12 | import org.apollo.audio.RecordManager;
|
---|
13 | import org.apollo.audio.SampledAudioManager;
|
---|
14 | import org.apollo.audio.util.MultiTrackPlaybackController;
|
---|
15 | import org.apollo.audio.util.PlaybackClock;
|
---|
16 | import org.apollo.audio.util.SoundDesk;
|
---|
17 | import org.apollo.gui.FrameLayoutDaemon;
|
---|
18 | import org.apollo.gui.FramePlaybackBarRenderer;
|
---|
19 | import org.apollo.gui.FrameRenderPasses;
|
---|
20 | import org.apollo.io.SampledAudioFileImporter;
|
---|
21 | import org.apollo.util.ApolloSystemLog;
|
---|
22 | import org.apollo.util.Mutable;
|
---|
23 | import org.apollo.widgets.FramePlayer;
|
---|
24 | import org.expeditee.actions.Actions;
|
---|
25 | import org.expeditee.core.BlockingRunnable;
|
---|
26 | import org.expeditee.gio.EcosystemManager;
|
---|
27 | import org.expeditee.gio.EcosystemManager.Ecosystem;
|
---|
28 | import org.expeditee.gio.GraphicsManager;
|
---|
29 | import org.expeditee.gio.InputManager.WindowEventListener;
|
---|
30 | import org.expeditee.gio.InputManager.WindowEventType;
|
---|
31 | import org.expeditee.gui.Browser;
|
---|
32 | import org.expeditee.gui.DisplayController;
|
---|
33 | import org.expeditee.gui.Frame;
|
---|
34 | import org.expeditee.gui.FrameIO;
|
---|
35 | import org.expeditee.items.Item;
|
---|
36 | import org.expeditee.items.Text;
|
---|
37 | import 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 | */
|
---|
47 | public 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 | }
|
---|