/[svn]/jsampler/trunk/src/org/jsampler/CC.java
ViewVC logotype

Diff of /jsampler/trunk/src/org/jsampler/CC.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1567 by iliev, Thu Dec 6 19:37:41 2007 UTC revision 1719 by iliev, Wed Mar 19 10:07:36 2008 UTC
# Line 42  import java.util.logging.StreamHandler; Line 42  import java.util.logging.StreamHandler;
42    
43  import javax.swing.Timer;  import javax.swing.Timer;
44    
45    import javax.swing.event.ChangeEvent;
46    import javax.swing.event.ChangeListener;
47    
48  import net.sf.juife.Task;  import net.sf.juife.Task;
49  import net.sf.juife.TaskQueue;  import net.sf.juife.TaskQueue;
50    
# Line 77  import org.w3c.dom.Document; Line 80  import org.w3c.dom.Document;
80  import org.w3c.dom.Node;  import org.w3c.dom.Node;
81    
82  import static org.jsampler.JSI18n.i18n;  import static org.jsampler.JSI18n.i18n;
83    import static org.jsampler.JSPrefs.MANUAL_SERVER_SELECT_ON_STARTUP;
84    
85    
86  /**  /**
# Line 132  public class CC { Line 136  public class CC {
136          scheduleTask(Task t) {          scheduleTask(Task t) {
137                  while(getTaskQueue().removeTask(t)) { }                  while(getTaskQueue().removeTask(t)) { }
138                                    
                 if(getTaskQueue().getPendingTaskCount() == 0) {  
                         if(t.equals(getTaskQueue().getRunningTask())) return;  
                 }  
                   
139                  getTaskQueue().add(t);                  getTaskQueue().add(t);
140          }          }
141                    
# Line 145  public class CC { Line 145  public class CC {
145          public static JSViewConfig          public static JSViewConfig
146          getViewConfig() { return viewConfig; }          getViewConfig() { return viewConfig; }
147                    
148            private static JSPrefs
149            preferences() { return getViewConfig().preferences(); }
150            
151          /**          /**
152           * Sets the configuration of the current view.           * Sets the configuration of the current view.
153           */           */
# Line 233  public class CC { Line 236  public class CC {
236                                    
237                  HF.setUIDefaultFont(Prefs.getInterfaceFont());                  HF.setUIDefaultFont(Prefs.getInterfaceFont());
238                                    
                 getClient().setServerAddress(Prefs.getLSAddress());  
                 getClient().setServerPort(Prefs.getLSPort());  
                   
239                  timer.setRepeats(false);                  timer.setRepeats(false);
240                                    
241                  timer.addActionListener(new ActionListener() {                  timer.addActionListener(new ActionListener() {
# Line 372  public class CC { Line 372  public class CC {
372          public static OrchestraListModel          public static OrchestraListModel
373          getOrchestras() { return orchestras; }          getOrchestras() { return orchestras; }
374                    
375            private final static ServerList servers = new ServerList();
376            
377            /** Returns the server list. */
378            public static ServerList
379            getServerList() { return servers; }
380            
381            private static ServerListListener serverListListener = new ServerListListener();
382            
383            private static class ServerListListener implements ChangeListener {
384                    public void
385                    stateChanged(ChangeEvent e) {
386                            saveServerList();
387                    }
388            }
389            
390            private static final Vector<ChangeListener> idtmListeners = new Vector<ChangeListener>();
391          private static InstrumentsDbTreeModel instrumentsDbTreeModel = null;          private static InstrumentsDbTreeModel instrumentsDbTreeModel = null;
392            
393          /**          /**
394           * Gets the tree model of the instruments database.           * Gets the tree model of the instruments database.
395           * If the currently used view doesn't have instruments           * If the currently used view doesn't have instruments
# Line 387  public class CC { Line 404  public class CC {
404                                    
405                  if(instrumentsDbTreeModel == null) {                  if(instrumentsDbTreeModel == null) {
406                          instrumentsDbTreeModel = new InstrumentsDbTreeModel();                          instrumentsDbTreeModel = new InstrumentsDbTreeModel();
407                            for(ChangeListener l : idtmListeners) l.stateChanged(null);
408                  }                  }
409                                    
410                  return instrumentsDbTreeModel;                  return instrumentsDbTreeModel;
411          }          }
412                    
413            public static void
414            addInstrumentsDbChangeListener(ChangeListener l) {
415                    idtmListeners.add(l);
416            }
417            
418            public static void
419            removeInstrumentsDbChangeListener(ChangeListener l) {
420                    idtmListeners.remove(l);
421            }
422            
423          /**          /**
424           * Loads the orchestras described in <code>&lt;jsampler_home&gt;/orchestras.xml</code>.           * Loads the orchestras described in <code>&lt;jsampler_home&gt;/orchestras.xml</code>.
425           * If file with name <code>orchestras.xml.bkp</code> exist in the JSampler's home           * If file with name <code>orchestras.xml.bkp</code> exist in the JSampler's home
# Line 405  public class CC { Line 433  public class CC {
433                                    
434                  try {                  try {
435                          String s = getJSamplerHome();                          String s = getJSamplerHome();
                         if(s == null) return;  
                         getOrchestras().addOrchestraListListener(getHandler());  
436                                                    
437                          File f = new File(s + File.separator + "orchestras.xml.bkp");                          File f = new File(s + File.separator + "orchestras.xml.bkp");
438                          if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");                          if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec");
# Line 419  public class CC { Line 445  public class CC {
445                  } catch(Exception x) {                  } catch(Exception x) {
446                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);                          getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
447                  }                  }
448                    
449                    getOrchestras().addOrchestraListListener(getHandler());
450          }          }
451                    
452                    
# Line 469  public class CC { Line 497  public class CC {
497          }          }
498                    
499          /**          /**
500             * Loads the servers' info described in <code>&lt;jsampler_home&gt;/servers.xml</code>.
501             * If file with name <code>servers.xml.bkp</code> exist in the JSampler's home
502             * directory, this means that the last save has failed. In that case a recovery file
503             * <code>servers.xml.rec</code> is created and a recovery procedure
504             * will be initiated.
505             */
506            public static void
507            loadServerList() {
508                    if(getJSamplerHome() == null) return;
509                    
510                    try {
511                            String s = getJSamplerHome();
512                            
513                            File f = new File(s + File.separator + "servers.xml.bkp");
514                            if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec");
515                            
516                            FileInputStream fis;
517                            fis = new FileInputStream(s + File.separator + "servers.xml");
518                            
519                            loadServerList(fis);
520                            fis.close();
521                    } catch(Exception x) {
522                            getLogger().log(Level.INFO, HF.getErrorMessage(x), x);
523                    }
524                    
525                    getServerList().addChangeListener(serverListListener);
526                    
527                    /* We should have at least one server to connect. */
528                    if(getServerList().getServerCount() == 0) {
529                            Server server = new Server();
530                            server.setName("127.0.0.1:8888");
531                            server.setAddress("127.0.0.1");
532                            server.setPort(8888);
533                            getServerList().addServer(server);
534                    }
535            }
536            
537            
538            private static void
539            loadServerList(InputStream in) {
540                    Document doc = DOMUtils.readObject(in);
541                    
542                    try { getServerList().readObject(doc.getDocumentElement()); }
543                    catch(Exception x) {
544                            HF.showErrorMessage(x, "Loading server list: ");
545                            return;
546                    }
547            }
548            
549            private static void
550            saveServerList() {
551                    try {
552                            String s = getJSamplerHome();
553                            if(s == null) return;
554                            
555                            HF.createBackup("servers.xml", "servers.xml.bkp");
556                            
557                            FileOutputStream fos;
558                            fos = new FileOutputStream(s + File.separator + "servers.xml", false);
559                            
560                            Document doc = DOMUtils.createEmptyDocument();
561                    
562                            Node node = doc.createElement("temp");
563                            doc.appendChild(node);
564                            
565                            getServerList().writeObject(doc, doc.getDocumentElement());
566                            
567                            doc.replaceChild(node.getFirstChild(), node);
568                    
569                            DOMUtils.writeObject(doc, fos);
570                            
571                            fos.close();
572                            
573                            HF.deleteFile("servers.xml.bkp");
574                    } catch(Exception x) {
575                            HF.showErrorMessage(x, "Saving server list: ");
576                            return;
577                    }
578            }
579            
580            /**
581           * The exit point of the application which ensures clean exit with default exit status 0.           * The exit point of the application which ensures clean exit with default exit status 0.
582           *  @see #cleanExit(int i)           *  @see #cleanExit(int i)
583           */           */
# Line 524  public class CC { Line 633  public class CC {
633          getSamplerModel() { return samplerModel; }          getSamplerModel() { return samplerModel; }
634                    
635          /**          /**
636             * Connects to LinuxSampler.
637             */
638            public static void
639            connect() { initSamplerModel(); }
640            
641            /**
642           * Reconnects to LinuxSampler.           * Reconnects to LinuxSampler.
643           */           */
644          public static void          public static void
645          reconnect() {          reconnect() { initSamplerModel(getCurrentServer()); }
646                  initSamplerModel();          
647                  fireReconnectEvent();          private static Server currentServer = null;
         }  
648                    
649          /**          /**
650           * This method updates the information about the backend state.           * Gets the server, to which the frontend is going to connect
651             * or is already connected.
652             */
653            public static Server
654            getCurrentServer() { return currentServer; }
655            
656            /**
657             * Sets the current server.
658           */           */
659          public static void          public static void
660            setCurrentServer(Server server) { currentServer = server; }
661            
662            /**
663             * This method updates the information about the backend state.
664             */
665            private static void
666          initSamplerModel() {          initSamplerModel() {
667                    Server srv = getMainFrame().getServer();
668                    if(srv == null) return;
669                    initSamplerModel(srv);
670            }
671            
672            /**
673             * This method updates the information about the backend state.
674             */
675            private static void
676            initSamplerModel(Server srv) {
677                    setCurrentServer(srv);
678                    final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort());
679                    
680                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();                  final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel();
681                                    
682                  final Global.GetServerInfo gsi = new Global.GetServerInfo();                  final Global.GetServerInfo gsi = new Global.GetServerInfo();
# Line 624  public class CC { Line 764  public class CC {
764                  cnt.addTaskListener(new TaskListener() {                  cnt.addTaskListener(new TaskListener() {
765                          public void                          public void
766                          taskPerformed(TaskEvent e) {                          taskPerformed(TaskEvent e) {
767                                  if(cnt.doneWithErrors()) return;                                  if(cnt.doneWithErrors()) {
768                                            setCurrentServer(null);
769                                            retryToConnect();
770                                            return;
771                                    }
772                                                                    
773                                  getTaskQueue().add(gsi);                                  getTaskQueue().add(gsi);
774                                  getTaskQueue().add(gaod);                                  getTaskQueue().add(gaod);
# Line 637  public class CC { Line 781  public class CC {
781                                  getTaskQueue().add(uc);                                  getTaskQueue().add(uc);
782                          }                          }
783                  });                  });
784                  getTaskQueue().add(cnt);                  
785                    ssa.addTaskListener(new TaskListener() {
786                            public void
787                            taskPerformed(TaskEvent e) {
788                                    CC.getTaskQueue().add(cnt);
789                            }
790                    });
791                    
792                    getSamplerModel().reset();
793                    if(instrumentsDbTreeModel != null) {
794                            instrumentsDbTreeModel.reset();
795                            instrumentsDbTreeModel = null;
796                    }
797                    
798                    getTaskQueue().removePendingTasks();
799                    getTaskQueue().add(ssa);
800                    
801                    fireReconnectEvent();
802            }
803            
804            private static void
805            retryToConnect() {
806                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
807                            public void
808                            run() { changeBackend(); }
809                    });
810            }
811            
812            public static void
813            changeBackend() {
814                    Server s = getMainFrame().getServer(true);
815                    if(s != null) initSamplerModel(s);
816          }          }
817                    
818          private static class GetFxSendsListener implements TaskListener {          private static class GetFxSendsListener implements TaskListener {
# Line 734  public class CC { Line 909  public class CC {
909                          sb.append("\r\n");                          sb.append("\r\n");
910                  }                  }
911                                    
912                    exportInstrMapsToLscpScript(lscpClient);
913                    sb.append(out.toString());
914                    out.reset();
915                    sb.append("\r\n");
916                    
917                  SamplerChannelModel[] channels = getSamplerModel().getChannels();                  SamplerChannelModel[] channels = getSamplerModel().getChannels();
918                                    
919                  for(int i = 0; i < channels.length; i++) {                  for(int i = 0; i < channels.length; i++) {
# Line 751  public class CC { Line 931  public class CC {
931                          sb.append("\r\n");                          sb.append("\r\n");
932                  }                  }
933                                    
                 exportInstrMapsToLscpScript(lscpClient);  
                 sb.append(out.toString());  
                 out.reset();  
                   
934                  return sb.toString();                  return sb.toString();
935          }          }
936                    
# Line 818  public class CC { Line 994  public class CC {
994                                  lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());                                  lscpCLient.setChannelMidiInputChannel(chnId, chn.getMidiInputChannel());
995                          }                          }
996                                                    
997                            if(chn.getEngine() != null) {
998                                    lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);
999                                    lscpCLient.setChannelVolume(chnId, chn.getVolume());
1000                                    int mapId = chn.getMidiInstrumentMapId();
1001                                    lscpCLient.setChannelMidiInstrumentMap(chnId, mapId);
1002                            }
1003                            
1004                          id = chn.getAudioOutputDevice();                          id = chn.getAudioOutputDevice();
1005                          if(id != -1) {                          if(id != -1) {
1006                                  for(int i = 0; i < sm.getAudioDeviceCount(); i++) {                                  for(int i = 0; i < sm.getAudioDeviceCount(); i++) {
# Line 837  public class CC { Line 1020  public class CC {
1020                                  }                                  }
1021                          }                          }
1022                                                    
                         if(chn.getEngine() != null) {  
                                 lscpCLient.loadSamplerEngine(chn.getEngine().getName(), chnId);  
                                 lscpCLient.setChannelVolume(chnId, chn.getVolume());  
                         }  
                           
1023                          String s = chn.getInstrumentFile();                          String s = chn.getInstrumentFile();
1024                          int i = chn.getInstrumentIndex();                          int i = chn.getInstrumentIndex();
1025                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);                          if(s != null) lscpCLient.loadInstrument(s, i, chnId, true);
# Line 872  public class CC { Line 1050  public class CC {
1050                  }                  }
1051          }          }
1052                    
1053            public static void
1054            scheduleInTaskQueue(final Runnable r) {
1055                    Task dummy = new Global.DummyTask();
1056                    dummy.addTaskListener(new TaskListener() {
1057                            public void
1058                            taskPerformed(TaskEvent e) {
1059                                    javax.swing.SwingUtilities.invokeLater(r);
1060                            }
1061                    });
1062                    
1063                    CC.getTaskQueue().add(dummy);
1064            }
1065            
1066            public static boolean
1067            verifyConnection() {
1068                    if(getCurrentServer() == null) {
1069                            HF.showErrorMessage(i18n.getError("CC.notConnected"));
1070                            return false;
1071                    }
1072                    
1073                    return true;
1074            }
1075            
1076                    
1077          private final static EventHandler eventHandler = new EventHandler();          private final static EventHandler eventHandler = new EventHandler();
1078                    
# Line 1001  public class CC { Line 1202  public class CC {
1202                  /** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */                  /** Invoked when the number of MIDI instruments in a MIDI instrument map is changed. */
1203                  public void                  public void
1204                  instrumentCountChanged(MidiInstrumentCountEvent e) {                  instrumentCountChanged(MidiInstrumentCountEvent e) {
1205                          getTaskQueue().add(new Midi.UpdateInstruments(e.getMapId()));                          scheduleTask(new Midi.UpdateInstruments(e.getMapId()));
1206                  }                  }
1207                                    
1208                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */                  /** Invoked when a MIDI instrument in a MIDI instrument map is changed. */

Legend:
Removed from v.1567  
changed lines
  Added in v.1719

  ViewVC Help
Powered by ViewVC