Project

General

Profile

Download (3.41 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2018 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.taxeditor.preference.wizard;
10

    
11
import org.eclipse.jface.viewers.AbstractTreeViewer;
12
import org.eclipse.jface.viewers.CheckboxTreeViewer;
13
import org.eclipse.jface.viewers.ILabelProvider;
14
import org.eclipse.jface.viewers.ITreeContentProvider;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.layout.GridData;
17
import org.eclipse.swt.layout.GridLayout;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Tree;
20
import org.eclipse.swt.widgets.TreeItem;
21

    
22
/**
23
 * @author pplitzner
24
 * @since Oct 26, 2018
25
 *
26
 */
27
public class CheckBoxTreeComposite extends Composite {
28

    
29
    private CheckboxTreeViewer viewer;
30

    
31
    public CheckBoxTreeComposite(Composite parent, ITreeContentProvider contentProvider, ILabelProvider labelProvider, int style) {
32
        super(parent, style);
33
        Composite composite = new Composite(parent, SWT.NULL);
34
        composite.setLayout(new GridLayout());
35
        Tree tree = new Tree(parent, SWT.BORDER | SWT.CHECK);
36
        tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
37
        tree.addListener(SWT.Selection, event -> {
38
            if (event.detail == SWT.CHECK) {
39
                TreeItem item = (TreeItem) event.item;
40
                Object data = item.getData();
41
                boolean checked = item.getChecked();
42
                if(checked){
43
                    viewer.expandToLevel(data, AbstractTreeViewer.ALL_LEVELS);
44
                }
45
                checkItems(item, checked);
46
                checkPath(item.getParentItem(), checked, false);
47
            }
48
        });
49
        viewer = new CheckboxTreeViewer(tree);
50
        viewer.setContentProvider(contentProvider);
51
        viewer.setLabelProvider(labelProvider);
52
    }
53

    
54
    private void checkPath(TreeItem item, boolean checked, boolean grayed) {
55
        if (item == null) {
56
            return;
57
        }
58
        if (grayed) {
59
            checked = true;
60
        } else {
61
            int index = 0;
62
            TreeItem[] items = item.getItems();
63
            while (index < items.length) {
64
                TreeItem child = items[index];
65
                if (child.getGrayed() || checked != child.getChecked()) {
66
                    checked = grayed = true;
67
                    break;
68
                }
69
                index++;
70
            }
71
        }
72
        item.setChecked(checked);
73
        item.setGrayed(grayed);
74
        checkPath(item.getParentItem(), checked, grayed);
75
    }
76

    
77
    private void checkItems(TreeItem item, boolean checked) {
78
        item.setGrayed(false);
79
        item.setChecked(checked);
80
        TreeItem[] items = item.getItems();
81
        for (int i = 0; i < items.length; i++) {
82
            checkItems(items[i], checked);
83
        }
84
    }
85

    
86
    public CheckboxTreeViewer getViewer() {
87
        return viewer;
88
    }
89

    
90
    /**
91
     * Checks the tree items corresponding to the given elements.<br>
92
     * <br>
93
     * <b>Note:</b> In order to fully reveal the checked elements they
94
     * have to be sorted according to the tree hierarchy.
95
     * @param elements
96
     */
97
    public void setCheckedElements(Object[] elements) {
98
        for (Object object : elements) {
99
            viewer.reveal(object);
100
            viewer.setChecked(object, true);
101
        }
102
    }
103

    
104
}
(10-10/14)