Commit 23b793d2 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

feat(app) add navigation section (#365)

Closes BS-16344
parent d2ce5e4a
......@@ -41,6 +41,7 @@ Export-Package: org.bonitasoft.studio.ui,
org.bonitasoft.studio.ui.editors,
org.bonitasoft.studio.ui.util,
org.bonitasoft.studio.ui.validator,
org.bonitasoft.studio.ui.viewer,
org.bonitasoft.studio.ui.widget,
org.bonitasoft.studio.ui.wizard,
org.bonitasoft.studio.ui.wizard.listener
......
/**
* Copyright (C) 2016 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.ui.viewer;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;
public class EditingSupportBuilder<T> {
private final ColumnViewer viewer;
private Predicate<T> canEditFunction = e -> true;
private Function<T, CellEditor> cellEditorFunction;
private Function<T, Object> valueProvider = Object::toString;
private BiConsumer<T, Object> valueUpdater = (e, v) -> Function.identity();
public EditingSupportBuilder(ColumnViewer viewer) {
this.viewer = viewer;
this.cellEditorFunction = e -> new TextCellEditor((Composite) viewer.getControl());
}
public EditingSupportBuilder<T> withCellEditorProvider(Function<T, CellEditor> cellEditorFunction) {
this.cellEditorFunction = cellEditorFunction;
return this;
}
public EditingSupportBuilder<T> withValueProvider(Function<T, Object> valueProvider) {
this.valueProvider = valueProvider;
return this;
}
public EditingSupportBuilder<T> withValueUpdater(BiConsumer<T, Object> valueUpdater) {
this.valueUpdater = valueUpdater;
return this;
}
public EditingSupportBuilder<T> withCanEditProvider(Predicate<T> canEditFunction) {
this.canEditFunction = canEditFunction;
return this;
}
public EditingSupport create() {
return new EditingSupport(viewer) {
@Override
protected void setValue(Object element, Object value) {
valueUpdater.accept((T) element, value);
viewer.getControl().getDisplay().asyncExec(() -> viewer.refresh(element));
}
@Override
protected Object getValue(Object element) {
return valueProvider.apply((T) element);
}
@Override
protected CellEditor getCellEditor(Object element) {
return cellEditorFunction.apply((T) element);
}
@Override
protected boolean canEdit(Object element) {
try {
final T e = (T) element;
return canEditFunction.test(e);
} catch (final ClassCastException e) {
return false;
}
}
};
}
}
/**
* Copyright (C) 2016 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.ui.viewer;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
public class LabelProviderBuilder<T> {
private Optional<Function<T, String>> textFunction = Optional.empty();
private Optional<Function<T, Image>> imageFunction = Optional.empty();
public LabelProviderBuilder<T> withTextProvider(Function<T, String> textFunction) {
this.textFunction = Optional.ofNullable(textFunction);
return this;
}
public LabelProviderBuilder<T> withImageProvider(Function<T, Image> imageFunction) {
this.imageFunction = Optional.ofNullable(imageFunction);
return this;
}
public ColumnLabelProvider createColumnLabelProvider() {
return new ColumnLabelProvider() {
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
*/
@Override
public String getText(Object element) {
try {
final T e = (T) element;
return textFunction.map(function -> function.apply(e)).orElse(super.getText(element));
} catch (final ClassCastException e) {
return super.getText(element);
}
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
*/
@Override
public Image getImage(Object element) {
try {
final T e = (T) element;
return imageFunction.map(function -> function.apply(e)).orElse(super.getImage(element));
} catch (final ClassCastException e) {
return super.getImage(element);
}
}
};
}
public LabelProvider createLabelProvider() {
return new LabelProvider() {
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
*/
@Override
public String getText(Object element) {
try {
final T e = (T) element;
return textFunction.map(function -> function.apply(e)).orElse(super.getText(element));
} catch (final ClassCastException e) {
return super.getText(element);
}
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
*/
@Override
public Image getImage(Object element) {
try {
final T e = (T) element;
return imageFunction.map(function -> function.apply(e)).orElse(super.getImage(element));
} catch (final ClassCastException e) {
return super.getImage(element);
}
}
};
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment