--- jsampler/trunk/src/org/jsampler/CC.java 2007/12/06 19:37:41 1567 +++ jsampler/trunk/src/org/jsampler/CC.java 2008/04/29 22:22:40 1729 @@ -42,6 +42,9 @@ import javax.swing.Timer; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + import net.sf.juife.Task; import net.sf.juife.TaskQueue; @@ -77,6 +80,7 @@ import org.w3c.dom.Node; import static org.jsampler.JSI18n.i18n; +import static org.jsampler.JSPrefs.MANUAL_SERVER_SELECT_ON_STARTUP; /** @@ -132,10 +136,6 @@ scheduleTask(Task t) { while(getTaskQueue().removeTask(t)) { } - if(getTaskQueue().getPendingTaskCount() == 0) { - if(t.equals(getTaskQueue().getRunningTask())) return; - } - getTaskQueue().add(t); } @@ -145,6 +145,9 @@ public static JSViewConfig getViewConfig() { return viewConfig; } + private static JSPrefs + preferences() { return getViewConfig().preferences(); } + /** * Sets the configuration of the current view. */ @@ -233,9 +236,6 @@ HF.setUIDefaultFont(Prefs.getInterfaceFont()); - getClient().setServerAddress(Prefs.getLSAddress()); - getClient().setServerPort(Prefs.getLSPort()); - timer.setRepeats(false); timer.addActionListener(new ActionListener() { @@ -372,7 +372,24 @@ public static OrchestraListModel getOrchestras() { return orchestras; } + private final static ServerList servers = new ServerList(); + + /** Returns the server list. */ + public static ServerList + getServerList() { return servers; } + + private static ServerListListener serverListListener = new ServerListListener(); + + private static class ServerListListener implements ChangeListener { + public void + stateChanged(ChangeEvent e) { + saveServerList(); + } + } + + private static final Vector idtmListeners = new Vector(); private static InstrumentsDbTreeModel instrumentsDbTreeModel = null; + /** * Gets the tree model of the instruments database. * If the currently used view doesn't have instruments @@ -383,15 +400,32 @@ */ public static InstrumentsDbTreeModel getInstrumentsDbTreeModel() { + if(CC.getSamplerModel().getServerInfo() == null) return null; if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) return null; if(instrumentsDbTreeModel == null) { instrumentsDbTreeModel = new InstrumentsDbTreeModel(); + for(ChangeListener l : idtmListeners) l.stateChanged(null); } return instrumentsDbTreeModel; } + public static void + addInstrumentsDbChangeListener(ChangeListener l) { + idtmListeners.add(l); + } + + public static void + removeInstrumentsDbChangeListener(ChangeListener l) { + idtmListeners.remove(l); + } + + private static final LostFilesModel lostFilesModel = new LostFilesModel(); + + public static LostFilesModel + getLostFilesModel() { return lostFilesModel; } + /** * Loads the orchestras described in <jsampler_home>/orchestras.xml. * If file with name orchestras.xml.bkp exist in the JSampler's home @@ -405,8 +439,6 @@ try { String s = getJSamplerHome(); - if(s == null) return; - getOrchestras().addOrchestraListListener(getHandler()); File f = new File(s + File.separator + "orchestras.xml.bkp"); if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec"); @@ -419,6 +451,8 @@ } catch(Exception x) { getLogger().log(Level.INFO, HF.getErrorMessage(x), x); } + + getOrchestras().addOrchestraListListener(getHandler()); } @@ -469,6 +503,87 @@ } /** + * Loads the servers' info described in <jsampler_home>/servers.xml. + * If file with name servers.xml.bkp exist in the JSampler's home + * directory, this means that the last save has failed. In that case a recovery file + * servers.xml.rec is created and a recovery procedure + * will be initiated. + */ + public static void + loadServerList() { + if(getJSamplerHome() == null) return; + + try { + String s = getJSamplerHome(); + + File f = new File(s + File.separator + "servers.xml.bkp"); + if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec"); + + FileInputStream fis; + fis = new FileInputStream(s + File.separator + "servers.xml"); + + loadServerList(fis); + fis.close(); + } catch(Exception x) { + getLogger().log(Level.INFO, HF.getErrorMessage(x), x); + } + + getServerList().addChangeListener(serverListListener); + + /* We should have at least one server to connect. */ + if(getServerList().getServerCount() == 0) { + Server server = new Server(); + server.setName("127.0.0.1:8888"); + server.setAddress("127.0.0.1"); + server.setPort(8888); + getServerList().addServer(server); + } + } + + + private static void + loadServerList(InputStream in) { + Document doc = DOMUtils.readObject(in); + + try { getServerList().readObject(doc.getDocumentElement()); } + catch(Exception x) { + HF.showErrorMessage(x, "Loading server list: "); + return; + } + } + + private static void + saveServerList() { + try { + String s = getJSamplerHome(); + if(s == null) return; + + HF.createBackup("servers.xml", "servers.xml.bkp"); + + FileOutputStream fos; + fos = new FileOutputStream(s + File.separator + "servers.xml", false); + + Document doc = DOMUtils.createEmptyDocument(); + + Node node = doc.createElement("temp"); + doc.appendChild(node); + + getServerList().writeObject(doc, doc.getDocumentElement()); + + doc.replaceChild(node.getFirstChild(), node); + + DOMUtils.writeObject(doc, fos); + + fos.close(); + + HF.deleteFile("servers.xml.bkp"); + } catch(Exception x) { + HF.showErrorMessage(x, "Saving server list: "); + return; + } + } + + /** * The exit point of the application which ensures clean exit with default exit status 0. * @see #cleanExit(int i) */ @@ -524,19 +639,50 @@ getSamplerModel() { return samplerModel; } /** + * Connects to LinuxSampler. + */ + public static void + connect() { initSamplerModel(); } + + /** * Reconnects to LinuxSampler. */ public static void - reconnect() { - initSamplerModel(); - fireReconnectEvent(); - } + reconnect() { initSamplerModel(getCurrentServer()); } + + private static Server currentServer = null; /** - * This method updates the information about the backend state. + * Gets the server, to which the frontend is going to connect + * or is already connected. + */ + public static Server + getCurrentServer() { return currentServer; } + + /** + * Sets the current server. */ public static void + setCurrentServer(Server server) { currentServer = server; } + + /** + * This method updates the information about the backend state. + */ + private static void initSamplerModel() { + Server srv = getMainFrame().getServer(); + if(srv == null) return; + initSamplerModel(srv); + } + + /** + * This method updates the information about the backend state. + */ + private static void + initSamplerModel(Server srv) { + setCurrentServer(srv); + final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort()); + final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); final Global.GetServerInfo gsi = new Global.GetServerInfo(); @@ -624,7 +770,11 @@ cnt.addTaskListener(new TaskListener() { public void taskPerformed(TaskEvent e) { - if(cnt.doneWithErrors()) return; + if(cnt.doneWithErrors()) { + setCurrentServer(null); + retryToConnect(); + return; + } getTaskQueue().add(gsi); getTaskQueue().add(gaod); @@ -637,7 +787,38 @@ getTaskQueue().add(uc); } }); - getTaskQueue().add(cnt); + + ssa.addTaskListener(new TaskListener() { + public void + taskPerformed(TaskEvent e) { + CC.getTaskQueue().add(cnt); + } + }); + + getSamplerModel().reset(); + if(instrumentsDbTreeModel != null) { + instrumentsDbTreeModel.reset(); + instrumentsDbTreeModel = null; + } + + getTaskQueue().removePendingTasks(); + getTaskQueue().add(ssa); + + fireReconnectEvent(); + } + + private static void + retryToConnect() { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + public void + run() { changeBackend(); } + }); + } + + public static void + changeBackend() { + Server s = getMainFrame().getServer(true); + if(s != null) initSamplerModel(s); } private static class GetFxSendsListener implements TaskListener { @@ -734,6 +915,11 @@ sb.append("\r\n"); } + exportInstrMapsToLscpScript(lscpClient); + sb.append(out.toString()); + out.reset(); + sb.append("\r\n"); + SamplerChannelModel[] channels = getSamplerModel().getChannels(); for(int i = 0; i < channels.length; i++) { @@ -751,10 +937,6 @@ sb.append("\r\n"); } - exportInstrMapsToLscpScript(lscpClient); - sb.append(out.toString()); - out.reset(); - return sb.toString(); } @@ -818,6 +1000,13 @@ lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel()); } + if(chn.getEngine() != null) { + lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId); + lscpCLient.setChannelVolume(chnId, chn.getVolume()); + int mapId = chn.getMidiInstrumentMapId(); + lscpCLient.setChannelMidiInstrumentMap(chnId, mapId); + } + id = chn.getAudioOutputDevice(); if(id != -1) { for(int i = 0; i < sm.getAudioDeviceCount(); i++) { @@ -837,11 +1026,6 @@ } } - if(chn.getEngine() != null) { - lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId); - lscpCLient.setChannelVolume(chnId, chn.getVolume()); - } - String s = chn.getInstrumentFile(); int i = chn.getInstrumentIndex(); if(s != null) lscpCLient.loadInstrument(s, i, chnId, true); @@ -872,6 +1056,29 @@ } } + public static void + scheduleInTaskQueue(final Runnable r) { + Task dummy = new Global.DummyTask(); + dummy.addTaskListener(new TaskListener() { + public void + taskPerformed(TaskEvent e) { + javax.swing.SwingUtilities.invokeLater(r); + } + }); + + CC.getTaskQueue().add(dummy); + } + + public static boolean + verifyConnection() { + if(getCurrentServer() == null) { + HF.showErrorMessage(i18n.getError("CC.notConnected")); + return false; + } + + return true; + } + private final static EventHandler eventHandler = new EventHandler(); @@ -1001,7 +1208,7 @@ /** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */ public void instrumentCountChanged(MidiInstrumentCountEvent e) { - getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId())); + scheduleTask(new Midi.UpdateInstruments(e.getMapId())); } /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */