Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Simon Scholz - Bug 460380
* Alexander Fedorov - Bug 548314
*******************************************************************************/
package org.eclipse.jface.viewers;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
/**
* A concrete tree-structured viewer based on an SWT Tree
* control with checkboxes on each node.
*
This class supports setting an {@link ICheckStateProvider} to
* set the checkbox states. To see standard SWT behavior, view
* SWT Snippet274.
*
* This class is not intended to be subclassed outside the viewer framework.
* It is designed to be instantiated with a pre-existing SWT tree control and configured
* with a domain-specific content provider, label provider, element filter (optional),
* and element sorter (optional).
*
* @noextend This class is not intended to be subclassed by clients.
*/
public class CheckboxTreeViewer extends TreeViewer implements ICheckable {
/**
* List of check state listeners (element type: ICheckStateListener).
*/
private ListenerList checkStateListeners = new ListenerList<>();
/**
* Provides the desired state of the check boxes.
*/
private ICheckStateProvider checkStateProvider;
/**
* Last item clicked on, or null if none.
*/
private TreeItem lastClickedItem = null;
/**
* Creates a tree viewer on a newly-created tree control under the given parent.
* The tree control is created using the SWT style bits: CHECK and BORDER.
* The viewer has no input, no content provider, a default label provider,
* no sorter, and no filters.
*
* @param parent the parent control
*/
public CheckboxTreeViewer(Composite parent) {
this(parent, SWT.BORDER);
}
/**
* Creates a tree viewer on a newly-created tree control under the given parent.
* The tree control is created using the given SWT style bits, plus the CHECK style bit.
* The viewer has no input, no content provider, a default label provider,
* no sorter, and no filters.
*
* @param parent the parent control
* @param style the SWT style bits
*/
public CheckboxTreeViewer(Composite parent, int style) {
this(new Tree(parent, SWT.CHECK | style));
}
/**
* Creates a tree viewer on the given tree control.
* The SWT.CHECK style bit must be set on the given tree control.
* The viewer has no input, no content provider, a default label provider,
* no sorter, and no filters.
*
* @param tree the tree control
*/
public CheckboxTreeViewer(Tree tree) {
super(tree);
}
@Override
public void addCheckStateListener(ICheckStateListener listener) {
checkStateListeners.add(listener);
}
/**
* Sets the {@link ICheckStateProvider} for this {@link CheckboxTreeViewer}.
* The check state provider will supply the logic for deciding whether the
* check box associated with each item should be checked, grayed or
* unchecked.
* @param checkStateProvider The provider.
* @since 3.5
*/
public void setCheckStateProvider(ICheckStateProvider checkStateProvider) {
this.checkStateProvider = checkStateProvider;
refresh();
}
/*
* Extends this method to update check box states.
*/
@Override
protected void doUpdateItem(Item item, Object element) {
super.doUpdateItem(item, element);
if(!item.isDisposed() && checkStateProvider != null) {
setChecked(element, checkStateProvider.isChecked(element));
setGrayed(element, checkStateProvider.isGrayed(element));
}
}
/**
* Applies the checked and grayed states of the given widget and its
* descendents.
*
* @param checked a set of elements (element type: Object)
* @param grayed a set of elements (element type: Object)
* @param widget the widget
*/
private void applyState(CustomHashtable checked, CustomHashtable grayed,
Widget widget) {
Item[] items = getChildren(widget);
for (Item item : items) {
if (item instanceof TreeItem) {
Object data = item.getData();
if (data != null) {
TreeItem ti = (TreeItem) item;
ti.setChecked(checked.containsKey(data));
ti.setGrayed(grayed.containsKey(data));
}
}
applyState(checked, grayed, item);
}
}
/**
* Notifies any check state listeners that the check state of an element has changed.
* Only listeners registered at the time this method is called are notified.
*
* @param event a check state changed event
*
* @see ICheckStateListener#checkStateChanged
*/
protected void fireCheckStateChanged(final CheckStateChangedEvent event) {
for (ICheckStateListener l : checkStateListeners) {
SafeRunnable.run(new SafeRunnable() {
@Override
public void run() {
l.checkStateChanged(event);
}
});
}
}
/**
* Gathers the checked and grayed states of the given widget and its
* descendents.
*
* @param checked a writable set of elements (element type: Object)
* @param grayed a writable set of elements (element type: Object)
* @param widget the widget
*/
private void gatherState(CustomHashtable checked, CustomHashtable grayed,
Widget widget) {
Item[] items = getChildren(widget);
for (Item item : items) {
if (item instanceof TreeItem) {
Object data = item.getData();
if (data != null) {
TreeItem ti = (TreeItem) item;
if (ti.getChecked()) {
checked.put(data, data);
}
if (ti.getGrayed()) {
grayed.put(data, data);
}
}
}
gatherState(checked, grayed, item);
}
}
@Override
public boolean getChecked(Object element) {
Widget widget = findItem(element);
if (widget instanceof TreeItem) {
return ((TreeItem) widget).getChecked();
}
return false;
}
/**
* Returns a list of checked elements in this viewer's tree,
* including currently hidden ones that are marked as
* checked but are under a collapsed ancestor.
*
* This method is typically used when preserving the interesting
* state of a viewer; setCheckedElements is used during the restore.
*
*
* @return the array of checked elements
*
* @see #setCheckedElements
*/
public Object[] getCheckedElements() {
List