--- jsampler/trunk/src/org/jsampler/CC.java 2006/08/07 18:01:57 910 +++ jsampler/trunk/src/org/jsampler/CC.java 2006/08/07 18:25:58 911 @@ -25,8 +25,12 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; +import java.util.Vector; + import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; @@ -35,6 +39,19 @@ import javax.swing.Timer; +import net.sf.juife.Task; +import net.sf.juife.TaskQueue; + +import net.sf.juife.event.TaskEvent; +import net.sf.juife.event.TaskListener; +import net.sf.juife.event.TaskQueueEvent; +import net.sf.juife.event.TaskQueueListener; + +import org.jsampler.event.OrchestraEvent; +import org.jsampler.event.OrchestraListEvent; +import org.jsampler.event.OrchestraListListener; +import org.jsampler.event.OrchestraListener; + import org.jsampler.task.*; import org.jsampler.view.JSMainFrame; @@ -43,22 +60,18 @@ import org.linuxsampler.lscp.Client; import org.linuxsampler.lscp.event.*; -import net.sf.juife.Task; -import net.sf.juife.TaskQueue; - -import net.sf.juife.event.TaskEvent; -import net.sf.juife.event.TaskListener; -import net.sf.juife.event.TaskQueueEvent; -import net.sf.juife.event.TaskQueueListener; +import org.w3c.dom.Document; +import org.w3c.dom.Node; /** - * + * This class serves as a 'Control Center' of the application. + * It also provides some fundamental routines and access to most used objects. * @author Grigor Iliev */ public class CC { private static Handler handler; - public static FileOutputStream fos; + private static FileOutputStream fos; private static JSMainFrame mainFrame = null; private static JSProgress progress = null; @@ -66,10 +79,13 @@ private final static Client lsClient = new Client(); private final static TaskQueue taskQueue = new TaskQueue(); - private final static Timer timer = new Timer(1000, null); + private final static Timer timer = new Timer(2000, null); - private final static EventHandler eventHandler = new EventHandler(); + /** + * Returns the logger to be used for logging events. + * @return The logger to be used for logging events. + */ public static Logger getLogger() { return Logger.getLogger ( @@ -78,21 +94,46 @@ ); } + /** + * Returns the task queue to be used for scheduling tasks + * for execution out of the event-dispatching thread. + * @return The task queue to be used for scheduling tasks + * for execution out of the event-dispatching thread. + */ public static TaskQueue getTaskQueue() { return taskQueue; } + /** + * Returns the main window of this application. + * @return The main window of this application. + */ public static JSMainFrame getMainFrame() { return mainFrame; } + /** + * Sets the main window of this application. + * @param mainFrame The main window of this application. + */ public static void setMainFrame(JSMainFrame mainFrame) { CC.mainFrame = mainFrame; } + /** + * Gets the progress indicator of this application. + * @return The progress indicator of this application. + */ public static JSProgress getProgressIndicator() { return progress; } + /** + * Sets the progress indicator to be used by this application. + * @param progress The progress indicator to be used by this application. + */ public static void setProgressIndicator(JSProgress progress) { CC.progress = progress; } + /** + * This method does the initial preparation of the application. + */ protected static void initJSampler() { fos = null; @@ -131,52 +172,106 @@ actionPerformed(ActionEvent e) { CC.getProgressIndicator().start(); } }); - taskQueue.addTaskQueueListener(new TaskQueueListener() { - 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; - } - } - }); + taskQueue.addTaskQueueListener(getHandler()); taskQueue.start(); - getClient().addChannelCountListener(eventHandler); - getClient().addChannelInfoListener(eventHandler); - getClient().addStreamCountListener(eventHandler); - getClient().addVoiceCountListener(eventHandler); - getClient().addTotalVoiceCountListener(eventHandler); + getClient().addChannelCountListener(getHandler()); + getClient().addChannelInfoListener(getHandler()); + getClient().addStreamCountListener(getHandler()); + getClient().addVoiceCountListener(getHandler()); + getClient().addTotalVoiceCountListener(getHandler()); + + loadOrchestras(); + + for(int i = 0; i < getOrchestras().getOrchestraCount(); i++) { + getOrchestras().getOrchestra(i).addOrchestraListener(getHandler()); + } + getOrchestras().addOrchestraListListener(getHandler()); + } + + private final static OrchestraListModel orchestras = new DefaultOrchestraListModel(); + + /** + * Returns a list containing all available orchestras. + * @return A list containing all available orchestras. + */ + public static OrchestraListModel + getOrchestras() { return orchestras; } + + private static void + loadOrchestras() { + String s = Prefs.getOrchestras(); + if(s == null) return; + + ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes()); + Document doc = DOMUtils.readObject(bais); + + try { getOrchestras().readObject(doc.getDocumentElement()); } + catch(Exception x) { HF.showErrorMessage(x, "Loading orchestras: "); } + } + + private static void + saveOrchestras() { + Document doc = DOMUtils.createEmptyDocument(); + + Node node = doc.createElement("temp"); + doc.appendChild(node); + + getOrchestras().writeObject(doc, doc.getDocumentElement()); + + doc.replaceChild(node.getFirstChild(), node); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DOMUtils.writeObject(doc, baos); + Prefs.setOrchestras(baos.toString()); } + /** + * The exit point of the application which ensures clean exit with default exit status 0. + * @see #cleanExit(int i) + */ public static void cleanExit() { cleanExit(0); } + /** + * The exit point of the application which ensures clean exit. + * @param i The exit status. + */ public static void cleanExit(int i) { CC.getLogger().fine("CC.jsEnded"); System.exit(i); } + /** + * Gets the Client object that is used to communicate with the backend. + * @return The Client object that is used to communicate with the backend. + */ public static Client getClient() { return lsClient; } + private static final Vector listeners = new Vector(); + + /** + * Registers the specified listener to be notified when reconnecting to LinuxSampler. + * @param l The ActionListener to register. + */ + public static void + addReconnectListener(ActionListener l) { listeners.add(l); } + + /** + * Removes the specified listener. + * @param l The ActionListener to remove. + */ + public static void + removeReconnectListener(ActionListener l) { listeners.remove(l); } + + private static void + fireReconnectEvent() { + ActionEvent e = new ActionEvent(CC.class, ActionEvent.ACTION_PERFORMED, null); + for(ActionListener l : listeners) l.actionPerformed(e); + } private static final SamplerModel samplerModel = new DefaultSamplerModel(); @@ -187,6 +282,18 @@ public static SamplerModel getSamplerModel() { return samplerModel; } + /** + * Reconnects to LinuxSampler. + */ + public static void + reconnect() { + initSamplerModel(); + fireReconnectEvent(); + } + + /** + * This method updates the information about the backend state. + */ public static void initSamplerModel() { final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); @@ -243,8 +350,14 @@ getTaskQueue().add(cnt); } + private final static EventHandler eventHandler = new EventHandler(); + + private static EventHandler + getHandler() { return eventHandler; } + private static class EventHandler implements ChannelCountListener, ChannelInfoListener, - StreamCountListener, VoiceCountListener, TotalVoiceCountListener { + StreamCountListener, VoiceCountListener, TotalVoiceCountListener, + TaskQueueListener, OrchestraListener, OrchestraListListener { /** Invoked when the number of channels has changed. */ public void @@ -335,5 +448,66 @@ totalVoiceCountChanged(TotalVoiceCountEvent e) { getTaskQueue().add(new UpdateTotalVoiceCount()); } + + /** + * Invoked to indicate that the state of a task queue is changed. + * This method is invoked only from the event-dispatching thread. + */ + public void + stateChanged(TaskQueueEvent e) { + switch(e.getEventID()) { + case TASK_FETCHED: + 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(); + getProgressIndicator().stop(); + break; + } + } + + /** Invoked when the name of orchestra is changed. */ + public void + nameChanged(OrchestraEvent e) { saveOrchestras(); } + + /** Invoked when the description of orchestra is changed. */ + public void + descriptionChanged(OrchestraEvent e) { saveOrchestras(); } + + /** Invoked when an instrument is added to the orchestra. */ + public void + instrumentAdded(OrchestraEvent e) { saveOrchestras(); } + + /** Invoked when an instrument is removed from the orchestra. */ + public void + instrumentRemoved(OrchestraEvent e) { saveOrchestras(); } + + /** Invoked when the settings of an instrument are changed. */ + public void + instrumentChanged(OrchestraEvent e) { saveOrchestras(); } + + /** Invoked when an orchestra is added to the orchestra list. */ + public void + orchestraAdded(OrchestraListEvent e) { + e.getOrchestraModel().addOrchestraListener(getHandler()); + saveOrchestras(); + } + + /** Invoked when an orchestra is removed from the orchestra list. */ + public void + orchestraRemoved(OrchestraListEvent e) { + e.getOrchestraModel().removeOrchestraListener(getHandler()); + saveOrchestras(); + } } }