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

Diff of /jsampler/trunk/src/org/jsampler/view/InstrumentsDbTreeModel.java

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

revision 1204 by iliev, Thu May 24 21:43:45 2007 UTC revision 1355 by iliev, Mon Sep 17 23:55:27 2007 UTC
# Line 48  import org.linuxsampler.lscp.DbInstrumen Line 48  import org.linuxsampler.lscp.DbInstrumen
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
# Line 69  public class InstrumentsDbTreeModel impl Line 72  public class InstrumentsDbTreeModel impl
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("/");
# Line 80  public class InstrumentsDbTreeModel impl Line 92  public class InstrumentsDbTreeModel impl
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                          }                          }
# Line 98  public class InstrumentsDbTreeModel impl Line 107  public class InstrumentsDbTreeModel impl
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);
# Line 131  public class InstrumentsDbTreeModel impl Line 140  public class InstrumentsDbTreeModel impl
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                    
# Line 350  public class InstrumentsDbTreeModel impl Line 359  public class InstrumentsDbTreeModel impl
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();
# Line 401  public class InstrumentsDbTreeModel impl Line 410  public class InstrumentsDbTreeModel impl
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;
# Line 429  public class InstrumentsDbTreeModel impl Line 439  public class InstrumentsDbTreeModel impl
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;
# Line 521  public class InstrumentsDbTreeModel impl Line 493  public class InstrumentsDbTreeModel impl
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                   */                   */
# Line 586  public class InstrumentsDbTreeModel impl Line 565  public class InstrumentsDbTreeModel impl
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) {
# Line 595  public class InstrumentsDbTreeModel impl Line 574  public class InstrumentsDbTreeModel impl
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);
# Line 677  public class InstrumentsDbTreeModel impl Line 656  public class InstrumentsDbTreeModel impl
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

Legend:
Removed from v.1204  
changed lines
  Added in v.1355

  ViewVC Help
Powered by ViewVC