From: Reino Ruusu Date: Fri, 3 Apr 2020 16:40:45 +0000 (+0300) Subject: Selection of highlight color for District Finder queries X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=6e5f34e9a8fa2dd0c83745e25285dc4c5e0a9ce5;p=simantics%2Fdistrict.git Selection of highlight color for District Finder queries gitlab #85 TODO: Element size controls Change-Id: Ifadc9f72a7efe6e7ace930b2d4c09b3133f1b9df --- diff --git a/org.simantics.district.selection.ui/META-INF/MANIFEST.MF b/org.simantics.district.selection.ui/META-INF/MANIFEST.MF index 86962fc5..523958b1 100644 --- a/org.simantics.district.selection.ui/META-INF/MANIFEST.MF +++ b/org.simantics.district.selection.ui/META-INF/MANIFEST.MF @@ -31,7 +31,8 @@ Require-Bundle: javax.inject, org.simantics.diagram.ontology, org.eclipse.e4.core.services, org.simantics.district.network.ontology;bundle-version="1.0.0", - org.simantics.layer0.utils + org.simantics.layer0.utils, + org.eclipse.nebula.widgets.tablecombo Bundle-ManifestVersion: 2 Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Bundle-Vendor: Semantum oy diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java index 33a428f4..06622b82 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java @@ -22,6 +22,7 @@ import org.simantics.layer0.Layer0; import org.simantics.ui.selection.AnyResource; import org.simantics.ui.selection.AnyVariable; import org.simantics.ui.selection.WorkbenchSelectionContentType; +import org.simantics.utils.ui.color.Color; public class ElementSelectionTools { @@ -82,4 +83,19 @@ public class ElementSelectionTools { } } + public static float[] colorToLiteral(Color color) { + // BGRA float values out + return new float[] { + (float)color.getB() / 255, + (float)color.getG() / 255, + (float)color.getR() / 255, + 1.f, + }; + } + + public static Color literalToColor(float[] color) { + // BGRA float values in + return new Color((int) (color[2] * 255), (int) (color[1] * 255), (int) (color[0] * 255)); + } + } diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java index cdddd928..4603eb8e 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java @@ -2,22 +2,27 @@ package org.simantics.district.selection.ui; import java.util.Collection; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; +import org.eclipse.jface.resource.ResourceManager; import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.ColumnViewerToolTipSupport; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; +import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.ITreeContentProvider; -import org.eclipse.jface.viewers.TreeSelection; -import org.eclipse.jface.viewers.TreeViewer; -import org.eclipse.jface.viewers.TreeViewerColumn; -import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.PaletteData; +import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.Table; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.common.procedure.adapter.SyncListenerAdapter; @@ -31,12 +36,17 @@ public class ElementSelectorTableUI extends Composite { private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class); - private TreeViewer viewer; + private TableViewer viewer; private ElementSelectionView view; + private ResourceManager resourceManager; + public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) { super(parent, style); + + resourceManager = new LocalResourceManager(JFaceResources.getResources(), this); + this.view = view; parent.setLayout(new FillLayout()); @@ -52,8 +62,8 @@ public class ElementSelectorTableUI extends Composite { configureTable(); } - public Tree getTree() { - return viewer.getTree(); + public Table getTable() { + return viewer.getTable(); } public ElementSelector getSelectedItem() { @@ -61,12 +71,12 @@ public class ElementSelectorTableUI extends Composite { return selection != null ? (ElementSelector) selection.getFirstElement() : null; } - private TreeViewer createViewer() { - return new TreeViewer(this, SWT.FULL_SELECTION); + private TableViewer createViewer() { + return new TableViewer(this, SWT.FULL_SELECTION | SWT.SINGLE); } private void configureTable() { - Tree table = viewer.getTree(); + Table table = viewer.getTable(); table.setHeaderVisible(true); table.setLinesVisible(true); @@ -79,17 +89,7 @@ public class ElementSelectorTableUI extends Composite { } private void setContentProvider() { - viewer.setContentProvider(new ITreeContentProvider() { - @Override - public boolean hasChildren(Object element) { - return false; - } - - @Override - public Object getParent(Object element) { - return null; - } - + viewer.setContentProvider(new IStructuredContentProvider() { @Override public Object[] getElements(Object inputElement) { if (inputElement == null || !(inputElement instanceof Collection)) @@ -97,21 +97,11 @@ public class ElementSelectorTableUI extends Composite { return ((Collection)inputElement).toArray(); } - - @Override - public Object[] getChildren(Object parentElement) { - return null; - } - - @Override - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput); - } }); } - private TreeViewerColumn createNameColumn() { - TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE); + private TableViewerColumn createNameColumn() { + TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE); column.getColumn().setText("Name"); column.getColumn().setWidth(200); column.getColumn().setResizable(true); @@ -119,19 +109,43 @@ public class ElementSelectorTableUI extends Composite { @Override public String getText(Object element) { ElementSelector selector = (ElementSelector) element; - return selector.getName(); + return " " + selector.getName(); } @Override public Image getImage(Object element) { - return null; + ElementSelector selector = (ElementSelector) element; + float[] color = selector.getColor(); + if (color == null) + return null; + + RGB rgb = ElementSelectionTools.literalToColor(color).getRgb(); + + PaletteData paletteData = new PaletteData(new RGB[] { new RGB(255, 255, 255), rgb }); + + // Create image with a colored horizontal bar + // TODO: Indicate line width + Image image = resourceManager.createImage(ImageDescriptor.createFromImageDataProvider(zoom -> { + int height = 8 * zoom / 100; + int width = 40 * zoom / 100; + ImageData imageData = new ImageData(width, height, 1, paletteData); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + imageData.setPixel(x, y, 1); + } + } + + return imageData; + })); + + return image; } }); return column; } - private TreeViewerColumn createQueryColumn() { - TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE); + private TableViewerColumn createQueryColumn() { + TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE); column.getColumn().setText("Query"); column.getColumn().setWidth(600); column.getColumn().setResizable(true); @@ -141,11 +155,6 @@ public class ElementSelectorTableUI extends Composite { ElementSelector selector = (ElementSelector) element; return selector.getExpression(); } - - @Override - public Image getImage(Object element) { - return null; - } }); return column; } @@ -177,7 +186,7 @@ public class ElementSelectorTableUI extends Composite { private final class DoubleClickListener implements IDoubleClickListener { @Override public void doubleClick(DoubleClickEvent event) { - TreeSelection selection = (TreeSelection) event.getViewer().getSelection(); + IStructuredSelection selection = (IStructuredSelection) event.getViewer().getSelection(); Display display = event.getViewer().getControl().getDisplay(); ElementSelector query = (ElementSelector) selection.getFirstElement(); diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java index 623f0f84..387afa7c 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java @@ -24,6 +24,8 @@ import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.LocalResourceManager; import org.eclipse.jface.resource.ResourceLocator; +import org.eclipse.jface.window.Window; +import org.eclipse.nebula.widgets.tablecombo.TableCombo; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -31,11 +33,15 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Widget; import org.simantics.Simantics; +import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -50,6 +56,7 @@ import org.simantics.db.layer0.request.PropertyInfo; import org.simantics.db.layer0.request.PropertyInfoRequest; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.stubs.G2DResource; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.region.ontology.DiagramRegionsResource; import org.simantics.district.route.ontology.RouteResource; @@ -69,12 +76,14 @@ import org.simantics.district.selection.ElementSelector.PropertySelector; import org.simantics.district.selection.ElementSelector.RegionCondition; import org.simantics.district.selection.ElementSelector.RouteCondition; import org.simantics.district.selection.ElementSelector.Selector; +import org.simantics.district.selection.ui.ElementSelectionTools; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.utils.datastructures.Arrays; import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.color.Color; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -116,6 +125,9 @@ public class EditSelectorDialog extends Dialog { private String name; private Text nameField; + private Color color; + private TableCombo colorCombo; + private Resource diagram; private Combo diagramField; @@ -158,7 +170,6 @@ public class EditSelectorDialog extends Dialog { // Called to read values from controls into conditions Updater updater = NULL_UPDATE; - @Inject public EditSelectorDialog(Shell shell, ElementSelector elementSelector, Collection currentSelection) { super(shell); @@ -200,6 +211,11 @@ public class EditSelectorDialog extends Dialog { condition = null; if (elementSelector != null) { + float[] color = elementSelector.getColor(); + if (color != null) { + this.color = ElementSelectionTools.literalToColor(color); + } + Generator generator = elementSelector.getGenerator(); if (generator instanceof ModelGenerator) { generatorIndex = 0; @@ -357,6 +373,13 @@ public class EditSelectorDialog extends Dialog { throw new ValidationException("Please enter a name"); } + { + int index = colorCombo.getSelectionIndex(); + if (index >= 0) { + color = new Color(colorCombo.getTable().getItem(index).getBackground().getRGB()); + } + } + componentType = componentTypes.get(componentTypeField.getSelectionIndex()); selectorIndex = selectorField.getSelectionIndex(); @@ -424,6 +447,11 @@ public class EditSelectorDialog extends Dialog { graph.claimLiteral(selection, L0.HasLabel, L0.String, name); graph.claim(selection, L0.PartOf, lib); + if (color != null) { + float[] value = ElementSelectionTools.colorToLiteral(color); + graph.claimLiteral(selection, ES.Selection_HasHighlightColor, G2DResource.getInstance(graph).Color, value, Bindings.FLOAT_ARRAY); + } + // Generator Resource generator = graph.newResource(); Resource generatorType; @@ -516,6 +544,58 @@ public class EditSelectorDialog extends Dialog { nameField.setText(name); GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo(nameField); + // Highlight style + Label label = new Label(content, SWT.NONE); + GridDataFactory.swtDefaults().applyTo(label); + label.setText("Highlight color"); + + colorCombo = new TableCombo(content, SWT.BORDER | SWT.READ_ONLY); + GridDataFactory.swtDefaults().hint(80, SWT.DEFAULT).applyTo(colorCombo); + Table colorTable = colorCombo.getTable(); + colorCombo.getTextControl().setEnabled(false); + Display display = getShell().getDisplay(); + colorTable.setFont(display.getSystemFont()); + createColorItem(colorTable, "Red", SWT.COLOR_RED); + createColorItem(colorTable, "Green", SWT.COLOR_GREEN); + createColorItem(colorTable, "Blue", SWT.COLOR_BLUE); + createColorItem(colorTable, "Yellow", SWT.COLOR_YELLOW); + createColorItem(colorTable, "Cyan", SWT.COLOR_CYAN); + createColorItem(colorTable, "Magenta", SWT.COLOR_MAGENTA); + TableItem other = new TableItem(colorTable, SWT.NONE); + other.setText("Other..."); + + if (color != null) { + int i; + for (i = 0; i < 6; i++) { + if (colorTable.getItem(i).getBackground().getRGB().equals(color.getRgb())) { + colorCombo.select(i); + break; + } + } + + if (i == 6) { + other.setBackground(resourceManager.createColor(color.getRgb())); + colorCombo.select(6); + } + } + + colorCombo.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + int index = colorCombo.getSelectionIndex(); + if (index == 6) { + org.simantics.utils.ui.color.ColorDialog colorDialog = new org.simantics.utils.ui.workbench.dialogs.ColorDialog(getParentShell()); + int result = colorDialog.open(); + if (result == Window.OK) { + other.setBackground(resourceManager.createColor(colorDialog.getColor().getRgb())); + colorCombo.clearSelection(); + colorCombo.select(6); + colorTable.redraw(); + } + } + } + }); + // Selector Label selectorLabel = new Label(content, SWT.NONE); selectorLabel.setText("Select"); @@ -632,18 +712,25 @@ public class EditSelectorDialog extends Dialog { sourceField.select(generatorIndex); // Condition - Label label = new Label(content, SWT.NONE); + label = new Label(content, SWT.NONE); GridDataFactory.swtDefaults().align(SWT.BEGINNING, SWT.CENTER).applyTo(label); label.setText("where"); conditionPanel = new Composite(content, SWT.NONE); - GridDataFactory.swtDefaults().span(1, 2).minSize(400, SWT.DEFAULT).grab(true, false).applyTo(conditionPanel); + GridDataFactory.swtDefaults().minSize(400, SWT.DEFAULT).grab(true, false).applyTo(conditionPanel); GridLayoutFactory.fillDefaults().numColumns(2).applyTo(conditionPanel); updater = updateConditionPanel(); return content; } + + private TableItem createColorItem(Table colorTable, String name, int color) { + TableItem item = new TableItem(colorTable, SWT.BORDER); + item.setText(name); + item.setBackground(colorTable.getDisplay().getSystemColor(color)); + return item; + } private Updater updateConditionPanel() { // Erase contents diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/ElementSelectionView.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/ElementSelectionView.java index dfc53328..16e6bb15 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/ElementSelectionView.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/ElementSelectionView.java @@ -115,7 +115,7 @@ public class ElementSelectionView { @PostConstruct public void createPartControl(Composite parent, EMenuService menuService) { table = new ElementSelectorTableUI(parent, SWT.BORDER, this); - if (!(menuService.registerContextMenu(this.table.getTree(), POPUP_ID))) + if (!(menuService.registerContextMenu(this.table.getTable(), POPUP_ID))) LOGGER.warn("Could not register context menu {}", POPUP_ID); } diff --git a/org.simantics.district.selection/META-INF/MANIFEST.MF b/org.simantics.district.selection/META-INF/MANIFEST.MF index 14fe8e0f..fae2f723 100644 --- a/org.simantics.district.selection/META-INF/MANIFEST.MF +++ b/org.simantics.district.selection/META-INF/MANIFEST.MF @@ -17,5 +17,6 @@ Require-Bundle: org.simantics.district.route;bundle-version="1.0.0", org.junit, org.simantics, org.simantics.structural.ontology, - org.simantics.modeling.ontology + org.simantics.modeling.ontology, + org.simantics.g2d.ontology;bundle-version="1.1.0" Export-Package: org.simantics.district.selection diff --git a/org.simantics.district.selection/graph/DiagramElementSelection.pgraph b/org.simantics.district.selection/graph/DiagramElementSelection.pgraph index 22071403..324268a2 100644 --- a/org.simantics.district.selection/graph/DiagramElementSelection.pgraph +++ b/org.simantics.district.selection/graph/DiagramElementSelection.pgraph @@ -2,6 +2,7 @@ L0 = DIA = DNR = REG = +G2D = ES = : L0.Ontology @L0.new @@ -14,9 +15,16 @@ ES.Selection -- ES.Selection.HasGenerator --> ES.Generator -- ES.Selection.HasCondition --> ES.Condition -- ES.Selection.HasSelector --> ES.Selector -- ES.Selection.HasHighlightColor --> G2D.Color -- ES.Selection.HasLineWidth --> L0.Float