48 |
import org.linuxsampler.lscp.event.InstrumentsDbEvent; |
import org.linuxsampler.lscp.event.InstrumentsDbEvent; |
49 |
import org.linuxsampler.lscp.event.InstrumentsDbListener; |
import org.linuxsampler.lscp.event.InstrumentsDbListener; |
50 |
|
|
51 |
|
import static org.linuxsampler.lscp.Parser.*; |
52 |
|
|
53 |
|
|
54 |
/** |
/** |
55 |
* |
* |
56 |
* @author Grigor Iliev |
* @author Grigor Iliev |
72 |
*/ |
*/ |
73 |
public |
public |
74 |
InstrumentsDbTreeModel(final ActionListener l) { |
InstrumentsDbTreeModel(final ActionListener l) { |
75 |
|
// TODO: This shouldn't be done in the event-dispatcing thread |
76 |
|
CC.getClient().addInstrumentsDbListener(getHandler()); |
77 |
|
/////// |
78 |
|
|
79 |
|
initDb(l); |
80 |
|
} |
81 |
|
|
82 |
|
private void |
83 |
|
initDb(final ActionListener l) { |
84 |
final InstrumentsDb.GetDrectory gdi = new InstrumentsDb.GetDrectory("/"); |
final InstrumentsDb.GetDrectory gdi = new InstrumentsDb.GetDrectory("/"); |
85 |
final InstrumentsDb.GetDrectories gd = new InstrumentsDb.GetDrectories("/"); |
final InstrumentsDb.GetDrectories gd = new InstrumentsDb.GetDrectories("/"); |
86 |
final InstrumentsDb.GetInstruments gi = new InstrumentsDb.GetInstruments("/"); |
final InstrumentsDb.GetInstruments gi = new InstrumentsDb.GetInstruments("/"); |
92 |
root = new DbDirectoryTreeNode(gdi.getResult()); |
root = new DbDirectoryTreeNode(gdi.getResult()); |
93 |
fireNodeStructureChanged(root); |
fireNodeStructureChanged(root); |
94 |
|
|
|
// TODO: This shouldn't be done in the event-dispatcing thread |
|
|
CC.getClient().addInstrumentsDbListener(getHandler()); |
|
|
/////// |
|
95 |
CC.getTaskQueue().add(gd); |
CC.getTaskQueue().add(gd); |
96 |
CC.getTaskQueue().add(gi); |
CC.getTaskQueue().add(gi); |
97 |
} |
} |
107 |
for(int i = 0; i < root.getChildCount(); i++) { |
for(int i = 0; i < root.getChildCount(); i++) { |
108 |
DbDirectoryTreeNode node = root.getChildAt(i); |
DbDirectoryTreeNode node = root.getChildAt(i); |
109 |
node.setConnected(true); |
node.setConnected(true); |
110 |
updateDirectoryContent(node, "/" + node.toString()); |
updateDirectoryContent(node, "/" + toEscapedFileName(node)); |
111 |
} |
} |
112 |
|
|
113 |
if(l != null) l.actionPerformed(null); |
if(l != null) l.actionPerformed(null); |
140 |
child.setConnected(true); |
child.setConnected(true); |
141 |
String pathName = getPathName(path.getPath()); |
String pathName = getPathName(path.getPath()); |
142 |
if(pathName.length() > 1) pathName += "/"; |
if(pathName.length() > 1) pathName += "/"; |
143 |
updateDirectoryContent(child, pathName + child.toString()); |
updateDirectoryContent(child, pathName + toEscapedFileName(child)); |
144 |
} |
} |
145 |
} |
} |
146 |
|
|
359 |
|
|
360 |
StringBuffer sb = new StringBuffer(); |
StringBuffer sb = new StringBuffer(); |
361 |
for(int i = 1; i < objs.length; i++) { |
for(int i = 1; i < objs.length; i++) { |
362 |
sb.append('/').append(objs[i].toString()); |
sb.append('/').append(toEscapedFileName(objs[i])); |
363 |
} |
} |
364 |
|
|
365 |
return sb.toString(); |
return sb.toString(); |
410 |
|
|
411 |
public DbDirectoryTreeNode |
public DbDirectoryTreeNode |
412 |
getNodeByPath(String path) { |
getNodeByPath(String path) { |
413 |
String[] dirs = getDirectoryPath(path); |
String[] dirs = getDirectoryList(path); |
414 |
if(dirs == null) return null; |
if(dirs == null) return null; |
415 |
if(dirs.length == 1) return root; |
if(dirs.length == 1) return root; |
416 |
|
if(root == null) return null; |
417 |
DbDirectoryTreeNode node = root; |
DbDirectoryTreeNode node = root; |
418 |
boolean found = false; |
boolean found = false; |
419 |
for(int i = 1; i < dirs.length; i++) { |
for(int i = 1; i < dirs.length; i++) { |
420 |
for(int k = 0; k < node.getChildCount(); k++) { |
for(int k = 0; k < node.getChildCount(); k++) { |
421 |
if(dirs[i].equals(node.getChildAt(k).toString())) { |
String s = toNonEscapedFileName(dirs[i]); |
422 |
|
if(s.equals(node.getChildAt(k).toString())) { |
423 |
node = node.getChildAt(k); |
node = node.getChildAt(k); |
424 |
found = true; |
found = true; |
425 |
break; |
break; |
439 |
return getPathName(getPathToRoot(node)); |
return getPathName(getPathToRoot(node)); |
440 |
} |
} |
441 |
|
|
|
private String[] |
|
|
getDirectoryPath(String path) { |
|
|
if(path == null || path.length() == 0) return null; |
|
|
if(path.charAt(0) != '/') return null; |
|
|
Vector<String> v = new Vector<String>(); |
|
|
v.add("/"); |
|
|
if(path.length() == 1) return v.toArray(new String[v.size()]); |
|
|
|
|
|
if(path.charAt(path.length() - 1) != '/') path += "/"; |
|
|
int i = 1; |
|
|
int j = path.indexOf('/', i); |
|
|
|
|
|
while(j != -1) { |
|
|
v.add(path.substring(i, j)); |
|
|
|
|
|
i = j + 1; |
|
|
if(i >= path.length()) return v.toArray(new String[v.size()]); |
|
|
j = path.indexOf('/', i); |
|
|
} |
|
|
|
|
|
return null; |
|
|
} |
|
|
|
|
|
public static String |
|
|
getParentDirectory(String path) { |
|
|
if(path == null || path.length() == 0) return null; |
|
|
if(path.charAt(0) != '/') return null; |
|
|
if(path.length() == 1) return null; |
|
|
|
|
|
if(path.charAt(path.length() - 1) == '/') { |
|
|
path = path.substring(0, path.length() - 1); |
|
|
} |
|
|
|
|
|
int i = path.lastIndexOf('/'); |
|
|
if(i == 0) return "/"; |
|
|
return path.substring(0, i); |
|
|
} |
|
|
|
|
442 |
/** |
/** |
443 |
* @param l A listener which will be notified when the operation is completed. |
* @param l A listener which will be notified when the operation is completed. |
444 |
*/ |
*/ |
445 |
public void |
public void |
446 |
loadPath(String path, final ActionListener l) { |
loadPath(String path, final ActionListener l) { |
447 |
// TODO: This method is lazily implemented. Should be optimized. |
// TODO: This method is lazily implemented. Should be optimized. |
448 |
final String[] dirs = getDirectoryPath(path); |
final String[] dirs = getDirectoryList(path); |
449 |
if(dirs == null) { |
if(dirs == null) { |
450 |
l.actionPerformed(null); |
l.actionPerformed(null); |
451 |
return; |
return; |
493 |
directoryCountChanged(final InstrumentsDbEvent e) { |
directoryCountChanged(final InstrumentsDbEvent e) { |
494 |
SwingUtilities.invokeLater(new Runnable() { |
SwingUtilities.invokeLater(new Runnable() { |
495 |
public void |
public void |
496 |
run() { |
run() { updateDirectoryCount(e); } |
|
DbDirectoryTreeNode node = getNodeByPath(e.getPathName()); |
|
|
if(node == null) { |
|
|
return; |
|
|
} |
|
|
if(!node.isConnected()) { |
|
|
return; |
|
|
} |
|
|
|
|
|
updateDirectoryContent(node, e.getPathName()); |
|
|
} |
|
497 |
}); |
}); |
498 |
} |
} |
499 |
|
|
500 |
|
private void |
501 |
|
updateDirectoryCount(InstrumentsDbEvent e) { |
502 |
|
if(e.getPathName().equals("/") && root == null) { |
503 |
|
/* |
504 |
|
* If the directory tree failed to load due to a database |
505 |
|
* corruption it may work now if the db has been formatted |
506 |
|
*/ |
507 |
|
initDb(null); |
508 |
|
} |
509 |
|
|
510 |
|
DbDirectoryTreeNode node = getNodeByPath(e.getPathName()); |
511 |
|
if(node == null) return; |
512 |
|
if(!node.isConnected()) return; |
513 |
|
|
514 |
|
updateDirectoryContent(node, e.getPathName()); |
515 |
|
} |
516 |
|
|
517 |
/** |
/** |
518 |
* Invoked when the settings of an instrument directory are changed. |
* Invoked when the settings of an instrument directory are changed. |
519 |
*/ |
*/ |
565 |
// If the directory is renamed by this frontend the |
// If the directory is renamed by this frontend the |
566 |
// directory should already be with the new name |
// directory should already be with the new name |
567 |
String s = getParentDirectory(e.getPathName()); |
String s = getParentDirectory(e.getPathName()); |
568 |
if(s.length() == 1) s += e.getNewName(); |
if(s.length() == 1) s += toEscapedFileName(e.getNewName()); |
569 |
else s += "/" + e.getNewName(); |
else s += "/" + toEscapedFileName(e.getNewName()); |
570 |
node = getNodeByPath(s); |
node = getNodeByPath(s); |
571 |
} |
} |
572 |
if(node == null || node.getParent() == null) { |
if(node == null || node.getParent() == null) { |
574 |
return; |
return; |
575 |
} |
} |
576 |
|
|
577 |
node.getInfo().setName(e.getNewName()); |
node.setName(e.getNewName()); |
578 |
DbDirectoryTreeNode parent = node.getParent(); |
DbDirectoryTreeNode parent = node.getParent(); |
579 |
|
|
580 |
int i = parent.getIndex(node); |
int i = parent.getIndex(node); |
656 |
DbDirectoryTreeNode node = getNodeByPath(dir); |
DbDirectoryTreeNode node = getNodeByPath(dir); |
657 |
if(node == null) return; |
if(node == null) return; |
658 |
|
|
659 |
String instr = e.getPathName(); |
String instr = getFileName(e.getPathName()); |
660 |
int i = instr.lastIndexOf('/'); |
if(instr == null) return; |
|
if(i != -1 && i < instr.length() - 1) instr = instr.substring(i + 1); |
|
|
else return; |
|
661 |
|
|
662 |
DbInstrumentInfo info = node.getInstrument(instr); |
DbInstrumentInfo info = node.getInstrument(toNonEscapedFileName(instr)); |
663 |
|
|
664 |
if(info == null) { |
if(info == null) { |
665 |
// If the instrument is renamed by this frontend the |
// If the instrument is renamed by this frontend the |