From: Jussi Koskela Date: Thu, 25 Jun 2020 06:34:57 +0000 (+0300) Subject: Even more fixes to layers X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=3cfa8ee6785da4a41c655c0f2a12a899b28f85d5;p=simantics%2Fplatform.git Even more fixes to layers Change-Id: Ie638616341f3ecdaf83de0611d64152358e04a57 --- diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java index af2a8da33..baab13a7e 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java @@ -22,7 +22,6 @@ import org.simantics.db.procedure.Listener; import org.simantics.diagram.synchronization.ErrorHandler; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.diagram.handler.SubstituteElementClass; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.IElement; @@ -80,14 +79,8 @@ public class ConnectionRequest extends BaseRequest2 { } @Override - public void execute(AsyncReadGraph graph, ElementClass mutableClazz) { - - List substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class); - for (SubstituteElementClass subs : substitutes) { - mutableClazz = subs.substitute(diagram, mutableClazz); - } - final ElementClass ec = mutableClazz; - + public void execute(AsyncReadGraph graph, final ElementClass ec) { + graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener() { @Override diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java index a19bc3dad..36a01b54c 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java @@ -11,8 +11,6 @@ *******************************************************************************/ package org.simantics.diagram.adapter; -import java.util.List; - import org.simantics.db.AsyncReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.Adapter; @@ -21,7 +19,6 @@ import org.simantics.db.procedure.AsyncProcedure; import org.simantics.diagram.synchronization.ErrorHandler; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.diagram.handler.SubstituteElementClass; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.IElement; import org.simantics.scl.runtime.tuple.Tuple3; @@ -78,13 +75,7 @@ public class ConnectionRequest2 extends BaseRequest2 { } @Override - public void execute(AsyncReadGraph graph, ElementClass mutableClazz) { - - List substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class); - for (SubstituteElementClass subs : substitutes) { - mutableClazz = subs.substitute(diagram, mutableClazz); - } - final ElementClass ec = mutableClazz; + public void execute(AsyncReadGraph graph, final ElementClass ec) { graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener() { diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java index 14b9f148a..a1fa5bee5 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java @@ -127,9 +127,7 @@ import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.IElementClassProvider; import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd; -import org.simantics.g2d.element.handler.ElementHandler; import org.simantics.g2d.element.handler.ElementLayerListener; -import org.simantics.g2d.element.handler.ElementLayers; import org.simantics.g2d.element.handler.TerminalTopology; import org.simantics.g2d.element.impl.Element; import org.simantics.g2d.layers.ILayer; @@ -766,19 +764,8 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID public ElementClass substitute(IDiagram d, ElementClass ec) { if (d != diagram) throw new IllegalArgumentException("specified diagram does not have this SubstituteElementClass handler"); - - // If the element class is our own, there's no point in creating - // a copy of it. - /*if (ec.contains(elementLayerListener)) - return ec;*/ - - List all = ec.getAll(); - List result = new ArrayList(all); - for (ElementHandler eh : all) { - if (eh instanceof ElementLayers) - result.add(elementLayerListener); - } - return ElementClass.compile(result, false).setId(ec.getId()); + // Nothing to substitute here + return ec; } } @@ -1199,6 +1186,11 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } } + @Override + public void flush() { + modificationQueue.flush(); + } + void changeTag(IElement e, Resource tag, boolean set) { Object object = e.getHint(ElementHints.KEY_OBJECT); Resource tagged = null; @@ -1213,7 +1205,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID if (tagged == null) return; - modificationQueue.async(new TagChange(tagged, tag, set), null); + modificationQueue.offer(new TagChange(tagged, tag, set), null); } }; @@ -1280,6 +1272,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID d.setHint(DiagramHints.KEY_LAYERS, layers); d.setHint(DiagramHints.KEY_LAYERS_EDITOR, layers); + d.setHint(DiagramHints.KEY_ELEMENT_LAYER_LISTENER, elementLayerListener); d.addCompositionVetoListener(diagramListener); d.addCompositionListener(diagramListener); diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java index 79e79a217..db02034e8 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java @@ -29,6 +29,7 @@ import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ServiceException; +import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.procedure.Listener; import org.simantics.diagram.stubs.DiagramResource; @@ -39,7 +40,6 @@ import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; -import org.simantics.g2d.element.handler.ElementLayers; import org.simantics.g2d.layers.IEditableLayer; import org.simantics.g2d.layers.IEditableLayer.ILayerListener; import org.simantics.g2d.layers.IEditableLayer.LayerChangeEvent; @@ -217,10 +217,6 @@ public class GraphLayerManager { if (removedName.equals(name)) { g.denyStatement(diagram, dia.HasLayer, l); deleteLayer(g, l); - - // NOTE: leave the layer tags intact, remove them gradually - // by checking the validity of all layer tags during element - // writeback. layers.remove(name); return; } @@ -228,7 +224,7 @@ public class GraphLayerManager { } void deleteLayer(WriteGraph g, Resource layer) throws DatabaseException { - g.deny(layer); + RemoverUtil.remove(g, layer); } } @@ -517,27 +513,6 @@ public class GraphLayerManager { } } - void putElementOnVisibleLayers(IDiagram diagram, IElement element) { - // Make the new element visible and focusable on all currently - // active layers. - ILayers diagramLayers = diagram.getHint(DiagramHints.KEY_LAYERS); - if (diagramLayers != null) { - element.setHint(ElementHints.KEY_VISIBLE_LAYERS, new HashSet()); - element.setHint(ElementHints.KEY_FOCUS_LAYERS, new HashSet()); - Set visibleLayers = diagramLayers.getVisibleLayers(); - - if (DEBUG_LAYERS) - System.out.println("Marking element visible and focusable only on visible layers: " + visibleLayers); - - for (ElementLayers elementLayers : element.getElementClass().getItemsByClass(ElementLayers.class)) { - for (ILayer layer : visibleLayers) { - elementLayers.setVisibility(element, layer, true); - elementLayers.setFocusability(element, layer, true); - } - } - } - } - public void putElementOnVisibleLayers(IDiagram diagram, WriteGraph g, Resource element) throws DatabaseException { // Make the new element visible and focusable on all currently // active layers. diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/DiagramHints.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/DiagramHints.java index 0c11d348a..9b36d845f 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/DiagramHints.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/DiagramHints.java @@ -23,6 +23,7 @@ import org.simantics.g2d.connection.IConnectionAdvisor; import org.simantics.g2d.diagram.handler.impl.PickContextImpl; import org.simantics.g2d.diagram.participant.ElementPainter; import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementLayerListener; import org.simantics.g2d.layers.ILayers; import org.simantics.g2d.layers.ILayersEditor; import org.simantics.g2d.routing.IRouter2; @@ -95,6 +96,8 @@ public class DiagramHints { public static final Key KEY_LAYERS_EDITOR = new KeyOf(ILayersEditor.class, "LAYERS_EDITOR"); + public static final Key KEY_ELEMENT_LAYER_LISTENER = new KeyOf(ElementLayerListener.class, "ELEMENT_LAYER_LISTENER"); + public static final Key KEY_FIXED_LAYERS = new KeyOf(String[].class, "FIXED_LAYERS"); public static final Key KEY_MUTATOR = new KeyOf(DiagramMutator.class, "MUTATIONS"); diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayerListener.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayerListener.java index 98dd9610e..e14bba9dc 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayerListener.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayerListener.java @@ -17,9 +17,10 @@ import org.simantics.g2d.layers.ILayer; /** * @author Tuukka Lehtonen */ -public interface ElementLayerListener extends ElementHandler { +public interface ElementLayerListener { void visibilityChanged(IElement e, ILayer layer, boolean visible); void focusabilityChanged(IElement e, ILayer layer, boolean focusable); + void flush(); } diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayers.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayers.java index 9edf28d41..60cdf395d 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayers.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayers.java @@ -11,8 +11,6 @@ *******************************************************************************/ package org.simantics.g2d.element.handler; -import java.util.Collection; - import org.simantics.g2d.element.IElement; import org.simantics.g2d.layers.ILayer; import org.simantics.g2d.layers.ILayers; @@ -26,7 +24,5 @@ public interface ElementLayers extends ElementHandler { boolean isVisible(IElement e, ILayer layer); boolean isFocusable(IElement e, ILayers layers); boolean isFocusable(IElement e, ILayer layer); - boolean setVisibility(IElement e, ILayer layer, boolean value); - boolean setFocusability(IElement e, ILayer layer, boolean value); } diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/SimpleElementLayers.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/SimpleElementLayers.java index d62e85b80..622e5322e 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/SimpleElementLayers.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/SimpleElementLayers.java @@ -16,7 +16,6 @@ import java.util.Set; import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; -import org.simantics.g2d.element.handler.ElementLayerListener; import org.simantics.g2d.element.handler.ElementLayers; import org.simantics.g2d.layers.ILayer; import org.simantics.g2d.layers.ILayers; @@ -84,46 +83,4 @@ public class SimpleElementLayers implements ElementLayers { } - @Override - public boolean setVisibility(IElement e, ILayer layer, boolean value) { - - Set elementLayers = (Set) e.getHint(ElementHints.KEY_VISIBLE_LAYERS); - if (elementLayers == null) - return false; - boolean result; - if (value) - result = elementLayers.add(layer); - else - result = elementLayers.remove(layer); - - if (result) { - for (ElementLayerListener ell : e.getElementClass().getItemsByClass(ElementLayerListener.class)) { - ell.visibilityChanged(e, layer, value); - } - } - - return result; - } - - @Override - public boolean setFocusability(IElement e, ILayer layer, boolean value) { - - Set elementLayers = (Set) e.getHint(ElementHints.KEY_FOCUS_LAYERS); - if (elementLayers == null) - return false; - boolean result; - if (value) - result = elementLayers.add(layer); - else - result = elementLayers.remove(layer); - - if (result) { - for (ElementLayerListener ell : e.getElementClass().getItemsByClass(ElementLayerListener.class)) { - ell.focusabilityChanged(e, layer, value); - } - } - - return result; - } - } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java index 87e18ed8e..3bce16ccb 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java @@ -56,7 +56,9 @@ import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.diagram.participant.Selection; import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementLayerListener; import org.simantics.g2d.element.handler.ElementLayers; import org.simantics.g2d.layers.IEditableLayer; import org.simantics.g2d.layers.ILayer; @@ -167,19 +169,6 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { return null; } - boolean setAttribute(IElement e, ILayer layer, Attribute attribute, boolean value) { - ElementClass ec = e.getElementClass(); - for (ElementLayers el : ec.getItemsByClass(ElementLayers.class)) { - switch (attribute) { - case Visible: - return el.setVisibility(e, layer, value); - case Focusable: - return el.setFocusability(e, layer, value); - } - } - return false; - } - Tristate getJointAttribute(Collection elements, ILayer layer, Attribute attribute) { Tristate state = null; for (IElement e : elements) { @@ -196,13 +185,39 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { return state; } - int setAttribute(Collection elements, ILayer layer, Attribute attribute, boolean value) { - int result = 0; - for (IElement e : elements) { - if (setAttribute(e, layer, attribute, value)) - ++result; + void setAttribute(Collection elements, ILayer layer, Attribute attribute, boolean value) { + // Short-circuit the hint updates so that the result will be immediately available for viewer.refresh(). + // There is no listener for the element layer hints so the viewer won't update the tristates automatically! + + IDiagram diagram = context.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM); + ElementLayerListener elementLayerListener = diagram.getHint(DiagramHints.KEY_ELEMENT_LAYER_LISTENER); + switch (attribute) { + case Visible: + for (IElement e : elements) { + Set elementLayers = (Set) e.getHint(ElementHints.KEY_VISIBLE_LAYERS); + if (elementLayers != null) { + if (value) + elementLayers.add(layer); + else + elementLayers.remove(layer); + } + elementLayerListener.visibilityChanged(e, layer, value); + } + break; + case Focusable: + for (IElement e : elements) { + Set elementLayers = (Set) e.getHint(ElementHints.KEY_FOCUS_LAYERS); + if (elementLayers != null) { + if (value) + elementLayers.add(layer); + else + elementLayers.remove(layer); + } + elementLayerListener.focusabilityChanged(e, layer, value); + } + break; } - return result; + elementLayerListener.flush(); } @@ -309,8 +324,6 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { } @Override public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { - // TODO Auto-generated method stub - System.out.println("REM"); } }); @@ -347,6 +360,25 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { } }; + private static String findFreshName(ILayers layers, String proposal) { + Set all = layers.getLayers(); + String name = proposal; + int i = 1; + while (true) { + boolean match = false; + for (ILayer layer : all) { + if (name.equals(layer.getName())) { + match = true; + break; + } + } + if (!match) + return name; + ++i; + name = proposal + " " + i; //$NON-NLS-1$ + } + } + private void initialize2(ILayersEditor layers, IDiagram diagram) { layers.addLayersListener(layersListener); @@ -355,25 +387,6 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { addButton.setToolTipText(Messages.DiagramLayersPage_NewTT); addButton.addSelectionListener(new SelectionListener() { - String findFreshName(ILayers layers, String proposal) { - Set all = layers.getLayers(); - String name = proposal; - int i = 1; - while (true) { - boolean match = false; - for (ILayer layer : all) { - if (name.equals(layer.getName())) { - match = true; - break; - } - } - if (!match) - return name; - ++i; - name = proposal + " " + i; //$NON-NLS-1$ - } - } - @Override public void widgetSelected(SelectionEvent e) { String name = findFreshName(layers, Messages.DiagramLayersPage_NewRole); @@ -572,8 +585,6 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { // FIXME: Eclipse currently eats F2 presses. This should be // implemented as a command handler or find some way to // force these listeners to have priority... - System.out.println("startediting"); //$NON-NLS-1$ - TreeItem[] items = viewer.getTree().getSelection(); if(items.length != 1) return; @@ -615,18 +626,16 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { rect = item.getBounds(1); if (rect.contains(pt)) { Tristate state = getJointAttribute(elements, layer, Attribute.Visible); - if (setAttribute(elements, layer, Attribute.Visible, state.toggle().toBoolean()) > 0) { - refresh(); - } + setAttribute(elements, layer, Attribute.Visible, state.toggle().toBoolean()); + refresh(); return; } Rectangle rect2 = item.getBounds(2); if (rect2.contains(pt)) { Tristate state = getJointAttribute(elements, layer, Attribute.Focusable); - if (setAttribute(elements, layer, Attribute.Focusable, state.toggle().toBoolean()) > 0) { - refresh(); - } + setAttribute(elements, layer, Attribute.Focusable, state.toggle().toBoolean()); + refresh(); return; } } @@ -783,9 +792,9 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { case SWT.FocusOut: if(layer instanceof IEditableLayer) { IEditableLayer l = (IEditableLayer)layer; - l.setName(text.getText()); - System.out.println("renamed layer to " + text.getText()); //$NON-NLS-1$ - //viewer.refresh(); + String name = findFreshName(layers, text.getText()); + l.setName(name); + viewer.refresh(); } // // Item may be disposed if the tree gets reset after a previous editing. @@ -831,7 +840,9 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { case SWT.TRAVERSE_RETURN: if(layer instanceof IEditableLayer) { IEditableLayer l = (IEditableLayer)layer; - l.setName(text.getText()); + String name = findFreshName(layers, text.getText()); + l.setName(name); + viewer.refresh(); //System.out.println("renamed layer to " + text.getText()); //viewer.refresh(); }