]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Properties
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 14 Jun 2010 14:10:18 +0000 (14:10 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 14 Jun 2010 14:10:18 +0000 (14:10 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16153 ac1ea38d-2e2b-0410-8846-a27921b304fc

16 files changed:
org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/dependencies/DependencyView.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/SysdynExperimentActivator.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExperimentTab.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModelTab.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResultTab.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SysdynPropertyPage.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/NameWidget.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java [new file with mode: 0644]

index fedd9c93b7437fab1cd76f6b4120c1dbe6d2c5d2..8c869e243bc4d1ebf4cfd1bafdff63b6ee468a15 100644 (file)
             id="org.simantics.sysdyn.ui.basicExperiment"\r
             name="Basic Experiment">\r
       </context>\r
+   </extension>\r
+   <extension\r
+         point="org.simantics.browsing.ui.common.selectionProcessorBinding">\r
+      <binding\r
+            browseContext="http://www.simantics.org/Sysdyn-1.0/Browser">\r
+         <implementation\r
+               class="org.simantics.sysdyn.ui.properties.ResourceSelectionProcessor">\r
+         </implementation>\r
+      </binding>\r
+   </extension>\r
+   <extension\r
+         point="org.eclipse.ui.propertyPages">\r
+      <page\r
+            class="org.simantics.sysdyn.ui.WorkbenchPropertyPage1"\r
+            id="org.simantics.sysdyn.ui.page1"\r
+            name="name">\r
+      </page>\r
    </extension>
 </plugin>\r
index bc107306027ab7963e248261d6ce72c64d776c25..630d55df462c9bd4df431e16fa6a9add1db0c91d 100644 (file)
@@ -18,7 +18,9 @@ import org.eclipse.swt.widgets.Composite;
 import org.simantics.browsing.ui.swt.ContextMenuInitializer;\r
 import org.simantics.browsing.ui.swt.GraphExplorerView;\r
 import org.simantics.browsing.ui.swt.IContextMenuInitializer;\r
+import org.simantics.browsing.ui.swt.IPropertyPage;\r
 import org.simantics.sysdyn.ui.Activator;\r
+import org.simantics.sysdyn.ui.properties.SysdynPropertyPage;\r
 \r
 \r
 public class SysdynBrowser extends GraphExplorerView {\r
@@ -32,6 +34,7 @@ public class SysdynBrowser extends GraphExplorerView {
     \r
     @Override\r
     protected Set<String> getBrowseContexts() {\r
+        System.out.println("browsecontextit: " + browseContexts);\r
         return browseContexts;\r
     }\r
     \r
@@ -45,4 +48,13 @@ public class SysdynBrowser extends GraphExplorerView {
         //IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();\r
         //toolBar.add(new HomeAction());\r
     }\r
+    \r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public Object getAdapter(Class adapter) {\r
+        System.out.println("get adapter: " + adapter);\r
+        if (adapter == IPropertyPage.class)\r
+            return new SysdynPropertyPage(getSite(), Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser"));\r
+        return super.getAdapter(adapter);\r
+    }\r
 }\r
index bb500430995df6aa1cba1c3443b3fffb081de75c..2be3a23152f26242f57601d5ef8672d348569b07 100644 (file)
@@ -1,5 +1,7 @@
 package org.simantics.sysdyn.ui.dependencies;\r
 \r
+import java.util.Set;\r
+\r
 import org.eclipse.core.runtime.IProgressMonitor;\r
 import org.eclipse.core.runtime.IStatus;\r
 import org.eclipse.core.runtime.Status;\r
@@ -26,6 +28,7 @@ import org.simantics.db.procedure.Listener;
 import org.simantics.graphviz.Graph;\r
 import org.simantics.graphviz.ui.GraphvizComponent;\r
 import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
 \r
 public class DependencyView extends ViewPart {\r
 \r
@@ -123,8 +126,10 @@ public class DependencyView extends ViewPart {
         if(selection instanceof IStructuredSelection) {                    \r
             Object[] els = ((IStructuredSelection) selection).toArray();\r
             if(els.length == 1) {\r
-                if(els[0] instanceof Resource && !els[0].equals(currentSelection)) {\r
-                    currentSelection = (Resource)els[0];\r
+                Set<Resource> ress = ISelectionUtils.filterSetSelection(selection, Resource.class);\r
+                Resource r = (ress.toArray(Resource.NONE))[0];\r
+                if(r != null && !r.equals(currentSelection)) {\r
+                    currentSelection = r;\r
                     readGraph(currentSelection);\r
                 }\r
             }\r
index f6ea01a3660d46b028e4c651507508adcadcf9ba..d57867f1e4e198335ac751637c6113e28d4ff295 100644 (file)
@@ -13,7 +13,7 @@ package org.simantics.sysdyn.ui.editor;
 \r
 import java.awt.Frame;\r
 import java.util.ArrayList;\r
-import java.util.Collection;\r
+import java.util.Collections;\r
 \r
 import javax.swing.SwingUtilities;\r
 \r
@@ -24,6 +24,7 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IEditorInput;\r
 import org.eclipse.ui.IEditorSite;\r
 import org.eclipse.ui.PartInitException;\r
+import org.simantics.browsing.ui.swt.IPropertyPage;\r
 import org.simantics.db.Builtins;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
@@ -46,7 +47,6 @@ import org.simantics.h2d.element.IElement;
 import org.simantics.h2d.element.IElementListener;\r
 import org.simantics.h2d.event.handler.DefaultEventHandlers;\r
 import org.simantics.layer0.utils.triggers.IActivation;\r
-import org.simantics.layer0.utils.triggers.IActivationManager;\r
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.objmap.IMapping;\r
 import org.simantics.objmap.IMappingListener;\r
@@ -60,6 +60,7 @@ import org.simantics.sysdyn.ui.actions.CreateCloud;
 import org.simantics.sysdyn.ui.actions.CreateStock;\r
 import org.simantics.sysdyn.ui.actions.CreateValve;\r
 import org.simantics.sysdyn.ui.actions.OpenContextMenu;\r
+import org.simantics.sysdyn.ui.properties.SysdynPropertyPage;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.workbench.ResourceEditorPart;\r
 import org.simantics.ui.workbench.TitleRequest;\r
@@ -322,6 +323,9 @@ public class SysdynDiagramEditor extends ResourceEditorPart {
                 return null;\r
             }\r
         }\r
+        if (adapter == IPropertyPage.class) {\r
+            return new SysdynPropertyPage(getSite(), Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser"));\r
+        }\r
         return super.getAdapter(adapter);\r
     }\r
 }\r
index 4404ca06c5ac461a5c6087131ff4c5483bbd4671..2e1555449fda2fc32749c3730afe1f3f261b265b 100644 (file)
@@ -41,13 +41,9 @@ import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Table;\r
 import org.eclipse.swt.widgets.TableItem;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.eclipse.ui.IEditorInput;\r
-import org.eclipse.ui.IEditorPart;\r
 import org.eclipse.ui.ISelectionListener;\r
 import org.eclipse.ui.IWorkbenchPart;\r
 import org.eclipse.ui.IWorkbenchPartReference;\r
-import org.eclipse.ui.PlatformUI;\r
 import org.eclipse.ui.part.ViewPart;\r
 import org.simantics.db.Builtins;\r
 import org.simantics.db.ReadGraph;\r
@@ -68,7 +64,6 @@ import org.simantics.sysdyn.representation.Variable;
 import org.simantics.sysdyn.ui.equation.expressions.ExpressionField;\r
 import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor;\r
 import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.workbench.ResourceEditorInput;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 import org.eclipse.jface.text.Position;\r
 \r
@@ -406,13 +401,8 @@ public class EquationView extends ViewPart implements ISelectionListener {
         if(!variables.isEmpty()) {\r
             ArrayList<String> modelVariables = new ArrayList<String>();\r
             Set<String> noSuchVariables = new HashSet<String>();\r
-            IEditorPart editor = \r
-                PlatformUI.getWorkbench()\r
-                .getActiveWorkbenchWindow().getActivePage().getActiveEditor();\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
+            SysdynModel model = sdm.getModel(configuration);\r
             Configuration conf = model.getConfiguration();\r
             ArrayList<IElement> elements = conf.elements;\r
             for(IElement e : elements) {\r
index fc5461a42219b2c019089061266cd0873506976f..85823f834a2a6a83b8fb7ed768712d5467e09d69 100644 (file)
@@ -159,7 +159,6 @@ public class SysdynExperimentActivator {
                     for (IProjectElement member : ((IModelDescriptor) model).members()) {\r
                         if (member instanceof IExperimentDescriptor && experiment.equals(member.get())) {\r
                             model.setHint(ProjectKeys.KEY_SELECTED_EXPERIMENT, member);\r
-                            System.out.println("MOIOI" + member);\r
                             project.setHint(ProjectKeys.KEY_SELECTED_UI_EXPERIMENT, member);\r
                             break;\r
                         }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java
new file mode 100644 (file)
index 0000000..3c2ff78
--- /dev/null
@@ -0,0 +1,18 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.management.ISessionContext;\r
+\r
+public class ConfigurationTab extends PropertyTabContributorImpl {\r
+\r
+    @Override\r
+    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+        // TODO Auto-generated method stub\r
+        \r
+    }\r
+\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
new file mode 100644 (file)
index 0000000..8c2a47c
--- /dev/null
@@ -0,0 +1,36 @@
+package org.simantics.sysdyn.ui.properties;\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.swt.graphics.Font;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.properties.widgets.NameWidget;\r
+import org.simantics.sysdyn.ui.properties.widgets.UnitWidget;\r
+\r
+public class EquationTab extends PropertyTabContributorImpl {\r
+\r
+    static public final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL);\r
+    \r
+    @Override\r
+    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+        Composite composite = new Composite(body, SWT.NONE);\r
+        GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
+        GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(composite);\r
+        NameWidget nameWidget = new NameWidget(composite, support, SWT.BORDER);\r
+        nameWidget.setTextFactory(new StringPropertyFactory(context, Builtins.URIs.HasName));\r
+        \r
+        UnitWidget unitWidget = new UnitWidget(composite, support, SWT.NONE);\r
+        unitWidget.setTextFactory(new StringPropertyFactory(context, SysdynResource.URIs.HasUnit));\r
+    }\r
+\r
+  \r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExperimentTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExperimentTab.java
new file mode 100644 (file)
index 0000000..1db9ef9
--- /dev/null
@@ -0,0 +1,16 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.management.ISessionContext;\r
+\r
+public class ExperimentTab extends PropertyTabContributorImpl {\r
+\r
+    @Override\r
+    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+        \r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModelTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModelTab.java
new file mode 100644 (file)
index 0000000..235c34c
--- /dev/null
@@ -0,0 +1,15 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.management.ISessionContext;\r
+\r
+public class ModelTab extends PropertyTabContributorImpl {\r
+\r
+    @Override\r
+    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+        \r
+    }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java
new file mode 100644 (file)
index 0000000..83d0eca
--- /dev/null
@@ -0,0 +1,65 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.browsing.ui.SelectionProcessor;\r
+import org.simantics.browsing.ui.swt.ComparableTabContributor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+public class ResourceSelectionProcessor implements SelectionProcessor<ISelection, ReadGraph>  {\r
+    \r
+    @Override\r
+    public Collection<?> process(ISelection selection, ReadGraph backend) {\r
+        Resource r = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
+        if(r != null)\r
+        try {\r
+            SysdynResource sr = SysdynResource.getInstance(backend);\r
+            ArrayList<ComparableTabContributor> tabs = new ArrayList<ComparableTabContributor>();\r
+            if (backend.isInstanceOf(r, sr.IndependentVariable))\r
+                return Collections.singleton(\r
+                    new ComparableTabContributor(\r
+                        new EquationTab(),\r
+                        0,\r
+                        r,\r
+                        "Equation"));\r
+            if (backend.isInstanceOf(r, sr.Configuration))\r
+                return Collections.singleton(\r
+                        new ComparableTabContributor(\r
+                            new ConfigurationTab(),\r
+                            0,\r
+                            r,\r
+                            "Configuration Properties"));\r
+            if (backend.isInstanceOf(r, sr.Experiment))\r
+                return Collections.singleton(\r
+                        new ComparableTabContributor(\r
+                            new ConfigurationTab(),\r
+                            0,\r
+                            r,\r
+                            "Experiment Properties"));\r
+            if (backend.isInstanceOf(r, sr.SysdynModel))\r
+                return Collections.singleton(\r
+                        new ComparableTabContributor(\r
+                            new ConfigurationTab(),\r
+                            0,\r
+                            r,\r
+                            "Model Properties"));\r
+            if (backend.isInstanceOf(r, sr.Result))\r
+                return Collections.singleton(\r
+                        new ComparableTabContributor(\r
+                            new ConfigurationTab(),\r
+                            0,\r
+                            r,\r
+                            "Result Properties"));\r
+        } catch (ServiceException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return Collections.emptyList();\r
+    }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResultTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResultTab.java
new file mode 100644 (file)
index 0000000..fc25355
--- /dev/null
@@ -0,0 +1,15 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.management.ISessionContext;\r
+\r
+public class ResultTab extends PropertyTabContributorImpl {\r
+\r
+    @Override\r
+    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+        \r
+    }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SysdynPropertyPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SysdynPropertyPage.java
new file mode 100644 (file)
index 0000000..a3b0f8f
--- /dev/null
@@ -0,0 +1,16 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import java.util.Set;\r
+\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.simantics.browsing.ui.swt.StandardPropertyPage;\r
+\r
+public class SysdynPropertyPage extends StandardPropertyPage {\r
+    \r
+    \r
+    public SysdynPropertyPage(IWorkbenchPartSite site, Set<String> set) {\r
+        super(site, set);\r
+        System.out.println("sysdynpropertypage");\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java
new file mode 100644 (file)
index 0000000..05bc7de
--- /dev/null
@@ -0,0 +1,5 @@
+package org.simantics.sysdyn.ui.properties.widgets;\r
+\r
+public class ExpressionTypeWidget {\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/NameWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/NameWidget.java
new file mode 100644 (file)
index 0000000..5bd9c18
--- /dev/null
@@ -0,0 +1,115 @@
+package org.simantics.sysdyn.ui.properties.widgets;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.VerifyEvent;\r
+import org.eclipse.swt.events.VerifyListener;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\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.procedure.Listener;\r
+import org.simantics.sysdyn.ui.properties.EquationTab;\r
+\r
+public class NameWidget implements Widget {\r
+    \r
+    final private org.eclipse.swt.widgets.Text nameText;\r
+    String originalName;\r
+    private ReadFactory<?, String> textFactory;\r
+    \r
+    public NameWidget(Composite parent, WidgetSupport support, int style) {\r
+        support.register(this);\r
+        nameText = new org.eclipse.swt.widgets.Text(parent, style);\r
+        nameText.setFont(EquationTab.FONT);\r
+        \r
+        GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
+\r
+        nameText.addVerifyListener(new VerifyListener() {\r
+\r
+            @Override\r
+            public void verifyText(VerifyEvent e) {\r
+                e.doit = false;\r
+                char c = e.character;   \r
+                if(c != ' ') {\r
+                    e.doit = true;\r
+                }\r
+            }\r
+        });\r
+        \r
+        nameText.addModifyListener(new ModifyListener() {\r
+\r
+            @Override\r
+            public void modifyText(ModifyEvent e) {\r
+                if(((Text)e.widget).getText().contains(" ")) {\r
+                    ((Text)e.widget).setText(originalName);\r
+                }\r
+            }\r
+        });\r
+\r
+        nameText.addKeyListener(new KeyListener() {\r
+\r
+            @Override\r
+            public void keyReleased(KeyEvent e) {\r
+\r
+            }\r
+\r
+            @Override\r
+            public void keyPressed(KeyEvent e) {\r
+                if(e.keyCode == SWT.ESC && originalName != null) {\r
+                    ((Text)e.widget).setText(originalName);\r
+                    ((Text)e.widget).setSelection(originalName.length());\r
+                }                \r
+            }\r
+        });\r
+    }\r
+\r
+    @Override\r
+    public void setInput(Object input) {\r
+        \r
+        if(textFactory != null) {\r
+            textFactory.listen(input, new Listener<String>() {\r
+\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    t.printStackTrace();\r
+                }\r
+\r
+                @Override\r
+                public void execute(final String text) {\r
+\r
+                    if(text == null) return;\r
+                    \r
+                    nameText.getDisplay().asyncExec(new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+                            \r
+                            if(isDisposed()) return;\r
+                            nameText.setText(text);\r
+                            \r
+                        }\r
+\r
+                    });\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return nameText.isDisposed();\r
+                }\r
+\r
+            });\r
+        }\r
+        \r
+    }\r
+    \r
+    public void setTextFactory(ReadFactory<?, String> textFactory) {\r
+        this.textFactory = textFactory;\r
+    }\r
+    \r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java
new file mode 100644 (file)
index 0000000..658b8c3
--- /dev/null
@@ -0,0 +1,153 @@
+package org.simantics.sysdyn.ui.properties.widgets;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.FontMetrics;\r
+import org.eclipse.swt.graphics.GC;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\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.Builtins;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.properties.EquationTab;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+public class UnitWidget implements Widget {\r
+\r
+    Combo unitCombo;\r
+    private ReadFactory<?, String> textFactory;\r
+\r
+    public UnitWidget(Composite parent, WidgetSupport support, int style) {\r
+        support.register(this);\r
+\r
+        Composite composite = new Composite(parent, style);\r
+        GridDataFactory.fillDefaults().applyTo(composite);\r
+        GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);\r
+\r
+        Label label = new Label(composite, SWT.SINGLE );\r
+        label.setFont(EquationTab.FONT);\r
+        label.setText("Unit:");\r
+        //label.setAlignment(SWT.RIGHT);\r
+        GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
+\r
+\r
+        unitCombo = new Combo(composite, SWT.DROP_DOWN | SWT.BORDER);\r
+        unitCombo.setFont(EquationTab.FONT);\r
+\r
+        int columns = 6;\r
+        GC gc = new GC (unitCombo);\r
+        FontMetrics fm = gc.getFontMetrics ();\r
+        int width = columns * fm.getAverageCharWidth ();\r
+        int height = fm.getHeight ();\r
+        gc.dispose ();        \r
+        GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo);\r
+    }\r
+\r
+    public void setTextFactory(ReadFactory<?, String> textFactory) {\r
+        this.textFactory = textFactory;\r
+    }\r
+\r
+    @Override\r
+    public void setInput(final Object input) {\r
+        if(textFactory != null) {\r
+            textFactory.listen(input, new Listener<String>() {\r
+\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    t.printStackTrace();\r
+                }\r
+\r
+                @Override\r
+                public void execute(final String text) {\r
+\r
+                    if(text == null) return;\r
+\r
+                    unitCombo.getDisplay().asyncExec(new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+\r
+                            if(isDisposed()) return;\r
+                            \r
+                            if(input instanceof ISelection) {\r
+                                ISelection selection = (ISelection)input;\r
+                                if(unitCombo.getItemCount() > 0) unitCombo.removeAll();\r
+                                for(String unit : getUnits(selection)) {\r
+                                    unitCombo.add(unit);\r
+                                }\r
+                            }\r
+                            if(unitCombo.indexOf(text) >= 0)\r
+                                unitCombo.select(unitCombo.indexOf(text));\r
+                            else\r
+                                unitCombo.setText(text);\r
+                        }\r
+\r
+                    });\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return unitCombo.isDisposed();\r
+                }\r
+\r
+            });\r
+        }\r
+    }\r
+\r
+    // Returns all the used units in the model\r
+    private ArrayList<String> getUnits(ISelection selection) {\r
+        ArrayList<String> units = new ArrayList<String>();\r
+        if(selection instanceof IStructuredSelection) {\r
+            final Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);\r
+            if(resource != null) {\r
+                try {\r
+                    units = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
+\r
+                        @Override\r
+                        public ArrayList<String> perform(ReadGraph graph)\r
+                        throws DatabaseException {\r
+                            ArrayList<String> units = new ArrayList<String>();\r
+\r
+                            SysdynResource sr = SysdynResource.getInstance(graph);\r
+                            Builtins b = graph.getBuiltins();\r
+                            Resource model = graph.getPossibleObject(resource, b.PartOf);\r
+                            if (model != null) {\r
+                                Collection<Resource> variables = graph.getObjects(model, b.ConsistsOf);\r
+                                for(Resource v : variables) {\r
+                                    Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
+                                    if (unit != null && !units.contains((String) unit)) {\r
+                                        units.add((String)unit);\r
+                                    }\r
+                                }\r
+                            }\r
+\r
+                            return units;\r
+\r
+                        }\r
+\r
+\r
+                    });\r
+                } catch (DatabaseException ee) {\r
+                    ExceptionUtils.logAndShowError(ee);\r
+                }\r
+            }\r
+        }\r
+        return units;\r
+    }\r
+}\r