]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Added mapping listener for updating the contents of the fields
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 30 Mar 2010 10:16:23 +0000 (10:16 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 30 Mar 2010 10:16:23 +0000 (10:16 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@15268 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/configuration/ConfigurationView.java

index bc9120f2084001845949242db8e86ea3966cc7cf..7f1bea276438323ee8b95a6da08dab25cc7c7e2f 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.configuration;\r
 \r
+import javax.swing.SwingUtilities;\r
+\r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.swt.SWT;\r
@@ -30,10 +32,13 @@ import org.eclipse.ui.IPartListener2;
 import org.eclipse.ui.IWorkbenchPartReference;\r
 import org.eclipse.ui.PlatformUI;\r
 import org.eclipse.ui.part.ViewPart;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.objmap.IMapping;\r
+import org.simantics.objmap.IMappingListener;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 import org.simantics.sysdyn.manager.SysdynModelManager;\r
 import org.simantics.sysdyn.representation.Configuration;\r
@@ -45,6 +50,7 @@ public class ConfigurationView  extends ViewPart {
 \r
     static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL);\r
 \r
+    IMapping mapping;\r
     SysdynDiagramEditor editor;\r
     Text name;\r
     Text startTime;\r
@@ -55,12 +61,13 @@ public class ConfigurationView  extends ViewPart {
     boolean disposed = false;\r
 \r
     IPartListener2 editorSelectionListener;\r
+    IMappingListener mappingListener;\r
 \r
     @Override\r
     public void createPartControl(Composite parent) {\r
         GridDataFactory.fillDefaults().applyTo(parent);\r
         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).spacing(3, 3).applyTo(parent);\r
-        \r
+\r
         Label label = new Label(parent, SWT.NONE);\r
         label.setText("Name");\r
 \r
@@ -76,7 +83,7 @@ public class ConfigurationView  extends ViewPart {
         GridDataFactory.fillDefaults().hint(size).applyTo(name);\r
         name.addKeyListener(new EscListener());\r
         name.addVerifyListener(new VerifyListener() {\r
-            \r
+\r
             @Override\r
             public void verifyText(VerifyEvent e) {\r
                 e.doit = false;\r
@@ -86,7 +93,7 @@ public class ConfigurationView  extends ViewPart {
                 }\r
             }\r
         });\r
-        \r
+\r
         label = new Label(parent, SWT.NONE);\r
         label.setText("Start time");\r
 \r
@@ -111,8 +118,9 @@ public class ConfigurationView  extends ViewPart {
             @Override\r
             public void partActivated(IWorkbenchPartReference partRef) {\r
                 if(partRef.getPart(false) instanceof SysdynDiagramEditor) {\r
-                    editor = (SysdynDiagramEditor)partRef.getPart(false);\r
-                    setFields();\r
+                    if(!((SysdynDiagramEditor)partRef.getPart(false)).equals(editor)) {\r
+                        initializeFields((SysdynDiagramEditor)partRef.getPart(false));\r
+                    }\r
                 }\r
             }\r
 \r
@@ -126,16 +134,11 @@ public class ConfigurationView  extends ViewPart {
 \r
             @Override\r
             public void partDeactivated(IWorkbenchPartReference partRef) {\r
-                if(partRef.getPart(false) instanceof ConfigurationView && !disposed && editor != null) {\r
+                if(partRef.getPart(false) instanceof ConfigurationView && !disposed && editor != null && mapping != null) {\r
                     boolean modified = false;\r
 \r
-                    IEditorInput input =  editor.getEditorInput();\r
-                    ResourceEditorInput rei = (ResourceEditorInput) input;\r
-                    SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
-                    SysdynModel model = sdm.getModel(rei.getResource()); \r
-                    Configuration conf = model.getConfiguration();\r
+                    Configuration conf = (Configuration)mapping.get(editor.getResourceInput().getResource());\r
 \r
-                    final IMapping mapping = model.getMapping();\r
                     if(!conf.name.equals(name.getText())){\r
                         conf.name = name.getText();\r
                         modified = true;\r
@@ -150,18 +153,7 @@ public class ConfigurationView  extends ViewPart {
                     }\r
 \r
                     if(modified) {\r
-                        mapping.rangeModified(conf);                    \r
-                        try {\r
-                            SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
-\r
-                                @Override\r
-                                public void perform(WriteGraph graph) throws DatabaseException {\r
-                                    mapping.updateDomain(graph);                         \r
-                                }\r
-                            });\r
-                        } catch (DatabaseException e) {\r
-                            e.printStackTrace();\r
-                        }\r
+                        mapping.rangeModified(conf);     \r
                     }\r
 \r
 \r
@@ -185,8 +177,9 @@ public class ConfigurationView  extends ViewPart {
             public void partVisible(IWorkbenchPartReference partRef) {\r
             }\r
         };\r
-        this.getViewSite().getPage().addPartListener(editorSelectionListener);\r
-        setFields();\r
+        this.getViewSite().getPage().addPartListener(editorSelectionListener);  \r
+\r
+        initializeFields(null);\r
     }\r
 \r
 \r
@@ -198,24 +191,32 @@ public class ConfigurationView  extends ViewPart {
     @Override\r
     public void dispose() {\r
         this.getViewSite().getPage().removePartListener(editorSelectionListener);\r
+        if(this.mapping != null)\r
+            this.mapping.removeMappingListener(mappingListener);\r
         disposed = true;\r
         super.dispose();\r
     }\r
 \r
-    private void setFields() {\r
-\r
-        if(editor == null && PlatformUI.getWorkbench()\r
-                .getActiveWorkbenchWindow().getActivePage().getActiveEditor() instanceof SysdynDiagramEditor) {\r
+    private void updateEditor(SysdynDiagramEditor editor) {\r
+        if(editor != null) {\r
+            this.editor = editor;\r
+        } else if(PlatformUI.getWorkbench().getActiveWorkbenchWindow()\r
+                .getActivePage().getActiveEditor() instanceof SysdynDiagramEditor) {\r
             editor = (SysdynDiagramEditor)PlatformUI.getWorkbench()\r
             .getActiveWorkbenchWindow().getActivePage().getActiveEditor();\r
         }\r
-        if(editor != null) {\r
-            IEditorInput input =  editor.getEditorInput();\r
-            ResourceEditorInput rei = (ResourceEditorInput) input;\r
-            SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
-            SysdynModel model = sdm.getModel(rei.getResource());\r
-            Configuration conf = model.getConfiguration();\r
+    }\r
+\r
+    private void initializeFields(SysdynDiagramEditor editor) {\r
+        updateEditor(editor);\r
+        updateMapping();\r
+        if(mapping != null)\r
+            setFields((Configuration)mapping.get(editor.getResourceInput().getResource()));\r
+    }\r
+\r
+    private void setFields(Configuration conf) {\r
 \r
+        if(editor != null && mapping != null) {       \r
             originalName = conf.name;\r
             name.setText(originalName);\r
             originalStart = conf.startTime.toString();\r
@@ -231,17 +232,18 @@ public class ConfigurationView  extends ViewPart {
 \r
         @Override\r
         public void keyPressed(KeyEvent e) {\r
-            Text text = (Text)e.widget;\r
-            String originalText = null;\r
-            if(text == name)\r
-                originalText = originalName;\r
-            else if(text == startTime)\r
-                originalText = originalStart;\r
-            else if(text == stopTime)\r
-                originalText = originalStop;\r
-            if(e.keyCode == SWT.ESC && originalText != null) {\r
-                text.setText(originalText);\r
-                text.setSelection(0);\r
+            if(e.keyCode == SWT.ESC) {\r
+                Text text = (Text)e.widget;\r
+                if(text == name) {\r
+                    text.setText(originalName);\r
+                    text.setSelection(text.getCharCount());\r
+                } else if(text == startTime) {\r
+                    text.setText(originalStart);\r
+                    text.setSelection(0);\r
+                } else if(text == stopTime) {\r
+                    text.setText(originalStop);\r
+                    text.setSelection(0);\r
+                }\r
             }\r
         }\r
 \r
@@ -273,4 +275,72 @@ public class ConfigurationView  extends ViewPart {
             }\r
         }\r
     }\r
+\r
+    private void updateMapping() {\r
+        if(editor != null) {\r
+            IEditorInput input =  editor.getEditorInput();\r
+            ResourceEditorInput rei = (ResourceEditorInput) input;\r
+            SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
+            SysdynModel model = sdm.getModel(rei.getResource()); \r
+            mapping = model.getMapping();\r
+            mappingListener = new IMappingListener() {\r
+\r
+                @Override\r
+                public void rangeModified() {\r
+                    SwingUtilities.invokeLater(new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+                            try {\r
+                                SimanticsUI.getSession().syncRequest(new WriteRequest() {                                \r
+                                    @Override\r
+                                    public void perform(WriteGraph graph)\r
+                                    throws DatabaseException {\r
+                                        mapping.updateDomain(graph);\r
+                                    }                                \r
+                                });\r
+                            } catch (DatabaseException e) {\r
+                                e.printStackTrace();\r
+                            }                        \r
+                        }\r
+\r
+                    });             \r
+                }\r
+\r
+                @Override\r
+                public void domainModified() {\r
+                    SwingUtilities.invokeLater(new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+                            try {\r
+                                SimanticsUI.getSession().syncRequest(new ReadRequest() {                            \r
+                                    @Override\r
+                                    public void run(ReadGraph graph) throws DatabaseException {\r
+                                        mapping.updateRange(graph);\r
+                                        if(editor != null) {\r
+                                            name.getDisplay().asyncExec(new Runnable() {\r
+\r
+                                                @Override\r
+                                                public void run() {\r
+                                                    Configuration conf = (Configuration)mapping.get(editor.getResourceInput().getResource());\r
+                                                    setFields(conf);                                             \r
+                                                }\r
+                                            });\r
+\r
+                                        }\r
+                                    }\r
+                                });\r
+                            } catch (DatabaseException e) {\r
+                                e.printStackTrace();\r
+                            }          \r
+                        }\r
+\r
+                    });                 \r
+                }\r
+\r
+            };\r
+            mapping.addMappingListener(mappingListener);\r
+        }\r
+    }\r
 }\r