Project

General

Profile

Revision 2204e24e

ID2204e24e6ad39c993196acb2d8a3637fe5cc5e7c
Parent 1d035f5d
Child 1415c660

Added by Patrick Plitzner 7 months ago

ref #7912 Add "Select direct children" button

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/wizard/CheckBoxTreeComposite.java
8 8
 */
9 9
package eu.etaxonomy.taxeditor.preference.wizard;
10 10

  
11
import java.util.Arrays;
12
import java.util.List;
13

  
11 14
import org.eclipse.jface.resource.JFaceResources;
12 15
import org.eclipse.jface.viewers.AbstractTreeViewer;
13 16
import org.eclipse.jface.viewers.CheckboxTreeViewer;
......
22 25
import org.eclipse.swt.layout.GridLayout;
23 26
import org.eclipse.swt.widgets.Button;
24 27
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.Label;
26 28
import org.eclipse.swt.widgets.Tree;
27 29
import org.eclipse.swt.widgets.TreeItem;
28 30
import org.eclipse.swt.widgets.Widget;
......
44 46
    private Button btnCollapse;
45 47
    private Button btnExpand;
46 48
    private Button btnSelectAllChildren;
49
    private Button btnSelectDirectChildren;
47 50

  
48 51
    public CheckBoxTreeComposite(Composite parent, ITreeContentProvider contentProvider, IBaseLabelProvider labelProvider, int style) {
49 52
        super(parent, style);
......
59 62
        Composite toolbar = new Composite(composite, SWT.NONE);
60 63
        toolbar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
61 64
        GridLayout gl_toolbar = new GridLayout(4, false);
65
        gl_toolbar.horizontalSpacing = 4;
62 66
        gl_toolbar.marginHeight = 0;
63 67
        gl_toolbar.verticalSpacing = 0;
64 68
        toolbar.setLayout(gl_toolbar);
......
73 77
        btnExpand.setImage(ImageResources.getImage(ImageResources.EXPAND));
74 78
        btnExpand.setToolTipText(Messages.CheckBoxTreeComposite_EXPAND);
75 79

  
76
        btnSelectAllChildren = new Button(toolbar, SWT.NONE);
77
        btnSelectAllChildren.addSelectionListener(this);
78
        btnSelectAllChildren.setImage(ImageResources.getImage(ImageResources.HIERARCHICAL));
79
        btnSelectAllChildren.setToolTipText(Messages.CheckBoxTreeComposite_TOGGLE_TREE_SELECTION);
80
        new Label(toolbar, SWT.NONE);
80
        btnSelectDirectChildren = new Button(toolbar, SWT.NONE);
81
        btnSelectDirectChildren.addSelectionListener(this);
82
        btnSelectDirectChildren.setImage(ImageResources.getImage(ImageResources.FLAT));
83
        btnSelectDirectChildren.setToolTipText("Select direct children");
84

  
85
                btnSelectAllChildren = new Button(toolbar, SWT.NONE);
86
                btnSelectAllChildren.addSelectionListener(this);
87
                btnSelectAllChildren.setImage(ImageResources.getImage(ImageResources.HIERARCHICAL));
88
                btnSelectAllChildren.setToolTipText(Messages.CheckBoxTreeComposite_TOGGLE_TREE_SELECTION);
81 89

  
82 90
        Tree tree = new Tree(composite, SWT.BORDER | SWT.CHECK);
83 91
        tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
......
171 179
        else if(e.widget == btnExpand){
172 180
            viewer.expandToLevel(firstElement, AbstractTreeViewer.ALL_LEVELS);
173 181
        }
182
        else if(e.widget == btnSelectDirectChildren){
183
            Widget item = viewer.testFindItem(firstElement);
184
            if(item instanceof TreeItem){
185
                viewer.expandToLevel(firstElement, 1);
186
                TreeItem treeItem = (TreeItem) item;
187
                TreeItem[] childItems = treeItem.getItems();
188
                List<TreeItem> childItemList = Arrays.asList(childItems);
189
                boolean allChecked = childItemList.stream().allMatch(childItem->childItem.getChecked());
190
                childItemList.stream().forEach(childItem->childItem.setChecked(!allChecked));
191
                TreeItem parent = getRoot(treeItem);
192
                updateItem(parent);
193
            }
194
        }
174 195
        else if(e.widget == btnSelectAllChildren){
175 196
            Widget item = viewer.testFindItem(firstElement);
176 197
            boolean checked = false;
......
181 202
                    break;
182 203
                }
183 204
            }
184
            if(item!=null){
205
            if(item instanceof TreeItem){
185 206
                viewer.expandToLevel(firstElement, AbstractTreeViewer.ALL_LEVELS);
186 207
                viewer.setSubtreeChecked(firstElement, !checked);
187
                TreeItem[] rootItems = viewer.getTree().getItems();
188
                for (TreeItem rootItem : rootItems) {
189
                    updateItem(rootItem);
190
                }
208
                TreeItem parent = getRoot((TreeItem) item);
209
                updateItem(parent);
191 210
            }
192 211
        }
193 212
    }
194 213

  
214

  
215
    /**
216
     * @param treeItem
217
     * @return
218
     */
219
    private TreeItem getRoot(TreeItem treeItem) {
220
        TreeItem parent = treeItem;
221
        while(parent.getParentItem()!=null){
222
            parent = parent.getParentItem();
223
        }
224
        return parent;
225
    }
226

  
195 227
    @Override
196 228
    public void widgetDefaultSelected(SelectionEvent e) {
197 229
    }

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)