Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    TreeGrid Selection: filter/reduce the selection

    Hello every body,

    i'm using TreeGrid with setSelectionType(SelectionStyle.MULTIPLE);
    i get selected nodes with ListGridRecord[] selection = grid.getSelection();

    now i would like to filter/reduce the selection array like following:
    if a parent and one of his children is selected, this children should be removed from selection.

    consider the example of processing a deletion:
    a folder node "/folder1/" and one of his children "/folder1/file.xxx" is selected

    if a try to delete the selected nodes (ie from a file system), first i delete the folder "/folder1/", the deletion of his children is actually not necessary, any way the error occures, because the file "/folder1/file.xxx" is already deleted...

    i hope, the idea/problem is clear, so how can i filter/reduce the selection to match my criteria?

    cheers
    andre

    #2
    this issue does not seem to be a very popular one...

    anyway this is how i solve it manually, looks like its working
    Code:
        public ListGridRecord[] getReducedSelection() {
            Tree tree = treeGrid.getTree();
            ListGridRecord[] selection = treeGrid.getSelection();
            Set<ListGridRecord> folders = new HashSet<ListGridRecord>();
            // collect all selected folders first:
            for (ListGridRecord lgr : selection) {
                if (lgr.getAttributeAsBoolean("isFolder")) {
                    folders.add(lgr);
                }
            }
            // build the result set
            Set<ListGridRecord> result = new HashSet<ListGridRecord>();
            for (ListGridRecord lgr : selection) {
                TreeNode[] parents = tree.getParents((TreeNode) lgr);
                boolean skip = false;
                for (TreeNode p : parents) {
                    if (folders.contains(p)) {
                        skip = true;
                        break;
                    }
                }
                if (!skip) {
                    result.add(lgr);
                }
            }
            return result.toArray(new ListGridRecord[result.size()]);
        }
    if anybody can do the trick more elegant, feel free to post another solution

    cheers
    andre

    Comment

    Working...
    X