Commit 46cdec90 authored by Adrien's avatar Adrien Committed by Romain Bioteau
Browse files

feat(access control) display attribute types (#767)

* types are displayed in trees, with a specific color for complex types

closes [BS-17379](https://bonitasoft.atlassian.net/browse/BS-17379)
parent dec0cdc4
......@@ -17,5 +17,7 @@ public interface ColorConstants {
static final RGB SUCCESS_RGB = new RGB(60, 130, 40);
static final RGB BLUE_RGB = new RGB(0, 0, 200);
static final RGB EDITING_RGB = new RGB(62, 190, 222);
static final RGB SIMPLE_TYPE_RGB = new RGB(163, 163, 117);
static final RGB COMPLEX_TYPE_RGB = new RGB(117, 163, 163);
}
......@@ -32,6 +32,8 @@ import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerColumn;
import org.eclipse.swt.SWT;
......@@ -43,6 +45,7 @@ import org.eclipse.swt.widgets.Display;
public class LabelProviderBuilder<T> {
private Optional<Function<T, String>> textFunction = Optional.empty();
private Optional<Function<T, StyledString>> styledStringFunction = Optional.empty();
private Optional<Function<T, Image>> imageFunction = Optional.empty();
private Optional<Function<T, IStatus>> statusProvider = Optional.empty();
private Optional<Function<T, Font>> fontProvider = Optional.empty();
......@@ -53,6 +56,11 @@ public class LabelProviderBuilder<T> {
return this;
}
public LabelProviderBuilder<T> withStyledStringProvider(Function<T, StyledString> styledStringFunction) {
this.styledStringFunction = Optional.ofNullable(styledStringFunction);
return this;
}
public LabelProviderBuilder<T> withImageProvider(Function<T, Image> imageFunction) {
this.imageFunction = Optional.ofNullable(imageFunction);
return this;
......@@ -73,51 +81,107 @@ public class LabelProviderBuilder<T> {
return this;
}
public ColumnLabelProvider createColumnLabelProvider() {
return new ColumnLabelProvider() {
public StyledCellLabelProvider createStyledCellLabelProvider() {
return new StyledCellLabelProvider() {
private Color errorColor;
private Color warningColor;
@Override
protected void initialize(ColumnViewer viewer, ViewerColumn column) {
public void initialize(ColumnViewer viewer, ViewerColumn column) {
super.initialize(viewer, column);
errorColor = new Color(Display.getDefault(), ColorConstants.ERROR_RGB);
warningColor = new Color(Display.getDefault(), ColorConstants.WARNING_RGB);
viewer.getColumnViewerEditor().addEditorActivationListener(refreshAllAfterEdit());
}
private ColumnViewerEditorActivationListener refreshAllAfterEdit() {
return new ColumnViewerEditorActivationListener() {
@Override
public void update(ViewerCell cell) {
T element = (T) cell.getElement();
StyledString styledString = getStyledString(element);
cell.setText(styledString.getString());
cell.setImage(getImage(element));
@Override
public void beforeEditorDeactivated(ColumnViewerEditorDeactivationEvent event) {
statusProvider.ifPresent(provider -> {
final IStatus status = provider.apply(element);
if (!status.isOK()) {
cell.setImage(statusImage(status, element));
statusColor(status).ifPresent(cell::setForeground);
}
});
@Override
public void beforeEditorActivated(ColumnViewerEditorActivationEvent event) {
}
cell.setStyleRanges(styledString.getStyleRanges());
}
@Override
public String getToolTipText(Object element) {
return tooltipProvider().apply((T) element).orElse(super.getToolTipText(element));
}
@Override
public void afterEditorDeactivated(ColumnViewerEditorDeactivationEvent event) {
viewersToUpdate.forEach(viewer -> {
if (viewer.getInput() instanceof Collection) {
viewer.getControl().getDisplay().asyncExec(
() -> {
if (viewer != null && !viewer.getControl().isDisposed()
&& viewer.getInput() != null) {
viewer.update(((Collection<Object>) viewer.getInput()).toArray(), null);
}
});
}
});
private StyledString getStyledString(T element) {
return styledStringFunction.map(function -> function.apply(element))
.orElse(new StyledString());
}
}
private Image getImage(T element) {
return imageFunction.map(function -> function.apply(element)).orElse(null);
}
@Override
public void afterEditorActivated(ColumnViewerEditorActivationEvent event) {
}
};
private Optional<Color> statusColor(IStatus status) {
switch (status.getSeverity()) {
case IStatus.ERROR:
return Optional.of(errorColor);
case IStatus.WARNING:
return Optional.of(warningColor);
default:
return Optional.empty();
}
}
private Image statusImage(IStatus status, T element) {
switch (status.getSeverity()) {
case IStatus.ERROR:
return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
case IStatus.WARNING:
return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
case IStatus.INFO:
return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
default:
return getImage(element);
}
}
private ColumnViewerEditorActivationListener refreshAllAfterEdit() {
return refreshAfterEditListener();
}
@Override
public void dispose() {
errorColor.dispose();
warningColor.dispose();
super.dispose();
}
};
}
public ColumnLabelProvider createColumnLabelProvider() {
return new ColumnLabelProvider() {
private Color errorColor;
private Color warningColor;
@Override
protected void initialize(ColumnViewer viewer, ViewerColumn column) {
super.initialize(viewer, column);
errorColor = new Color(Display.getDefault(), ColorConstants.ERROR_RGB);
warningColor = new Color(Display.getDefault(), ColorConstants.WARNING_RGB);
viewer.getColumnViewerEditor().addEditorActivationListener(refreshAllAfterEdit());
}
private ColumnViewerEditorActivationListener refreshAllAfterEdit() {
return refreshAfterEditListener();
}
@Override
......@@ -143,19 +207,7 @@ public class LabelProviderBuilder<T> {
@Override
public String getToolTipText(Object element) {
if (statusProvider.isPresent()) {
final IStatus status = statusProvider.get().apply((T) element);
if (!status.isOK()) {
if (status.isMultiStatus()) {
return Arrays.asList(((MultiStatus) status).getChildren()).stream()
.filter(s -> !s.isOK())
.map(IStatus::getMessage)
.reduce((message1, message2) -> String.format("%s\n%s", message1, message2)).orElse("");
}
return status.getMessage();
}
}
return super.getToolTipText(element);
return tooltipProvider().apply((T) element).orElse(super.getToolTipText(element));
}
@Override
......@@ -229,4 +281,55 @@ public class LabelProviderBuilder<T> {
};
}
private Function<T, Optional<String>> tooltipProvider() {
return element -> {
if (statusProvider.isPresent()) {
final IStatus status = statusProvider.get().apply(element);
if (!status.isOK()) {
if (status.isMultiStatus()) {
return Optional.ofNullable(Arrays.asList(((MultiStatus) status).getChildren()).stream()
.filter(s -> !s.isOK())
.map(IStatus::getMessage)
.reduce((message1, message2) -> String.format("%s\n%s", message1, message2)).orElse(""));
}
return Optional.ofNullable(status.getMessage());
}
}
return Optional.empty();
};
}
private ColumnViewerEditorActivationListener refreshAfterEditListener() {
return new ColumnViewerEditorActivationListener() {
@Override
public void beforeEditorDeactivated(ColumnViewerEditorDeactivationEvent event) {
}
@Override
public void beforeEditorActivated(ColumnViewerEditorActivationEvent event) {
}
@Override
public void afterEditorDeactivated(ColumnViewerEditorDeactivationEvent event) {
viewersToUpdate.forEach(viewer -> {
if (viewer.getInput() instanceof Collection) {
viewer.getControl().getDisplay().asyncExec(
() -> {
if (viewer != null && !viewer.getControl().isDisposed()
&& viewer.getInput() != null) {
viewer.update(((Collection<Object>) viewer.getInput()).toArray(), null);
}
});
}
});
}
@Override
public void afterEditorActivated(ColumnViewerEditorActivationEvent event) {
}
};
}
}
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