commit 2204e24e6ad39c993196acb2d8a3637fe5cc5e7c
Author: Patrick Plitzner <p.plitzner@bgbm.org>
Date:   Mon Jan 28 17:11:49 2019 +0100

    ref #7912 Add "Select direct children" button

diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/wizard/CheckBoxTreeComposite.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/wizard/CheckBoxTreeComposite.java
index 8ae178151..2616ad643 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/wizard/CheckBoxTreeComposite.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/wizard/CheckBoxTreeComposite.java
@@ -8,6 +8,9 @@
  */
 package eu.etaxonomy.taxeditor.preference.wizard;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.viewers.AbstractTreeViewer;
 import org.eclipse.jface.viewers.CheckboxTreeViewer;
@@ -22,7 +25,6 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Tree;
 import org.eclipse.swt.widgets.TreeItem;
 import org.eclipse.swt.widgets.Widget;
@@ -44,6 +46,7 @@ public class CheckBoxTreeComposite extends Composite implements SelectionListene
     private Button btnCollapse;
     private Button btnExpand;
     private Button btnSelectAllChildren;
+    private Button btnSelectDirectChildren;
 
     public CheckBoxTreeComposite(Composite parent, ITreeContentProvider contentProvider, IBaseLabelProvider labelProvider, int style) {
         super(parent, style);
@@ -59,6 +62,7 @@ public class CheckBoxTreeComposite extends Composite implements SelectionListene
         Composite toolbar = new Composite(composite, SWT.NONE);
         toolbar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
         GridLayout gl_toolbar = new GridLayout(4, false);
+        gl_toolbar.horizontalSpacing = 4;
         gl_toolbar.marginHeight = 0;
         gl_toolbar.verticalSpacing = 0;
         toolbar.setLayout(gl_toolbar);
@@ -73,11 +77,15 @@ public class CheckBoxTreeComposite extends Composite implements SelectionListene
         btnExpand.setImage(ImageResources.getImage(ImageResources.EXPAND));
         btnExpand.setToolTipText(Messages.CheckBoxTreeComposite_EXPAND);
 
-        btnSelectAllChildren = new Button(toolbar, SWT.NONE);
-        btnSelectAllChildren.addSelectionListener(this);
-        btnSelectAllChildren.setImage(ImageResources.getImage(ImageResources.HIERARCHICAL));
-        btnSelectAllChildren.setToolTipText(Messages.CheckBoxTreeComposite_TOGGLE_TREE_SELECTION);
-        new Label(toolbar, SWT.NONE);
+        btnSelectDirectChildren = new Button(toolbar, SWT.NONE);
+        btnSelectDirectChildren.addSelectionListener(this);
+        btnSelectDirectChildren.setImage(ImageResources.getImage(ImageResources.FLAT));
+        btnSelectDirectChildren.setToolTipText("Select direct children");
+
+                btnSelectAllChildren = new Button(toolbar, SWT.NONE);
+                btnSelectAllChildren.addSelectionListener(this);
+                btnSelectAllChildren.setImage(ImageResources.getImage(ImageResources.HIERARCHICAL));
+                btnSelectAllChildren.setToolTipText(Messages.CheckBoxTreeComposite_TOGGLE_TREE_SELECTION);
 
         Tree tree = new Tree(composite, SWT.BORDER | SWT.CHECK);
         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
@@ -171,6 +179,19 @@ public class CheckBoxTreeComposite extends Composite implements SelectionListene
         else if(e.widget == btnExpand){
             viewer.expandToLevel(firstElement, AbstractTreeViewer.ALL_LEVELS);
         }
+        else if(e.widget == btnSelectDirectChildren){
+            Widget item = viewer.testFindItem(firstElement);
+            if(item instanceof TreeItem){
+                viewer.expandToLevel(firstElement, 1);
+                TreeItem treeItem = (TreeItem) item;
+                TreeItem[] childItems = treeItem.getItems();
+                List<TreeItem> childItemList = Arrays.asList(childItems);
+                boolean allChecked = childItemList.stream().allMatch(childItem->childItem.getChecked());
+                childItemList.stream().forEach(childItem->childItem.setChecked(!allChecked));
+                TreeItem parent = getRoot(treeItem);
+                updateItem(parent);
+            }
+        }
         else if(e.widget == btnSelectAllChildren){
             Widget item = viewer.testFindItem(firstElement);
             boolean checked = false;
@@ -181,17 +202,28 @@ public class CheckBoxTreeComposite extends Composite implements SelectionListene
                     break;
                 }
             }
-            if(item!=null){
+            if(item instanceof TreeItem){
                 viewer.expandToLevel(firstElement, AbstractTreeViewer.ALL_LEVELS);
                 viewer.setSubtreeChecked(firstElement, !checked);
-                TreeItem[] rootItems = viewer.getTree().getItems();
-                for (TreeItem rootItem : rootItems) {
-                    updateItem(rootItem);
-                }
+                TreeItem parent = getRoot((TreeItem) item);
+                updateItem(parent);
             }
         }
     }
 
+
+    /**
+     * @param treeItem
+     * @return
+     */
+    private TreeItem getRoot(TreeItem treeItem) {
+        TreeItem parent = treeItem;
+        while(parent.getParentItem()!=null){
+            parent = parent.getParentItem();
+        }
+        return parent;
+    }
+
     @Override
     public void widgetDefaultSelected(SelectionEvent e) {
     }
