de.tototec.utils.jface.viewer.TreeViewerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of de.tototec.utils.jface.viewer Show documentation
Show all versions of de.tototec.utils.jface.viewer Show documentation
Utility classes to work with SWT/JFace Viewer API
The newest version!
package de.tototec.utils.jface.viewer;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerEditor;
import org.eclipse.jface.viewers.TreeViewerFocusCellManager;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.TreeColumn;
/**
* Mutable builder for {@link TreeViewer}s.
*/
public class TreeViewerBuilder {
private BiFunction activationStrategy;
private int features = ColumnViewerEditor.DEFAULT;
private boolean ownerDrawHighlighter;
private List viewerFilters;
private List> columnBuilders;
private Boolean resizable;
private Boolean moveable;
private boolean withTableLayout;
private Function colorProvider;
public TreeViewerBuilder() {
}
public TreeViewerBuilder withTableLayout() {
this.withTableLayout = true;
return this;
}
public TreeViewerBuilder setColumnsResizable(final boolean resizable) {
this.resizable = resizable;
return this;
}
public TreeViewerBuilder setColumnsMoveable(final boolean moveable) {
this.moveable = moveable;
return this;
}
public TreeViewerBuilder setEditorActivationStrategy(
final BiFunction activationStrategy) {
this.activationStrategy = activationStrategy;
return this;
}
/**
* Features flags are:
*
* - {@link ColumnViewerEditor#DEFAULT} - Tabbing from cell to cell is
* turned off
* - {@link ColumnViewerEditor#KEYBOARD_ACTIVATION} - Style mask used to
* enable keyboard activation.
* - {@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW} - Should if the end
* of the row is reach started from the
* beginning in the same row.
* - {@link ColumnViewerEditor#TABBING_HORIZONTAL} - Should tabbing from
* column to column with in one row be
* supported.
* - {@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR} - Should if
* the end of the row is reach started from
* the start/end of the row below/above.
* - {@link ColumnViewerEditor#TABBING_VERTICAL} - Support tabbing to Cell
* above/below the current cell.
*
*
*
* @param featuresFlags
* @return
*/
public TreeViewerBuilder setEditorFeatures(final int featuresFlags) {
this.features = featuresFlags;
return this;
}
public TreeViewerBuilder setCellOwnerDrawHighlighter(final boolean ownerDrawHighlighter) {
this.ownerDrawHighlighter = ownerDrawHighlighter;
return this;
}
public TreeViewerBuilder addFilter(final ViewerFilter viewerFilter) {
if (viewerFilters == null) {
viewerFilters = new LinkedList();
}
this.viewerFilters.add(viewerFilter);
return this;
}
public TreeViewerBuilder addColumn(final ViewerColumnBuilder treeViewerColumnBuilder) {
if (columnBuilders == null) {
columnBuilders = new LinkedList>();
}
columnBuilders.add(treeViewerColumnBuilder);
return this;
}
public ViewerColumnBuilder addColumn() {
final ViewerColumnBuilder colBuilder = new ViewerColumnBuilder<>();
addColumn(colBuilder);
return colBuilder;
}
public TreeViewerBuilder setColorProvider(final Function colorProvider) {
this.colorProvider = colorProvider;
return this;
}
public TreeViewer apply(final TreeViewer treeViewer) {
if (colorProvider == null) {
colorProvider = new ColorProvider(treeViewer.getControl());
}
if (withTableLayout) {
treeViewer.getTree().setLayout(new TableLayout());
}
if (columnBuilders != null) {
for (final ViewerColumnBuilder columnBuilder : columnBuilders) {
columnBuilder.setColorProvider(colorProvider);
columnBuilder.build(treeViewer);
}
}
if (viewerFilters != null) {
treeViewer.setFilters(viewerFilters.toArray(new ViewerFilter[viewerFilters.size()]));
}
final TreeViewerFocusCellManager focusCellMgr;
if (ownerDrawHighlighter) {
focusCellMgr = new TreeViewerFocusCellManager(treeViewer, new FocusCellOwnerDrawHighlighter(treeViewer));
} else {
focusCellMgr = null;
}
final ColumnViewerEditorActivationStrategy actStrategy;
if (activationStrategy != null) {
actStrategy = new ColumnViewerEditorActivationStrategy(treeViewer) {
@Override
protected boolean isEditorActivationEvent(final ColumnViewerEditorActivationEvent event) {
return activationStrategy.apply(treeViewer, event);
}
};
} else {
actStrategy = new ColumnViewerEditorActivationStrategy(treeViewer);
}
if (actStrategy != null && focusCellMgr == null) {
TreeViewerEditor.create(treeViewer, actStrategy, features);
} else if (actStrategy != null && focusCellMgr != null) {
TreeViewerEditor.create(treeViewer, focusCellMgr, actStrategy, features);
}
if (resizable != null) {
for (final TreeColumn column : treeViewer.getTree().getColumns()) {
column.setResizable(resizable.booleanValue());
}
}
if (moveable != null) {
for (final TreeColumn column : treeViewer.getTree().getColumns()) {
column.setMoveable(moveable.booleanValue());
}
}
return treeViewer;
}
}