]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Even more fixes to layers
authorJussi Koskela <jussi.koskela@semantum.fi>
Thu, 25 Jun 2020 06:34:57 +0000 (09:34 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 29 Jul 2020 15:16:14 +0000 (18:16 +0300)
Change-Id: Ie638616341f3ecdaf83de0611d64152358e04a57

bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java
bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java
bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/DiagramHints.java
bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayerListener.java
bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/ElementLayers.java
bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/SimpleElementLayers.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java

index af2a8da33f49471fba8ec2ca8d52f87be05989ad..baab13a7e9eaedd8a21ab2b7a4324e096b09f052 100644 (file)
@@ -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<Resource, IElement> {
                             }
 
                             @Override
-                            public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
-
-                                List<SubstituteElementClass> 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<IElement>() {
 
                                     @Override
index a19bc3dad141921dae5a46e67277e611406c672a..36a01b54c641a0e1a6a67dac6dd2fbec481aad7e 100644 (file)
@@ -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<Resource, Tuple3> {
                             }
 
                             @Override
-                            public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
-
-                                List<SubstituteElementClass> 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<IElement>() {
 
index 14b9f148a1baa1d8f5fe1ab320d40e0370ccc202..a1fa5bee59dd6d609c6d2eaf1846e82346c3406c 100644 (file)
@@ -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<ElementHandler> all = ec.getAll();
-            List<ElementHandler> result = new ArrayList<ElementHandler>(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);
index 79e79a2174033c7cf67fe3b2a21f4eac294b0bdd..db02034e8115e76ebc27431e18b59ec9335bcd8d 100644 (file)
@@ -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<ILayer>());
-            element.setHint(ElementHints.KEY_FOCUS_LAYERS, new HashSet<ILayer>());
-            Set<ILayer> 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.
index 0c11d348ab62fcc3860437b1e8a72f779f5349d5..9b36d845f9673d27868ff7c03e01f1664d30c620 100644 (file)
@@ -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");
index 98dd9610e9f20b6adc2fb4a9a68b35de6392d8d3..e14bba9dcfbabd2e65e81cdbe8dffa92f16e1186 100644 (file)
@@ -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();
 
 }
index 9edf28d4128ec768fa4b1d88963905163a0babb7..60cdf395d887ae1c4f69df3e1ccf04b4f3606c5e 100644 (file)
@@ -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);
 
 }
index d62e85b800f3ea77233d2e91f22d7797b5df5755..622e5322eb19baba8b6ddfa580b336b5d6c6fb4b 100644 (file)
@@ -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<ILayer> elementLayers = (Set<ILayer>) 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<ILayer> elementLayers = (Set<ILayer>) 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;
-    }
-
 }
index 87e18ed8e3a9b7818edb33bed324e6cbe58b7dc2..3bce16ccb99da72f42c7a4fd0197c90e2cc676dd 100644 (file)
@@ -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<IElement> 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<IElement> elements, ILayer layer, Attribute attribute, boolean value) {
-        int result = 0;
-        for (IElement e : elements) {
-            if (setAttribute(e, layer, attribute, value))
-                ++result;
+    void setAttribute(Collection<IElement> 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<ILayer> elementLayers = (Set<ILayer>) 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<ILayer> elementLayers = (Set<ILayer>) 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<ILayer> 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<ILayer> 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();
                                 }