25 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
27 |
|
|
28 |
|
import java.io.ByteArrayInputStream; |
29 |
|
import java.io.ByteArrayOutputStream; |
30 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
31 |
|
|
32 |
|
import java.util.Vector; |
33 |
|
|
34 |
import java.util.logging.Handler; |
import java.util.logging.Handler; |
35 |
import java.util.logging.Level; |
import java.util.logging.Level; |
36 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
39 |
|
|
40 |
import javax.swing.Timer; |
import javax.swing.Timer; |
41 |
|
|
42 |
|
import net.sf.juife.Task; |
43 |
|
import net.sf.juife.TaskQueue; |
44 |
|
|
45 |
|
import net.sf.juife.event.TaskEvent; |
46 |
|
import net.sf.juife.event.TaskListener; |
47 |
|
import net.sf.juife.event.TaskQueueEvent; |
48 |
|
import net.sf.juife.event.TaskQueueListener; |
49 |
|
|
50 |
|
import org.jsampler.event.OrchestraEvent; |
51 |
|
import org.jsampler.event.OrchestraListEvent; |
52 |
|
import org.jsampler.event.OrchestraListListener; |
53 |
|
import org.jsampler.event.OrchestraListener; |
54 |
|
|
55 |
import org.jsampler.task.*; |
import org.jsampler.task.*; |
56 |
|
|
57 |
import org.jsampler.view.JSMainFrame; |
import org.jsampler.view.JSMainFrame; |
60 |
import org.linuxsampler.lscp.Client; |
import org.linuxsampler.lscp.Client; |
61 |
import org.linuxsampler.lscp.event.*; |
import org.linuxsampler.lscp.event.*; |
62 |
|
|
63 |
import net.sf.juife.Task; |
import org.w3c.dom.Document; |
64 |
import net.sf.juife.TaskQueue; |
import org.w3c.dom.Node; |
|
|
|
|
import net.sf.juife.event.TaskEvent; |
|
|
import net.sf.juife.event.TaskListener; |
|
|
import net.sf.juife.event.TaskQueueEvent; |
|
|
import net.sf.juife.event.TaskQueueListener; |
|
65 |
|
|
66 |
|
|
67 |
/** |
/** |
68 |
* |
* This class serves as a 'Control Center' of the application. |
69 |
|
* It also provides some fundamental routines and access to most used objects. |
70 |
* @author Grigor Iliev |
* @author Grigor Iliev |
71 |
*/ |
*/ |
72 |
public class CC { |
public class CC { |
73 |
private static Handler handler; |
private static Handler handler; |
74 |
public static FileOutputStream fos; |
private static FileOutputStream fos; |
75 |
|
|
76 |
private static JSMainFrame mainFrame = null; |
private static JSMainFrame mainFrame = null; |
77 |
private static JSProgress progress = null; |
private static JSProgress progress = null; |
79 |
private final static Client lsClient = new Client(); |
private final static Client lsClient = new Client(); |
80 |
|
|
81 |
private final static TaskQueue taskQueue = new TaskQueue(); |
private final static TaskQueue taskQueue = new TaskQueue(); |
82 |
private final static Timer timer = new Timer(1000, null); |
private final static Timer timer = new Timer(2000, null); |
83 |
|
|
|
private final static EventHandler eventHandler = new EventHandler(); |
|
84 |
|
|
85 |
|
/** |
86 |
|
* Returns the logger to be used for logging events. |
87 |
|
* @return The logger to be used for logging events. |
88 |
|
*/ |
89 |
public static Logger |
public static Logger |
90 |
getLogger() { |
getLogger() { |
91 |
return Logger.getLogger ( |
return Logger.getLogger ( |
94 |
); |
); |
95 |
} |
} |
96 |
|
|
97 |
|
/** |
98 |
|
* Returns the task queue to be used for scheduling tasks |
99 |
|
* for execution out of the event-dispatching thread. |
100 |
|
* @return The task queue to be used for scheduling tasks |
101 |
|
* for execution out of the event-dispatching thread. |
102 |
|
*/ |
103 |
public static TaskQueue |
public static TaskQueue |
104 |
getTaskQueue() { return taskQueue; } |
getTaskQueue() { return taskQueue; } |
105 |
|
|
106 |
|
/** |
107 |
|
* Returns the main window of this application. |
108 |
|
* @return The main window of this application. |
109 |
|
*/ |
110 |
public static JSMainFrame |
public static JSMainFrame |
111 |
getMainFrame() { return mainFrame; } |
getMainFrame() { return mainFrame; } |
112 |
|
|
113 |
|
/** |
114 |
|
* Sets the main window of this application. |
115 |
|
* @param mainFrame The main window of this application. |
116 |
|
*/ |
117 |
public static void |
public static void |
118 |
setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; } |
setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; } |
119 |
|
|
120 |
|
/** |
121 |
|
* Gets the progress indicator of this application. |
122 |
|
* @return The progress indicator of this application. |
123 |
|
*/ |
124 |
public static JSProgress |
public static JSProgress |
125 |
getProgressIndicator() { return progress; } |
getProgressIndicator() { return progress; } |
126 |
|
|
127 |
|
/** |
128 |
|
* Sets the progress indicator to be used by this application. |
129 |
|
* @param progress The progress indicator to be used by this application. |
130 |
|
*/ |
131 |
public static void |
public static void |
132 |
setProgressIndicator(JSProgress progress) { CC.progress = progress; } |
setProgressIndicator(JSProgress progress) { CC.progress = progress; } |
133 |
|
|
134 |
|
/** |
135 |
|
* This method does the initial preparation of the application. |
136 |
|
*/ |
137 |
protected static void |
protected static void |
138 |
initJSampler() { |
initJSampler() { |
139 |
fos = null; |
fos = null; |
172 |
actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); } |
actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); } |
173 |
}); |
}); |
174 |
|
|
175 |
taskQueue.addTaskQueueListener(new TaskQueueListener() { |
taskQueue.addTaskQueueListener(getHandler()); |
|
public void |
|
|
stateChanged(TaskQueueEvent e) { |
|
|
switch(e.getEventID()) { |
|
|
case TASK_FETCHED: |
|
|
CC.getProgressIndicator().setString ( |
|
|
((Task)e.getSource()).getDescription() |
|
|
); |
|
|
break; |
|
|
case TASK_DONE: |
|
|
EnhancedTask t = (EnhancedTask)e.getSource(); |
|
|
if(t.doneWithErrors() && !t.isStopped()) |
|
|
HF.showErrorMessage(t.getErrorMessage()); |
|
|
break; |
|
|
case NOT_IDLE: |
|
|
timer.start(); |
|
|
break; |
|
|
case IDLE: |
|
|
timer.stop(); |
|
|
CC.getProgressIndicator().stop(); |
|
|
break; |
|
|
} |
|
|
} |
|
|
}); |
|
176 |
|
|
177 |
taskQueue.start(); |
taskQueue.start(); |
178 |
|
|
179 |
getClient().addChannelCountListener(eventHandler); |
getClient().addChannelCountListener(getHandler()); |
180 |
getClient().addChannelInfoListener(eventHandler); |
getClient().addChannelInfoListener(getHandler()); |
181 |
getClient().addStreamCountListener(eventHandler); |
getClient().addStreamCountListener(getHandler()); |
182 |
getClient().addVoiceCountListener(eventHandler); |
getClient().addVoiceCountListener(getHandler()); |
183 |
getClient().addTotalVoiceCountListener(eventHandler); |
getClient().addTotalVoiceCountListener(getHandler()); |
184 |
|
|
185 |
|
loadOrchestras(); |
186 |
|
|
187 |
|
for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) { |
188 |
|
getOrchestras().getOrchestra(i).addOrchestraListener(getHandler()); |
189 |
|
} |
190 |
|
getOrchestras().addOrchestraListListener(getHandler()); |
191 |
|
} |
192 |
|
|
193 |
|
private final static OrchestraListModel orchestras = new DefaultOrchestraListModel(); |
194 |
|
|
195 |
|
/** |
196 |
|
* Returns a list containing all available orchestras. |
197 |
|
* @return A list containing all available orchestras. |
198 |
|
*/ |
199 |
|
public static OrchestraListModel |
200 |
|
getOrchestras() { return orchestras; } |
201 |
|
|
202 |
|
private static void |
203 |
|
loadOrchestras() { |
204 |
|
String s = Prefs.getOrchestras(); |
205 |
|
if(s == null) return; |
206 |
|
|
207 |
|
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes()); |
208 |
|
Document doc = DOMUtils.readObject(bais); |
209 |
|
|
210 |
|
try { getOrchestras().readObject(doc.getDocumentElement()); } |
211 |
|
catch(Exception x) { HF.showErrorMessage(x, "Loading orchestras: "); } |
212 |
|
} |
213 |
|
|
214 |
|
private static void |
215 |
|
saveOrchestras() { |
216 |
|
Document doc = DOMUtils.createEmptyDocument(); |
217 |
|
|
218 |
|
Node node = doc.createElement("temp"); |
219 |
|
doc.appendChild(node); |
220 |
|
|
221 |
|
getOrchestras().writeObject(doc, doc.getDocumentElement()); |
222 |
|
|
223 |
|
doc.replaceChild(node.getFirstChild(), node); |
224 |
|
|
225 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
226 |
|
DOMUtils.writeObject(doc, baos); |
227 |
|
Prefs.setOrchestras(baos.toString()); |
228 |
} |
} |
229 |
|
|
230 |
|
/** |
231 |
|
* The exit point of the application which ensures clean exit with default exit status 0. |
232 |
|
* @see #cleanExit(int i) |
233 |
|
*/ |
234 |
public static void |
public static void |
235 |
cleanExit() { cleanExit(0); } |
cleanExit() { cleanExit(0); } |
236 |
|
|
237 |
|
/** |
238 |
|
* The exit point of the application which ensures clean exit. |
239 |
|
* @param i The exit status. |
240 |
|
*/ |
241 |
public static void |
public static void |
242 |
cleanExit(int i) { |
cleanExit(int i) { |
243 |
CC.getLogger().fine("CC.jsEnded"); |
CC.getLogger().fine("CC.jsEnded"); |
244 |
System.exit(i); |
System.exit(i); |
245 |
} |
} |
246 |
|
|
247 |
|
/** |
248 |
|
* Gets the <code>Client</code> object that is used to communicate with the backend. |
249 |
|
* @return The <code>Client</code> object that is used to communicate with the backend. |
250 |
|
*/ |
251 |
public static Client |
public static Client |
252 |
getClient() { return lsClient; } |
getClient() { return lsClient; } |
253 |
|
|
254 |
|
private static final Vector<ActionListener> listeners = new Vector<ActionListener>(); |
255 |
|
|
256 |
|
/** |
257 |
|
* Registers the specified listener to be notified when reconnecting to LinuxSampler. |
258 |
|
* @param l The <code>ActionListener</code> to register. |
259 |
|
*/ |
260 |
|
public static void |
261 |
|
addReconnectListener(ActionListener l) { listeners.add(l); } |
262 |
|
|
263 |
|
/** |
264 |
|
* Removes the specified listener. |
265 |
|
* @param l The <code>ActionListener</code> to remove. |
266 |
|
*/ |
267 |
|
public static void |
268 |
|
removeReconnectListener(ActionListener l) { listeners.remove(l); } |
269 |
|
|
270 |
|
private static void |
271 |
|
fireReconnectEvent() { |
272 |
|
ActionEvent e = new ActionEvent(CC.class, ActionEvent.ACTION_PERFORMED, null); |
273 |
|
for(ActionListener l : listeners) l.actionPerformed(e); |
274 |
|
} |
275 |
|
|
276 |
private static final SamplerModel samplerModel = new DefaultSamplerModel(); |
private static final SamplerModel samplerModel = new DefaultSamplerModel(); |
277 |
|
|
282 |
public static SamplerModel |
public static SamplerModel |
283 |
getSamplerModel() { return samplerModel; } |
getSamplerModel() { return samplerModel; } |
284 |
|
|
285 |
|
/** |
286 |
|
* Reconnects to LinuxSampler. |
287 |
|
*/ |
288 |
|
public static void |
289 |
|
reconnect() { |
290 |
|
initSamplerModel(); |
291 |
|
fireReconnectEvent(); |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
|
* This method updates the information about the backend state. |
296 |
|
*/ |
297 |
public static void |
public static void |
298 |
initSamplerModel() { |
initSamplerModel() { |
299 |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
350 |
getTaskQueue().add(cnt); |
getTaskQueue().add(cnt); |
351 |
} |
} |
352 |
|
|
353 |
|
private final static EventHandler eventHandler = new EventHandler(); |
354 |
|
|
355 |
|
private static EventHandler |
356 |
|
getHandler() { return eventHandler; } |
357 |
|
|
358 |
private static class EventHandler implements ChannelCountListener, ChannelInfoListener, |
private static class EventHandler implements ChannelCountListener, ChannelInfoListener, |
359 |
StreamCountListener, VoiceCountListener, TotalVoiceCountListener { |
StreamCountListener, VoiceCountListener, TotalVoiceCountListener, |
360 |
|
TaskQueueListener, OrchestraListener, OrchestraListListener { |
361 |
|
|
362 |
/** Invoked when the number of channels has changed. */ |
/** Invoked when the number of channels has changed. */ |
363 |
public void |
public void |
448 |
totalVoiceCountChanged(TotalVoiceCountEvent e) { |
totalVoiceCountChanged(TotalVoiceCountEvent e) { |
449 |
getTaskQueue().add(new UpdateTotalVoiceCount()); |
getTaskQueue().add(new UpdateTotalVoiceCount()); |
450 |
} |
} |
451 |
|
|
452 |
|
/** |
453 |
|
* Invoked to indicate that the state of a task queue is changed. |
454 |
|
* This method is invoked only from the event-dispatching thread. |
455 |
|
*/ |
456 |
|
public void |
457 |
|
stateChanged(TaskQueueEvent e) { |
458 |
|
switch(e.getEventID()) { |
459 |
|
case TASK_FETCHED: |
460 |
|
getProgressIndicator().setString ( |
461 |
|
((Task)e.getSource()).getDescription() |
462 |
|
); |
463 |
|
break; |
464 |
|
case TASK_DONE: |
465 |
|
EnhancedTask t = (EnhancedTask)e.getSource(); |
466 |
|
if(t.doneWithErrors() && !t.isStopped()) |
467 |
|
HF.showErrorMessage(t.getErrorMessage()); |
468 |
|
break; |
469 |
|
case NOT_IDLE: |
470 |
|
timer.start(); |
471 |
|
break; |
472 |
|
case IDLE: |
473 |
|
timer.stop(); |
474 |
|
getProgressIndicator().stop(); |
475 |
|
break; |
476 |
|
} |
477 |
|
} |
478 |
|
|
479 |
|
/** Invoked when the name of orchestra is changed. */ |
480 |
|
public void |
481 |
|
nameChanged(OrchestraEvent e) { saveOrchestras(); } |
482 |
|
|
483 |
|
/** Invoked when the description of orchestra is changed. */ |
484 |
|
public void |
485 |
|
descriptionChanged(OrchestraEvent e) { saveOrchestras(); } |
486 |
|
|
487 |
|
/** Invoked when an instrument is added to the orchestra. */ |
488 |
|
public void |
489 |
|
instrumentAdded(OrchestraEvent e) { saveOrchestras(); } |
490 |
|
|
491 |
|
/** Invoked when an instrument is removed from the orchestra. */ |
492 |
|
public void |
493 |
|
instrumentRemoved(OrchestraEvent e) { saveOrchestras(); } |
494 |
|
|
495 |
|
/** Invoked when the settings of an instrument are changed. */ |
496 |
|
public void |
497 |
|
instrumentChanged(OrchestraEvent e) { saveOrchestras(); } |
498 |
|
|
499 |
|
/** Invoked when an orchestra is added to the orchestra list. */ |
500 |
|
public void |
501 |
|
orchestraAdded(OrchestraListEvent e) { |
502 |
|
e.getOrchestraModel().addOrchestraListener(getHandler()); |
503 |
|
saveOrchestras(); |
504 |
|
} |
505 |
|
|
506 |
|
/** Invoked when an orchestra is removed from the orchestra list. */ |
507 |
|
public void |
508 |
|
orchestraRemoved(OrchestraListEvent e) { |
509 |
|
e.getOrchestraModel().removeOrchestraListener(getHandler()); |
510 |
|
saveOrchestras(); |
511 |
|
} |
512 |
} |
} |
513 |
} |
} |