]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Added name validation for other elements than variable, module, and enumeration....
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 7 Mar 2013 11:32:41 +0000 (11:32 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 7 Mar 2013 11:32:41 +0000 (11:32 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27026 ac1ea38d-2e2b-0410-8846-a27921b304fc

41 files changed:
org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/EnumerationNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionLibraryNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModelNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleTypeNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SharedFunctionLibraryNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SheetNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/VariableNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynCopyPasteStrategy.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/ModelicaSourceViewerConfiguration.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionLibraryTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTypeTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/NameAndArrayRangeModifyListener.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/Function.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/FunctionLibraryNameInputValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/FunctionNameInputValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/ModelNameInputValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/ModuleTypeNameInputValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameInputValidator.java [moved from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java with 58% similarity]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNamePropertyModifier.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/FunctionLibraryNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/FunctionNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModuleTypeNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SharedFunctionLibraryNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SheetNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameUtils.java [deleted file]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java

index c5c6a38c3610525f4bacd6276a986027352d9d8b..d3a9e321d6a24724116668c8fc0dd1918e6782a6 100644 (file)
                         args="org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode"\r
                         property="org.simantics.sysdyn.ui.nodeClass">\r
                   </test>\r
+                  <test\r
+                        args="org.simantics.sysdyn.ui.browser.nodes.SheetNode"\r
+                        property="org.simantics.sysdyn.ui.nodeClass">\r
+                  </test>\r
+                  <test\r
+                        args="org.simantics.sysdyn.ui.browser.nodes.BarChartNode"\r
+                        property="org.simantics.sysdyn.ui.nodeClass">\r
+                  </test>\r
                   <!--\r
                   <test\r
                         args="org.simantics.document.workbench.browser_old.DocumentationNode"\r
index beaf98a304226774ce44dc5474e316a99d131b9e..cc3283efacf4911f5de5ffd2f38ff00a7d0f4162 100644 (file)
@@ -16,7 +16,7 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.ui.ExceptionUtils;\r
 \r
-public class EnumerationNode extends VariableNode<Resource> implements IDeletableNode {\r
+public class EnumerationNode extends VariableNode<Resource> implements /*IModifiableNode,*/ IDeletableNode {\r
 \r
        public EnumerationNode(Resource resource) {\r
                super(resource);\r
@@ -25,6 +25,22 @@ public class EnumerationNode extends VariableNode<Resource> implements IDeletabl
        public EnumerationNode(Variable variable, Resource represents) {\r
         super(variable, represents);\r
        }\r
+    /*\r
+    @Override\r
+    public Modifier getModifier(String columnId) {\r
+        \r
+        Session session = SimanticsUI.getSession();\r
+        LabelModifier modifier = new LabelModifier(session, data, session.getService(Layer0.class).HasName) {\r
+            @Override\r
+            public String isValid(String label) {\r
+                if (!new VariableNameValidator().isValid(data, label))\r
+                    return "Not valid";\r
+                else\r
+                    return null;\r
+            }\r
+        };\r
+        return modifier;\r
+    }*/\r
 \r
        @Override\r
        public void delete() throws DeleteException {\r
index 52ded0024a575f8baa93e032ce47a05e25794557..00a3dcb5ff87d2240f7211993b6f8c0b798bc00e 100644 (file)
@@ -25,6 +25,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.RemoverUtil;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.FunctionLibraryNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 \r
@@ -41,13 +42,12 @@ public class FunctionLibraryNode<T> extends AbstractNode<Resource> implements ID
                        Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
                        LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
                                @Override\r
-                               public String isValid(String label) {\r
-                                       if (label.isEmpty())\r
-                                               return "Empty label not allowed";\r
-                                       if (label.contains(" "))\r
-                                               return "Spaces are not allowed";\r
-                                       return null;\r
-                               }\r
+                   public String isValid(String label) {\r
+                       if (!new FunctionLibraryNameValidator().isValid(data, label))\r
+                           return "Not valid";\r
+                       else\r
+                           return null;\r
+                   }\r
                        };\r
                        return modifier;\r
                } catch (DatabaseException e) {\r
index c3900b25318573fb1122a87e7819d61a804ee4d0..21312513b4c2d206ed87c53098b9bb4bfc8641f9 100644 (file)
@@ -24,6 +24,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.RemoverUtil;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.FunctionNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 \r
@@ -38,14 +39,13 @@ public class FunctionNode extends VariableNode<Resource> implements IDeletableNo
                try {\r
                        Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
                        LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
-                               @Override\r
-                               public String isValid(String label) {\r
-                                       if (label.isEmpty())\r
-                                               return "Empty label not allowed";\r
-                                       if (label.contains(" "))\r
-                                               return "Spaces are not allowed";\r
-                                       return null;\r
-                               }\r
+                @Override\r
+                public String isValid(String label) {\r
+                    if (!new FunctionNameValidator().isValid(data, label))\r
+                        return "Not valid";\r
+                    else\r
+                        return null;\r
+                }\r
                        };\r
                        return modifier;\r
                } catch (DatabaseException e) {\r
index 2652d55242e4f718d322020ec4009b7f86a7f4d6..eb7229b1e080914d8cd9e3a93b9a9eaca63d578c 100644 (file)
@@ -32,6 +32,7 @@ import org.simantics.db.request.Write;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.ModelNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.ui.ExceptionUtils;\r
 \r
@@ -86,11 +87,10 @@ public class ModelNode extends AbstractNode<Resource> implements IDoubleClickabl
         LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data) {\r
             @Override\r
             public String isValid(String label) {\r
-                if (label.isEmpty())\r
-                    return "Empty label not allowed";\r
-                if (label.contains(" "))\r
-                    return "Spaces are not allowed";\r
-                return null;\r
+                if (!new ModelNameValidator().isValid(data, label))\r
+                    return "Not valid";\r
+                else\r
+                    return null;\r
             }\r
             \r
             @Override\r
index 0f2dc7c5ebfdeb595d9ce6609a3503ad42ebcefe..2c947be3e95b4d7342d48e061fe3ce24d075abf2 100644 (file)
@@ -22,7 +22,7 @@ import org.simantics.layer0.Layer0;
 import org.simantics.structural.stubs.StructuralResource2;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
-public class ModuleNode extends ConfigurationNode<Resource> {\r
+public class ModuleNode extends ConfigurationNode<Resource>/* implements IModifiableNode*/ {\r
 \r
        Resource configuration;\r
        \r
@@ -60,4 +60,24 @@ public class ModuleNode extends ConfigurationNode<Resource> {
             return new DefaultPasteHandler(configuration);\r
         return super.getAdapter(adapter);\r
     }\r
+\r
+    /*@Override\r
+    public Modifier getModifier(String columnId) {\r
+        try {\r
+            Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+            LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+                @Override\r
+                public String isValid(String label) {\r
+                    if (!new VariableNameValidator().isValid(data, label))\r
+                        return "Not valid";\r
+                    else\r
+                        return null;\r
+                }\r
+            };\r
+            return modifier;\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return null;\r
+    }*/\r
 }\r
index a10c7c1fc7ecee26b79610e5391fafcb3e926349..22e62b2f03b1169b787ef95cf79ab24a6f3c26cb 100644 (file)
@@ -42,6 +42,7 @@ import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.ui.utils.ModuleTypeNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
 public class ModuleTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {\r
@@ -123,9 +124,10 @@ public class ModuleTypeNode extends AbstractNode<Resource> implements IDeletable
                     LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) {\r
                         @Override\r
                         public String isValid(String label) {\r
-                            if (label.isEmpty())\r
-                                return "Empty name not allowed";\r
-                            return null;\r
+                            if (!new ModuleTypeNameValidator().isValid(data, label))\r
+                                return "Not valid";\r
+                            else\r
+                                return null;\r
                         }\r
                     };\r
 \r
index e003f140bf732f7bb034749100c0e67c6a2bb26b..e2269ebaa27f445fd4bb687553cd786115e5c62f 100644 (file)
@@ -13,12 +13,15 @@ package org.simantics.sysdyn.ui.browser.nodes;
 \r
 import org.simantics.browsing.ui.common.node.DeleteException;\r
 import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.SharedFunctionLibraryNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 \r
@@ -43,6 +46,26 @@ public class SharedFunctionLibraryNode extends FunctionLibraryNode<Resource> imp
                        }\r
                });\r
        }\r
+\r
+    @Override\r
+    public Modifier getModifier(String columnId) {\r
+        try {\r
+            Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+            LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+                @Override\r
+                public String isValid(String label) {\r
+                    if (!new SharedFunctionLibraryNameValidator().isValid(data, label))\r
+                        return "Not valid";\r
+                    else\r
+                        return null;\r
+                }\r
+            };\r
+            return modifier;\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return null;\r
+    }\r
        \r
        @Override\r
        public void drop(Object data) {\r
index 8df2d43db50b15410d01d1e702703d542c704739..5ed9c24368a73435e002591e45fb7918ba6c15e2 100644 (file)
@@ -15,6 +15,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.db.layer0.variable.Variables;\r
 import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.ui.utils.SheetNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.workbench.ResourceEditorInput2;\r
 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
@@ -32,9 +33,10 @@ public class SheetNode extends AbstractNode<Resource> implements IModifiableNode
         LabelModifier modifier = new LabelModifier(session, data, session.getService(Layer0.class).HasName) {\r
             @Override\r
             public String isValid(String label) {\r
-                if (label.isEmpty())\r
-                    return "Empty label not allowed";\r
-                return null;\r
+                if (!new SheetNameValidator().isValid(data, label))\r
+                    return "Not valid";\r
+                else\r
+                    return null;\r
             }\r
         };\r
         return modifier;\r
index cada925b03b5811fe60da77ff240581189639bfe..71973d3414230e5a7e7c5c495a5a8eda233600d5 100644 (file)
@@ -15,7 +15,7 @@ import org.simantics.browsing.ui.common.node.AbstractNode;
 import org.simantics.db.Resource;\r
 import org.simantics.db.layer0.variable.Variable;\r
 \r
-public class VariableNode<T> extends AbstractNode<Resource> {\r
+public class VariableNode<T> extends AbstractNode<Resource>/* implements IModifiableNode*/ {\r
 \r
        Variable variable;\r
 \r
@@ -31,4 +31,24 @@ public class VariableNode<T> extends AbstractNode<Resource> {
        public Variable getVariable() {\r
                return variable;\r
        }\r
+\r
+    /*@Override\r
+    public Modifier getModifier(String columnId) {\r
+        try {\r
+            Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+            LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+                @Override\r
+                public String isValid(String label) {\r
+                    if (!new VariableNameValidator().isValid(data, label))\r
+                        return "Not valid";\r
+                    else\r
+                        return null;\r
+                }\r
+            };\r
+            return modifier;\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return null;\r
+    }*/\r
 }\r
index df0ed021d6d5875c83b15ffa2193b4cfb6585385..319a4baa1abce713fce04ab2e449a938e50e79f1 100644 (file)
@@ -28,7 +28,7 @@ import org.simantics.diagram.handler.PasteOperation;
 import org.simantics.diagram.handler.Paster;\r
 import org.simantics.diagram.handler.Paster.NodeMap;\r
 import org.simantics.modeling.ModelingResources;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 import org.simantics.utils.ui.ErrorLogger;\r
 \r
 /**\r
@@ -97,7 +97,7 @@ public class SysdynCopyPasteStrategy extends DefaultCopyPasteStrategy {
                     // Check for each name individually\r
                     for(String originalName : names.keySet()) {\r
                         String newName = names.get(originalName);\r
-                        VariableNameUtils.renameInEquations(graph, copy, originalName, newName);\r
+                        new VariableNameValidator().renameInEquations(graph, copy, originalName, newName);\r
                     }\r
                 }\r
             }\r
index a55280638d17a4d61d2e5b9e9627b6170749a039..b623b9d7f2998a52b9f16cafcdca2a13e5a783bb 100644 (file)
@@ -34,7 +34,7 @@ import org.simantics.g2d.utils.Alignment;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.scenegraph.g2d.G2DParentNode;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.Callback;\r
 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
@@ -102,7 +102,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif
                                        @Override\r
                                        public void textChanged() {\r
                                                TextNode node = (TextNode) e.getHint(SG_NODE);\r
-                                               if(!VariableNameUtils.isValid(component, node.getText(), false)) {\r
+                                               if(!new VariableNameValidator().isValid(component, node.getText(), false)) {\r
                                                        node.setColor(Color.RED);\r
                                                } else {\r
                                                        node.setColor(ElementUtils.getTextColor(e, Color.BLACK));\r
@@ -135,7 +135,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif
                                        public void textEditingCancelled() {\r
                                                TextNode node = (TextNode) e.getHint(SG_NODE);\r
                                                if (node != null) {\r
-                                                       if(VariableNameUtils.isValid(component, node.getText(), false))\r
+                                                       if(new VariableNameValidator().isValid(component, node.getText(), false))\r
                                                                node.setColor(ElementUtils.getTextColor(e, Color.BLACK));\r
                                                        endEdit(node);\r
                                                }\r
@@ -147,10 +147,10 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif
                                                if (node == null)\r
                                                        return;\r
                                                String text = node.getText();\r
-                                               if(!VariableNameUtils.isValid(component, text, false)) {\r
+                                               if(!new VariableNameValidator().isValid(component, text, false)) {\r
                                                        text = textBeforeEdit;\r
                                                        node.setText(text);\r
-                                                       if(VariableNameUtils.isValid(component, text, false))\r
+                                                       if(new VariableNameValidator().isValid(component, text, false))\r
                                                                node.setColor(ElementUtils.getTextColor(e, Color.BLACK));\r
                                                } else {\r
                                                        Object o = e.getHint(ElementHints.KEY_OBJECT);\r
@@ -160,7 +160,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif
                                                                    @Override\r
                                                                    public void perform(WriteGraph graph) throws DatabaseException {\r
                                                                        Resource configuration = graph.getPossibleObject(component, Layer0.getInstance(graph).PartOf);\r
-                                                                       VariableNameUtils.renameInAllEquations(graph, configuration, textBeforeEdit, textAfterEdit);\r
+                                                                       new VariableNameValidator().renameInAllEquations(graph, configuration, textBeforeEdit, textAfterEdit);\r
                                                                    }\r
                                                                });\r
                                                        }\r
index dfc4fd80ae36afb0957b6c57695de73cd9a634e0..84c0055fc5699b8f726404792a5a25eba586b723 100644 (file)
@@ -31,7 +31,8 @@ import org.eclipse.swt.graphics.Color;
 \r
 public class ModelicaSourceViewerConfiguration extends SourceViewerConfiguration {\r
 \r
-       public IPresentationReconciler getPresentationReconciler(\r
+       @Override\r
+    public IPresentationReconciler getPresentationReconciler(\r
                        ISourceViewer sourceViewer) {\r
                PresentationReconciler pr = new PresentationReconciler();\r
                DefaultDamagerRepairer ddr = new DefaultDamagerRepairer(\r
@@ -80,62 +81,77 @@ public class ModelicaSourceViewerConfiguration extends SourceViewerConfiguration
                 || (c >= '0' && c <= '9') || c == '_';\r
        }\r
        \r
-       static final Set<String> keywords = new HashSet<String>();\r
-       \r
-       static {\r
-               keywords.add("within");\r
-               keywords.add("final");\r
-               keywords.add("public");\r
-               keywords.add("protected");\r
-               keywords.add("connect");\r
-               keywords.add("when");\r
-               keywords.add("then");\r
-               keywords.add("elsewhen");\r
-               keywords.add("if");\r
-               keywords.add("end");\r
-               keywords.add("elseif");\r
-               keywords.add("else");\r
-               keywords.add("for");\r
-               keywords.add("while");\r
-               keywords.add("loop");\r
-               keywords.add("der");\r
-               keywords.add("enumeration");\r
-               keywords.add("extends");\r
-               keywords.add("class");\r
-               keywords.add("partial");\r
-               keywords.add("encapsulated");\r
-               keywords.add("model");\r
-               keywords.add("record");\r
-               keywords.add("block");\r
-               keywords.add("expandable");\r
-               keywords.add("connector");\r
-               keywords.add("type");\r
-               keywords.add("package");\r
-               keywords.add("function");\r
-               keywords.add("import");\r
-               keywords.add("external");\r
-               keywords.add("constrainedby");\r
-               keywords.add("redeclare");\r
-               keywords.add("replaceable");\r
-               keywords.add("flow");\r
-               keywords.add("discrete");\r
-               keywords.add("parameter");\r
-               keywords.add("constant");\r
-               keywords.add("input");\r
-               keywords.add("output");\r
-               keywords.add("annotation");\r
-               keywords.add("false");\r
-               keywords.add("true");\r
-               keywords.add("each");\r
-               keywords.add("initial");\r
-               keywords.add("algorithm");\r
-               keywords.add("equation");\r
-               keywords.add("or");\r
-               keywords.add("and");\r
-               keywords.add("not");\r
-               keywords.add("break");\r
-               keywords.add("return");         \r
-       }\r
+       // Includes also tokens that are strictly speaking not keywords\r
+       // but should be handled in (almost) a similar fashion.\r
+       public static final Set<String> keywords = new HashSet<String>();\r
+\r
+    static {\r
+        keywords.add("algorithm");\r
+        keywords.add("discrete");\r
+        keywords.add("false");\r
+        keywords.add("model");\r
+        keywords.add("redeclare");\r
+        keywords.add("and");\r
+        keywords.add("each");\r
+        keywords.add("final");\r
+        keywords.add("not");\r
+        keywords.add("replaceable");\r
+        keywords.add("annotation");\r
+        keywords.add("else");\r
+        keywords.add("flow");\r
+        keywords.add("operator");\r
+        keywords.add("return");\r
+        keywords.add("assert");\r
+        keywords.add("elseif");\r
+        keywords.add("for");\r
+        keywords.add("or");\r
+        keywords.add("stream");\r
+        keywords.add("block");\r
+        keywords.add("elsewhen");\r
+        keywords.add("function");\r
+        keywords.add("outer");\r
+        keywords.add("then");\r
+        keywords.add("break");\r
+        keywords.add("encapsulated");\r
+        keywords.add("if");\r
+        keywords.add("output");\r
+        keywords.add("true");\r
+        keywords.add("class");\r
+        keywords.add("end");\r
+        keywords.add("import");\r
+        keywords.add("package");\r
+        keywords.add("type");\r
+        keywords.add("connect");\r
+        keywords.add("enumeration");\r
+        keywords.add("in");\r
+        keywords.add("parameter");\r
+        keywords.add("when");\r
+        keywords.add("connector");\r
+        keywords.add("equation");\r
+        keywords.add("initial");\r
+        keywords.add("partial");\r
+        keywords.add("while");\r
+        keywords.add("constant");\r
+        keywords.add("expandable");\r
+        keywords.add("inner");\r
+        keywords.add("protected");\r
+        keywords.add("within");\r
+        keywords.add("constrainedby");\r
+        keywords.add("extends");\r
+        keywords.add("input");\r
+        keywords.add("public");\r
+        keywords.add("der");\r
+        keywords.add("external");\r
+        keywords.add("loop");\r
+        keywords.add("record");\r
+        keywords.add("time");\r
+        keywords.add("pure");\r
+        keywords.add("impure");\r
+        keywords.add("Real");\r
+        keywords.add("Boolean");\r
+        keywords.add("Integer");\r
+        keywords.add("String");\r
+    }\r
 \r
        static IToken modelicaKeywordToken = null;\r
        public IToken getModelicaKeywordToken() {\r
index 96dea950c51521d1b01e96885faede1d3aa72034..540eda469d90100f65687fef9f6fa10e99a7c8ef 100644 (file)
@@ -40,7 +40,7 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.sysdyn.ui.properties.widgets.factories.ComboStringPropertyModifier;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.ModelNameInputValidator;\r
 \r
 /**\r
  * Tab displaying configuration properties. Displayed for model and configuration.\r
@@ -87,15 +87,7 @@ public class ConfigurationTab extends LabelPropertyTabContributor {
         TrackedText name = new TrackedText(composite, support, SWT.BORDER);\r
         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
         name.addModifyListener(new ModelLabelModifier(context, Layer0.URIs.HasLabel));\r
-        name.setInputValidator(new IInputValidator() {\r
-            \r
-            @Override\r
-            public String isValid(String newText) {\r
-                if (!VariableNameUtils.isValid(newText))\r
-                    return "Sorry but spaces and special characters are not allowed for names right now";\r
-                return null;\r
-            }\r
-        });\r
+        name.setInputValidator(new ModelNameInputValidator(support));\r
 \r
         GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getWidget());\r
 \r
index cddbd10acb5a06e9e9a757b7c2250e54fe1308f9..265325a285493a3c24a92c97690555ef15068238 100644 (file)
@@ -60,7 +60,7 @@ import org.simantics.sysdyn.manager.SysdynModelManager;
 import org.simantics.sysdyn.ui.properties.widgets.arrays.EnumerationIndexNode;\r
 import org.simantics.sysdyn.ui.properties.widgets.arrays.ReplaceableIndexesWidget;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.ArrayMap;\r
 import org.simantics.utils.ui.AdaptionUtils;\r
@@ -86,7 +86,7 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge
         TrackedText nameText = new TrackedText(container, support, SWT.BORDER);\r
         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
         nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName));\r
-        nameText.setInputValidator(new VariableNameValidator(support));\r
+        nameText.setInputValidator(new VariableNameInputValidator(support));\r
         GridDataFactory.fillDefaults().grab(true, false).span(4,1).applyTo(nameText.getWidget());\r
         \r
         indexExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
index bed25ff2bf751cff4838752d555482c0363baf59..eab753cb396580415111a8aed9fc48a7d0b036e6 100644 (file)
@@ -73,7 +73,7 @@ import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget;
 import org.simantics.sysdyn.ui.properties.widgets.arrays.NameAndArrayRangeModifyListener;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionComposite;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionWidgetInput;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.Pair;\r
 import org.simantics.utils.ui.AdaptionUtils;\r
@@ -140,7 +140,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget {
                GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite);\r
 \r
                arrayEquationCombo =  new ArrayExpressionCombo(nameComposite, support, SWT.DROP_DOWN | SWT.BORDER);\r
-               arrayEquationCombo.setInputValidator(new VariableNameValidator(support));\r
+               arrayEquationCombo.setInputValidator(new VariableNameInputValidator(support));\r
                GridDataFactory.fillDefaults().grab(true, false).applyTo(arrayEquationCombo.getWidget());\r
                \r
                deleteExpression = new Button(nameComposite, support, SWT.NONE);\r
index b198c15dd509830d1d53b75789a13ae7b6d8f20f..5164aa97f1e4208816a0f8828c7b49769d77a20f 100644 (file)
@@ -22,6 +22,7 @@ import org.simantics.browsing.ui.swt.widgets.TrackedText;
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.FunctionLibraryNameInputValidator;\r
 \r
 public class FunctionLibraryTab extends LabelPropertyTabContributor {\r
 \r
@@ -36,6 +37,7 @@ public class FunctionLibraryTab extends LabelPropertyTabContributor {
                TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
                nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
                nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+        nameText.setInputValidator(new FunctionLibraryNameInputValidator(support));\r
                GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
 \r
 \r
index 2817708fdb314d58e79dfad43746dad2074c2898..d7c57f34c3384ee7f23c30e318f5ab5395a001ce 100644 (file)
@@ -26,6 +26,7 @@ import org.simantics.db.management.ISessionContext;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.ui.properties.widgets.FunctionLabelFactory;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.FunctionNameInputValidator;\r
 import org.simantics.sysdyn.ui.properties.widgets.functions.FunctionCodeWidget;\r
 \r
 public class FunctionTab extends LabelPropertyTabContributor {\r
@@ -43,6 +44,7 @@ public class FunctionTab extends LabelPropertyTabContributor {
         TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
         nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+        nameText.setInputValidator(new FunctionNameInputValidator(support));\r
         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(nameText.getWidget());\r
         \r
         \r
index e55f2acd95416348dc516a476217d5642a6b70ce..9578e18b9886a9aba89cd23ce4c836a83bcae098 100644 (file)
@@ -38,7 +38,7 @@ import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget;
 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
 import org.simantics.utils.datastructures.Pair;\r
 import org.simantics.utils.ui.validators.DoubleValidator;\r
 \r
@@ -60,7 +60,7 @@ public class InputVariableTab  extends LabelPropertyTabContributor {
         TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
         nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName));\r
-        nameText.setInputValidator(new VariableNameValidator(support));\r
+        nameText.setInputValidator(new VariableNameInputValidator(support));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
 \r
         Composite defaultValueComposite = new Composite(composite, SWT.NONE);\r
index 34f5ac22c43edb1cb4495fb9d0a2bdc74fcf508b..030746cc1505d8b3ed21bfbe0883c8823c644a81 100644 (file)
@@ -31,6 +31,7 @@ import org.simantics.db.management.ISessionContext;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
 import org.simantics.utils.datastructures.ArrayMap;\r
 \r
 public class ModuleTab extends LabelPropertyTabContributor implements Widget {\r
@@ -47,6 +48,7 @@ public class ModuleTab extends LabelPropertyTabContributor implements Widget {
         TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
         nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+        nameText.setInputValidator(new VariableNameInputValidator(support));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
         \r
         Label label = new Label(composite, SWT.NONE);\r
index fe40aee7d018809099e9ba8a17696600ff9d52ca..79ebe9abf08d8ed12eaeaa8e3ffc9470fee8b0b7 100644 (file)
@@ -22,6 +22,7 @@ import org.simantics.browsing.ui.swt.widgets.TrackedText;
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.ModuleTypeNameInputValidator;\r
 \r
 public class ModuleTypeTab extends LabelPropertyTabContributor {\r
 \r
@@ -33,6 +34,7 @@ public class ModuleTypeTab extends LabelPropertyTabContributor {
         TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
         nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+        nameText.setInputValidator(new ModuleTypeNameInputValidator(support));\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
     }\r
 \r
index e5aaef7e545a48e001da43b53d591863bc10b1a4..45f1e1a12ddf79a82fae71412ea5f86044a35f29 100644 (file)
@@ -12,7 +12,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.request.Read;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
 public class EnumerationIndexNode  extends AbstractNode<Resource> implements IModifiableNode, IDoubleClickableNode {\r
@@ -47,7 +47,7 @@ public class EnumerationIndexNode  extends AbstractNode<Resource> implements IMo
 \r
             @Override\r
             public String isValid(String label) {\r
-               if(!VariableNameUtils.isValid(label))\r
+               if(!new VariableNameValidator().isValidModelica(label))\r
                        return "Not valid";\r
                 return null;\r
             }\r
index af72b798daf4afd218e946ddefd25722b2195c85..90b4319cd7a8824add5d8a744b4c571cba20a0a2 100644 (file)
@@ -3,7 +3,6 @@ package org.simantics.sysdyn.ui.properties.widgets.arrays;
 import java.util.LinkedHashMap;\r
 import java.util.StringTokenizer;\r
 \r
-import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.swt.widgets.Combo;\r
 import org.simantics.Simantics;\r
 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
@@ -24,7 +23,7 @@ import org.simantics.layer0.Layer0;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.properties.widgets.ArrayExpressionCombo;\r
 import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
 /**\r
@@ -51,7 +50,8 @@ public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Res
                this.lastInput = input;\r
        } \r
                \r
-       public void modifyText(TrackedModifyEvent e) {          \r
+       @Override\r
+    public void modifyText(TrackedModifyEvent e) {             \r
                Combo combo = (Combo)e.getWidget();\r
                LinkedHashMap<?, ?> data = (LinkedHashMap<?, ?>) combo.getData();\r
                \r
@@ -62,7 +62,7 @@ public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Res
                                @Override\r
                                public Resource perform(ReadGraph graph)\r
                                                throws DatabaseException {\r
-                                       Resource variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
+                                       Resource variable = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
                                        return graph.getPossibleObject(variable, SysdynResource.getInstance(graph).IndependentVariable_activeExpression);\r
                                }\r
                                \r
@@ -80,7 +80,7 @@ public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Res
                        for(Object key : data.keySet()) {\r
                                int index = arrayExpressionCombo.getLastSelectedIndex() < 0 ? 0 : arrayExpressionCombo.getLastSelectedIndex();\r
                                if((Integer)combo.getData((String)key) == index) {\r
-                                       lastExpression =  (Resource) data.get((String)key);\r
+                                       lastExpression =  (Resource) data.get(key);\r
                                        break;\r
                                }\r
                        }\r
@@ -106,7 +106,7 @@ public class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl<Res
                String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName);\r
                if(!originalName.equals(newName)) {\r
                    Resource configuration = graph.getPossibleObject(variable, Layer0.getInstance(graph).PartOf);\r
-                       VariableNameUtils.renameInAllEquations(graph, configuration, originalName, newName);\r
+                   new VariableNameValidator().renameInAllEquations(graph, configuration, originalName, newName);\r
                        graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName);\r
                }\r
 \r
index 46e75fca5c2cf99ec4267e5dab76565b5df57456..d2f646baabdb9c66369a048706dee85f7f80285e 100644 (file)
@@ -43,7 +43,7 @@ import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.RGB;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.swt.widgets.Table;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.modelica.ModelicaSourceViewerConfiguration;\r
 \r
 @SuppressWarnings("restriction")\r
 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
@@ -119,7 +119,7 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
                }, defaultToken);\r
                \r
 \r
-               for(String s : VariableNameUtils.keywords) {\r
+               for(String s : ModelicaSourceViewerConfiguration.keywords) {\r
                        reservedWord.addWord(s, reserved);\r
                }\r
 \r
index 5817d1fec8717821a33159388985be3454241a38..6ddacd646e1ea524a6fd43c39cb457b27bf38386 100644 (file)
@@ -198,6 +198,23 @@ public class Function implements Comparable<Function>{
         return functions;\r
     }\r
     \r
+    /**\r
+     * Get all built-in Modelica functions.\r
+     * Should be used if called inside a transaction\r
+     * @return ArrayList containing all built-in functions.\r
+     */\r
+    public static ArrayList<Function> getAllBuiltInFunctions(ReadGraph graph) throws DatabaseException {\r
+        ArrayList<Function> functions = new ArrayList<Function>();\r
+    \r
+        // Add different types of functions.\r
+        functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions, Type.SYSDYN));\r
+        functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Vensim_Functions, Type.VENSIM));\r
+        functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Functions, Type.MODELICA));\r
+        functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Array_Functions, Type.MODELICA_ARRAY));\r
+        \r
+        return functions;\r
+    }\r
+    \r
     /**\r
      * Get all built-in Modelica functions.\r
      * @return ArrayList containing all built-in functions.\r
@@ -209,17 +226,8 @@ public class Function implements Comparable<Function>{
         try {\r
              result = SimanticsUI.getSession().syncRequest(new Read<ArrayList<Function>>() {\r
                 @Override\r
-                public ArrayList<Function> perform(ReadGraph graph)\r
-                        throws DatabaseException {\r
-                    ArrayList<Function> functions = new ArrayList<Function>();\r
-\r
-                    // Add different types of functions.\r
-                    functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions, Type.SYSDYN));\r
-                    functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Vensim_Functions, Type.VENSIM));\r
-                    functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Functions, Type.MODELICA));\r
-                    functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Array_Functions, Type.MODELICA_ARRAY));\r
-                    \r
-                    return functions;\r
+                public ArrayList<Function> perform(ReadGraph graph) throws DatabaseException {\r
+                    return getAllBuiltInFunctions(graph);\r
                 }\r
             });\r
         }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/FunctionLibraryNameInputValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/FunctionLibraryNameInputValidator.java
new file mode 100644 (file)
index 0000000..18c1a41
--- /dev/null
@@ -0,0 +1,52 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.properties.widgets.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.FunctionLibraryNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for user defined function libraries.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class FunctionLibraryNameInputValidator implements IInputValidator, Widget {\r
+\r
+    private Resource lastInput = null;\r
+    \r
+    public FunctionLibraryNameInputValidator(WidgetSupport support) {\r
+        support.register(this);\r
+    }\r
+    \r
+    /**\r
+     * Checks that the syntax of the given name is valid and there \r
+     * are no other components that have the same name in the configuration.\r
+     */\r
+    @Override\r
+    public String isValid(final String newText) {\r
+        if (!new FunctionLibraryNameValidator().isValid(lastInput, newText))\r
+            return "Not valid";\r
+        else\r
+            return null;\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+        lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/FunctionNameInputValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/FunctionNameInputValidator.java
new file mode 100644 (file)
index 0000000..a5cce2b
--- /dev/null
@@ -0,0 +1,52 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.properties.widgets.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.FunctionNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for user defined functions.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class FunctionNameInputValidator implements IInputValidator, Widget {\r
+\r
+    private Resource lastInput = null;\r
+    \r
+    public FunctionNameInputValidator(WidgetSupport support) {\r
+        support.register(this);\r
+    }\r
+    \r
+    /**\r
+     * Checks that the syntax of the given name is valid and there \r
+     * are no other components that have the same name in the configuration.\r
+     */\r
+    @Override\r
+    public String isValid(final String newText) {\r
+        if (!new FunctionNameValidator().isValid(lastInput, newText))\r
+            return "Not valid";\r
+        else\r
+            return null;\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+        lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/ModelNameInputValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/ModelNameInputValidator.java
new file mode 100644 (file)
index 0000000..625e955
--- /dev/null
@@ -0,0 +1,52 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.properties.widgets.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.ModelNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for model.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class ModelNameInputValidator implements IInputValidator, Widget {\r
+\r
+    private Resource lastInput = null;\r
+    \r
+    public ModelNameInputValidator(WidgetSupport support) {\r
+        support.register(this);\r
+    }\r
+    \r
+    /**\r
+     * Checks that the syntax of the given name is valid and there \r
+     * are no other components that have the same name in the configuration.\r
+     */\r
+    @Override\r
+    public String isValid(final String newText) {\r
+        if (!new ModelNameValidator().isValid(lastInput, newText))\r
+            return "Not valid";\r
+        else\r
+            return null;\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+        lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/ModuleTypeNameInputValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/ModuleTypeNameInputValidator.java
new file mode 100644 (file)
index 0000000..1340525
--- /dev/null
@@ -0,0 +1,52 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.properties.widgets.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.ModuleTypeNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for module types.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class ModuleTypeNameInputValidator implements IInputValidator, Widget {\r
+\r
+    private Resource lastInput = null;\r
+    \r
+    public ModuleTypeNameInputValidator(WidgetSupport support) {\r
+        support.register(this);\r
+    }\r
+    \r
+    /**\r
+     * Checks that the syntax of the given name is valid and there \r
+     * are no other components that have the same name in the configuration.\r
+     */\r
+    @Override\r
+    public String isValid(final String newText) {\r
+        if (!new ModuleTypeNameValidator().isValid(lastInput, newText))\r
+            return "Not valid";\r
+        else\r
+            return null;\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+        lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+    }\r
+\r
+}\r
similarity index 58%
rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java
rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameInputValidator.java
index 9d635e71675c7945d15a3017b7f31e54ccef9de2..3a6af0ce5794c2529beaeaff1b41d152541c23ba 100644 (file)
 package org.simantics.sysdyn.ui.properties.widgets.factories;\r
 \r
 import org.eclipse.jface.dialogs.IInputValidator;\r
-import org.eclipse.jface.viewers.ISelection;\r
 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.management.ISessionContext;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
-public class VariableNameValidator implements IInputValidator, Widget {\r
+/**\r
+ * Name validator container for module types.\r
+ * @author Teemu Lempinen\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class VariableNameInputValidator implements IInputValidator, Widget {\r
 \r
        private Resource lastInput = null;\r
        \r
-       public VariableNameValidator(WidgetSupport support) {\r
+       public VariableNameInputValidator(WidgetSupport support) {\r
                support.register(this);\r
        }\r
        \r
@@ -34,36 +39,15 @@ public class VariableNameValidator implements IInputValidator, Widget {
         */\r
        @Override\r
        public String isValid(final String newText) {\r
-        if (!VariableNameUtils.isValid(lastInput, newText, true))\r
-            return "Not valid";\r
+           if (!new VariableNameValidator().isValid(lastInput, newText, true))\r
+               return "Not valid";\r
         else\r
                return null;\r
-        /*\r
-        if(lastInput == null) return null;\r
-        String result = null;\r
-        try {\r
-                       result = SimanticsUI.getSession().syncRequest(new Read<String>(){\r
-\r
-                               @Override\r
-                               public String perform(ReadGraph graph) throws DatabaseException {\r
-                                       if(VariableNameUtils.nameIsTaken(graph, lastInput, newText)) {\r
-                                               return "Name is taken.";\r
-                                       }\r
-                                       return null;\r
-                               }\r
-                               \r
-                       }) ;\r
-               } catch (DatabaseException e) {\r
-                       e.printStackTrace();\r
-               }\r
-        return result;\r
-        */\r
-\r
        }\r
 \r
        @Override\r
        public void setInput(ISessionContext context, Object input) {\r
-               lastInput = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
+               lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
        }\r
 \r
 }\r
index 56d55d35815ef39809a9e8a3b4380f8e762513b3..297b12b526843b226ee16bd2a43baa2d2bb10d84 100644 (file)
@@ -18,7 +18,7 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.layer0.Layer0;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
 \r
 public class VariableNamePropertyModifier extends TextModifyListenerImpl<Resource> {\r
 \r
@@ -40,7 +40,7 @@ public class VariableNamePropertyModifier extends TextModifyListenerImpl<Resourc
            Layer0 L0 = Layer0.getInstance(graph);\r
                String originalName = graph.getRelatedValue(variable, L0.HasName);\r
         Resource configuration = graph.getPossibleObject(variable, L0.PartOf);\r
-        VariableNameUtils.renameInAllEquations(graph, configuration, originalName, text);\r
+        new VariableNameValidator().renameInAllEquations(graph, configuration, originalName, text);\r
                graph.claimLiteral(variable, graph.getResource(propertyURI), text, Bindings.STRING);\r
        }\r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/FunctionLibraryNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/FunctionLibraryNameValidator.java
new file mode 100644 (file)
index 0000000..bf67e47
--- /dev/null
@@ -0,0 +1,74 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for function libraries.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class FunctionLibraryNameValidator extends NameValidator {\r
+\r
+    @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource functionLibrary, String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+        \r
+        if(functionLibrary == null)\r
+            return false;\r
+        \r
+        Resource containingLibrary = graph.getPossibleObject(functionLibrary, l0.PartOf);\r
+        if(containingLibrary == null)\r
+            return true;\r
+        \r
+        // Check if the function is right under the model.\r
+        if (graph.isInstanceOf(containingLibrary, sr.SysdynModel)) {\r
+            Resource configurationResource = graph.getPossibleObject(containingLibrary, SIMU.HasConfiguration);\r
+            if(configurationResource == null)\r
+                return true;\r
+            SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+            sysdynModel.update(graph);\r
+            \r
+            if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+            \r
+            // Check that the functioLibrary name != model name (== containingLibrary name)\r
+            if (NameUtils.getSafeName(graph, containingLibrary).equals(name)) return true;\r
+            \r
+            if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+            if (nameTakenBySharedFunctionLibrary(graph, containingLibrary, null, name)) return true;\r
+            \r
+            if (nameTakenBySheet(sysdynModel, name)) return true;\r
+        }\r
+\r
+        // Containing library may also be the model\r
+        if (nameTakenByItemUnderLibrary(graph, containingLibrary, functionLibrary, name)) return true;\r
+\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+    @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/FunctionNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/FunctionNameValidator.java
new file mode 100644 (file)
index 0000000..95b7d97
--- /dev/null
@@ -0,0 +1,70 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for functions.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class FunctionNameValidator extends NameValidator {\r
+\r
+    @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName) throws DatabaseException {\r
+        // TODO\r
+    }\r
+    \r
+    @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException {\r
+        // TODO\r
+    }\r
+\r
+    @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource function, String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+        \r
+        if(function == null)\r
+            return false;\r
+        \r
+        Resource library = graph.getPossibleObject(function, l0.PartOf);\r
+        if(library == null)\r
+            return true;\r
+        \r
+        // Check if the function is right under the model.\r
+        if (graph.isInstanceOf(library, sr.SysdynModel)) {\r
+            Resource configurationResource = graph.getPossibleObject(library, SIMU.HasConfiguration);\r
+            if(configurationResource == null)\r
+                return true;\r
+            SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+            sysdynModel.update(graph);\r
+            \r
+            if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+            \r
+            // Check that the function name != model name (== library name)\r
+            if (NameUtils.getSafeName(graph, library).equals(name)) return true;\r
+            \r
+            if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+            if (nameTakenBySharedFunctionLibrary(graph, library, null, name)) return true;\r
+            \r
+            if (nameTakenBySheet(sysdynModel, name)) return true;\r
+        }\r
+\r
+        // Containing library may also be the model\r
+        if (nameTakenByItemUnderLibrary(graph, library, function, name)) return true;\r
+\r
+        return false;\r
+    }\r
+
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelNameValidator.java
new file mode 100644 (file)
index 0000000..ec53c88
--- /dev/null
@@ -0,0 +1,56 @@
+package org.simantics.sysdyn.ui.utils;\r
+\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.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for model.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class ModelNameValidator extends NameValidator {\r
+\r
+    @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource model, String name) throws DatabaseException {\r
+        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+        \r
+        if(model == null)\r
+            return false;\r
+\r
+        if (nameTakenByFunctionFunctionLibraryOrModuleType(graph, model, name)) return true;\r
+        \r
+        Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);\r
+        if(configurationResource == null)\r
+            return true;\r
+        SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+        sysdynModel.update(graph);\r
+        \r
+        if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+        \r
+        if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+        if (nameTakenBySharedFunctionLibrary(graph, model, null, name)) return true;\r
+\r
+        if (nameTakenBySheet(sysdynModel, name)) return true;\r
+\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+            throws DatabaseException {\r
+\r
+    }\r
+\r
+    @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+            throws DatabaseException {\r
+\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModuleTypeNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModuleTypeNameValidator.java
new file mode 100644 (file)
index 0000000..8a3c851
--- /dev/null
@@ -0,0 +1,84 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for module types.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class ModuleTypeNameValidator extends NameValidator {\r
+\r
+    @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource moduleTypeSymbol, String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+        ModelingResources mr = ModelingResources.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        \r
+        if(moduleTypeSymbol == null)\r
+            return false;\r
+\r
+        Resource moduleType;\r
+        if (graph.isInheritedFrom(moduleTypeSymbol, sr.Module)) {\r
+            // Renamed in model browser\r
+            moduleType = moduleTypeSymbol;\r
+        } else {\r
+            // Renamed in ModuleTypeTab\r
+            moduleType = graph.getPossibleObject(moduleTypeSymbol, mr.SymbolToComponentType);\r
+        }\r
+        if(moduleType == null)\r
+            return true;\r
+        \r
+        // All ModuleTypes are under the top level model\r
+        Resource model = graph.getPossibleObject(moduleType, l0.PartOf);\r
+        if(model == null)\r
+            return true;\r
+\r
+        Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);\r
+        if(configurationResource == null)\r
+            return true;\r
+        SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+        sysdynModel.update(graph);\r
+        \r
+        if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+        \r
+        // Check that the module name != model name\r
+        if (NameUtils.getSafeName(graph, model).equals(name)) return true;\r
+        \r
+        if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+        if (nameTakenBySharedFunctionLibrary(graph, model, null, name)) return true;\r
+        \r
+        if (nameTakenBySheet(sysdynModel, name)) return true;\r
+\r
+        if (nameTakenByItemUnderLibrary(graph, model, moduleType, name)) return true;\r
+\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+    @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java
new file mode 100644 (file)
index 0000000..b349eb5
--- /dev/null
@@ -0,0 +1,335 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import java.util.Collection;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Module;\r
+import org.simantics.sysdyn.representation.Sheet;\r
+import org.simantics.sysdyn.representation.Variable;\r
+import org.simantics.sysdyn.ui.modelica.ModelicaSourceViewerConfiguration;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.Function;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+\r
+/**\r
+ * This class is used when renaming an element to check that there\r
+ * are no elements in the database that would cause a conflict. There\r
+ * are two kinds of different conflicts:\r
+ * 1) There is an element in the database which has the same URI.\r
+ *    Adding another causes damage or even a fatal corruption in the \r
+ *    database.\r
+ * 2) Multiple different elements in the database are accessed with\r
+ *    identical identifiers in the generated Modelica model. This will\r
+ *    prevent the simulation from running. \r
+ * \r
+ * @author Tuomas Miettinen\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public abstract class NameValidator {\r
+\r
+    /**\r
+     * This method tells the validator whether the proposed name is taken. To be more precise,\r
+     * it tells if the proposed name should not be used for any reason. E.g. similarly named\r
+     * resources cause problems in some occasions and are fine in others. In addition, there\r
+     * (at least )are two reasons to decline a name proposition: firstly, the data base might\r
+     * have a similarly named entity, and secondly, accepting a proposed name would lead in\r
+     * naming conflicts in the generated OpenModelica model.\r
+     * \r
+     * @param graph\r
+     * @param resource the Resource which is being renamed\r
+     * @param name the proposed name\r
+     * @return true iff the name proposition is denied\r
+     * @throws DatabaseException\r
+     */\r
+    protected abstract boolean nameIsTaken(ReadGraph graph, Resource resource, String name) throws DatabaseException;\r
+\r
+    /**\r
+     * Replaces variable names in all the expressions in the defined configuration. \r
+     * Replace doesn't have to replace words in other configurations than the configuration where the renamed variable is\r
+     * because input variables handle the transitions between configurations (modules).\r
+     * \r
+     * @param graph WriteGraph\r
+     * @param configuration The configuration where the renamed variable is located\r
+     * @param originalName The original name of the variable\r
+     * @param newName New name of the variable\r
+     */\r
+    public abstract void renameInEquations(WriteGraph graph, Resource resource, String originalName, String newName) throws DatabaseException;\r
+\r
+    public abstract void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException;\r
+\r
+    /**\r
+     * Checks that the syntax of the given name is valid and there \r
+     * are no other functions that have the same name in neither the \r
+     * configuration nor among built in functions\r
+     *  \r
+     * @param graph ReadGraph\r
+     * @param resource The variable that is being renamed\r
+     * @param name The new name of the variable\r
+     * @return true iff the new name is valid\r
+     * @throws DatabaseException\r
+     */\r
+    public boolean isValid(ReadGraph graph, Resource resource, String name) throws DatabaseException {\r
+        // Decline empty string\r
+        if(name.length() < 1) return false;\r
+        \r
+        // Decline names based on the type of the resource\r
+        if(nameIsTaken(graph, resource, name)) return false;\r
+        \r
+        // Decline names which are against Modelica naming rules\r
+        if(!isValidModelica(name)) return false;\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Checks that the syntax of the given name is valid and there \r
+     * are no other variables that have the same name in the configuration\r
+     *  \r
+     * @param variable The variable that is being renamed\r
+     * @param name The new name of the variable\r
+     * @return true iff the new name is valid\r
+     * @throws DatabaseException\r
+     */\r
+    public boolean isValid(final Resource resource, final String name) {\r
+        boolean result = false;\r
+\r
+        try {\r
+            result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+                @Override\r
+                public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                    return isValid(graph, resource, name);\r
+                }\r
+\r
+            });\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Checks that the syntax of the given name is valid \r
+     * and that it is not a keyword in Modelica.\r
+     * \r
+     * @param name name that is validated\r
+     * @return true iff the syntax of the name is valid\r
+     */\r
+    public boolean isValidModelica(String name) {\r
+        String lowerCase = name.toLowerCase();\r
+        Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*+");\r
+        Matcher m = p.matcher(lowerCase);\r
+        if (!m.matches() || ModelicaSourceViewerConfiguration.keywords.contains(name))\r
+            return false;\r
+        else\r
+            return true;\r
+    }\r
+\r
+    /**\r
+     * Checks that there is no similarly named spread sheet in the top level model.\r
+     * \r
+     * @param graph ReadGraph\r
+     * @param configurationResource Resource of the configuration of the model\r
+     * @param resource Resource being validated\r
+     * @param name proposed new name for resource\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    protected boolean nameTakenBySheet(\r
+            ReadGraph graph, \r
+            Resource configurationResource, \r
+            Resource resource, \r
+            String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);\r
+        for (Resource r : graph.getObjects(configurationResource, l0.ConsistsOf)) {\r
+            if (graph.isInstanceOf(r, SHEET.Book)) {\r
+                for (Resource s : graph.getObjects(r, l0.ConsistsOf)) {\r
+                    if (NameUtils.getSafeName(graph, s).equals(name) && !s.equals(resource)) {\r
+                        return true;\r
+                    }\r
+                }\r
+                // Assume only one book\r
+                break;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+    /**\r
+     * Checks that there is no similarly named spread sheet in the top level model.\r
+     * \r
+     * @param sysdynModel the model\r
+     * @param name proposed new name for resource\r
+     * @return\r
+     */\r
+    public boolean nameTakenBySheet(\r
+            SysdynModel sysdynModel, \r
+            String name) {\r
+       Configuration configuration = sysdynModel.getConfiguration();\r
+        if (configuration == null)\r
+            return true;\r
+        for (IElement e : configuration.getElements()) {\r
+            if (e instanceof Sheet) {\r
+                if (((Sheet)e).getName().equals(name)) {\r
+                    return true;\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Check that there are no similarly named functions, module types, or any\r
+     * other items in the same library. There are actually plenty of items that have\r
+     * to be avoided, probably all of them, they are all included in this test. \r
+     * \r
+     * @param graph ReadGraph\r
+     * @param library Resource which is studied\r
+     * @param resource Resource being validated\r
+     * @param name proposed new name for resource\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    protected boolean nameTakenByItemUnderLibrary(\r
+            ReadGraph graph,\r
+            Resource library,\r
+            Resource resource,\r
+            String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        for (Resource r : graph.getObjects(library, l0.ConsistsOf)) {\r
+            if (NameUtils.getSafeName(graph, r).equals(name) && !r.equals(resource)) {\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+    /**\r
+     * Check that there is no similarly named function library among shared function libraries.\r
+     * \r
+     * @param graph ReadGraph\r
+     * @param model resource of the top level model under which the resource is\r
+     * @param resource Resource being validated\r
+     * @param name proposed new name for resource\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    protected boolean nameTakenBySharedFunctionLibrary(\r
+            ReadGraph graph,\r
+            Resource model, \r
+            Resource resource,\r
+            String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        Collection<Resource> linkedResources = graph.getObjects(model, l0.IsLinkedTo);\r
+        for (Resource r : linkedResources) {\r
+            // Find the "Shared functions" library \r
+            if (graph.isInstanceOf(r, sr.SharedFunctionOntology)) {\r
+                if (NameUtils.getSafeName(graph, r).equals(name) && !r.equals(resource)) {\r
+                    return true;\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+    /**\r
+     * Check that there is no similarly named function among built-in functions\r
+     * \r
+     * @param graph ReadGraph\r
+     * @param name name that is checked\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    protected boolean nameTakenByBuiltInFunction(\r
+            ReadGraph graph, \r
+            String name) throws DatabaseException {\r
+        for (Function f : Function.getAllBuiltInFunctions(graph)) {\r
+            if (f.getName().equals(name)) {\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+    /**\r
+     * Check that the top level of the model doesn't contain \r
+     * a variable or module with the same name.\r
+     * \r
+     * @param graph ReadGraph\r
+     * @param sysdynModel the model\r
+     * @param resource Resource being validated\r
+     * @param name proposed new name for resource\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    protected boolean nameTakenByTopLevelVariableOrModule(\r
+            ReadGraph graph,\r
+            SysdynModel sysdynModel,\r
+            Resource resource,\r
+            String name) throws DatabaseException {\r
+        Configuration configuration = sysdynModel.getConfiguration();\r
+        if(configuration == null)\r
+            return true;\r
+        IElement current = sysdynModel.getElement(resource);\r
+        for(IElement e : configuration.getElements()) {\r
+            if(e instanceof Variable) {\r
+                Variable v = (Variable) e;\r
+                if(!v.equals(current) && v.getName() != null && v.getName().equals(name)) {\r
+                    return true;\r
+                }\r
+            } else if (e instanceof Module) {\r
+                Module m = (Module)e;\r
+                if(!m.equals(current) && m.getName() != null && m.getName().equals(name)) {\r
+                    return true;\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+    /**\r
+     * Check that there is no similarly named function, function library,\r
+     * or module type directly under a library\r
+     * \r
+     * This function should not be used by functions, function libraries, or module types\r
+     *\r
+     * @param graph ReadGraph\r
+     * @param library Resource which is studied\r
+     * @param name proposed new name for resource\r
+     * @return\r
+     * @throws DatabaseException\r
+     */\r
+    protected boolean nameTakenByFunctionFunctionLibraryOrModuleType(\r
+            ReadGraph graph,\r
+            Resource library,\r
+            String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        for (Resource r : graph.getObjects(library, l0.ConsistsOf)) {\r
+            if (graph.isInstanceOf(r, sr.SysdynModelicaFunctionLibrary)\r
+                    || graph.isInstanceOf(r, sr.SysdynModelicaFunction)\r
+                    || graph.isInheritedFrom(r, sr.Module)) {\r
+                if (NameUtils.getSafeName(graph, r).equals(name)) {\r
+                    return true;\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SharedFunctionLibraryNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SharedFunctionLibraryNameValidator.java
new file mode 100644 (file)
index 0000000..d48e025
--- /dev/null
@@ -0,0 +1,70 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for shared function libraries.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class SharedFunctionLibraryNameValidator extends NameValidator {\r
+\r
+    @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource functionLibrary, String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+        \r
+        if(functionLibrary == null)\r
+            return false;\r
+        \r
+        // Do the checking for all models that are linked to that shared function library\r
+        for (Resource model : graph.getObjects(functionLibrary, l0.IsLinkedTo_Inverse)) {\r
+            if(model == null)\r
+                return true;\r
+\r
+            Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);\r
+            if(configurationResource == null)\r
+                return true;\r
+            SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+            sysdynModel.update(graph);\r
+            \r
+            if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+\r
+            // Check that the functionLibrary name != model name\r
+            if (NameUtils.getSafeName(graph, model).equals(name)) return true;\r
+            \r
+            if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+            if (nameTakenBySharedFunctionLibrary(graph, model, functionLibrary, name)) return true;\r
+            \r
+            if (nameTakenBySheet(sysdynModel, name)) return true;\r
+            \r
+            if (nameTakenByItemUnderLibrary(graph, model, null, name)) return true;\r
+        }\r
+        \r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+    @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SheetNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/SheetNameValidator.java
new file mode 100644 (file)
index 0000000..115f58e
--- /dev/null
@@ -0,0 +1,70 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for spread sheets.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class SheetNameValidator extends NameValidator {\r
+\r
+    @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource sheet, String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        \r
+        if(sheet == null)\r
+            return false;\r
+        \r
+        Resource book = graph.getPossibleObject(sheet, l0.PartOf);\r
+        if(book == null)\r
+            return true;\r
+        \r
+        Resource configurationResource = graph.getPossibleObject(book, l0.PartOf);\r
+        if(configurationResource == null)\r
+            return true;\r
+        SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+        sysdynModel.update(graph);\r
+        \r
+        Resource model = graph.getPossibleObject(configurationResource, l0.PartOf);\r
+        if(model == null)\r
+            return true;\r
+\r
+        if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, sheet, name)) return true;\r
+        \r
+        // Check that the sheet name != model name\r
+        if (NameUtils.getSafeName(graph, model).equals(name)) return true;\r
+        \r
+        if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+        if (nameTakenBySharedFunctionLibrary(graph, model, null, name)) return true;\r
+        \r
+        if (nameTakenBySheet(graph, configurationResource, sheet, name)) return true;\r
+\r
+        if (nameTakenByItemUnderLibrary(graph, model, null, name)) return true;\r
+\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+    @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+            throws DatabaseException {\r
+        // TODO Auto-generated method stub\r
+\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameUtils.java
deleted file mode 100644 (file)
index f3b3704..0000000
+++ /dev/null
@@ -1,289 +0,0 @@
-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.sysdyn.ui.utils;\r
-\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.StringTokenizer;\r
-import java.util.regex.Matcher;\r
-import java.util.regex.Pattern;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.manager.SysdynModel;\r
-import org.simantics.sysdyn.representation.Configuration;\r
-import org.simantics.sysdyn.representation.IElement;\r
-import org.simantics.sysdyn.representation.Model;\r
-import org.simantics.sysdyn.representation.Module;\r
-import org.simantics.sysdyn.representation.ModuleType;\r
-import org.simantics.sysdyn.representation.Variable;\r
-import org.simantics.ui.SimanticsUI;\r
-\r
-public class VariableNameUtils {\r
-\r
-       /**\r
-        * Replaces variable names in all the expressions in the defined configuration. \r
-        * Replace doesn't have to replace words in other configurations than the configuration where the renamed variable is\r
-        * because input variables handle the transitions between configurations (modules).\r
-        * \r
-        * @param graph WriteGraph\r
-        * @param configuration The configuration where the renamed variable is located\r
-        * @param originalName The original name of the variable\r
-        * @param newName New name of the variable\r
-        */\r
-       public static void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName) throws DatabaseException {\r
-               /*FIXME: \r
-                * How this works when range used in equations has the same string as\r
-                * the renamed variable? Should it be possible? \r
-                */ \r
-        if(originalName.equals(newName))\r
-            return; // Do nothing\r
-               \r
-        Layer0 l0 = Layer0.getInstance(graph);\r
-        SysdynResource sr = SysdynResource.getInstance(graph);\r
-        \r
-        Resource expressions = graph.getPossibleObject(variable, sr.Variable_expressionList);\r
-        if(expressions  != null) {\r
-            List<Resource> expressionList = ListUtils.toList(graph, expressions);\r
-            for(Resource s : expressionList) {\r
-                for(Resource p : graph.getPredicates(s)) {\r
-                    Resource o = graph.getPossibleObject(s, p);\r
-                    if(o != null && graph.isInstanceOf(o, l0.String) && !p.equals(sr.Expression_arrayRange)) {\r
-                        String string = graph.getRelatedValue(s, p);\r
-                        String replaced  = replaceAllWords(string, originalName, newName);\r
-                        graph.claimLiteral(s, p, replaced);\r
-                    }\r
-                }\r
-            }\r
-        }\r
-       }\r
-       \r
-       public static void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException {\r
-           if(originalName.equals(newName))\r
-               return; // Do nothing\r
-\r
-           Layer0 l0 = Layer0.getInstance(graph);\r
-           SysdynResource sr = SysdynResource.getInstance(graph);\r
-           for(Resource r : graph.getObjects(configuration, l0.ConsistsOf)) {\r
-               if(graph.isInstanceOf(r, sr.IndependentVariable)) {\r
-                   renameInEquations(graph, r, originalName, newName);\r
-               }\r
-           }\r
-       }\r
-\r
-       private static String replaceAllWords(String original, String find, String replacement) {\r
-               if(!original.contains(find)) return original;\r
-               StringBuilder result = new StringBuilder(original.length());\r
-               String delimiters = "+-*/(){}[],.: \t\n\r\f";\r
-               StringTokenizer st = new StringTokenizer(original, delimiters, true);\r
-               while (st.hasMoreTokens()) {\r
-                       String w = st.nextToken();\r
-                       if (w.equals(find)) {\r
-                               result.append(replacement);\r
-                       } else {\r
-                               result.append(w);\r
-                       }\r
-               }\r
-               return result.toString();\r
-       }\r
-\r
-\r
-       private static boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException {\r
-               if(variable == null)\r
-                       return false;\r
-               SysdynModel model = ModelUtils.getModel(graph, variable);\r
-               if(model == null)\r
-                       return true;\r
-               Configuration configuration = model.getConfiguration();\r
-               if(configuration == null)\r
-                   return true;\r
-               \r
-               // Browse through variables and modules within the current module\r
-               IElement current = model.getElement(variable);\r
-               for(IElement e : configuration.getElements()) {\r
-                       if(e instanceof Variable) {\r
-                               Variable v = (Variable) e;\r
-                               if(!v.equals(current) && v.getName() != null && v.getName().equals(name)) {\r
-                                       return true;\r
-                               }\r
-                       } else if (e instanceof Module) {\r
-                Module m = (Module)e;\r
-                if(!m.equals(current) && m.getName() != null && m.getName().equals(name)) {\r
-                    return true;\r
-                }\r
-            }\r
-               }\r
-               \r
-               // Browse through all ModuleTypes within the whole model\r
-               Model rootModel = null;\r
-               ModuleType moduleType = configuration.getModuleType();\r
-               if (moduleType != null) {\r
-                   Object o = moduleType.getParent();\r
-                   if (o instanceof Model) {\r
-                       rootModel = (Model)o;\r
-                   }\r
-               } else {\r
-                   rootModel = configuration.getModel();\r
-               }\r
-               if (rootModel != null)\r
-                   return rootModel.containsModuleType(name);\r
-               \r
-               // We should not go here I think\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Checks that the syntax of the given name is valid and there \r
-        * are no other variables that have the same name in the configuration\r
-        *  \r
-        * @param graph ReadGraph\r
-        * @param variable The variable that is being renamed\r
-        * @param name The new name of the variable\r
-        * @return\r
-        * @throws DatabaseException\r
-        */\r
-       public static boolean isValid(ReadGraph graph, Resource variable, String name, boolean hasRange) throws DatabaseException {\r
-               if(name.length() < 1)\r
-                       return false;\r
-               if(hasRange) {\r
-                       String range = null;\r
-                       if(name.contains("[")) {\r
-                               StringTokenizer st = new StringTokenizer(name, "[]", true);\r
-                               if(st.countTokens() != 4)\r
-                                       return false;\r
-                               name = st.nextToken();\r
-                               if(!st.nextToken().equals("[")) return false;\r
-                               range = st.nextToken();\r
-                               if(!st.nextToken().equals("]")) return false;\r
-                       }\r
-                       if(range != null && !ArrayVariableUtils.isRangeValid(graph, variable, range)) return false;\r
-               }\r
-               if(nameIsTaken(graph, variable, name)) return false;\r
-               if(!isValid(name)) return false;\r
-               return true;\r
-       }\r
-\r
-       \r
-       /**\r
-        * Checks that the syntax of the given name is valid and there \r
-        * are no other variables that have the same name in the configuration\r
-        *  \r
-        * @param variable The variable that is being renamed\r
-        * @param name The new name of the variable\r
-        * @return\r
-        * @throws DatabaseException\r
-        */\r
-       public static boolean isValid(final Resource variable, final String name, final boolean hasRange) {\r
-               boolean result = false;\r
-               try {\r
-                       result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
-\r
-                               @Override\r
-                               public Boolean perform(ReadGraph graph) throws DatabaseException {\r
-                                       return isValid(graph, variable, name, hasRange);\r
-                               }\r
-\r
-                       });\r
-               } catch (DatabaseException e) {\r
-                       e.printStackTrace();\r
-               }\r
-               return result;\r
-       }\r
-\r
-       /**\r
-        * Checks that the syntax of the given name is valid \r
-        * and that it is not a keyword in Modelica.\r
-        * \r
-        * @param name\r
-        * @return\r
-        */\r
-       public static boolean isValid(String name) {\r
-               String lowerCase = name.toLowerCase();\r
-               Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*+");\r
-               Matcher m = p.matcher(lowerCase);\r
-               if (!m.matches() || keywords.contains(name))\r
-                       return false;\r
-               else\r
-                       return true;\r
-       }\r
-\r
-       public static final Set<String> keywords = new HashSet<String>();\r
-\r
-       static {\r
-               keywords.add("algorithm");\r
-               keywords.add("discrete");\r
-               keywords.add("false");\r
-               keywords.add("model");\r
-               keywords.add("redeclare");\r
-               keywords.add("and");\r
-               keywords.add("each");\r
-               keywords.add("final");\r
-               keywords.add("not");\r
-               keywords.add("replaceable");\r
-               keywords.add("annotation");\r
-               keywords.add("else");\r
-               keywords.add("flow");\r
-               keywords.add("operator");\r
-               keywords.add("return");\r
-               keywords.add("assert");\r
-               keywords.add("elseif");\r
-               keywords.add("for");\r
-               keywords.add("or");\r
-               keywords.add("stream");\r
-               keywords.add("block");\r
-               keywords.add("elsewhen");\r
-               keywords.add("function");\r
-               keywords.add("outer");\r
-               keywords.add("then");\r
-               keywords.add("break");\r
-               keywords.add("encapsulated");\r
-               keywords.add("if");\r
-               keywords.add("output");\r
-               keywords.add("true");\r
-               keywords.add("class");\r
-               keywords.add("end");\r
-               keywords.add("import");\r
-               keywords.add("package");\r
-               keywords.add("type");\r
-               keywords.add("connect");\r
-               keywords.add("enumeration");\r
-               keywords.add("in");\r
-               keywords.add("parameter");\r
-               keywords.add("when");\r
-               keywords.add("connector");\r
-               keywords.add("equation");\r
-               keywords.add("initial");\r
-               keywords.add("partial");\r
-               keywords.add("while");\r
-               keywords.add("constant");\r
-               keywords.add("expandable");\r
-               keywords.add("inner");\r
-               keywords.add("protected");\r
-               keywords.add("within");\r
-               keywords.add("constrainedby");\r
-               keywords.add("extends");\r
-               keywords.add("input");\r
-               keywords.add("public");\r
-               keywords.add("der");\r
-               keywords.add("external");\r
-               keywords.add("loop");\r
-               keywords.add("record");\r
-               keywords.add("time");\r
-       }\r
-}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java
new file mode 100644 (file)
index 0000000..969517b
--- /dev/null
@@ -0,0 +1,219 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import java.util.List;\r
+import java.util.StringTokenizer;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.Model;\r
+import org.simantics.sysdyn.representation.ModuleType;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+/**\r
+ * Name validator for variables, modules, and enumerations.\r
+ * @author Teemu Lempinen\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class VariableNameValidator extends NameValidator {\r
+\r
+       @Override\r
+    public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName) throws DatabaseException {\r
+               /*FIXME: \r
+                * How this works when range used in equations has the same string as\r
+                * the renamed variable? Should it be possible? \r
+                */ \r
+        if(originalName.equals(newName))\r
+            return; // Do nothing\r
+               \r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        \r
+        Resource expressions = graph.getPossibleObject(variable, sr.Variable_expressionList);\r
+        if(expressions  != null) {\r
+            List<Resource> expressionList = ListUtils.toList(graph, expressions);\r
+            for(Resource s : expressionList) {\r
+                for(Resource p : graph.getPredicates(s)) {\r
+                    Resource o = graph.getPossibleObject(s, p);\r
+                    if(o != null && graph.isInstanceOf(o, l0.String) && !p.equals(sr.Expression_arrayRange)) {\r
+                        String string = graph.getRelatedValue(s, p);\r
+                        String replaced  = replaceAllWords(string, originalName, newName);\r
+                        graph.claimLiteral(s, p, replaced);\r
+                    }\r
+                }\r
+            }\r
+        }\r
+       }\r
+       \r
+       @Override\r
+    public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException {\r
+           if(originalName.equals(newName))\r
+               return; // Do nothing\r
+\r
+           Layer0 l0 = Layer0.getInstance(graph);\r
+           SysdynResource sr = SysdynResource.getInstance(graph);\r
+           for(Resource r : graph.getObjects(configuration, l0.ConsistsOf)) {\r
+               if(graph.isInstanceOf(r, sr.IndependentVariable)) {\r
+                   renameInEquations(graph, r, originalName, newName);\r
+               }\r
+           }\r
+       }\r
+\r
+       private String replaceAllWords(String original, String find, String replacement) {\r
+               if(!original.contains(find)) return original;\r
+               StringBuilder result = new StringBuilder(original.length());\r
+               String delimiters = "+-*/(){}[],.: \t\n\r\f";\r
+               StringTokenizer st = new StringTokenizer(original, delimiters, true);\r
+               while (st.hasMoreTokens()) {\r
+                       String w = st.nextToken();\r
+                       if (w.equals(find)) {\r
+                               result.append(replacement);\r
+                       } else {\r
+                               result.append(w);\r
+                       }\r
+               }\r
+               return result.toString();\r
+       }\r
+\r
+\r
+       @Override\r
+    protected boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+        if(variable == null)\r
+                       return false;\r
+        \r
+               SysdynModel sysdynModel = ModelUtils.getModel(graph, variable);\r
+               if(sysdynModel == null)\r
+                       return true;\r
+               sysdynModel.update(graph);\r
+        \r
+               Configuration configuration = sysdynModel.getConfiguration();\r
+               if(configuration == null)\r
+                   return true;\r
+               \r
+               // Get the top level model\r
+        Model rootModel = null;\r
+        ModuleType moduleType = configuration.getModuleType();\r
+        if (moduleType != null) {\r
+            Object o = moduleType.getParent();\r
+            if (o instanceof Model) {\r
+                rootModel = (Model)o;\r
+            }\r
+        } else {\r
+            rootModel = configuration.getModel();\r
+        }\r
+        \r
+        // Get the top level model (SysdynModel)\r
+        // Resource library should be used with care if it is not instance of sr.SysdynModel\r
+        Resource library = graph.getPossibleObject(variable, l0.PartOf);\r
+        if(library == null) {\r
+            return true;\r
+        } else {\r
+            library = graph.getPossibleObject(library, l0.PartOf);\r
+            if(library == null) {\r
+                return true;\r
+            }\r
+        }\r
+\r
+               // Check if the function is right under the model.\r
+        if (graph.isInstanceOf(library, sr.SysdynModel)) {\r
+            // Check that the function name != model name (== library name)\r
+            if (NameUtils.getSafeName(graph, library).equals(name)) return true;\r
+            \r
+            if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+            if (nameTakenBySharedFunctionLibrary(graph, library, null, name)) return true;\r
+            \r
+            if (nameTakenBySheet(sysdynModel, name)) return true;\r
+        }\r
+\r
+               if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, variable, name)) return true;\r
+\r
+               if (rootModel != null) {\r
+                   // Browse through all ModuleTypes within the whole model\r
+               if (rootModel.containsModuleType(name))\r
+                       return true;\r
+               }\r
+               \r
+               if (nameTakenByFunctionFunctionLibraryOrModuleType(graph, library, name)) return true;\r
+               \r
+               return false;\r
+       }\r
+\r
+       /**\r
+        * Checks that the syntax of the given name is valid and there \r
+        * are no other variables that have the same name in the configuration\r
+        *  \r
+        * @param graph ReadGraph\r
+        * @param variable The variable that is being renamed\r
+        * @param name The new name of the variable\r
+        * @return\r
+        * @throws DatabaseException\r
+        */\r
+       public boolean isValid(ReadGraph graph, Resource variable, String name, boolean hasRange) throws DatabaseException {\r
+           if(hasRange) {\r
+                       String range = null;\r
+                       if(name.contains("[")) {\r
+                               StringTokenizer st = new StringTokenizer(name, "[]", true);\r
+                               if(st.countTokens() != 4)\r
+                                       return false;\r
+                               name = st.nextToken();\r
+                               if(!st.nextToken().equals("[")) return false;\r
+                               range = st.nextToken();\r
+                               if(!st.nextToken().equals("]")) return false;\r
+                       }\r
+                       if(range != null && !ArrayVariableUtils.isRangeValid(graph, variable, range)) return false;\r
+               }\r
+               return isValid(graph, variable, name);\r
+       }\r
+\r
+       \r
+       /**\r
+        * Checks that the syntax of the given name is valid and there \r
+        * are no other variables that have the same name in the configuration\r
+        *  \r
+        * @param variable The variable that is being renamed\r
+        * @param name The new name of the variable\r
+        * @return\r
+        * @throws DatabaseException\r
+        */\r
+       public boolean isValid(final Resource variable, final String name, final boolean hasRange) {\r
+               boolean result = false;\r
+               try {\r
+                       result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+                               @Override\r
+                               public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                                       return isValid(graph, variable, name, hasRange);\r
+                               }\r
+\r
+                       });\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               return result;\r
+       }\r
+\r
+}\r
index bb4e3f66fd4a476af0cfce38e94c9f39753ec396..5403623ec0060f360bb0f06de421c5a3b7cf3577 100644 (file)
@@ -178,6 +178,30 @@ public class Model {
         return false;\r
     }\r
     \r
+    /**\r
+     * Determine if this Model consists of a certain Function or \r
+     * FunctionLibrary ON THE TOP LEVEL.\r
+     * @param elementName The name of the element that is sought \r
+     * @return true iff this consists of element\r
+     */\r
+    public boolean containsFunctionOrLibrary(String elementName) {\r
+        if(consistsOf != null ) {\r
+            for(Object o : consistsOf) {\r
+                if(o instanceof Function) {\r
+                    Function f = (Function)o;\r
+                    if (f.getName().equals(elementName)) \r
+                        return true;\r
+                }\r
+                else if(o instanceof FunctionLibrary) {\r
+                    FunctionLibrary fl = (FunctionLibrary)o;\r
+                    if (fl.getName().equals(elementName)) \r
+                        return true;\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
     /**\r
      * Add built-in functions to functions map. Built-in functions do not have any library path.\r
      * @param library Built-in function library\r