]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18288 ac1ea38d-2e2b...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 7 Oct 2010 06:49:32 +0000 (06:49 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 7 Oct 2010 06:49:32 +0000 (06:49 +0000)
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java

index b88a8208d4a5129d35679d038122ceb1a0c562b4..49a839cacf3907d18ca5527588d909a5c67e7d20 100644 (file)
@@ -49,193 +49,193 @@ import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidato
 \r
 public class EquationTab extends PropertyTabContributorImpl {\r
 \r
-    TrackedCombo expressionTypeCombo, unitCombo;\r
-    ShortcutTabWidget shortcutTabWidget;\r
-    ExpressionWidget expressionWidget;\r
-    org.eclipse.ui.IPartListener2 focusLostListener;  \r
-    IWorkbenchSite site;\r
-\r
-    @Override\r
-    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
-       this.site = site;\r
-        Composite composite = new Composite(body, SWT.NONE);\r
-        GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
-        GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite);\r
-        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
-        GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
-\r
-\r
-        Label label = new Label(composite, SWT.SINGLE );\r
-        label.setText("Type:");\r
-        GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
-\r
-        expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
-\r
-        \r
-        label = new Label(composite, SWT.SINGLE );\r
-        label.setText("Unit:");\r
-        GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
-\r
-\r
-        unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER);\r
\r
-        shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE);\r
-\r
-\r
-        Composite expressionComposite = new Composite(composite, SWT.NONE);\r
-        GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite);\r
-        expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE);\r
-        expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable());\r
-\r
-        addListeners();\r
-    }\r
-\r
-    private void addListeners() {\r
-        \r
-        expressionTypeCombo.setItemFactory(new ReadFactoryImpl<Resource, Map<String,Object>>() {\r
-\r
-            @Override\r
-            public Map<String, Object> perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
-\r
-                Map<String, Object> map = new HashMap<String, Object>();\r
-                SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
-                final ExpressionType[] expressionTypes;\r
-                if(graph.isInstanceOf(input, sr.Auxiliary))\r
-                    expressionTypes = ExpressionTypes.auxiliaryExpressions;\r
-                else if(graph.isInstanceOf(input, sr.Stock))\r
-                    expressionTypes = ExpressionTypes.stockExpressions;\r
-                else if(graph.isInstanceOf(input, sr.Valve))\r
-                    expressionTypes = ExpressionTypes.valveExpressions;\r
-                else\r
-                    expressionTypes = new ExpressionType[] {};\r
-                \r
-                for(ExpressionType et : expressionTypes) {\r
-                    map.put(et.toString(), et);\r
-                }\r
-                return map;\r
-            }\r
-        });\r
-\r
-        expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl<Resource, String>() {\r
-\r
-            @Override\r
-            public String perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
-                return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString();\r
-            }\r
-        });\r
-        \r
-        expressionTypeCombo.addModifyListener(new TextModifyListener() {\r
-            \r
-            @Override\r
-            public void modifyText(TrackedModifyEvent e) {\r
-                expressionWidget.displayExpression(e.getText(), false);\r
-                expressionWidget.save();                \r
-            }\r
-        });\r
-\r
-        \r
-        unitCombo.setItemFactory(new ReadFactoryImpl<Resource, Map<String,Object>>() {\r
-\r
-            @Override\r
-            public Map<String, Object> perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
-                Map<String, Object> map = new HashMap<String, Object>();\r
-\r
-                SysdynResource sr = SysdynResource.getInstance(graph);\r
-                Layer0 l0 = Layer0.getInstance(graph);\r
-                Resource model = graph.getPossibleObject(input, l0.PartOf);\r
-                if (model != null) {\r
-                    Collection<Resource> variables = graph.getObjects(model, l0.ConsistsOf);\r
-                    for(Resource v : variables) {\r
-                        Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
-                        if (unit != null && !map.keySet().contains((String) unit)) {\r
-                            map.put((String)unit, (String)unit);\r
-\r
-                        }\r
-                    }\r
-                }\r
-                return map;\r
-            }\r
-        });\r
-        unitCombo.setSelectionFactory(new ReadFactoryImpl<Resource, String>() {\r
-\r
-            @Override\r
-            public String perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
-                String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).HasUnit);\r
-                if(unit == null)\r
-                    return "";\r
-                else \r
-                    return unit;\r
-            }\r
-        });\r
-\r
-        unitCombo.addModifyListener(new ComboModifyListenerImpl<Resource>() {\r
-\r
-            @Override\r
-            public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException {\r
-                graph.claimLiteral(input, SysdynResource.getInstance(graph).HasUnit, text);\r
-            }\r
-        });\r
-\r
-\r
-        shortcutTabWidget.addMouseListener(new MouseListener(){\r
-\r
-            @Override\r
-            public void mouseDoubleClick(MouseEvent e) {\r
-                Table table = (Table)e.widget;\r
-                TableItem item = table.getItem(new Point(e.x, e.y));\r
-                if(item != null) {\r
-                    final String var = (String)item.getData();\r
-                    table.getDisplay().asyncExec(new Runnable() {\r
-\r
-                        @Override\r
-                        public void run() {\r
-                            if(expressionWidget!= null) {\r
-                                expressionWidget.getExpression().replaceSelection(var);\r
-                                expressionWidget.getExpression().focus();\r
-                                expressionWidget.validateFields();\r
-                            }\r
-                        }\r
-                    });\r
-                }\r
-            }\r
-\r
-            @Override\r
-            public void mouseDown(MouseEvent e) {\r
-                expressionWidget.getExpression().focus();\r
-            }\r
-\r
-            @Override\r
-            public void mouseUp(MouseEvent e) {\r
-            }\r
-\r
-        });\r
-        shortcutTabWidget.addFocusListener(new FocusListener() {\r
-\r
-            @Override\r
-            public void focusGained(FocusEvent e) {    \r
-            }\r
-\r
-            @Override\r
-            public void focusLost(FocusEvent e) {\r
-                ((Table)e.widget).deselectAll();\r
-            }\r
-\r
-        });\r
-\r
-        expressionWidget.addModifyListener(new ModifyListener() {\r
-\r
-            @Override\r
-            public void modifyText(ModifyEvent e) {\r
-                expressionWidget.validateFields();\r
-            }\r
-        });\r
-        \r
-        expressionWidget.addVerifyKeyListener(new VerifyKeyListener() {\r
-                       \r
+       TrackedCombo expressionTypeCombo, unitCombo;\r
+       ShortcutTabWidget shortcutTabWidget;\r
+       ExpressionWidget expressionWidget;\r
+       org.eclipse.ui.IPartListener2 focusLostListener;  \r
+       IWorkbenchSite site;\r
+\r
+       @Override\r
+       public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+               this.site = site;\r
+               Composite composite = new Composite(body, SWT.NONE);\r
+               GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
+               GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite);\r
+               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
+               GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
+\r
+\r
+               Label label = new Label(composite, SWT.SINGLE );\r
+               label.setText("Type:");\r
+               GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
+\r
+               expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
+\r
+\r
+               label = new Label(composite, SWT.SINGLE );\r
+               label.setText("Unit:");\r
+               GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
+\r
+\r
+               unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER);\r
+\r
+               shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE);\r
+\r
+\r
+               Composite expressionComposite = new Composite(composite, SWT.NONE);\r
+               GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite);\r
+               expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE);\r
+               expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable());\r
+\r
+               addListeners();\r
+       }\r
+\r
+       private void addListeners() {\r
+\r
+               expressionTypeCombo.setItemFactory(new ReadFactoryImpl<Resource, Map<String,Object>>() {\r
+\r
+                       @Override\r
+                       public Map<String, Object> perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+\r
+                               Map<String, Object> map = new HashMap<String, Object>();\r
+                               SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+                               final ExpressionType[] expressionTypes;\r
+                               if(graph.isInstanceOf(input, sr.Auxiliary))\r
+                                       expressionTypes = ExpressionTypes.auxiliaryExpressions;\r
+                               else if(graph.isInstanceOf(input, sr.Stock))\r
+                                       expressionTypes = ExpressionTypes.stockExpressions;\r
+                               else if(graph.isInstanceOf(input, sr.Valve))\r
+                                       expressionTypes = ExpressionTypes.valveExpressions;\r
+                               else\r
+                                       expressionTypes = new ExpressionType[] {};\r
+\r
+                               for(ExpressionType et : expressionTypes) {\r
+                                       map.put(et.toString(), et);\r
+                               }\r
+                               return map;\r
+                       }\r
+               });\r
+\r
+               expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl<Resource, String>() {\r
+\r
+                       @Override\r
+                       public String perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+                               return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString();\r
+                       }\r
+               });\r
+\r
+               expressionTypeCombo.addModifyListener(new TextModifyListener() {\r
+\r
+                       @Override\r
+                       public void modifyText(TrackedModifyEvent e) {\r
+                               expressionWidget.displayExpression(e.getText(), false);\r
+                               expressionWidget.save();                \r
+                       }\r
+               });\r
+\r
+\r
+               unitCombo.setItemFactory(new ReadFactoryImpl<Resource, Map<String,Object>>() {\r
+\r
+                       @Override\r
+                       public Map<String, Object> perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+                               Map<String, Object> map = new HashMap<String, Object>();\r
+\r
+                               SysdynResource sr = SysdynResource.getInstance(graph);\r
+                               Layer0 l0 = Layer0.getInstance(graph);\r
+                               Resource model = graph.getPossibleObject(input, l0.PartOf);\r
+                               if (model != null) {\r
+                                       Collection<Resource> variables = graph.getObjects(model, l0.ConsistsOf);\r
+                                       for(Resource v : variables) {\r
+                                               Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
+                                               if (unit != null && !map.keySet().contains((String) unit)) {\r
+                                                       map.put((String)unit, (String)unit);\r
+\r
+                                               }\r
+                                       }\r
+                               }\r
+                               return map;\r
+                       }\r
+               });\r
+               unitCombo.setSelectionFactory(new ReadFactoryImpl<Resource, String>() {\r
+\r
+                       @Override\r
+                       public String perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+                               String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).HasUnit);\r
+                               if(unit == null)\r
+                                       return "";\r
+                               else \r
+                                       return unit;\r
+                       }\r
+               });\r
+\r
+               unitCombo.addModifyListener(new ComboModifyListenerImpl<Resource>() {\r
+\r
+                       @Override\r
+                       public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException {\r
+                               graph.claimLiteral(input, SysdynResource.getInstance(graph).HasUnit, text);\r
+                       }\r
+               });\r
+\r
+\r
+               shortcutTabWidget.addMouseListener(new MouseListener(){\r
+\r
+                       @Override\r
+                       public void mouseDoubleClick(MouseEvent e) {\r
+                               Table table = (Table)e.widget;\r
+                               TableItem item = table.getItem(new Point(e.x, e.y));\r
+                               if(item != null) {\r
+                                       final String var = (String)item.getData();\r
+                                       table.getDisplay().asyncExec(new Runnable() {\r
+\r
+                                               @Override\r
+                                               public void run() {\r
+                                                       if(expressionWidget!= null) {\r
+                                                               expressionWidget.getExpression().replaceSelection(var);\r
+                                                               expressionWidget.getExpression().focus();\r
+                                                               expressionWidget.validateFields();\r
+                                                       }\r
+                                               }\r
+                                       });\r
+                               }\r
+                       }\r
+\r
+                       @Override\r
+                       public void mouseDown(MouseEvent e) {\r
+                               expressionWidget.getExpression().focus();\r
+                       }\r
+\r
+                       @Override\r
+                       public void mouseUp(MouseEvent e) {\r
+                       }\r
+\r
+               });\r
+               shortcutTabWidget.addFocusListener(new FocusListener() {\r
+\r
+                       @Override\r
+                       public void focusGained(FocusEvent e) {    \r
+                       }\r
+\r
+                       @Override\r
+                       public void focusLost(FocusEvent e) {\r
+                               ((Table)e.widget).deselectAll();\r
+                       }\r
+\r
+               });\r
+\r
+               expressionWidget.addModifyListener(new ModifyListener() {\r
+\r
+                       @Override\r
+                       public void modifyText(ModifyEvent e) {\r
+                               expressionWidget.validateFields();\r
+                       }\r
+               });\r
+\r
+               expressionWidget.addVerifyKeyListener(new VerifyKeyListener() {\r
+\r
                        @Override\r
                        public void verifyKey(VerifyEvent event) {\r
                                if(event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {\r
@@ -247,34 +247,39 @@ public class EquationTab extends PropertyTabContributorImpl {
                                }\r
                        }\r
                });\r
-        \r
-        focusLostListener = new org.eclipse.ui.IPartListener2()\r
-        {\r
-            public void partInputChanged(IWorkbenchPartReference partRef) {}\r
-            public void partVisible(IWorkbenchPartReference partRef) {}\r
-            public void partHidden(IWorkbenchPartReference partRef) {}\r
-            public void partOpened(IWorkbenchPartReference partRef) {}\r
-            public void partDeactivated(IWorkbenchPartReference partRef)\r
-            {\r
-                if(partRef.getPart(false) instanceof PropertyPageView) {\r
-                    // Save expressions\r
-                    if(expressionWidget != null) {\r
-                       expressionWidget.save();\r
-                    }\r
-                    partRef.getPart(false).getSite().getPage().removePartListener(this);\r
-                }\r
-            }\r
-            public void partClosed(IWorkbenchPartReference partRef) {}\r
-            public void partBroughtToTop(IWorkbenchPartReference partRef) {}\r
-            public void partActivated(IWorkbenchPartReference partRef) {}\r
-        };\r
-        site.getPage().addPartListener(focusLostListener);\r
-    }\r
-    \r
-    @Override\r
-    public void dispose() {\r
-       if(focusLostListener != null && site != null)\r
-               site.getPage().removePartListener(focusLostListener);\r
-       super.dispose();\r
-    }\r
+\r
+               if(focusLostListener == null) {\r
+                       focusLostListener = new org.eclipse.ui.IPartListener2()\r
+                       {\r
+                               public void partInputChanged(IWorkbenchPartReference partRef) {}\r
+                               public void partVisible(IWorkbenchPartReference partRef) {}\r
+                               public void partHidden(IWorkbenchPartReference partRef) {}\r
+                               public void partOpened(IWorkbenchPartReference partRef) {}\r
+                               public void partDeactivated(IWorkbenchPartReference partRef)\r
+                               {\r
+                                       if(partRef.getPart(false) instanceof PropertyPageView) {\r
+                                               PropertyPageView ppv = (PropertyPageView)partRef.getPart(false);\r
+                                               if(ppv.getCurrentPage() instanceof SysdynPropertyPage) {\r
+                                                       // Save expressions\r
+                                                       if(expressionWidget != null) {\r
+                                                               expressionWidget.save();\r
+                                                       }\r
+                                                       site.getPage().removePartListener(this);\r
+                                               }\r
+                                       }\r
+                               }\r
+                               public void partClosed(IWorkbenchPartReference partRef) {}\r
+                               public void partBroughtToTop(IWorkbenchPartReference partRef) {}\r
+                               public void partActivated(IWorkbenchPartReference partRef) {}\r
+                       };\r
+                       site.getPage().addPartListener(focusLostListener);\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void dispose() {\r
+               if(focusLostListener != null && site != null)\r
+                       site.getPage().removePartListener(focusLostListener);\r
+               super.dispose();\r
+       }\r
 }\r
index db69a53ec9c7e0425eaa165d853aef395629431f..426deda8b538d031ce19d8d434d72db088c1d808 100644 (file)
@@ -105,6 +105,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                        @Override\r
                        public void run(ReadGraph g) throws DatabaseException {\r
                                Variable variable = getVariable(g, resources[0], runtime);\r
+                               if(variable == null) return;\r
                                SysdynModel model = getSysdynModel(g, Variables.getModel(g, variable));\r
                                Runnable listener = new Runnable() {\r
                                        @Override\r