From: lempinen Date: Thu, 7 Oct 2010 06:49:32 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18288 ac1ea38d-2e2b... X-Git-Tag: simantics-1.2.0~5 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5b6ff6b3816fe524341a1222aed0079359815073;p=simantics%2Fsysdyn.git git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18288 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 index b88a8208..49a839ca 100644 --- 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 @@ -49,193 +49,193 @@ import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidato public class EquationTab extends PropertyTabContributorImpl { - TrackedCombo expressionTypeCombo, unitCombo; - ShortcutTabWidget shortcutTabWidget; - ExpressionWidget expressionWidget; - org.eclipse.ui.IPartListener2 focusLostListener; - IWorkbenchSite site; - - @Override - public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { - this.site = site; - Composite composite = new Composite(body, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); - GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite); - TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); - nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName)); - nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName)); - nameText.setInputValidator(new VariableNameValidator(support)); - GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); - - - Label label = new Label(composite, SWT.SINGLE ); - label.setText("Type:"); - GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); - - expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); - - - label = new Label(composite, SWT.SINGLE ); - label.setText("Unit:"); - GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); - - - unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER); - - shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE); - - - Composite expressionComposite = new Composite(composite, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite); - expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE); - expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable()); - - addListeners(); - } - - private void addListeners() { - - expressionTypeCombo.setItemFactory(new ReadFactoryImpl>() { - - @Override - public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { - - Map map = new HashMap(); - SysdynResource sr = SysdynResource.getInstance(graph); - - final ExpressionType[] expressionTypes; - if(graph.isInstanceOf(input, sr.Auxiliary)) - expressionTypes = ExpressionTypes.auxiliaryExpressions; - else if(graph.isInstanceOf(input, sr.Stock)) - expressionTypes = ExpressionTypes.stockExpressions; - else if(graph.isInstanceOf(input, sr.Valve)) - expressionTypes = ExpressionTypes.valveExpressions; - else - expressionTypes = new ExpressionType[] {}; - - for(ExpressionType et : expressionTypes) { - map.put(et.toString(), et); - } - return map; - } - }); - - expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl() { - - @Override - public String perform(ReadGraph graph, final Resource input) throws DatabaseException { - return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString(); - } - }); - - expressionTypeCombo.addModifyListener(new TextModifyListener() { - - @Override - public void modifyText(TrackedModifyEvent e) { - expressionWidget.displayExpression(e.getText(), false); - expressionWidget.save(); - } - }); - - - unitCombo.setItemFactory(new ReadFactoryImpl>() { - - @Override - public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { - Map map = new HashMap(); - - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - Resource model = graph.getPossibleObject(input, l0.PartOf); - if (model != null) { - Collection variables = graph.getObjects(model, l0.ConsistsOf); - for(Resource v : variables) { - Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit); - if (unit != null && !map.keySet().contains((String) unit)) { - map.put((String)unit, (String)unit); - - } - } - } - return map; - } - }); - unitCombo.setSelectionFactory(new ReadFactoryImpl() { - - @Override - public String perform(ReadGraph graph, final Resource input) throws DatabaseException { - String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).HasUnit); - if(unit == null) - return ""; - else - return unit; - } - }); - - unitCombo.addModifyListener(new ComboModifyListenerImpl() { - - @Override - public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException { - graph.claimLiteral(input, SysdynResource.getInstance(graph).HasUnit, text); - } - }); - - - shortcutTabWidget.addMouseListener(new MouseListener(){ - - @Override - public void mouseDoubleClick(MouseEvent e) { - Table table = (Table)e.widget; - TableItem item = table.getItem(new Point(e.x, e.y)); - if(item != null) { - final String var = (String)item.getData(); - table.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - if(expressionWidget!= null) { - expressionWidget.getExpression().replaceSelection(var); - expressionWidget.getExpression().focus(); - expressionWidget.validateFields(); - } - } - }); - } - } - - @Override - public void mouseDown(MouseEvent e) { - expressionWidget.getExpression().focus(); - } - - @Override - public void mouseUp(MouseEvent e) { - } - - }); - shortcutTabWidget.addFocusListener(new FocusListener() { - - @Override - public void focusGained(FocusEvent e) { - } - - @Override - public void focusLost(FocusEvent e) { - ((Table)e.widget).deselectAll(); - } - - }); - - expressionWidget.addModifyListener(new ModifyListener() { - - @Override - public void modifyText(ModifyEvent e) { - expressionWidget.validateFields(); - } - }); - - expressionWidget.addVerifyKeyListener(new VerifyKeyListener() { - + TrackedCombo expressionTypeCombo, unitCombo; + ShortcutTabWidget shortcutTabWidget; + ExpressionWidget expressionWidget; + org.eclipse.ui.IPartListener2 focusLostListener; + IWorkbenchSite site; + + @Override + public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + this.site = site; + Composite composite = new Composite(body, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite); + TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); + nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName)); + nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName)); + nameText.setInputValidator(new VariableNameValidator(support)); + GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); + + + Label label = new Label(composite, SWT.SINGLE ); + label.setText("Type:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); + + + label = new Label(composite, SWT.SINGLE ); + label.setText("Unit:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + + unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER); + + shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE); + + + Composite expressionComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite); + expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE); + expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable()); + + addListeners(); + } + + private void addListeners() { + + expressionTypeCombo.setItemFactory(new ReadFactoryImpl>() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + + Map map = new HashMap(); + SysdynResource sr = SysdynResource.getInstance(graph); + + final ExpressionType[] expressionTypes; + if(graph.isInstanceOf(input, sr.Auxiliary)) + expressionTypes = ExpressionTypes.auxiliaryExpressions; + else if(graph.isInstanceOf(input, sr.Stock)) + expressionTypes = ExpressionTypes.stockExpressions; + else if(graph.isInstanceOf(input, sr.Valve)) + expressionTypes = ExpressionTypes.valveExpressions; + else + expressionTypes = new ExpressionType[] {}; + + for(ExpressionType et : expressionTypes) { + map.put(et.toString(), et); + } + return map; + } + }); + + expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString(); + } + }); + + expressionTypeCombo.addModifyListener(new TextModifyListener() { + + @Override + public void modifyText(TrackedModifyEvent e) { + expressionWidget.displayExpression(e.getText(), false); + expressionWidget.save(); + } + }); + + + unitCombo.setItemFactory(new ReadFactoryImpl>() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + Map map = new HashMap(); + + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getPossibleObject(input, l0.PartOf); + if (model != null) { + Collection variables = graph.getObjects(model, l0.ConsistsOf); + for(Resource v : variables) { + Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit); + if (unit != null && !map.keySet().contains((String) unit)) { + map.put((String)unit, (String)unit); + + } + } + } + return map; + } + }); + unitCombo.setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).HasUnit); + if(unit == null) + return ""; + else + return unit; + } + }); + + unitCombo.addModifyListener(new ComboModifyListenerImpl() { + + @Override + public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException { + graph.claimLiteral(input, SysdynResource.getInstance(graph).HasUnit, text); + } + }); + + + shortcutTabWidget.addMouseListener(new MouseListener(){ + + @Override + public void mouseDoubleClick(MouseEvent e) { + Table table = (Table)e.widget; + TableItem item = table.getItem(new Point(e.x, e.y)); + if(item != null) { + final String var = (String)item.getData(); + table.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if(expressionWidget!= null) { + expressionWidget.getExpression().replaceSelection(var); + expressionWidget.getExpression().focus(); + expressionWidget.validateFields(); + } + } + }); + } + } + + @Override + public void mouseDown(MouseEvent e) { + expressionWidget.getExpression().focus(); + } + + @Override + public void mouseUp(MouseEvent e) { + } + + }); + shortcutTabWidget.addFocusListener(new FocusListener() { + + @Override + public void focusGained(FocusEvent e) { + } + + @Override + public void focusLost(FocusEvent e) { + ((Table)e.widget).deselectAll(); + } + + }); + + expressionWidget.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + expressionWidget.validateFields(); + } + }); + + expressionWidget.addVerifyKeyListener(new VerifyKeyListener() { + @Override public void verifyKey(VerifyEvent event) { if(event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) { @@ -247,34 +247,39 @@ public class EquationTab extends PropertyTabContributorImpl { } } }); - - focusLostListener = new org.eclipse.ui.IPartListener2() - { - public void partInputChanged(IWorkbenchPartReference partRef) {} - public void partVisible(IWorkbenchPartReference partRef) {} - public void partHidden(IWorkbenchPartReference partRef) {} - public void partOpened(IWorkbenchPartReference partRef) {} - public void partDeactivated(IWorkbenchPartReference partRef) - { - if(partRef.getPart(false) instanceof PropertyPageView) { - // Save expressions - if(expressionWidget != null) { - expressionWidget.save(); - } - partRef.getPart(false).getSite().getPage().removePartListener(this); - } - } - public void partClosed(IWorkbenchPartReference partRef) {} - public void partBroughtToTop(IWorkbenchPartReference partRef) {} - public void partActivated(IWorkbenchPartReference partRef) {} - }; - site.getPage().addPartListener(focusLostListener); - } - - @Override - public void dispose() { - if(focusLostListener != null && site != null) - site.getPage().removePartListener(focusLostListener); - super.dispose(); - } + + if(focusLostListener == null) { + focusLostListener = new org.eclipse.ui.IPartListener2() + { + public void partInputChanged(IWorkbenchPartReference partRef) {} + public void partVisible(IWorkbenchPartReference partRef) {} + public void partHidden(IWorkbenchPartReference partRef) {} + public void partOpened(IWorkbenchPartReference partRef) {} + public void partDeactivated(IWorkbenchPartReference partRef) + { + if(partRef.getPart(false) instanceof PropertyPageView) { + PropertyPageView ppv = (PropertyPageView)partRef.getPart(false); + if(ppv.getCurrentPage() instanceof SysdynPropertyPage) { + // Save expressions + if(expressionWidget != null) { + expressionWidget.save(); + } + site.getPage().removePartListener(this); + } + } + } + public void partClosed(IWorkbenchPartReference partRef) {} + public void partBroughtToTop(IWorkbenchPartReference partRef) {} + public void partActivated(IWorkbenchPartReference partRef) {} + }; + site.getPage().addPartListener(focusLostListener); + } + } + + @Override + public void dispose() { + if(focusLostListener != null && site != null) + site.getPage().removePartListener(focusLostListener); + super.dispose(); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java index db69a53e..426deda8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java @@ -105,6 +105,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen @Override public void run(ReadGraph g) throws DatabaseException { Variable variable = getVariable(g, resources[0], runtime); + if(variable == null) return; SysdynModel model = getSysdynModel(g, Variables.getModel(g, variable)); Runnable listener = new Runnable() { @Override