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