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 |
|
|
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 |
/** |
/** |
149 |
public static JSViewConfig |
public static JSViewConfig |
150 |
getViewConfig() { return viewConfig; } |
getViewConfig() { return viewConfig; } |
151 |
|
|
152 |
|
private static JSPrefs |
153 |
|
preferences() { return getViewConfig().preferences(); } |
154 |
|
|
155 |
/** |
/** |
156 |
* Sets the configuration of the current view. |
* Sets the configuration of the current view. |
157 |
*/ |
*/ |
240 |
|
|
241 |
HF.setUIDefaultFont(Prefs.getInterfaceFont()); |
HF.setUIDefaultFont(Prefs.getInterfaceFont()); |
242 |
|
|
|
getClient().setServerAddress(Prefs.getLSAddress()); |
|
|
getClient().setServerPort(Prefs.getLSPort()); |
|
|
|
|
243 |
timer.setRepeats(false); |
timer.setRepeats(false); |
244 |
|
|
245 |
timer.addActionListener(new ActionListener() { |
timer.addActionListener(new ActionListener() { |
376 |
public static OrchestraListModel |
public static OrchestraListModel |
377 |
getOrchestras() { return orchestras; } |
getOrchestras() { return orchestras; } |
378 |
|
|
379 |
|
private final static ServerList servers = new ServerList(); |
380 |
|
|
381 |
|
/** Returns the server list. */ |
382 |
|
public static ServerList |
383 |
|
getServerList() { return servers; } |
384 |
|
|
385 |
|
private static ServerListListener serverListListener = new ServerListListener(); |
386 |
|
|
387 |
|
private static class ServerListListener implements ChangeListener { |
388 |
|
public void |
389 |
|
stateChanged(ChangeEvent e) { |
390 |
|
saveServerList(); |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
|
private static final Vector<ChangeListener> idtmListeners = new Vector<ChangeListener>(); |
395 |
private static InstrumentsDbTreeModel instrumentsDbTreeModel = null; |
private static InstrumentsDbTreeModel instrumentsDbTreeModel = null; |
396 |
|
|
397 |
/** |
/** |
398 |
* Gets the tree model of the instruments database. |
* Gets the tree model of the instruments database. |
399 |
* If the currently used view doesn't have instruments |
* If the currently used view doesn't have instruments |
408 |
|
|
409 |
if(instrumentsDbTreeModel == null) { |
if(instrumentsDbTreeModel == null) { |
410 |
instrumentsDbTreeModel = new InstrumentsDbTreeModel(); |
instrumentsDbTreeModel = new InstrumentsDbTreeModel(); |
411 |
|
for(ChangeListener l : idtmListeners) l.stateChanged(null); |
412 |
} |
} |
413 |
|
|
414 |
return instrumentsDbTreeModel; |
return instrumentsDbTreeModel; |
415 |
} |
} |
416 |
|
|
417 |
|
public static void |
418 |
|
addInstrumentsDbChangeListener(ChangeListener l) { |
419 |
|
idtmListeners.add(l); |
420 |
|
} |
421 |
|
|
422 |
|
public static void |
423 |
|
removeInstrumentsDbChangeListener(ChangeListener l) { |
424 |
|
idtmListeners.remove(l); |
425 |
|
} |
426 |
|
|
427 |
/** |
/** |
428 |
* Loads the orchestras described in <code><jsampler_home>/orchestras.xml</code>. |
* Loads the orchestras described in <code><jsampler_home>/orchestras.xml</code>. |
429 |
* 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 |
437 |
|
|
438 |
try { |
try { |
439 |
String s = getJSamplerHome(); |
String s = getJSamplerHome(); |
|
if(s == null) return; |
|
|
getOrchestras().addOrchestraListListener(getHandler()); |
|
440 |
|
|
441 |
File f = new File(s + File.separator + "orchestras.xml.bkp"); |
File f = new File(s + File.separator + "orchestras.xml.bkp"); |
442 |
if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec"); |
if(f.isFile()) HF.createBackup("orchestras.xml.bkp", "orchestras.xml.rec"); |
449 |
} catch(Exception x) { |
} catch(Exception x) { |
450 |
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
451 |
} |
} |
452 |
|
|
453 |
|
getOrchestras().addOrchestraListListener(getHandler()); |
454 |
} |
} |
455 |
|
|
456 |
|
|
501 |
} |
} |
502 |
|
|
503 |
/** |
/** |
504 |
|
* Loads the servers' info described in <code><jsampler_home>/servers.xml</code>. |
505 |
|
* If file with name <code>servers.xml.bkp</code> exist in the JSampler's home |
506 |
|
* directory, this means that the last save has failed. In that case a recovery file |
507 |
|
* <code>servers.xml.rec</code> is created and a recovery procedure |
508 |
|
* will be initiated. |
509 |
|
*/ |
510 |
|
public static void |
511 |
|
loadServerList() { |
512 |
|
if(getJSamplerHome() == null) return; |
513 |
|
|
514 |
|
try { |
515 |
|
String s = getJSamplerHome(); |
516 |
|
|
517 |
|
File f = new File(s + File.separator + "servers.xml.bkp"); |
518 |
|
if(f.isFile()) HF.createBackup("servers.xml.bkp", "servers.xml.rec"); |
519 |
|
|
520 |
|
FileInputStream fis; |
521 |
|
fis = new FileInputStream(s + File.separator + "servers.xml"); |
522 |
|
|
523 |
|
loadServerList(fis); |
524 |
|
fis.close(); |
525 |
|
} catch(Exception x) { |
526 |
|
getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
527 |
|
} |
528 |
|
|
529 |
|
getServerList().addChangeListener(serverListListener); |
530 |
|
|
531 |
|
/* We should have at least one server to connect. */ |
532 |
|
if(getServerList().getServerCount() == 0) { |
533 |
|
Server server = new Server(); |
534 |
|
server.setName("127.0.0.1:8888"); |
535 |
|
server.setAddress("127.0.0.1"); |
536 |
|
server.setPort(8888); |
537 |
|
getServerList().addServer(server); |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
|
|
542 |
|
private static void |
543 |
|
loadServerList(InputStream in) { |
544 |
|
Document doc = DOMUtils.readObject(in); |
545 |
|
|
546 |
|
try { getServerList().readObject(doc.getDocumentElement()); } |
547 |
|
catch(Exception x) { |
548 |
|
HF.showErrorMessage(x, "Loading server list: "); |
549 |
|
return; |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
|
private static void |
554 |
|
saveServerList() { |
555 |
|
try { |
556 |
|
String s = getJSamplerHome(); |
557 |
|
if(s == null) return; |
558 |
|
|
559 |
|
HF.createBackup("servers.xml", "servers.xml.bkp"); |
560 |
|
|
561 |
|
FileOutputStream fos; |
562 |
|
fos = new FileOutputStream(s + File.separator + "servers.xml", false); |
563 |
|
|
564 |
|
Document doc = DOMUtils.createEmptyDocument(); |
565 |
|
|
566 |
|
Node node = doc.createElement("temp"); |
567 |
|
doc.appendChild(node); |
568 |
|
|
569 |
|
getServerList().writeObject(doc, doc.getDocumentElement()); |
570 |
|
|
571 |
|
doc.replaceChild(node.getFirstChild(), node); |
572 |
|
|
573 |
|
DOMUtils.writeObject(doc, fos); |
574 |
|
|
575 |
|
fos.close(); |
576 |
|
|
577 |
|
HF.deleteFile("servers.xml.bkp"); |
578 |
|
} catch(Exception x) { |
579 |
|
HF.showErrorMessage(x, "Saving server list: "); |
580 |
|
return; |
581 |
|
} |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
* 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. |
586 |
* @see #cleanExit(int i) |
* @see #cleanExit(int i) |
587 |
*/ |
*/ |
637 |
getSamplerModel() { return samplerModel; } |
getSamplerModel() { return samplerModel; } |
638 |
|
|
639 |
/** |
/** |
640 |
|
* Connects to LinuxSampler. |
641 |
|
*/ |
642 |
|
public static void |
643 |
|
connect() { initSamplerModel(); } |
644 |
|
|
645 |
|
/** |
646 |
* Reconnects to LinuxSampler. |
* Reconnects to LinuxSampler. |
647 |
*/ |
*/ |
648 |
public static void |
public static void |
649 |
reconnect() { |
reconnect() { initSamplerModel(getCurrentServer()); } |
650 |
initSamplerModel(); |
|
651 |
fireReconnectEvent(); |
private static Server currentServer = null; |
|
} |
|
652 |
|
|
653 |
/** |
/** |
654 |
* This method updates the information about the backend state. |
* Gets the server, to which the frontend is going to connect |
655 |
|
* or is already connected. |
656 |
|
*/ |
657 |
|
public static Server |
658 |
|
getCurrentServer() { return currentServer; } |
659 |
|
|
660 |
|
/** |
661 |
|
* Sets the current server. |
662 |
*/ |
*/ |
663 |
public static void |
public static void |
664 |
|
setCurrentServer(Server server) { currentServer = server; } |
665 |
|
|
666 |
|
/** |
667 |
|
* This method updates the information about the backend state. |
668 |
|
*/ |
669 |
|
private static void |
670 |
initSamplerModel() { |
initSamplerModel() { |
671 |
|
Server srv = getMainFrame().getServer(); |
672 |
|
if(srv == null) return; |
673 |
|
initSamplerModel(srv); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
|
* This method updates the information about the backend state. |
678 |
|
*/ |
679 |
|
private static void |
680 |
|
initSamplerModel(Server srv) { |
681 |
|
setCurrentServer(srv); |
682 |
|
final SetServerAddress ssa = new SetServerAddress(srv.getAddress(), srv.getPort()); |
683 |
|
|
684 |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
final DefaultSamplerModel model = (DefaultSamplerModel)getSamplerModel(); |
685 |
|
|
686 |
final Global.GetServerInfo gsi = new Global.GetServerInfo(); |
final Global.GetServerInfo gsi = new Global.GetServerInfo(); |
768 |
cnt.addTaskListener(new TaskListener() { |
cnt.addTaskListener(new TaskListener() { |
769 |
public void |
public void |
770 |
taskPerformed(TaskEvent e) { |
taskPerformed(TaskEvent e) { |
771 |
if(cnt.doneWithErrors()) return; |
if(cnt.doneWithErrors()) { |
772 |
|
setCurrentServer(null); |
773 |
|
retryToConnect(); |
774 |
|
return; |
775 |
|
} |
776 |
|
|
777 |
getTaskQueue().add(gsi); |
getTaskQueue().add(gsi); |
778 |
getTaskQueue().add(gaod); |
getTaskQueue().add(gaod); |
785 |
getTaskQueue().add(uc); |
getTaskQueue().add(uc); |
786 |
} |
} |
787 |
}); |
}); |
788 |
getTaskQueue().add(cnt); |
|
789 |
|
ssa.addTaskListener(new TaskListener() { |
790 |
|
public void |
791 |
|
taskPerformed(TaskEvent e) { |
792 |
|
CC.getTaskQueue().add(cnt); |
793 |
|
} |
794 |
|
}); |
795 |
|
|
796 |
|
getSamplerModel().reset(); |
797 |
|
if(instrumentsDbTreeModel != null) { |
798 |
|
instrumentsDbTreeModel.reset(); |
799 |
|
instrumentsDbTreeModel = null; |
800 |
|
} |
801 |
|
|
802 |
|
getTaskQueue().removePendingTasks(); |
803 |
|
getTaskQueue().add(ssa); |
804 |
|
|
805 |
|
fireReconnectEvent(); |
806 |
|
} |
807 |
|
|
808 |
|
private static void |
809 |
|
retryToConnect() { |
810 |
|
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
811 |
|
public void |
812 |
|
run() { changeBackend(); } |
813 |
|
}); |
814 |
|
} |
815 |
|
|
816 |
|
public static void |
817 |
|
changeBackend() { |
818 |
|
Server s = getMainFrame().getServer(true); |
819 |
|
if(s != null) initSamplerModel(s); |
820 |
} |
} |
821 |
|
|
822 |
private static class GetFxSendsListener implements TaskListener { |
private static class GetFxSendsListener implements TaskListener { |
1051 |
} |
} |
1052 |
} |
} |
1053 |
|
|
1054 |
|
public static void |
1055 |
|
scheduleInTaskQueue(final Runnable r) { |
1056 |
|
Task dummy = new Global.DummyTask(); |
1057 |
|
dummy.addTaskListener(new TaskListener() { |
1058 |
|
public void |
1059 |
|
taskPerformed(TaskEvent e) { |
1060 |
|
javax.swing.SwingUtilities.invokeLater(r); |
1061 |
|
} |
1062 |
|
}); |
1063 |
|
|
1064 |
|
CC.getTaskQueue().add(dummy); |
1065 |
|
} |
1066 |
|
|
1067 |
|
public static boolean |
1068 |
|
verifyConnection() { |
1069 |
|
if(getCurrentServer() == null) { |
1070 |
|
HF.showErrorMessage(i18n.getError("CC.notConnected")); |
1071 |
|
return false; |
1072 |
|
} |
1073 |
|
|
1074 |
|
return true; |
1075 |
|
} |
1076 |
|
|
1077 |
|
|
1078 |
private final static EventHandler eventHandler = new EventHandler(); |
private final static EventHandler eventHandler = new EventHandler(); |
1079 |
|
|