]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
bugfixes in objmap, annotating missing expressions in diagram
authorniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 29 Jan 2010 14:06:26 +0000 (14:06 +0000)
committerniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 29 Jan 2010 14:06:26 +0000 (14:06 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13775 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.h2d/src/org/simantics/h2d/node/TextNode.java
org.simantics.objmap/src/org/simantics/objmap/annotations/UpdateMethod.java [new file with mode: 0644]
org.simantics.objmap/src/org/simantics/objmap/annotations/factories/UpdateMethodFactory.java [new file with mode: 0644]
org.simantics.objmap/src/org/simantics/objmap/impl/Mapping.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/AuxiliaryElement.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockElement.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java

index 559e6e4d161ac1a1093aabf9f95d26498f6399dc..6c359cf9c5258a4799c75f6d16f2368f4e765ba5 100644 (file)
@@ -12,7 +12,6 @@ import java.awt.datatransfer.Transferable;
 import java.awt.event.KeyEvent;\r
 import java.awt.event.MouseEvent;\r
 import java.awt.font.FontRenderContext;\r
-import java.awt.geom.AffineTransform;\r
 import java.awt.geom.Line2D;\r
 import java.awt.geom.Rectangle2D;\r
 \r
@@ -61,6 +60,11 @@ public class TextNode extends G2DNode {
         this.scale = scale;\r
     }\r
     \r
+    @SyncField({"color"})\r
+    public void setColor(Color color) {\r
+        this.color = color;\r
+    }\r
+    \r
     public String getText() {\r
        return text;\r
     }\r
diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/UpdateMethod.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/UpdateMethod.java
new file mode 100644 (file)
index 0000000..e15a0dc
--- /dev/null
@@ -0,0 +1,20 @@
+package org.simantics.objmap.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+import org.simantics.objmap.annotations.factories.UpdateMethodFactory;\r
+import org.simantics.objmap.annotations.meta.HasMethodRuleFactory;\r
+\r
+/**\r
+ * Specifies that the annotated method should be called\r
+ * to update range object.\r
+ * @author Hannu Niemistö\r
+ */\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+@HasMethodRuleFactory(UpdateMethodFactory.class)\r
+public @interface UpdateMethod {\r
+}\r
diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/UpdateMethodFactory.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/UpdateMethodFactory.java
new file mode 100644 (file)
index 0000000..77c2e30
--- /dev/null
@@ -0,0 +1,51 @@
+package org.simantics.objmap.annotations.factories;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Method;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.IFunction;\r
+import org.simantics.objmap.IMappingRule;\r
+import org.simantics.objmap.MappingException;\r
+import org.simantics.objmap.rules.factory.IMethodRuleFactory;\r
+\r
+public class UpdateMethodFactory implements IMethodRuleFactory {\r
+\r
+    static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
+    \r
+    @Override\r
+    public IMappingRule create(ReadGraph g, \r
+            Annotation annotation, \r
+            final Method method)\r
+            throws DatabaseException {\r
+        method.setAccessible(true);\r
+        return new IMappingRule() {\r
+            \r
+            @Override\r
+            public boolean updateRange(ReadGraph g, IFunction<Resource, Object> map,\r
+                    Resource domainElement, Object rangeElement)\r
+                    throws MappingException {\r
+                LOGGER.info("    UpdateMethodFactory.updateRange");      \r
+                try {\r
+                    return (Boolean)method.invoke(rangeElement, g, domainElement);\r
+                } catch (Exception e) {\r
+                    // TODO Auto-generated catch block\r
+                    e.printStackTrace();\r
+                }\r
+                return false;\r
+            }\r
+            \r
+            @Override\r
+            public boolean updateDomain(WriteGraph g, IFunction<Object, Resource> map,\r
+                    Resource domainElement, Object rangeElement)\r
+                    throws MappingException {\r
+                return false;\r
+            }\r
+        };\r
+    }\r
+\r
+}\r
index fb31e2c6824b68358e7a5bcdfe08b13b14b99a64..ef63054749d4222b3f31e05dcb17a061fd8a7d11 100644 (file)
@@ -59,12 +59,16 @@ public class Mapping implements IMapping {
        }\r
        \r
        private void createDomain(WriteGraph g, Link link) throws MappingException {\r
+           LOGGER.info("        createDomain for " + link.rangeElement);\r
                ILinkType type = schema.linkTypeOfRangeElement(link.rangeElement);\r
                Resource domainElement = type.createDomainElement(g, link.rangeElement);\r
                \r
                link.type = type;\r
                link.domainElement = domainElement;\r
                domain.put(domainElement, link);\r
+               \r
+        // TODO Should we do this only if the mapping is listening?\r
+        domainModified(link);\r
        }\r
        \r
        private void createRange(ReadGraph g, Link link) throws MappingException {\r
@@ -164,22 +168,15 @@ public class Mapping implements IMapping {
                public Object get(Resource element) throws MappingException {\r
                        Link link = domain.get(element);\r
                        if(link == null) {\r
-                               ILinkType type = schema.linkTypeOfDomainElement(g, element);\r
-                               Object rangeElement = type.createRangeElement(g, element);\r
-                               \r
-                               link = new Link(type, element, rangeElement);\r
-                               domain.put(element, link);                      \r
-                               range.put(rangeElement, link);\r
-                               link.domainModified = true;\r
-                               modifiedDomainLinks.add(link);\r
-                               \r
-                               return rangeElement;                            \r
-                       }\r
-                       else {\r
-                               if(link.type == null) \r
-                                       createRange(g, link);\r
-                               return link.rangeElement;\r
+                           link = new Link(null, element, null);\r
+                   link.domainModified = true;\r
+                   modifiedDomainLinks.add(link);\r
+                           domain.put(element, link);           \r
+                           createRange(g, link);       \r
                        }\r
+                       else if(link.type == null) \r
+                               createRange(g, link);\r
+            return link.rangeElement;\r
                }\r
                \r
        };\r
@@ -196,22 +193,15 @@ public class Mapping implements IMapping {
                public Resource get(Object element) throws MappingException {\r
                        Link link = range.get(element);\r
                        if(link == null) {\r
-                               ILinkType type = schema.linkTypeOfRangeElement(element); \r
-                               Resource domainElement = type.createDomainElement(g, element);\r
-                               \r
-                               link = new Link(type, domainElement, element);\r
-                               domain.put(domainElement, link);                        \r
-                               range.put(element, link);\r
-                               link.rangeModified = true;\r
-                               modifiedRangeLinks.add(link);\r
-                               \r
-                               return domainElement;                           \r
-                       }\r
-                       else {\r
-                               if(link.type == null)\r
-                                       createDomain(g, link);\r
-                               return link.domainElement;\r
+                           link = new Link(null, null, element);\r
+                           link.rangeModified = true;\r
+                modifiedRangeLinks.add(link);\r
+                           range.put(element, link);\r
+                           createDomain(g, link);                              \r
                        }\r
+                       else if(link.type == null)\r
+                               createDomain(g, link);\r
+                       return link.domainElement;\r
                }\r
                \r
        };\r
@@ -227,7 +217,7 @@ public class Mapping implements IMapping {
        }\r
        \r
        @Override\r
-       public Collection<Resource> updateDomain(WriteGraph g) throws MappingException {\r
+       public synchronized Collection<Resource> updateDomain(WriteGraph g) throws MappingException {\r
            LOGGER.info("Mapping.updateDomain");\r
                RangeToDomain map = new RangeToDomain(g);\r
                ArrayList<Resource> updated = new ArrayList<Resource>();\r
@@ -236,10 +226,10 @@ public class Mapping implements IMapping {
                    \r
                        Link link = modifiedRangeLinks.remove(modifiedRangeLinks.size()-1);\r
                        link.rangeModified = false;\r
-                       if(link.domainModified) {\r
+                       /*if(link.domainModified) {\r
                                link.domainModified = false;\r
                                modifiedDomainLinks.remove(link);\r
-                       }\r
+                       }*/\r
                        \r
                        if(link.type == null) {\r
                                createDomain(g, link);\r
@@ -252,17 +242,19 @@ public class Mapping implements IMapping {
        }\r
        \r
        @Override\r
-       public Collection<Object> updateRange(ReadGraph g) throws MappingException {\r
+       public synchronized Collection<Object> updateRange(ReadGraph g) throws MappingException {\r
            LOGGER.info("Mapping.updateRange");\r
                DomainToRange map = new DomainToRange(g);\r
                ArrayList<Object> updated = new ArrayList<Object>();\r
-               while(!modifiedDomainLinks.isEmpty()) {\r
+               while(!modifiedDomainLinks.isEmpty()) {             \r
+                   LOGGER.info("    modifiedDomainLinks.size() = " + modifiedDomainLinks.size());\r
+                   \r
                        Link link = modifiedDomainLinks.remove(modifiedDomainLinks.size()-1);\r
                        link.domainModified = false;\r
-                       if(link.rangeModified) {\r
+                       /*if(link.rangeModified) {\r
                                link.rangeModified = false;\r
                                modifiedRangeLinks.remove(link);\r
-                       }\r
+                       }*/\r
                        \r
                        if(link.type == null) {\r
                                createRange(g, link);\r
@@ -312,13 +304,16 @@ public class Mapping implements IMapping {
        }\r
 \r
        void domainModified(Link link) {\r
-           if(!link.domainModified) {\r
-            link.domainModified = true;\r
-            modifiedDomainLinks.add(link);\r
-            if(modifiedDomainLinks.size() == 1) {\r
-                for(IMappingListener listener : listeners)\r
-                    listener.domainModified();\r
-            }\r
+           if(!link.domainModified) {          \r
+               synchronized(modifiedDomainLinks) {\r
+                   LOGGER.info("        domainModified for " + link.rangeElement);\r
+                link.domainModified = true;\r
+                modifiedDomainLinks.add(link);\r
+                if(modifiedDomainLinks.size() == 1) {\r
+                    for(IMappingListener listener : listeners)\r
+                        listener.domainModified();\r
+                }\r
+               }\r
         }\r
        }\r
        \r
@@ -331,12 +326,14 @@ public class Mapping implements IMapping {
 \r
        void rangeModified(Link link) {\r
            if(!link.rangeModified) {\r
-            link.rangeModified = true;\r
-            modifiedRangeLinks.add(link);\r
-            if(modifiedRangeLinks.size() == 1) {\r
-                for(IMappingListener listener : listeners)\r
-                    listener.rangeModified();\r
-            }\r
+               synchronized(modifiedRangeLinks) {\r
+                link.rangeModified = true;\r
+                modifiedRangeLinks.add(link);\r
+                if(modifiedRangeLinks.size() == 1) {\r
+                    for(IMappingListener listener : listeners)\r
+                        listener.rangeModified();\r
+                }\r
+               }\r
         }\r
        }\r
        \r
index 813be9ad5fd661afe57ec2d0eb1507946753b8ae..fee183cb1946a6f2b62439329a9f958282367842 100644 (file)
@@ -78,7 +78,7 @@ public class SysdynDiagramEditor extends ResourceEditorPart {
                mapping.addMappingListener(new IMappingListener() {\r
                        \r
                        @Override\r
-                       public void rangeModified() {\r
+                       public void rangeModified() {                       \r
                                session.asyncRequest(new WriteRequest() {\r
                                        \r
                                        @Override\r
index e10f97f6a2ea75cad7eded6395a4f89a4c2a5caa..e143b7a54a82de1f7cc4df8fe4ad69bdef65960a 100644 (file)
@@ -4,12 +4,12 @@ import org.simantics.objmap.annotations.GraphType;
 \r
 @GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
 public class AuxiliaryElement extends TextElement {\r
-       \r
+        \r
        public AuxiliaryElement() {     \r
        }\r
        \r
        public AuxiliaryElement(String label, double x, double y) {\r
            super(label, x, y);\r
-       }\r
+       }       \r
        \r
 }\r
index 01c6692766ba8302d9c7ce57f90cc0a16e845039..a171f60c196c030df83bfdc9d91a4bba64e811b1 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.sysdyn.ui.elements;
 \r
 import org.simantics.h2d.node.RectangleNode;\r
 import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.UpdateMethod;\r
 import org.simantics.scenegraph.g2d.G2DParentNode;\r
 \r
 @GraphType("http://www.simantics.org/Sysdyn#Stock")\r
index 860091f8f2ae0e15f48bb8587af72dca703205a6..53d32ab2bc3c921a616a62942338155a881787d8 100644 (file)
@@ -6,6 +6,9 @@ import java.awt.font.FontRenderContext;
 import java.awt.geom.AffineTransform;\r
 import java.awt.geom.Rectangle2D;\r
 \r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.h2d.action.IAction;\r
 import org.simantics.h2d.editor.IDiagramEditor;\r
 import org.simantics.h2d.element.handler.Connectable;\r
@@ -18,7 +21,9 @@ import org.simantics.h2d.node.ITextListener;
 import org.simantics.h2d.node.TextNode;\r
 import org.simantics.objmap.annotations.GraphType;\r
 import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.objmap.annotations.UpdateMethod;\r
 import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.sysdyn.SysdynResource;\r
 \r
 @GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
 public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler {\r
@@ -91,8 +96,11 @@ public class TextElement extends RectangularElement implements Movable, Connecta
                 textBounds.getHeight()+YPADDING*2\r
                 );      \r
     \r
-        if(textNode != null)\r
-            textNode.init(label, FONT, Color.BLACK, textX, textY, FONT_SCALE);\r
+        if(textNode != null) {\r
+            textNode.init(label, FONT, hasEquation ? Color.BLACK : Color.RED, \r
+                    textX, textY, FONT_SCALE);\r
+            oldHasEquation = hasEquation;\r
+        }\r
        }\r
        \r
        protected void update() {\r
@@ -170,5 +178,30 @@ public class TextElement extends RectangularElement implements Movable, Connecta
         }\r
         return false;\r
     }  \r
+\r
+       boolean oldHasEquation = false;\r
+       boolean hasEquation = false;\r
        \r
+       @UpdateMethod\r
+    boolean readHasExpression(ReadGraph g, Resource resource) {\r
+        try {\r
+            System.out.println("readHasExpression " +\r
+                    g.getRelatedValue(resource, \r
+                            g.getBuiltins().HasName));\r
+        } catch (DatabaseException e1) {\r
+            // TODO Auto-generated catch block\r
+            e1.printStackTrace();\r
+        }\r
+        SysdynResource sr = SysdynResource.getInstance(g);\r
+        try {\r
+            hasEquation = \r
+                !g.getObjects(resource, sr.HasExpression).isEmpty();\r
+            if(oldHasEquation != hasEquation) \r
+                update();            \r
+        } catch (DatabaseException e) {\r
+            // TODO Auto-generated catch block\r
+            e.printStackTrace();\r
+        }       \r
+        return false;\r
+    }\r
 }\r
index 02d7920047ac24a9b8a3e686c9cc9c2c8e210099..40774e988bd2278f948958d75c1c841a0519dca7 100644 (file)
@@ -112,7 +112,6 @@ public class TrendView extends ViewPart {
             \r
             @Override\r
             public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
-                System.out.println(PinTrend.getState());\r
                 if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState()))\r
                     return;\r
                 if(selection instanceof IStructuredSelection) {                    \r