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