*******************************************************************************/\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
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
\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
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
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
}\r
}\r
});\r
- \r
+\r
label = new Label(parent, SWT.NONE);\r
label.setText("Start time");\r
\r
@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
\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
}\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
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
@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
\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
}\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