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.resource.JFaceResources;
|
12
|
import org.eclipse.jface.viewers.AbstractTreeViewer;
|
13
|
import org.eclipse.jface.viewers.CheckboxTreeViewer;
|
14
|
import org.eclipse.jface.viewers.IBaseLabelProvider;
|
15
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
16
|
import org.eclipse.jface.viewers.ITreeSelection;
|
17
|
import org.eclipse.swt.SWT;
|
18
|
import org.eclipse.swt.events.SelectionEvent;
|
19
|
import org.eclipse.swt.events.SelectionListener;
|
20
|
import org.eclipse.swt.graphics.Font;
|
21
|
import org.eclipse.swt.layout.GridData;
|
22
|
import org.eclipse.swt.layout.GridLayout;
|
23
|
import org.eclipse.swt.widgets.Button;
|
24
|
import org.eclipse.swt.widgets.Composite;
|
25
|
import org.eclipse.swt.widgets.Label;
|
26
|
import org.eclipse.swt.widgets.Tree;
|
27
|
import org.eclipse.swt.widgets.TreeItem;
|
28
|
import org.eclipse.swt.widgets.Widget;
|
29
|
|
30
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
31
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
32
|
|
33
|
/**
|
34
|
* @author pplitzner
|
35
|
* @since Oct 26, 2018
|
36
|
*
|
37
|
*/
|
38
|
public class CheckBoxTreeComposite extends Composite implements SelectionListener {
|
39
|
|
40
|
private static final Font FONT_DEFAULT = JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
|
41
|
private static final Font FONT_ITALIC = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
|
42
|
private static final Font FONT_BOLD = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
|
43
|
private CheckboxTreeViewer viewer;
|
44
|
private Button btnCollapse;
|
45
|
private Button btnExpand;
|
46
|
private Button btnSelectAllChildren;
|
47
|
|
48
|
public CheckBoxTreeComposite(Composite parent, ITreeContentProvider contentProvider, IBaseLabelProvider labelProvider, int style) {
|
49
|
super(parent, style);
|
50
|
GridLayout layout = new GridLayout();
|
51
|
parent.setLayout(layout);
|
52
|
Composite composite = new Composite(parent, SWT.NULL);
|
53
|
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
54
|
|
55
|
GridLayout gl_composite = layout;
|
56
|
gl_composite.marginHeight = 0;
|
57
|
composite.setLayout(gl_composite);
|
58
|
|
59
|
Composite toolbar = new Composite(composite, SWT.NONE);
|
60
|
toolbar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
|
61
|
GridLayout gl_toolbar = new GridLayout(4, false);
|
62
|
gl_toolbar.marginHeight = 0;
|
63
|
gl_toolbar.verticalSpacing = 0;
|
64
|
toolbar.setLayout(gl_toolbar);
|
65
|
|
66
|
btnCollapse = new Button(toolbar, SWT.NONE);
|
67
|
btnCollapse.addSelectionListener(this);
|
68
|
btnCollapse.setImage(ImageResources.getImage(ImageResources.COLLAPSE_ALL));
|
69
|
btnCollapse.setToolTipText(Messages.CheckBoxTreeComposite_COLLAPSE_ALL);
|
70
|
|
71
|
btnExpand = new Button(toolbar, SWT.NONE);
|
72
|
btnExpand.addSelectionListener(this);
|
73
|
btnExpand.setImage(ImageResources.getImage(ImageResources.EXPAND));
|
74
|
btnExpand.setToolTipText(Messages.CheckBoxTreeComposite_EXPAND);
|
75
|
|
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);
|
81
|
|
82
|
Tree tree = new Tree(composite, SWT.BORDER | SWT.CHECK);
|
83
|
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
84
|
tree.addListener(SWT.Selection, event -> {
|
85
|
if (event.detail == SWT.CHECK) {
|
86
|
TreeItem item = (TreeItem) event.item;
|
87
|
updateItem(item);
|
88
|
}
|
89
|
});
|
90
|
viewer = new CheckboxTreeViewer(tree);
|
91
|
viewer.setContentProvider(contentProvider);
|
92
|
viewer.setLabelProvider(labelProvider);
|
93
|
}
|
94
|
|
95
|
|
96
|
private void updateItem(TreeItem treeItem){
|
97
|
TreeItem parentItem = treeItem.getParentItem();
|
98
|
if(parentItem!=null){
|
99
|
updateItem(parentItem);
|
100
|
}
|
101
|
else{
|
102
|
updateChildItems(treeItem);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
private void updateChildItems(TreeItem treeItem){
|
107
|
treeItem.setFont(FONT_DEFAULT);
|
108
|
TreeItem[] children = treeItem.getItems();
|
109
|
if(children.length==0){
|
110
|
return;
|
111
|
}
|
112
|
int checkedCount = 0;
|
113
|
for (TreeItem child : children) {
|
114
|
updateChildItems(child);
|
115
|
if(child.getChecked()){
|
116
|
if ((child.getFont().equals(FONT_BOLD)) || (child.getItems().length == 0)) {
|
117
|
checkedCount++;
|
118
|
}
|
119
|
//some children are checked
|
120
|
treeItem.setFont(FONT_ITALIC);
|
121
|
}
|
122
|
else if(child.getFont().equals(FONT_ITALIC)
|
123
|
|| child.getFont().equals(FONT_BOLD)){
|
124
|
//some children are checked
|
125
|
treeItem.setFont(FONT_ITALIC);
|
126
|
}
|
127
|
}
|
128
|
if(checkedCount==children.length){
|
129
|
//all children are checked
|
130
|
treeItem.setFont(FONT_BOLD);
|
131
|
}
|
132
|
else if(!treeItem.getFont().equals(FONT_ITALIC)){
|
133
|
if(checkedCount>0){
|
134
|
//some children are checked
|
135
|
treeItem.setFont(FONT_ITALIC);
|
136
|
}
|
137
|
else{
|
138
|
//no children are checked
|
139
|
treeItem.setFont(FONT_DEFAULT);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
|
145
|
public CheckboxTreeViewer getViewer() {
|
146
|
return viewer;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Checks the tree items corresponding to the given elements.<br>
|
151
|
* @param elements
|
152
|
*/
|
153
|
public void setCheckedElements(Object[] elements) {
|
154
|
viewer.setCheckedElements(elements);
|
155
|
for (Object element: elements) {
|
156
|
viewer.reveal(element);
|
157
|
}
|
158
|
TreeItem[] rootItems = viewer.getTree().getItems();
|
159
|
for (TreeItem rootItem : rootItems) {
|
160
|
updateItem(rootItem);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
@Override
|
165
|
public void widgetSelected(SelectionEvent e) {
|
166
|
ITreeSelection structuredSelection = viewer.getStructuredSelection();
|
167
|
Object firstElement = structuredSelection.getFirstElement();
|
168
|
if(e.widget == btnCollapse){
|
169
|
viewer.collapseAll();
|
170
|
}
|
171
|
else if(e.widget == btnExpand){
|
172
|
viewer.expandToLevel(firstElement, AbstractTreeViewer.ALL_LEVELS);
|
173
|
}
|
174
|
else if(e.widget == btnSelectAllChildren){
|
175
|
Widget item = viewer.testFindItem(firstElement);
|
176
|
boolean checked = false;
|
177
|
Object[] checkedElements = viewer.getCheckedElements();
|
178
|
for (Object checkedElement : checkedElements) {
|
179
|
if(checkedElement.equals(firstElement)){
|
180
|
checked = true;
|
181
|
break;
|
182
|
}
|
183
|
}
|
184
|
if(item!=null){
|
185
|
viewer.expandToLevel(firstElement, AbstractTreeViewer.ALL_LEVELS);
|
186
|
viewer.setSubtreeChecked(firstElement, !checked);
|
187
|
TreeItem[] rootItems = viewer.getTree().getItems();
|
188
|
for (TreeItem rootItem : rootItems) {
|
189
|
updateItem(rootItem);
|
190
|
}
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
public void widgetDefaultSelected(SelectionEvent e) {
|
197
|
}
|
198
|
|
199
|
private enum CheckState{
|
200
|
CHECKED,
|
201
|
GRAYED,
|
202
|
NONE
|
203
|
}
|
204
|
|
205
|
}
|