Changeset 6826


Ignore:
Timestamp:
2004-02-18T10:24:52+13:00 (20 years ago)
Author:
mdewsnip
Message:

Rewrote the refresh function, not necessarily because there was anything wrong with it, but just so I understand it now. It's a fair bit shorter and simpler (well to me, anyway!) now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/file/FileSystemModel.java

    r6656 r6826  
    1818    implements TreeExpansionListener, TreeWillExpandListener {
    1919
     20    private int counter = 0;
    2021    private DragTree tree;
    2122    private FileFilter current_filter;
     
    118119    }
    119120
     121
     122    public void refresh(TreePath path)
     123    {
     124    // If no path is set, take the path to the root node (ie. update the whole tree)
     125    if (path == null) {
     126        // System.err.println("\nFileSystemModel.refresh(entire tree).");
     127        path = new TreePath(((FileNode) root).getPath());
     128    }
     129    // else {
     130    //     System.err.println("\nFileSystemModel.refresh(" + path + ").");
     131    // }
     132
     133    // Can only refresh if the model is currently being displayed in a tree
     134    if (tree == null) {
     135        return;
     136    }
     137
     138    // Record all the expanded paths under this node
     139    Enumeration old_expanded_paths_enumeration = tree.getExpandedDescendants(path);
     140    if (old_expanded_paths_enumeration == null) {
     141        return;
     142    }
     143
     144    // Map and unmap the node to refresh its contents
     145    FileNode node = (FileNode) path.getLastPathComponent();
     146    node.unmap();
     147    node.map();
     148
     149    // Sort the old expanded paths by length, smallest first
     150    ArrayList old_expanded_paths_list = Collections.list(old_expanded_paths_enumeration);
     151    Collections.sort(old_expanded_paths_list, new TreePathComparator());
     152
     153    // Restore each of the expanded paths
     154    for (int i = 0; i < old_expanded_paths_list.size(); i++) {
     155        TreePath old_expanded_path = (TreePath) old_expanded_paths_list.get(i);
     156        // System.err.println("Expanded path: " + old_expanded_path);
     157
     158        // Build up the new path in the tree
     159        TreePath current_path = new TreePath(path.getPath());
     160        FileNode current_node = node;
     161
     162        // Traverse the tree to find the node to expand (or find it no longer exists)
     163        while (!current_path.toString().equals(old_expanded_path.toString())) {
     164        // System.err.println("Current path: " + current_path);
     165
     166        FileNode old_expanded_node =
     167            (FileNode) old_expanded_path.getPathComponent(current_path.getPathCount());
     168        // System.err.println("Looking for: " + old_expanded_node);
     169
     170        // Find the child node that matches the next element in the path
     171        boolean found = false;
     172        for (int j = 0; j < current_node.getChildCount(); j++) {
     173            FileNode child_node = (FileNode) current_node.getChildAt(j);
     174            // System.err.println("Child node: " + child_node);
     175            if (child_node.equals(old_expanded_node)) {
     176            // System.err.println("Found!");
     177            current_path = current_path.pathByAddingChild(child_node);
     178            current_node = child_node;
     179            found = true;
     180            break;
     181            }
     182        }
     183
     184        // The node was not found, so we cannot expand this path
     185        if (!found) {
     186            // System.err.println("Not found...");
     187            break;
     188        }
     189        }
     190
     191        // If we have built up the correct path, expand it
     192        if (current_path.toString().equals(old_expanded_path.toString())) {
     193        tree.expandPath(current_path);
     194        }
     195    }
     196    }
     197
     198
     199    private class TreePathComparator
     200    implements Comparator {
     201
     202    public int compare(Object o1, Object o2)
     203    {
     204        return (((TreePath) o1).getPathCount() - ((TreePath) o2).getPathCount());
     205    }
     206    }
     207
     208
    120209    /** Used to refresh the contents of the FileNode indicated by the tree path. While the refresh itself is a piece of the proverbial, the restoring of expanded folders afterwards is a nightmare, mainly because all of the tree paths are no longer valid (remember that refresh involves throwing away the node currents children and remapping them).
    121210     * @param path The TreePath to the node to be refreshed.
    122211     */
    123     public void refresh(TreePath path) {
    124     // If no path is set, take the path to the root node (ie update the whole tree)
    125     if(path == null) {
    126         ///ystem.err.println("Refresh entire tree.");
    127         path = new TreePath(((FileNode)root).getPath());
    128     }
    129     //else {
    130     //    ///ystem.err.println("Refresh: " + path.getLastPathComponent());
    131     //}
    132     // Only a valid action if this model is currently being displayed in a tree.
    133     if(tree != null) {
    134         // Retrieve the error node.
    135         FileNode node = (FileNode) path.getLastPathComponent();
    136         // If this error node is a dummy node (ie has no associated file) we can't unmap it, so we iterate through its children refreshing each in turn. The exception being Greenstone Collections, as it is a dummy node but we can map/unmap it
    137         if(node.getFile() == null && !node.toString().equals(Dictionary.get("Tree.World"))) {
    138         for(int i = 0; i < node.getChildCount(); i++) {
    139             FileNode child = (FileNode) node.getChildAt(i);
    140             refresh(new TreePath(child.getPath()));
    141             child = null;
    142         }
    143         }
    144         // Otherwise we refresh this node.
    145         else {
    146         // Record all of the expanded paths under this node. How come getExpandedDescendants returns more results each time.
    147         Enumeration old_tree_paths = tree.getExpandedDescendants(path);
    148         // Refresh the tree structure.
    149         node.unmap();
    150         node.map();
    151         // Fire the appropriate event.
    152         nodeStructureChanged(node);
    153         // Then (painfully) restore the expanded paths. Note that the paths stored in the enumeration no longer exist, so I have to restore by node matching.
    154         //counter = 0;
    155         ///ystem.err.println("After:");
    156         while(old_tree_paths != null && old_tree_paths.hasMoreElements()) {
    157             //counter++;
    158             FileNode current_node = node;
    159             TreePath old_tree_path = (TreePath) old_tree_paths.nextElement();
    160             ///ystem.err.println(counter + ". " + old_tree_path);
    161             TreePath new_tree_path = new TreePath(path.getPath()); // Why isn't there a treepath copy constructor!
    162             boolean not_found = false;
    163             while(!not_found && old_tree_path.getPathCount() > new_tree_path.getPathCount()) {
    164             // Retrieve the new node in the old tree path
    165             FileNode old_path_node = (FileNode) old_tree_path.getPathComponent(new_tree_path.getPathCount());
    166             // Ensure the current node is mapped
    167             current_node.map();
    168             // Now attempt to match it to a child of the new paths last node.
    169             boolean found = false;
    170             for(int i = 0; !found && i < current_node.getChildCount(); i++) {
    171                 FileNode target = (FileNode) current_node.getChildAt(i);
    172                 ///ystem.err.println("Comparing " + old_path_node.toString() + " with " + target);
    173                 if(target.toString().equals(old_path_node.toString())) {
    174                 current_node = target;
    175                 found = true;
    176                 }
    177                 target = null;
    178             }
    179             old_path_node = null;
    180             // If we found a match, we add that to the new Tree Path and continue.
    181             if(found) {
    182                 ///ystem.err.println("Found a match!");
    183                 new_tree_path = new_tree_path.pathByAddingChild(current_node);
    184             }
    185             // We also have to record if we were unable to find a match in the current nodes children.
    186             else {
    187                 ///ystem.err.println("Node not found.");
    188                 not_found = true;
    189             }
    190             }
    191             old_tree_path = null;
    192             // If we've got this far, and haven't hit a not found, then we can assume we have the appropriate path, and ask the program to expand the new tree path
    193             if(!not_found) {
    194             tree.expandPath(new_tree_path);
    195             ///ystem.err.println(" -> Expanded " + new_tree_path);
    196             }
    197             else {
    198             ///ystem.err.println(" -> Cannot Expand " + new_tree_path);
    199             }
    200             new_tree_path = null;
    201             current_node = null;
    202         }
    203         node = null;
    204         }
    205     }
    206     else {
    207         ///ystem.err.println("No Tree!");
    208     }
    209     }
    210 
    211     public void setFilter(String pattern) {
    212     if(pattern != null) {
    213         current_filter = new FileFilter(pattern, false);
    214     }
    215     else {
    216         current_filter = null;
    217     }
    218     filters = null;
    219     }
     212//      public void oldRefresh(TreePath path) {
     213//      // If no path is set, take the path to the root node (ie update the whole tree)
     214//      if(path == null) {
     215//          System.err.println("Refresh entire tree.");
     216//          path = new TreePath(((FileNode)root).getPath());
     217//      }
     218//      else {
     219//          System.err.println("FileSystemModel.refresh: " + path.getLastPathComponent());
     220//      }
     221//      // Only a valid action if this model is currently being displayed in a tree.
     222//      if(tree != null) {
     223//          System.err.println("Refreshing tree " + tree);
     224//          // Retrieve the error node.
     225//          FileNode node = (FileNode) path.getLastPathComponent();
     226//          // If this error node is a dummy node (ie has no associated file) we can't unmap it, so we iterate through its children refreshing each in turn. The exception being Greenstone Collections, as it is a dummy node but we can map/unmap it
     227//          if(node.getFile() == null && !node.toString().equals(Dictionary.get("Tree.World"))) {
     228//          for(int i = 0; i < node.getChildCount(); i++) {
     229//              FileNode child = (FileNode) node.getChildAt(i);
     230//              refresh(new TreePath(child.getPath()));
     231//              child = null;
     232//          }
     233//          }
     234//          // Otherwise we refresh this node.
     235//          else {
     236//          // Record all of the expanded paths under this node. How come getExpandedDescendants returns more results each time.
     237//          Enumeration old_tree_paths = tree.getExpandedDescendants(path);
     238//          // Refresh the tree structure.
     239//          node.unmap();
     240//          node.map();
     241//          // Fire the appropriate event.
     242//          nodeStructureChanged(node);
     243//          // Then (painfully) restore the expanded paths. Note that the paths stored in the enumeration no longer exist, so I have to restore by node matching.
     244//          //counter = 0;
     245//          ///ystem.err.println("After:");
     246//          while(old_tree_paths != null && old_tree_paths.hasMoreElements()) {
     247//              //counter++;
     248//              FileNode current_node = node;
     249//              TreePath old_tree_path = (TreePath) old_tree_paths.nextElement();
     250//              ///ystem.err.println(counter + ". " + old_tree_path);
     251//              TreePath new_tree_path = new TreePath(path.getPath()); // Why isn't there a treepath copy constructor!
     252//              boolean not_found = false;
     253//              while(!not_found && old_tree_path.getPathCount() > new_tree_path.getPathCount()) {
     254//              // Retrieve the new node in the old tree path
     255//              FileNode old_path_node = (FileNode) old_tree_path.getPathComponent(new_tree_path.getPathCount());
     256//              // Ensure the current node is mapped
     257//              current_node.map();
     258//              // Now attempt to match it to a child of the new paths last node.
     259//              boolean found = false;
     260//              for(int i = 0; !found && i < current_node.getChildCount(); i++) {
     261//                  FileNode target = (FileNode) current_node.getChildAt(i);
     262//                  ///ystem.err.println("Comparing " + old_path_node.toString() + " with " + target);
     263//                  if(target.toString().equals(old_path_node.toString())) {
     264//                  current_node = target;
     265//                  found = true;
     266//                  }
     267//                  target = null;
     268//              }
     269//              old_path_node = null;
     270//              // If we found a match, we add that to the new Tree Path and continue.
     271//              if(found) {
     272//                  ///ystem.err.println("Found a match!");
     273//                  new_tree_path = new_tree_path.pathByAddingChild(current_node);
     274//              }
     275//              // We also have to record if we were unable to find a match in the current nodes children.
     276//              else {
     277//                  ///ystem.err.println("Node not found.");
     278//                  not_found = true;
     279//              }
     280//              }
     281//              old_tree_path = null;
     282//              // If we've got this far, and haven't hit a not found, then we can assume we have the appropriate path, and ask the program to expand the new tree path
     283//              if(!not_found) {
     284//              tree.expandPath(new_tree_path);
     285//              ///ystem.err.println(" -> Expanded " + new_tree_path);
     286//              }
     287//              else {
     288//              ///ystem.err.println(" -> Cannot Expand " + new_tree_path);
     289//              }
     290//              new_tree_path = null;
     291//              current_node = null;
     292//          }
     293//          node = null;
     294//          }
     295//      }
     296//      else {
     297//          ///ystem.err.println("No Tree!");
     298//      }
     299//      }
     300
     301      public void setFilter(String pattern) {
     302    if(pattern != null) {
     303        current_filter = new FileFilter(pattern, false);
     304    }
     305    else {
     306        current_filter = null;
     307    }
     308    filters = null;
     309      }
    220310
    221311    /* private void setPermanentFilter(String pattern) {
Note: See TracChangeset for help on using the changeset viewer.