de.tototec.utils.jface.viewer.TableViewerBuilder 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.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TableViewerFocusCellManager;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.TableColumn;
/**
* Mutable builder for {@link TableViewer}s.
*/
public class TableViewerBuilder {
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 TableViewerBuilder() {
}
public TableViewerBuilder withTableLayout() {
this.withTableLayout = true;
return this;
}
public TableViewerBuilder setColumnsResizable(final boolean resizable) {
this.resizable = resizable;
return this;
}
public TableViewerBuilder setColumnsMoveable(final boolean moveable) {
this.moveable = moveable;
return this;
}
public TableViewerBuilder 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 TableViewerBuilder setEditorFeatures(final int featuresFlags) {
this.features = featuresFlags;
return this;
}
/**
* If set to true
, ensures only a cell is highlighted, and not
* the full row.
*/
public TableViewerBuilder setCellOwnerDrawHighlighter(final boolean ownerDrawHighlighter) {
this.ownerDrawHighlighter = ownerDrawHighlighter;
return this;
}
public TableViewerBuilder addFilter(final ViewerFilter viewerFilter) {
if (viewerFilters == null) {
viewerFilters = new LinkedList();
}
this.viewerFilters.add(viewerFilter);
return this;
}
public TableViewerBuilder addColumn(final ViewerColumnBuilder tableViewerColumnBuilder) {
if (columnBuilders == null) {
columnBuilders = new LinkedList>();
}
columnBuilders.add(tableViewerColumnBuilder);
return this;
}
public ViewerColumnBuilder addColumn() {
final ViewerColumnBuilder colBuilder = new ViewerColumnBuilder<>();
addColumn(colBuilder);
return colBuilder;
}
public TableViewerBuilder setColorProvider(final Function colorProvider) {
this.colorProvider = colorProvider;
return this;
}
public TableViewer apply(final TableViewer tableViewer) {
if (colorProvider == null) {
colorProvider = new ColorProvider(tableViewer.getControl());
}
if (withTableLayout) {
tableViewer.getTable().setLayout(new TableLayout());
}
boolean toolTipSupport = false;
if (columnBuilders != null) {
for (final ViewerColumnBuilder columnBuilder : columnBuilders) {
columnBuilder.setColorProvider(colorProvider);
toolTipSupport = toolTipSupport || columnBuilder.needsToolTipSupport();
columnBuilder.build(tableViewer);
}
}
if (toolTipSupport) {
ColumnViewerToolTipSupport.enableFor(tableViewer);
}
if (viewerFilters != null) {
tableViewer.setFilters(viewerFilters.toArray(new ViewerFilter[viewerFilters.size()]));
}
final TableViewerFocusCellManager focusCellMgr;
if (ownerDrawHighlighter) {
focusCellMgr = new TableViewerFocusCellManager(tableViewer, new FocusCellOwnerDrawHighlighter(tableViewer));
} else {
focusCellMgr = null;
}
final ColumnViewerEditorActivationStrategy actStrategy;
if (activationStrategy != null) {
actStrategy = new ColumnViewerEditorActivationStrategy(tableViewer) {
@Override
protected boolean isEditorActivationEvent(final ColumnViewerEditorActivationEvent event) {
return activationStrategy.apply(tableViewer, event);
}
};
} else {
actStrategy = new ColumnViewerEditorActivationStrategy(tableViewer);
}
if (actStrategy != null && focusCellMgr == null) {
TableViewerEditor.create(tableViewer, actStrategy, features);
} else if (actStrategy != null && focusCellMgr != null) {
TableViewerEditor.create(tableViewer, focusCellMgr, actStrategy, features);
}
if (resizable != null) {
for (final TableColumn column : tableViewer.getTable().getColumns()) {
column.setResizable(resizable.booleanValue());
}
}
if (moveable != null) {
for (final TableColumn column : tableViewer.getTable().getColumns()) {
column.setMoveable(moveable.booleanValue());
}
}
return tableViewer;
}
}