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