Changeset 4618


Ignore:
Timestamp:
2003-06-12T17:21:47+12:00 (21 years ago)
Author:
jmt12
Message:

2030120: Optimised and modified selection conditions to allow for mixed selections, i.e. both files and folders.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/DragTreeSelectionModel.java

    r4545 r4618  
    4848    implements MouseListener {
    4949
    50     boolean immediate = false;
     50    private boolean immediate = false;
     51    private boolean mixed_selection = false;
    5152
    5253    private int type               = -1;
     
    5960    static private final int SET   =  1;
    6061
    61     public DragTreeSelectionModel(DragTree tree) {
     62    /** Constructor.
     63     * @param tree the DragTree which will use this selection model.
     64     * @param mixed_selection true if this selection model allows selections to contain files and folders, false otherwise.
     65     */
     66    public DragTreeSelectionModel(DragTree tree, boolean mixed_selection) {
    6267    super();
     68    this.mixed_selection = mixed_selection;
    6369    setSelectionMode(DISCONTIGUOUS_TREE_SELECTION);
    6470    tree.addMouseListener(this);
     
    240246    }
    241247
    242     /** Ensure that the given path is appropriate to add to the current selection, preventing mixed selections of files and folder. We also must check that no path is a ancestor/descendant of another. There is also a slight optimization in that if the current selection contains only files (which if the rules are followed, and the first 'test' node is a file, then it must) there is no point in checking the remaining files in the selection. Its a different story for folders however as we have to ensure no folder is in the lineage of another, even if they are all folders! */
     248    /** Ensure that the given path is appropriate to add to the current selection, preventing mixed selections of files and folder if required. We also must check that no path is a ancestor/descendant of another.
     249     */
    243250    private boolean isAppropriate(TreePath path) {
    244251    boolean appropriate = true;
    245252    TreeNode new_node = (TreeNode) path.getLastPathComponent();
     253    // If there is a current selection
    246254    if(selection != null && selection.length > 0) {
    247         TreeNode test_node = (TreeNode) selection[0].getLastPathComponent();
    248         appropriate = appropriate && (new_node.isLeaf() == test_node.isLeaf());
    249         if(!test_node.isLeaf()) {
    250         appropriate = appropriate && !path.isDescendant(selection[0]) && !selection[0].isDescendant(path);
    251         for(int i = 1; appropriate && selection != null && i < selection.length; i++) {
    252             TreeNode current_node = (TreeNode) selection[i].getLastPathComponent();
    253             appropriate = appropriate && (new_node.isLeaf() == current_node.isLeaf());             
    254             appropriate = appropriate && !path.isDescendant(selection[i]) && !selection[i].isDescendant(path);
    255         }
     255        for(int i = 0; appropriate && i < selection.length; i++) {
     256        TreeNode current_node = (TreeNode) selection[i].getLastPathComponent();
     257        appropriate = appropriate && (mixed_selection || new_node.isLeaf() == current_node.isLeaf());
     258        appropriate = appropriate && !path.isDescendant(selection[i]) && !selection[i].isDescendant(path);     
    256259        }
    257260    }
    258261    return appropriate;
    259262    }
    260     /** Ensure that the given paths are appropriate to add to the current selection, preventing mixed selections of files and folder. We also must check that no path is a ancestor/descendant of another. One last detail to keep in mind is that adding selections depends upon the current selection, whereas set the selection paths doesn't (replaces them instead) and thus no check of the current paths is needed. */
     263    /** Ensure that the given paths are appropriate to add to the current selection, preventing mixed selections of files and folder if required. We also must check that no path is a ancestor/descendant of another. One last detail to keep in mind is that adding selections depends upon the current selection, whereas set the selection paths doesn't (replaces them instead) and thus no check of the current paths is needed. */
    261264    private boolean isAppropriate(TreePath[] paths, boolean check_current) {
    262265    boolean appropriate = true;
    263     if(paths != null) {
    264         if(paths.length >= 2) {
    265         // Prevent folders being added to a previous selection of files and vice-versa
    266         // First check that the new selection are all of the same type
    267         for(int i = 0; appropriate && paths != null && i < paths.length - 1; i++) {
    268             TreeNode one_node = (TreeNode) paths[i].getLastPathComponent();
    269             TreeNode other_node = (TreeNode) paths[i+1].getLastPathComponent();
    270             appropriate = appropriate && (one_node.isLeaf() == other_node.isLeaf());
    271             appropriate = appropriate && !paths[i].isDescendant(paths[i+1]) && !paths[i+1].isDescendant(paths[i]);
    272         }
    273         }
    274         // Now we check the current selection against the first node in our new selection
    275         if(appropriate && check_current) {
    276         appropriate = isAppropriate(paths[0]);
     266    if(check_current && paths != null && paths.length > 0) {
     267        for(int i = 0; appropriate && i < paths.length; i++) {
     268        appropriate = appropriate && isAppropriate(paths[i]);
    277269        }
    278270    }
Note: See TracChangeset for help on using the changeset viewer.