Combination of Simantics-platform related changes and fixes for district 47/747/5
authorjsimomaa <jani.simomaa@gmail.com>
Fri, 5 May 2017 04:31:32 +0000 (07:31 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Wed, 26 Jul 2017 04:18:20 +0000 (07:18 +0300)
Some platform changes for district

Change-Id: Ib7aa44432f3e468ca11542debf33191b8403c742

Some more fixes to platform for district

Change-Id: Ifd718c20d5a98806e93c365b45ce3bdd003b4118

13 files changed:
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java
bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java
bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementPainter.java
bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/AWTImage.java
bundles/org.simantics.g2d/src/org/simantics/g2d/participant/RulerPainter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/ImportSVGPNG.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewer.java
bundles/org.simantics.modeling/scl/Simantics/Diagram.scl
bundles/org.simantics.modeling/src/org/simantics/modeling/svg/CreateSVGElement.java
bundles/org.simantics.platform.ui.ontology/graph/PlatformUIViews.pgraph
bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ImageNode.java
bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java

index a6f2e0a77f4f354c146f98d52e38072a0f1cb7fd..198b4b341114616aa75137b7a719f8162ccaf40d 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/
 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;
@@ -20,6 +22,7 @@ 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;
 import org.slf4j.Logger;
@@ -110,8 +113,12 @@ public class NodeRequest extends BaseRequest2<Resource, IElement> {
                             }
 
                             @Override
-                            public void execute(AsyncReadGraph graph, final ElementClass clazz) {
-
+                            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 clazz = mutableClazz;
                                 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
 
                                     @Override
index 41947ef993c49bc937b39eedddb0a0cbf3d25460..e92ce1b41da779df96b1d08359241a7b959fa6b1 100644 (file)
@@ -433,8 +433,10 @@ public class GraphLayerManager {
                 graph.forHasStatement(element, gl.getVisible(), element, new AsyncProcedureAdapter<Boolean>() {
                     @Override
                     public void execute(AsyncReadGraph graph, Boolean result) {
-                        synchronized (visible) {
-                            visible.add(l);
+                        if (result) {
+                            synchronized (visible) {
+                                visible.add(l);
+                            }
                         }
                         if (DEBUG_LAYERS)
                             System.out.println("    Visible on layer '" + gl.getName() + "'");
@@ -452,8 +454,10 @@ public class GraphLayerManager {
                 graph.forHasStatement(element, gl.getFocusable(), element, new AsyncProcedureAdapter<Boolean>() {
                     @Override
                     public void execute(AsyncReadGraph graph, Boolean result) {
-                        synchronized (focusable) {
-                            focusable.add(l);
+                        if (result) {
+                            synchronized (focusable) {
+                                focusable.add(l);
+                            }
                         }
                         if (DEBUG_LAYERS)
                             System.out.println("    Focusable on layer '" + gl.getName() + "'");
index d69ef69c69fadec257a3e07e2fc31a600cd5ed15..eb9e33607d5500197cc7c4fd8d0c061036d835c3 100644 (file)
@@ -1012,8 +1012,9 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
         Shape shape = ElementUtils.getElementShapeOrBounds(e);
         Rectangle2D bounds = shape.getBounds2D();
         //System.out.println("selection bounds: "+bounds);
-        final double margin = 1;
-        bounds.setFrame(bounds.getMinX() - margin, bounds.getMinY() - margin, bounds.getWidth() + 2*margin, bounds.getHeight() + 2*margin);
+        final double marginX = 1 / selectionTransform.getScaleX();
+        final double marginY = 1 / selectionTransform.getScaleY();
+        bounds.setFrame(bounds.getMinX() - marginX, bounds.getMinY() - marginY, bounds.getWidth() + 2*marginX, bounds.getHeight() + 2*marginY);
 
         List<SelectionSpecification> ss = e.getElementClass().getItemsByClass(SelectionSpecification.class);
         if (!ss.isEmpty()) {
index e547a663e8e8844024f110485ee85ba81d7ee302..0fbe895be551d6a75650db1decf5f5651ad56db1 100644 (file)
@@ -34,13 +34,19 @@ public class AWTImage extends AbstractImage implements Image {
 
     BufferedImage bi;
     Rectangle2D rect;
+    float alpha;
 
-    public AWTImage(BufferedImage bi) {
+    public AWTImage(BufferedImage bi, float alpha) {
         assert(bi!=null);
         this.bi = bi;
+        this.alpha = alpha;
         rect = new Rectangle2D.Double(bi.getMinX(),bi.getMinY(),bi.getWidth(), bi.getHeight());
     }
 
+    public AWTImage(BufferedImage bi) {
+        this(bi, 1.0f);
+    }
+
     @Override
     public Rectangle2D getBounds() {
         return rect;
@@ -55,6 +61,8 @@ public class AWTImage extends AbstractImage implements Image {
     public Node init(G2DParentNode parent) {
         ImageNode node = parent.getOrCreateNode("image", ImageNode.class);
         node.setImage(bi);
+        node.setAlpha(alpha);
+        node.setZIndex(-100);
         return node;
     }
 
index e4f90791e2123b8fa1c01bf1566ae82de90ac0f7..bffe0c9df4862a160f95ac0e61610dffb1d2a1e1 100644 (file)
@@ -102,17 +102,21 @@ public class RulerPainter extends AbstractCanvasParticipant {
 
     @SGInit
     public void initSG(G2DParentNode parent) {
-        node = parent.addNode("ruler", RulerNode.class);
+        node = parent.addNode("ruler", getNodeClass());
         node.setZIndex(PAINT_PRIORITY);
         updateNode();
     }
 
+    protected Class<? extends RulerNode> getNodeClass() {
+        return RulerNode.class;
+    }
+
     @SGCleanup
     public void cleanupSG() {
         node.remove();
     }
 
-    void updateNode() {
+    protected void updateNode() {
         node.setEnabled(isPaintingEnabled());
         node.setGridSize(getGridSize());
     }
index 931bfdf3d60d073c0b63c271dbda2c163f060c41..b864c4bcf56b3eff3b9d5a30e7338d5d60645018 100644 (file)
@@ -36,7 +36,12 @@ import org.simantics.db.common.request.IndexRoot;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.request.Read;
+import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
+import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
 import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
 import org.simantics.g2d.participant.MouseUtil;
 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
 import org.simantics.modeling.ModelingResources;
@@ -112,10 +117,12 @@ public class ImportSVGPNG {
         IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
         Resource composite = input.getResource();
 
-        addSVG(mpos.getX(), mpos.getY(), composite);
+        IDiagram idiagram = viewer.getAdapter(IDiagram.class);
+
+        addSVG(mpos.getX(), mpos.getY(), composite, idiagram);
     }
 
-    public static void addSVG(final double mposX, final double mposY, final Resource composite) {
+    public static void addSVG(final double mposX, final double mposY, final Resource composite, IDiagram idiagram) {
 
         Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
 
@@ -135,9 +142,20 @@ public class ImportSVGPNG {
 
                 @Override
                 public void perform(WriteGraph g) throws DatabaseException {
-                    Commands.get(g, "Simantics/Diagram/createSVGElement")
+                    Object svg = Commands.get(g, "Simantics/Diagram/createSVGElementR")
                             .execute(g, g.syncRequest(new IndexRoot(composite)),
                                      composite, suffix(filename), data, mposX, mposY);
+
+                    if (svg != null && svg instanceof Resource) {
+                        Resource resource = (Resource) svg;
+                        // 7. Put the element on all the currently active layers if possible.
+                        IModifiableSynchronizationContext context = idiagram.getHint(SynchronizationHints.CONTEXT);
+                        GraphLayerManager glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
+                        if (glm != null) {
+                            glm.removeFromAllLayers(g, resource);
+                            glm.putElementOnVisibleLayers(idiagram, g, resource);
+                        }
+                    }
                 }
 
             });
index b5381917d4b8acd3dd2e03f571b8a334d273e6de..1762dea7f6108d34b0a265731387adc9325cb002 100644 (file)
@@ -727,9 +727,7 @@ public class DiagramViewer
         addKeyBindingParticipants(ctx);
 
         // Grid & Ruler & Background
-        ctx.add(new GridPainter());
-        ctx.add(new RulerPainter());
-        ctx.add(new BackgroundPainter());
+        addGridRulerBackgroundParticipants(ctx);
 
         h.setHint(Hints.KEY_DISPLAY_PAGE, diagramPreferences.get(DiagramPreferences.P_DISPLAY_PAGE_SIZE));
         h.setHint(Hints.KEY_DISPLAY_MARGINS, diagramPreferences.get(DiagramPreferences.P_DISPLAY_MARGINS));
@@ -776,6 +774,12 @@ public class DiagramViewer
         ctx.setLocked(false);
     }
 
+    protected void addGridRulerBackgroundParticipants(CanvasContext ctx) {
+        ctx.add(new GridPainter());
+        ctx.add(new RulerPainter());
+        ctx.add(new BackgroundPainter());
+    }
+
     protected void loadPageSettings(ICanvasContext ctx) {
         DiagramDesc diagramDesc = null;
 
index bf5c589e85f8f2a248d55a71c5244bc1332b2fae..554a25edad081a50f4388688d19820b359440942 100644 (file)
@@ -805,8 +805,10 @@ setTransform element transform = claimRelatedValueWithType element DIA.HasTransf
     
 importJava "org.simantics.modeling.svg.CreateSVGElement" where
     createSVGElement :: Resource -> String -> ByteArray -> Double -> Double -> <WriteGraph> ()
+    createSVGElementR :: Resource -> String -> ByteArray -> Double -> Double -> <WriteGraph> Resource
     
     importSVGElement :: Resource -> File -> Double -> Double -> <WriteGraph> ()
+    importSVGElementR :: Resource -> File -> Double -> Double -> <WriteGraph> Resource
     
 importJava "org.simantics.diagram.synchronization.graph.RemoveElement" where
     removeElement :: Resource -> Resource -> <WriteGraph> ()
index 994d417c99b815ba4c3deae54511777a84e8fc05..1f302a64db4c2b7262339366bdb438123e7b3d11 100644 (file)
@@ -19,6 +19,10 @@ import org.simantics.utils.FileUtils;
 public class CreateSVGElement {
 
     public static void createSVGElement(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException {
+        createSVGElement(g, diagram, suffix, data, mposX, mposY);
+    }
+
+    public static Resource createSVGElementR(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException {
 
         Layer0 L0 = Layer0.getInstance(g);
         DiagramResource DIA = DiagramResource.getInstance(g);
@@ -50,19 +54,22 @@ public class CreateSVGElement {
             throw new DatabaseException("Unknown image format " + suffix);
         OrderedSetUtils.addFirst(g, diagram, element);
         g.claim(diagram, L0.ConsistsOf, element);
-
+        return element;
     }
-    
+
     public static void importSVGElement(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException {
-        
+        importSVGElementR(graph, diagram, file, posX, posY);
+    }
+
+    public static Resource importSVGElementR(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException {
         final byte[] data = FileUtils.readFile(file);
-        createSVGElement(graph, diagram, suffix(file.getName()), data, posX, posY);
+        return createSVGElementR(graph, diagram, suffix(file.getName()), data, posX, posY);
     }
-    
+
     private static String suffix(String fileName) {
         int len = fileName.length();
         if(len < 3) return null;
         else return fileName.substring(len-3,len).toLowerCase();
     }
-    
+
 }
index a265df8b136b86e9711d84843e5b8417d3e9cba3..eeab91d816030948d4c98212a87b9dcca30915eb 100644 (file)
@@ -101,7 +101,7 @@ VIEWS.SharedLibraryContribution : SWT.TypedVariableTabContribution
 
 //VIEWS.SharedLibraryContribution2 : SWT.TypedVariableTabContribution
 //    SEL.AbstractVariableTabContribution.HasPriority 1
-//    SEL.AbstractTypedVariableTabContribution.HasType L0.SharedOntology
+//    SEL.AbstractTypedTabContribution.HasType L0.SharedOntology
 //    SWT.TypedVariableTabContribution.HasView SharedLibraries
 //    L0.HasLabel "Shared Libraries"
 
index df35e968ef41501bd537425e97a513759719d747..43cf64eb0a2159f9d1fce6f12b4ed6351dfe6dd3 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/
 package org.simantics.scenegraph.g2d.nodes;
 
+import java.awt.AlphaComposite;
+import java.awt.Composite;
 import java.awt.Graphics2D;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
@@ -28,6 +30,7 @@ public class ImageNode extends G2DNode {
 
     protected Boolean visible = Boolean.TRUE;
     protected BufferedImage img = null;
+    protected float alpha = 1.0f;
 
     @SyncField("visible")
     public void setVisible(Boolean visible) {
@@ -50,6 +53,10 @@ public class ImageNode extends G2DNode {
         img = src;
     }
 
+    public void setAlpha(float alpha) {
+        this.alpha = Math.max(0.0f, Math.min(alpha, 1.0f));
+    }
+
     @Override
     public void render(Graphics2D g) {
         if (!visible || img == null) return;
@@ -65,8 +72,17 @@ public class ImageNode extends G2DNode {
 //            Rectangle2D b = parent.getBoundsInLocal();
 //            g.drawImage(img, (int)b.getMinX(), (int)b.getMinY(), (int)b.getWidth()+(int)b.getMinX(), (int)b.getHeight()+(int)b.getMinY(), 0, 0, img.getWidth(), img.getHeight(), null);
 //        }
+
+        Composite old = null;
+        if (alpha < 1.0f) {
+            old = g.getComposite();
+            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
+        }
+
         g.drawImage(img, 0, 0, null);
 
+        if (old != null)
+            g.setComposite(old);
         if (ot != null)
             g.setTransform(ot);
     }
index f7e0d1e5c6b5cb05d280b9a5ae514758723b2370..27c1c1fd2bae4c1d2469ea24af1d0259647b152c 100644 (file)
@@ -57,7 +57,7 @@ public class RulerNode extends G2DNode {
     public void render(Graphics2D g) {
         if (!enabled)
             return;
-
+        
         AffineTransform tr = g.getTransform();
         double scaleX = Math.abs(tr.getScaleX());
         double scaleY = Math.abs(tr.getScaleY());
@@ -109,7 +109,9 @@ public class RulerNode extends G2DNode {
         // Vertical ruler
         for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) {
             if(x > 20) {
-                String str = formatValue((x-offsetX)/scaleX);
+                double val = (x-offsetX)/scaleX / getTransform().getScaleX();
+                double modifiedValue = modifyHorizontalValue(val);
+                String str = formatValue(modifiedValue);
                 FontMetrics fm = g.getFontMetrics();
                 Rectangle2D r = fm.getStringBounds(str, g);
                 if((x-r.getWidth()/2) > previousText) {
@@ -140,10 +142,9 @@ public class RulerNode extends G2DNode {
         previousText = -100;
         for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) {
             if(y > 20) {
-                double val = (y-offsetY)/scaleY;
-                if (MAP_Y_SCALING)
-                    val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
-                String str = formatValue(val);
+                double val = (y-offsetY)/scaleY / getTransform().getScaleY();
+                double modifiedValue = modifyVerticalValue(val);
+                String str = formatValue(modifiedValue);
                 FontMetrics fm = g.getFontMetrics();
                 Rectangle2D r = fm.getStringBounds(str, g);
                 if(y-1+r.getHeight()/2 > previousText) {
@@ -175,6 +176,26 @@ public class RulerNode extends G2DNode {
         g.setTransform(tr);
     }
 
+    /**
+     * A method for subclasses to alter the actual X-value of the ruler 
+     * 
+     * @param value
+     * @return possibly modified X-value 
+     */
+    protected double modifyHorizontalValue(double value) {
+        return value;
+    }
+
+    /**
+     * A method for subclasses to alter the actual Y-value of the ruler 
+     * 
+     * @param value
+     * @return possibly modified Y-value 
+     */
+    protected double modifyVerticalValue(double value) {
+        return value;
+    }
+
     private static final transient int    MAX_DIGITS = 5;
     private static final transient double EPSILON    = 0.01;
     private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 4);
index 3c1888a0112c2c2f382e82c05915372ce51d440d..6d7daeb3c6ff191f8e04e444240bf99776f699d3 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Map;
 
 import org.eclipse.e4.core.contexts.IEclipseContext;
 import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.commands.MCommand;
 import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
@@ -14,9 +15,12 @@ import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
 import org.eclipse.e4.ui.workbench.modeling.EModelService;
 import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
+import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
 import org.simantics.db.Resource;
 import org.simantics.utils.datastructures.ArrayMap;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
 
 /**
  * @author Tuukka Lehtonen
@@ -166,5 +170,32 @@ public class E4WorkbenchUtils {
             part = partService.createPart(partId);
         return part;
     }
+    
+    public static MCommand getMCommandById(String id) {
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        MApplication application = context.get(MApplication.class);
+        for (MCommand command : application.getCommands()) {
+            if (id.equals(command.getElementId())) {
+                return command;
+            }
+        }
+        return null;
+    }
 
+    @SuppressWarnings("restriction")
+    public static IEditorPart getActiveIEditorPart(MPart mActiveEditorPart) {
+        // TODO: Fix this when we get rid of CompatibilityEditors
+        IEditorPart activeEditor = null;
+        if (mActiveEditorPart != null) {
+            Object editor = mActiveEditorPart.getObject();
+            if (editor instanceof CompatibilityEditor) {
+                CompatibilityEditor compEditor = (CompatibilityEditor) editor;
+                activeEditor = compEditor.getEditor();
+            } else {
+                // TODO: This is not good practice with E4 but an OK fallback for now
+                activeEditor = WorkbenchUtils.getActiveEditor();
+            }
+        }
+        return activeEditor;
+    }
 }