From: jsimomaa Date: Tue, 28 Jul 2020 10:57:49 +0000 (+0300) Subject: Finder visualisation coloring to diagram & checkbox for enable/disable X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F56%2F4356%2F1;p=simantics%2Fdistrict.git Finder visualisation coloring to diagram & checkbox for enable/disable gitlab #86 Change-Id: I5ef0badf730300778b8ae6ff7c73eeb158fd66d1 (cherry picked from commit ade944a1d12ca6b83f3ec4d87b7f97fc7d05f770) --- 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 4603eb8e..79602077 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 @@ -6,13 +6,15 @@ 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.CheckStateChangedEvent; +import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.ColumnViewerToolTipSupport; import org.eclipse.jface.viewers.DoubleClickEvent; +import org.eclipse.jface.viewers.ICheckStateListener; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; @@ -36,7 +38,7 @@ public class ElementSelectorTableUI extends Composite { private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class); - private TableViewer viewer; + private CheckboxTableViewer viewer; private ElementSelectionView view; @@ -44,24 +46,40 @@ public class ElementSelectorTableUI extends Composite { public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) { super(parent, style); - + resourceManager = new LocalResourceManager(JFaceResources.getResources(), this); - + this.view = view; - + parent.setLayout(new FillLayout()); - // GridDataFactory.fillDefaults().grab(true, true).applyTo(this); - // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this); + // GridDataFactory.fillDefaults().grab(true, true).applyTo(this); + // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this); this.setLayout(new FillLayout()); viewer = createViewer(); addSelectionListener(); + addCheckStateListener(); setContentProvider(); createSelectorListener(parent); enableToolTips(); configureTable(); } + private void addCheckStateListener() { + viewer.addCheckStateListener(new ICheckStateListener() { + + @Override + public void checkStateChanged(CheckStateChangedEvent event) { + boolean checked = event.getChecked(); + if (checked) { + view.performSelection(getDisplay(), (ElementSelector) event.getElement()); + } else { + view.clearResultVisualisation((ElementSelector) event.getElement()); + } + } + }); + } + public Table getTable() { return viewer.getTable(); } @@ -71,17 +89,18 @@ public class ElementSelectorTableUI extends Composite { return selection != null ? (ElementSelector) selection.getFirstElement() : null; } - private TableViewer createViewer() { - return new TableViewer(this, SWT.FULL_SELECTION | SWT.SINGLE); + private CheckboxTableViewer createViewer() { + return CheckboxTableViewer.newCheckList(this, SWT.FULL_SELECTION | SWT.SINGLE); } private void configureTable() { - Table table = viewer.getTable(); + Table table = getTable(); table.setHeaderVisible(true); table.setLinesVisible(true); createNameColumn(); createQueryColumn(); + //createQueryActiveColumn(); } private void enableToolTips() { @@ -94,8 +113,8 @@ public class ElementSelectorTableUI extends Composite { public Object[] getElements(Object inputElement) { if (inputElement == null || !(inputElement instanceof Collection)) return new Object[0]; - - return ((Collection)inputElement).toArray(); + + return ((Collection) inputElement).toArray(); } }); } @@ -111,18 +130,18 @@ public class ElementSelectorTableUI extends Composite { ElementSelector selector = (ElementSelector) element; return " " + selector.getName(); } - + @Override public Image getImage(Object element) { 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 -> { @@ -134,10 +153,10 @@ public class ElementSelectorTableUI extends Composite { imageData.setPixel(x, y, 1); } } - + return imageData; })); - + return image; } }); @@ -160,23 +179,24 @@ public class ElementSelectorTableUI extends Composite { } private void createSelectorListener(Composite parent) { - Simantics.getSession().asyncRequest(new ElementSelectionTools.SelectionsRequest(), new SyncListenerAdapter>() { - public void execute(ReadGraph graph, Collection result) { - parent.getDisplay().asyncExec(() -> { - viewer.setInput(result); - }); - } + Simantics.getSession().asyncRequest(new ElementSelectionTools.SelectionsRequest(), + new SyncListenerAdapter>() { + public void execute(ReadGraph graph, Collection result) { + parent.getDisplay().asyncExec(() -> { + viewer.setInput(result); + }); + } - @Override - public void exception(ReadGraph graph, Throwable t) throws DatabaseException { - LOGGER.error("Error getting element selector list", t); - } + @Override + public void exception(ReadGraph graph, Throwable t) throws DatabaseException { + LOGGER.error("Error getting element selector list", t); + } - @Override - public boolean isDisposed() { - return ElementSelectorTableUI.this.isDisposed(); - } - }); + @Override + public boolean isDisposed() { + return ElementSelectorTableUI.this.isDisposed(); + } + }); } private void addSelectionListener() { @@ -189,8 +209,16 @@ public class ElementSelectorTableUI extends Composite { IStructuredSelection selection = (IStructuredSelection) event.getViewer().getSelection(); Display display = event.getViewer().getControl().getDisplay(); ElementSelector query = (ElementSelector) selection.getFirstElement(); - + view.performSelection(display, query); + + viewer.setChecked(selection.getFirstElement(), true); } } + + @Override + public void dispose() { + view.dispose(); + super.dispose(); + } } 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 16e6bb15..31d1c6e3 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 @@ -1,9 +1,13 @@ package org.simantics.district.selection.ui.parts; +import java.awt.Color; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import javax.annotation.PostConstruct; import javax.inject.Inject; @@ -34,6 +38,7 @@ import org.simantics.db.layer0.SelectionHints; import org.simantics.db.layer0.request.ActiveModels; import org.simantics.db.request.Read; import org.simantics.district.network.ui.DistrictNetworkUIUtil; +import org.simantics.district.network.ui.participants.DistrictFinderVisualisationParticipant; import org.simantics.district.selection.ElementSelector; import org.simantics.district.selection.ElementSelector.DiagramGenerator; import org.simantics.district.selection.ElementSelector.ExplicitGenerator; @@ -41,6 +46,7 @@ import org.simantics.district.selection.ElementSelector.PropertySelector; import org.simantics.district.selection.ElementSelector.SelectionResult; import org.simantics.district.selection.ui.ElementSelectionTools; import org.simantics.district.selection.ui.ElementSelectorTableUI; +import org.simantics.scl.runtime.tuple.Tuple2; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -123,18 +129,28 @@ public class ElementSelectionView { public void setFocus() { table.setFocus(); } - + public ElementSelector getSelectedItem() { return table.getSelectedItem(); } + + private Map resultCache = new HashMap<>(); public void performSelection(Display display, ElementSelector query) { try { Collection models = Simantics.getSession().syncRequest(new ActiveModels(Simantics.getProjectResource())); final Resource model = models.isEmpty() ? null : models.iterator().next(); + Tuple2 selectionResult = resultCache.get(query); + if (selectionResult != null) { + // clear existing result and visualisation + clearResultVisualisation(query); + } + SelectionResult result = performQuery(query, model); + resultCache.put(query, new Tuple2(result, model)); + if (result.tailCount != result.tailSize) { showArbitraryResultWarning(query, result); } @@ -143,20 +159,32 @@ public class ElementSelectionView { openDiagramWithSelection(display, result); } - StructuredSelection selection = makeSelection(model, result); + StructuredSelection selection = makeSelection(model, result, query.getColor()); selectionService.setPostSelection(selection); sendSelectionEvent(selection); } catch (DatabaseException e) { LOGGER.error("Element selection query failed", e); } } + + public void clearResultVisualisation(ElementSelector query) { + Tuple2 selectionResult = resultCache.get(query); + if (selectionResult != null) { + SelectionResult result = (SelectionResult) selectionResult.c0; + Resource model = (Resource) selectionResult.c1; + eventBroker.send(SELECTION_EVENT_ID, makeSelection(model, result, null)); + } + } - private StructuredSelection makeSelection(final Resource model, SelectionResult result) { + private StructuredSelection makeSelection(final Resource model, SelectionResult result, float[] fs) { return new StructuredSelection(result.elements.stream() .map(p0 -> { AdaptableHintContext selectionElement = new ElementSelectionTools.SelectionElement(SelectionHints.STD_KEYS); selectionElement.setHint(SelectionHints.KEY_MAIN, p0); selectionElement.setHint(SelectionHints.KEY_MODEL, model); + if (fs != null) { + selectionElement.setHint(DistrictFinderVisualisationParticipant.COLOR_KEY, new Color(fs[2], fs[1], fs[0], fs[3])); + } return selectionElement; }) .toArray()); @@ -191,4 +219,11 @@ public class ElementSelectionView { }); return result; } + + public void dispose() { + for (Entry entry : resultCache.entrySet()) { + ElementSelector query = entry.getKey(); + clearResultVisualisation(query); + } + } }