1 | package org.apollo;
|
---|
2 |
|
---|
3 | import java.awt.Image;
|
---|
4 | import java.awt.Toolkit;
|
---|
5 | import java.awt.event.WindowEvent;
|
---|
6 | import java.awt.event.WindowListener;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.SwingUtilities;
|
---|
13 | import javax.swing.UIManager;
|
---|
14 |
|
---|
15 | import org.apollo.actions.ApolloActions;
|
---|
16 | import org.apollo.agents.MelodySearch;
|
---|
17 | import org.apollo.audio.ApolloPlaybackMixer;
|
---|
18 | import org.apollo.audio.RecordManager;
|
---|
19 | import org.apollo.audio.SampledAudioManager;
|
---|
20 | import org.apollo.audio.util.MultiTrackPlaybackController;
|
---|
21 | import org.apollo.audio.util.PlaybackClock;
|
---|
22 | import org.apollo.audio.util.SoundDesk;
|
---|
23 | import org.apollo.gui.FrameLayoutDaemon;
|
---|
24 | import org.apollo.gui.FramePlaybackBarRenderer;
|
---|
25 | import org.apollo.gui.FrameRenderPasses;
|
---|
26 | import org.apollo.io.SampledAudioFileImporter;
|
---|
27 | import org.apollo.util.ApolloSystemLog;
|
---|
28 | import org.apollo.util.Mutable;
|
---|
29 | import org.apollo.widgets.FramePlayer;
|
---|
30 | import org.expeditee.actions.Actions;
|
---|
31 | import org.expeditee.gui.Browser;
|
---|
32 | import org.expeditee.gui.Frame;
|
---|
33 | import org.expeditee.gui.FrameIO;
|
---|
34 | import org.expeditee.gui.FrameKeyboardActions;
|
---|
35 | import org.expeditee.gui.FrameMouseActions;
|
---|
36 | import org.expeditee.settings.UserSettings;
|
---|
37 | import org.expeditee.importer.FrameDNDTransferHandler;
|
---|
38 | import org.expeditee.items.Item;
|
---|
39 | import org.expeditee.items.Text;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Provides initialization and shutdown services for the Apollo system.
|
---|
43 | *
|
---|
44 | * @author Brook Novak
|
---|
45 | *
|
---|
46 | */
|
---|
47 | public final class ApolloSystem {
|
---|
48 |
|
---|
49 | // TODO: Create actual frames
|
---|
50 | public static final String SYSTEM_FRAMESET_NAME = "apollosystem";
|
---|
51 | public static final String HELP_TOP_FRAMENAME = SYSTEM_FRAMESET_NAME + 2;
|
---|
52 |
|
---|
53 | // TODO: How to get good results: collection (moteef) and querry
|
---|
54 | // TODO: How to omit indexing on tracks
|
---|
55 | public static final String HELP_MELODYSEARCH_FRAMENAME = SYSTEM_FRAMESET_NAME + 3;
|
---|
56 |
|
---|
57 | public static final String SETTINGS_NAME_TIMELINE_RMARGIN = "timelinerightmargin";
|
---|
58 | public static final String SETTINGS_NAME_TIMELINE_LMARGIN = "timelineleftmargin";
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | public static boolean useQualityGraphics = true;
|
---|
63 |
|
---|
64 | private ApolloSystem() {
|
---|
65 | }
|
---|
66 |
|
---|
67 | private static boolean hasInitialized = false;
|
---|
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 | SwingUtilities.invokeLater(new Runnable() {
|
---|
79 | public void run() {
|
---|
80 |
|
---|
81 | try {
|
---|
82 |
|
---|
83 | URL url = ClassLoader.getSystemResource("org/apollo/icons/mainicon.png");
|
---|
84 |
|
---|
85 | if (url != null) {
|
---|
86 | Image img = Toolkit.getDefaultToolkit().getImage(url);
|
---|
87 | Browser._theBrowser.setIconImage(img);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | } catch (Exception e) {
|
---|
93 | e.printStackTrace();
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Ensure that resources are released before the application is closed.
|
---|
97 | Browser._theBrowser.addWindowListener(new WindowListener() {
|
---|
98 |
|
---|
99 | public void windowActivated(WindowEvent e) {
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void windowClosed(WindowEvent e) {
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void windowClosing(WindowEvent e) {
|
---|
106 | ApolloSystem.shutdown();
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void windowDeactivated(WindowEvent e) {
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void windowDeiconified(WindowEvent e) {
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void windowIconified(WindowEvent e) {
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void windowOpened(WindowEvent e) {
|
---|
119 | }
|
---|
120 |
|
---|
121 | });
|
---|
122 |
|
---|
123 | Browser._theBrowser.getContentPane().removeKeyListener(FrameKeyboardActions.getInstance());
|
---|
124 | Browser._theBrowser.removeKeyListener(FrameKeyboardActions.getInstance());
|
---|
125 |
|
---|
126 | Browser._theBrowser.getContentPane().addKeyListener(new AudioFrameKeyboardActions());
|
---|
127 | Browser._theBrowser.addKeyListener(new AudioFrameKeyboardActions());
|
---|
128 |
|
---|
129 | // Filter out some special mouse move cases
|
---|
130 | AudioFrameMouseActions apolloMouseFilter = new AudioFrameMouseActions();
|
---|
131 | Browser._theBrowser.getMouseEventRouter().removeExpediteeMouseMotionListener(
|
---|
132 | FrameMouseActions.getInstance());
|
---|
133 | Browser._theBrowser.getMouseEventRouter().addExpediteeMouseMotionListener(apolloMouseFilter);
|
---|
134 |
|
---|
135 | Browser._theBrowser.getMouseEventRouter().removeExpediteeMouseListener(
|
---|
136 | FrameMouseActions.getInstance());
|
---|
137 | Browser._theBrowser.getMouseEventRouter().addExpediteeMouseListener(apolloMouseFilter);
|
---|
138 | }
|
---|
139 | });
|
---|
140 |
|
---|
141 | // Set title
|
---|
142 | //Browser._theBrowser.setTitle("Apollo");
|
---|
143 |
|
---|
144 | loadSettings();
|
---|
145 |
|
---|
146 | ApolloSystemLog.println(" Preparing sub-systems...");
|
---|
147 |
|
---|
148 | SampledAudioManager.getInstance();
|
---|
149 |
|
---|
150 | RecordManager.getInstance();
|
---|
151 |
|
---|
152 | ApolloPlaybackMixer.getInstance();
|
---|
153 |
|
---|
154 | FrameLayoutDaemon.getInstance();
|
---|
155 |
|
---|
156 | FrameRenderPasses.getInstance();
|
---|
157 |
|
---|
158 | PlaybackClock.getInstance();
|
---|
159 |
|
---|
160 | FramePlaybackBarRenderer.getInstance();
|
---|
161 |
|
---|
162 | // Setup for importing audio
|
---|
163 | FrameDNDTransferHandler.getInstance().addCustomFileImporter(
|
---|
164 | new SampledAudioFileImporter());
|
---|
165 |
|
---|
166 | ApolloSystemLog.println(" Loading actions and agents...");
|
---|
167 |
|
---|
168 | // Add apollo actions
|
---|
169 | Actions.LoadMethods(ApolloActions.class);
|
---|
170 |
|
---|
171 | Set<String> agents = new HashSet<String>();
|
---|
172 | agents.add(MelodySearch.class.getName());
|
---|
173 |
|
---|
174 | Collection<String> omitted = Actions.addAgents(agents);
|
---|
175 |
|
---|
176 | for (String agent : omitted) {
|
---|
177 |
|
---|
178 | if (agent == null || agent.length() == 0) continue;
|
---|
179 |
|
---|
180 | String name = agent;
|
---|
181 |
|
---|
182 | int index = agent.lastIndexOf('.');
|
---|
183 | if (index > 0 && agent.length() > (index - 1)) {
|
---|
184 | name = agent.substring(index);
|
---|
185 | }
|
---|
186 | ApolloSystemLog.println(" WARNING: Failed to add agent \"" + name + "\"");
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | ApolloSystemLog.println(" Loading banks...");
|
---|
191 | SoundDesk.getInstance(); // loads upon creation
|
---|
192 |
|
---|
193 | ApolloSystemLog.println("Initialized");
|
---|
194 |
|
---|
195 | hasInitialized = true;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * TODO: This is temporary and should be integrated with expeditees settings system.. once it is desinged
|
---|
200 | * to support plugins...
|
---|
201 | *
|
---|
202 | * When invoked, the apollo setting frame is loaded and parsed... setting apollo-specific settings.
|
---|
203 | */
|
---|
204 | public static void loadSettings() {
|
---|
205 |
|
---|
206 | // Load apollo settings frame from the default profile
|
---|
207 | Frame profile = FrameIO.LoadProfile(UserSettings.DEFAULT_PROFILE_NAME);
|
---|
208 | if (profile == null) {
|
---|
209 | try {
|
---|
210 | profile = FrameIO.CreateNewProfile(UserSettings.DEFAULT_PROFILE_NAME);
|
---|
211 | } catch (Exception e) {
|
---|
212 | e.printStackTrace();
|
---|
213 | return;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | assert(profile != null);
|
---|
217 |
|
---|
218 | for (Item i : profile.getItems()) {
|
---|
219 | if (i instanceof Text) {
|
---|
220 | Text textItem = (Text)i;
|
---|
221 |
|
---|
222 | if (textItem.getText().toLowerCase().trim().startsWith(SETTINGS_NAME_TIMELINE_LMARGIN)) {
|
---|
223 | Mutable.Integer val = stripNameValueStringInteger(textItem.getText());
|
---|
224 | if (val != null) {
|
---|
225 | FrameLayoutDaemon.getInstance().setTimelineMargins(
|
---|
226 | val.value,
|
---|
227 | FrameLayoutDaemon.getInstance().getRightMargin());
|
---|
228 | }
|
---|
229 |
|
---|
230 | } else if (textItem.getText().toLowerCase().trim().startsWith(SETTINGS_NAME_TIMELINE_RMARGIN)) {
|
---|
231 | Mutable.Integer val = stripNameValueStringInteger(textItem.getText());
|
---|
232 | if (val != null) {
|
---|
233 | FrameLayoutDaemon.getInstance().setTimelineMargins(
|
---|
234 | FrameLayoutDaemon.getInstance().getLeftMargin(),
|
---|
235 | val.value);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | }
|
---|
242 |
|
---|
243 | private static Mutable.Integer stripNameValueStringInteger(String namevalue) {
|
---|
244 | assert (namevalue != null);
|
---|
245 | int valueIndex = namevalue.indexOf(':') + 1;
|
---|
246 | if (valueIndex == 0 || valueIndex >= (namevalue.length() - 1)) return null;
|
---|
247 |
|
---|
248 | try {
|
---|
249 | int value = Integer.parseInt(namevalue.substring(valueIndex));
|
---|
250 | return Mutable.createMutableInteger(value);
|
---|
251 | } catch (NumberFormatException e) { /* Consume*/ }
|
---|
252 |
|
---|
253 | return null;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Releases all resources currently used by the SampledAudioManager.
|
---|
260 | */
|
---|
261 | public static void shutdown() {
|
---|
262 |
|
---|
263 | ApolloSystemLog.println("Saving banks...");
|
---|
264 | SoundDesk.getInstance().saveMasterMix();
|
---|
265 | SoundDesk.getInstance().saveMixes();
|
---|
266 |
|
---|
267 | FramePlayer.saveTypedFrames();
|
---|
268 |
|
---|
269 | ApolloSystemLog.println("Releasing resources...");
|
---|
270 |
|
---|
271 | ApolloPlaybackMixer.getInstance().releaseResources();
|
---|
272 | RecordManager.getInstance().releaseResources(); // blocking
|
---|
273 | MultiTrackPlaybackController.getInstance().releaseResources();
|
---|
274 |
|
---|
275 | ApolloSystemLog.println("Subsystems shutdown");
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * @return
|
---|
280 | * True if has initialized.
|
---|
281 | */
|
---|
282 | public static boolean isInitialized() {
|
---|
283 | return hasInitialized;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * The apollo main just ensures that apollo is initialized during startup.
|
---|
290 | *
|
---|
291 | * This may eventually become redundant once Expeditee implements a plugin system
|
---|
292 | * that allows a plugin / mod to initialize itself at started...
|
---|
293 | *
|
---|
294 | * @param args
|
---|
295 | */
|
---|
296 | public static void main(String[] args) {
|
---|
297 |
|
---|
298 | try {
|
---|
299 | UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
|
---|
300 | }
|
---|
301 | catch (Exception e) {
|
---|
302 | e.printStackTrace();
|
---|
303 | return;
|
---|
304 | }
|
---|
305 |
|
---|
306 | // Run expeditee
|
---|
307 | Browser.main(args);
|
---|
308 |
|
---|
309 | // Initialize apollo
|
---|
310 | SwingUtilities.invokeLater(new Runnable() {
|
---|
311 | public void run() {
|
---|
312 | ApolloSystem.initialize();
|
---|
313 | }
|
---|
314 | });
|
---|
315 | }
|
---|
316 |
|
---|
317 | }
|
---|