From 504fe6380bb10e6c99744332bb2e09c81e7763a0 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 30 Mar 2010 10:16:23 +0000 Subject: [PATCH] Added mapping listener for updating the contents of the fields git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@15268 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/configuration/ConfigurationView.java | 164 +++++++++++++----- 1 file changed, 117 insertions(+), 47 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/configuration/ConfigurationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/configuration/ConfigurationView.java index bc9120f2..7f1bea27 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/configuration/ConfigurationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/configuration/ConfigurationView.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.configuration; +import javax.swing.SwingUtilities; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; @@ -30,10 +32,13 @@ import org.eclipse.ui.IPartListener2; import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; +import org.simantics.db.ReadGraph; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.IMapping; +import org.simantics.objmap.IMappingListener; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.Configuration; @@ -45,6 +50,7 @@ public class ConfigurationView extends ViewPart { static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL); + IMapping mapping; SysdynDiagramEditor editor; Text name; Text startTime; @@ -55,12 +61,13 @@ public class ConfigurationView extends ViewPart { boolean disposed = false; IPartListener2 editorSelectionListener; + IMappingListener mappingListener; @Override public void createPartControl(Composite parent) { GridDataFactory.fillDefaults().applyTo(parent); GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).spacing(3, 3).applyTo(parent); - + Label label = new Label(parent, SWT.NONE); label.setText("Name"); @@ -76,7 +83,7 @@ public class ConfigurationView extends ViewPart { GridDataFactory.fillDefaults().hint(size).applyTo(name); name.addKeyListener(new EscListener()); name.addVerifyListener(new VerifyListener() { - + @Override public void verifyText(VerifyEvent e) { e.doit = false; @@ -86,7 +93,7 @@ public class ConfigurationView extends ViewPart { } } }); - + label = new Label(parent, SWT.NONE); label.setText("Start time"); @@ -111,8 +118,9 @@ public class ConfigurationView extends ViewPart { @Override public void partActivated(IWorkbenchPartReference partRef) { if(partRef.getPart(false) instanceof SysdynDiagramEditor) { - editor = (SysdynDiagramEditor)partRef.getPart(false); - setFields(); + if(!((SysdynDiagramEditor)partRef.getPart(false)).equals(editor)) { + initializeFields((SysdynDiagramEditor)partRef.getPart(false)); + } } } @@ -126,16 +134,11 @@ public class ConfigurationView extends ViewPart { @Override public void partDeactivated(IWorkbenchPartReference partRef) { - if(partRef.getPart(false) instanceof ConfigurationView && !disposed && editor != null) { + if(partRef.getPart(false) instanceof ConfigurationView && !disposed && editor != null && mapping != null) { boolean modified = false; - IEditorInput input = editor.getEditorInput(); - ResourceEditorInput rei = (ResourceEditorInput) input; - SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); - SysdynModel model = sdm.getModel(rei.getResource()); - Configuration conf = model.getConfiguration(); + Configuration conf = (Configuration)mapping.get(editor.getResourceInput().getResource()); - final IMapping mapping = model.getMapping(); if(!conf.name.equals(name.getText())){ conf.name = name.getText(); modified = true; @@ -150,18 +153,7 @@ public class ConfigurationView extends ViewPart { } if(modified) { - mapping.rangeModified(conf); - try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - mapping.updateDomain(graph); - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } + mapping.rangeModified(conf); } @@ -185,8 +177,9 @@ public class ConfigurationView extends ViewPart { public void partVisible(IWorkbenchPartReference partRef) { } }; - this.getViewSite().getPage().addPartListener(editorSelectionListener); - setFields(); + this.getViewSite().getPage().addPartListener(editorSelectionListener); + + initializeFields(null); } @@ -198,24 +191,32 @@ public class ConfigurationView extends ViewPart { @Override public void dispose() { this.getViewSite().getPage().removePartListener(editorSelectionListener); + if(this.mapping != null) + this.mapping.removeMappingListener(mappingListener); disposed = true; super.dispose(); } - private void setFields() { - - if(editor == null && PlatformUI.getWorkbench() - .getActiveWorkbenchWindow().getActivePage().getActiveEditor() instanceof SysdynDiagramEditor) { + private void updateEditor(SysdynDiagramEditor editor) { + if(editor != null) { + this.editor = editor; + } else if(PlatformUI.getWorkbench().getActiveWorkbenchWindow() + .getActivePage().getActiveEditor() instanceof SysdynDiagramEditor) { editor = (SysdynDiagramEditor)PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage().getActiveEditor(); } - if(editor != null) { - IEditorInput input = editor.getEditorInput(); - ResourceEditorInput rei = (ResourceEditorInput) input; - SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); - SysdynModel model = sdm.getModel(rei.getResource()); - Configuration conf = model.getConfiguration(); + } + + private void initializeFields(SysdynDiagramEditor editor) { + updateEditor(editor); + updateMapping(); + if(mapping != null) + setFields((Configuration)mapping.get(editor.getResourceInput().getResource())); + } + + private void setFields(Configuration conf) { + if(editor != null && mapping != null) { originalName = conf.name; name.setText(originalName); originalStart = conf.startTime.toString(); @@ -231,17 +232,18 @@ public class ConfigurationView extends ViewPart { @Override public void keyPressed(KeyEvent e) { - Text text = (Text)e.widget; - String originalText = null; - if(text == name) - originalText = originalName; - else if(text == startTime) - originalText = originalStart; - else if(text == stopTime) - originalText = originalStop; - if(e.keyCode == SWT.ESC && originalText != null) { - text.setText(originalText); - text.setSelection(0); + if(e.keyCode == SWT.ESC) { + Text text = (Text)e.widget; + if(text == name) { + text.setText(originalName); + text.setSelection(text.getCharCount()); + } else if(text == startTime) { + text.setText(originalStart); + text.setSelection(0); + } else if(text == stopTime) { + text.setText(originalStop); + text.setSelection(0); + } } } @@ -273,4 +275,72 @@ public class ConfigurationView extends ViewPart { } } } + + private void updateMapping() { + if(editor != null) { + IEditorInput input = editor.getEditorInput(); + ResourceEditorInput rei = (ResourceEditorInput) input; + SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); + SysdynModel model = sdm.getModel(rei.getResource()); + mapping = model.getMapping(); + mappingListener = new IMappingListener() { + + @Override + public void rangeModified() { + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) + throws DatabaseException { + mapping.updateDomain(graph); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + }); + } + + @Override + public void domainModified() { + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + try { + SimanticsUI.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + mapping.updateRange(graph); + if(editor != null) { + name.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + Configuration conf = (Configuration)mapping.get(editor.getResourceInput().getResource()); + setFields(conf); + } + }); + + } + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + }); + } + + }; + mapping.addMappingListener(mappingListener); + } + } } -- 2.47.1