From 411b2f32a83643073a15af5e97e9a8cef33d1dde Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 21 Jun 2010 11:59:01 +0000 Subject: [PATCH] git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16258 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/properties/EquationTab.java | 1 + .../sysdyn/ui/properties/LookupTableTab.java | 77 ++++ .../ResourceSelectionProcessor.java | 10 +- .../properties/widgets/ChartTableWidget.java | 230 +++++++++ .../ui/properties/widgets/ChartWidget.java | 219 +++++++++ .../widgets/expressions/LookupChartInfo.java | 63 --- .../widgets/expressions/LookupChartPanel.java | 33 +- .../expressions/LookupInputOutputTable.java | 101 +++- .../widgets/expressions/LookupPopup.java | 435 ------------------ .../expressions/WithLookupExpression.java | 159 ++++--- .../factories/DoublePropertyFactory.java | 1 - .../factories/DoublePropertyModifier.java | 23 + 12 files changed, 753 insertions(+), 599 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LookupTableTab.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartInfo.java delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupPopup.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyModifier.java 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 378ad63e..ac34c633 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 @@ -78,6 +78,7 @@ public class EquationTab extends PropertyTabContributorImpl { @Override public void widgetSelected(SelectionEvent e) { expressionWidget.displayExpression(expressionTypeWidget.getSelecetedType(), false); + expressionWidget.save(); } @Override diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LookupTableTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LookupTableTab.java new file mode 100644 index 00000000..0fb17cf9 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LookupTableTab.java @@ -0,0 +1,77 @@ +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.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; +import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.management.ISessionContext; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.ChartTableWidget; +import org.simantics.sysdyn.ui.properties.widgets.ChartWidget; +import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; +import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier; + +public class LookupTableTab extends PropertyTabContributorImpl { + + @Override + public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + + + Composite baseContainer = new Composite(body, SWT.NONE); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(baseContainer); + GridDataFactory.fillDefaults().grab(true, true).applyTo(baseContainer); + + Composite Ycontainer = new Composite(baseContainer, SWT.NONE); + GridLayoutFactory.fillDefaults().applyTo(Ycontainer); + GridDataFactory.fillDefaults().grab(false, true).applyTo(Ycontainer); + + TrackedText maxYText = new TrackedText(Ycontainer, support, SWT.BORDER); + maxYText.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasMaxY)); + maxYText.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasMaxY)); + + Label l = new Label(Ycontainer, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(false, true).applyTo(l); + + TrackedText minYText = new TrackedText(Ycontainer, support, SWT.BORDER); + minYText.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasMinY)); + minYText.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasMinY)); + + + Composite chartContainer = new Composite(baseContainer, SWT.NONE); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(chartContainer); + GridDataFactory.fillDefaults().grab(true, true).applyTo(chartContainer); + + @SuppressWarnings("unused") + ChartWidget chartWidget = new ChartWidget(chartContainer, support, SWT.NONE); + + Composite chartTableContainer = new Composite(baseContainer, SWT.NONE); + GridLayoutFactory.fillDefaults().applyTo(chartTableContainer); + GridDataFactory.fillDefaults().grab(false, true).span(1, 2).applyTo(chartTableContainer); + + @SuppressWarnings("unused") + ChartTableWidget chartTableWidget = new ChartTableWidget(chartTableContainer, support, SWT.NONE); + + l = new Label(baseContainer, SWT.NONE); + + Composite Xcontainer = new Composite(baseContainer, SWT.NONE); + GridLayoutFactory.fillDefaults().numColumns(3).applyTo(Xcontainer); + GridDataFactory.fillDefaults().grab(true, false).applyTo(Xcontainer); + + TrackedText minXText = new TrackedText(Xcontainer, support, SWT.BORDER); + minXText.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasMinX)); + minXText.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasMinX)); + + l = new Label(Xcontainer, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, false).applyTo(l); + + TrackedText maxXText = new TrackedText(Xcontainer, support, SWT.BORDER); + maxXText.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasMaxX)); + maxXText.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasMaxX)); + + } +} 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 index 52404a86..18fd9e91 100644 --- 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 @@ -40,9 +40,17 @@ public class ResourceSelectionProcessor implements SelectionProcessor() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + String lookup = graph.getRelatedValue(expression, sr.HasLookup); + return lookup; + } + }, new Listener() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(String lookup) { + TableParser parser = new TableParser(new StringReader("")); + parser.ReInit(new StringReader(lookup)); + table.clearTable(); + try { + parser.table(); + ArrayList xTokens = parser.getXTokens(); + for(Token token : xTokens) { + table.addLocation(new Point2D.Double( + Double.parseDouble(token.image), + Double.parseDouble(token.next.next.image))); + } + } catch (ParseException e1) { + } + } + + @Override + public boolean isDisposed() { + return table.isDisposed(); + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + table.addListener(SWT.Modify, new org.eclipse.swt.widgets.Listener() { + + @Override + public void handleEvent(Event event) { + tableModified(); + } + }); + } + + + @SuppressWarnings("unchecked") + private void tableModified() { + StringBuilder b = new StringBuilder(); + b.append("{"); + Iterator iterator = ((ArrayList)table.getTableViewer().getInput()).iterator(); + while(iterator.hasNext()){ + InputOutput io = iterator.next(); + b.append("{" + io.getInput(String.class) + "," + io.getOutput(String.class) + "}"); + if(iterator.hasNext()) + b.append(","); + } + b.append("}"); + final String table = b.toString(); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + graph.claimValue(expression, sr.HasLookup, table); + } + }); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java new file mode 100644 index 00000000..0dbf66a1 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java @@ -0,0 +1,219 @@ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.GridLayout; +import java.awt.geom.Ellipse2D; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Iterator; +import javax.swing.JComponent; +import javax.swing.JPanel; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.axis.ValueAxis; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.plot.XYPlot; +import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; +import org.jfree.data.general.SeriesChangeEvent; +import org.jfree.data.general.SeriesChangeListener; +import org.jfree.data.xy.XYDataItem; +import org.jfree.data.xy.XYDataset; +import org.jfree.data.xy.XYSeries; +import org.jfree.data.xy.XYSeriesCollection; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.Read; +import org.simantics.g2d.chassis.SWTAWTComponent; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.tableParser.ParseException; +import org.simantics.sysdyn.tableParser.TableParser; +import org.simantics.sysdyn.tableParser.Token; +import org.simantics.sysdyn.ui.properties.widgets.expressions.LookupChartPanel; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ISelectionUtils; + +public class ChartWidget implements Widget { + + JFreeChart chart; + LookupChartPanel chartPanel; + SWTAWTComponent swtawtcomponent; + + public ChartWidget(Composite parent, WidgetSupport support, int style) { + support.register(this); + chartPanel = new LookupChartPanel(createChart()); + chartPanel.setMouseZoomable(true, false); + chartPanel.setDomainZoomable(false); + chartPanel.setRangeZoomable(false); + + swtawtcomponent = new SWTAWTComponent(parent, SWT.BORDER) { + @Override + protected JComponent createSwingComponent() { + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(1, 1)); + panel.add(chartPanel); + panel.doLayout(); + return panel; + } + }; + GridDataFactory.fillDefaults().grab(true, true).applyTo(swtawtcomponent); + swtawtcomponent.populate(); + + } + + @Override + public void setInput(ISessionContext context, Object input) { + + final Resource expression = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + + class Auxiliary { + Double minX, minY, maxX, maxY; + String table; + } + + try { + SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Auxiliary perform(ReadGraph graph) throws DatabaseException { + Auxiliary auxiliary = new Auxiliary(); + SysdynResource sr = SysdynResource.getInstance(graph); + auxiliary.minX = graph.getRelatedValue(expression, sr.HasMinX); + auxiliary.maxX = graph.getRelatedValue(expression, sr.HasMaxX); + auxiliary.minY = graph.getRelatedValue(expression, sr.HasMinY); + auxiliary.maxY = graph.getRelatedValue(expression, sr.HasMaxY); + auxiliary.table = graph.getRelatedValue(expression, sr.HasLookup); + return auxiliary; + } + }, new Listener() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(Auxiliary result) { + + XYDataset dataset = createDataset(result.table); + chartPanel.resetChart(dataset); + chartPanel.addSeriesChangeListener(new _SeriesChangeListener(expression)); + + XYPlot plot = (XYPlot) chart.getPlot(); + ValueAxis rangeAxis = plot.getRangeAxis(); + rangeAxis.setAutoRange(false); + rangeAxis.setRange(result.minY, result.maxY); + ValueAxis domainAxis = plot.getDomainAxis(); + domainAxis.setAutoRange(false); + domainAxis.setRange(result.minX, result.maxX); + + } + + @Override + public boolean isDisposed() { + return swtawtcomponent.isDisposed(); + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + private JFreeChart createChart() { + XYDataset dataset = createDataset(null); + chart = ChartFactory.createXYLineChart(null, null, null, + dataset, PlotOrientation.VERTICAL, false, true, false); + XYPlot plot = (XYPlot) chart.getPlot(); + XYLineAndShapeRenderer renderer + = (XYLineAndShapeRenderer) plot.getRenderer(); + renderer.setBaseShapesVisible(true); + renderer.setDrawOutlines(true); + renderer.setUseFillPaint(true); + renderer.setBaseFillPaint(Color.white); + renderer.setSeriesStroke(0, new BasicStroke(3.0f)); + renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f)); + renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); + return chart; + } + + public XYDataset createDataset(String table) { + XYSeries series = new XYSeries("Series"); + + if(table != null) { + TableParser parser = new TableParser(new StringReader("")); + parser.ReInit(new StringReader(table)); + try { + parser.table(); + ArrayList xTokens = parser.getXTokens(); + for(Token token : xTokens) { + series.add(Double.parseDouble(token.image), + Double.parseDouble(token.next.next.image)); + } + } catch (ParseException e1) { + } + } + + XYSeriesCollection dataset = new XYSeriesCollection(); + dataset.addSeries(series); + return dataset; + } + + public LookupChartPanel getChartPanel() { + return this.chartPanel; + } + + public JFreeChart getChart() { + return this.chart; + } + + private class _SeriesChangeListener implements SeriesChangeListener { + + Resource expression; + + public _SeriesChangeListener(Resource expression) { + this.expression = expression; + } + @Override + public void seriesChanged(SeriesChangeEvent event) { + if(chartPanel.isDragging()) return; + + StringBuilder b = new StringBuilder(); + b.append("{"); + XYSeriesCollection collection = (XYSeriesCollection) ((XYPlot)chart.getPlot()).getDataset(); + XYSeries series = collection.getSeries(0); + if(series.isEmpty()) + return; + Iterator iterator = series.getItems().iterator(); + while(iterator.hasNext()){ + XYDataItem item = (XYDataItem)iterator.next(); + b.append("{" + item.getX() + "," + item.getY() + "}"); + if(iterator.hasNext()) + b.append(","); + } + b.append("}"); + final String table = b.toString(); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + graph.claimValue(expression, sr.HasLookup, table); + } + }); + + } + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartInfo.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartInfo.java deleted file mode 100644 index e556d048..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartInfo.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.simantics.sysdyn.ui.properties.widgets.expressions; - -import java.util.Map; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.ui.SimanticsUI; - -public class LookupChartInfo { - - String lookupTable, expression; - Double minX, maxX, minY, maxY; - - private static Double MINX = 0.0; - private static Double MAXX = 10.0; - private static Double MINY = 0.0; - private static Double MAXY = 10.0; - - public LookupChartInfo(String expression, String lookupTable, final Resource variable, Map data) { - this.lookupTable = lookupTable != null ? lookupTable : ""; - this.expression = expression != null ? expression : ""; - this.minX = (Double)data.get("minX"); - this.maxX = (Double)data.get("maxX"); - this.minY = (Double)data.get("minY"); - this.maxY = (Double)data.get("maxY"); - if(variable != null && (minX == null || maxX == null || minY == null || maxY == null)) { - try { - SimanticsUI.getSession().syncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if(expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) { - minX = graph.getPossibleRelatedValue(expression, sr.HasMinX); - if(minX == null) minX = MINX; - maxX = graph.getPossibleRelatedValue(expression, sr.HasMaxX); - if(maxX == null) maxX = MAXX; - minY = graph.getPossibleRelatedValue(expression, sr.HasMinY); - if(minY == null) minY = MINY; - maxY = graph.getPossibleRelatedValue(expression, sr.HasMaxY); - if(maxY == null) maxY = MAXY; - } else { - defaultValues(); - } - - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - } - } - - private void defaultValues() { - minX = MINX; - maxX = MAXX; - minY = MINY; - maxY = MAXY; - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartPanel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartPanel.java index 21224331..6258aea6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartPanel.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupChartPanel.java @@ -13,6 +13,8 @@ import org.jfree.chart.entity.ChartEntity; import org.jfree.chart.entity.PlotEntity; import org.jfree.chart.entity.XYItemEntity; import org.jfree.chart.plot.XYPlot; +import org.jfree.data.general.SeriesChangeListener; +import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; @@ -23,7 +25,7 @@ public class LookupChartPanel extends ChartPanel { private boolean drawing; private XYSeries series; private JFreeChart chart; - private LookupInputOutputTable table; + private SeriesChangeListener changeListener; public LookupChartPanel(JFreeChart chart) { super(chart); @@ -32,10 +34,6 @@ public class LookupChartPanel extends ChartPanel { series = collection.getSeries(0); } - public void setTable(LookupInputOutputTable table) { - this.table = table; - } - public void mouseDragged(MouseEvent e) { if(dragPrevEntity != null) { @@ -87,9 +85,13 @@ public class LookupChartPanel extends ChartPanel { } public void mouseReleased(MouseEvent e) { - - dragPrevEntity = null; + if(isDragging()) { + dragPrevEntity = null; + if(changeListener != null) + changeListener.seriesChanged(null); + } drawing = false; + } public void mouseClicked(MouseEvent e) @@ -130,14 +132,18 @@ public class LookupChartPanel extends ChartPanel { public void addLocationToSeries(Point2D location) { if(series.indexOf(location.getX()) < 0) { series.add(location.getX(), location.getY()); - table.addLocation(location); } } public void removeItemFromSeries(int item){ - Point2D location = new Point2D.Double(series.getX(item).doubleValue(),series.getY(item).doubleValue()); series.remove(item); - table.removeLocation(location); + } + + public void resetChart(XYDataset dataset) { + XYPlot plot = (XYPlot)chart.getPlot(); + plot.setDataset(dataset); + XYSeriesCollection collection = (XYSeriesCollection) plot.getDataset(); + series = collection.getSeries(0); } private Point2D getMouseLocation(MouseEvent e) { @@ -148,5 +154,12 @@ public class LookupChartPanel extends ChartPanel { return p; } + public void addSeriesChangeListener(SeriesChangeListener listener) { + this.changeListener = listener; + this.series.addChangeListener(changeListener); + } + public boolean isDragging() { + return dragPrevEntity != null; + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java index 9ed5030f..f9f5c03b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupInputOutputTable.java @@ -8,6 +8,7 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.ICellModifier; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TableViewer; @@ -17,6 +18,9 @@ import org.eclipse.jface.viewers.ViewerComparator; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Item; +import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; @@ -26,9 +30,9 @@ public class LookupInputOutputTable extends Composite { public static final String OUTPUT = "Output"; public static final String[] PROPS = { INPUT, OUTPUT }; - Table table; - TableViewer tableViewer; - List tableRows; + private Table table; + private TableViewer tableViewer; + private List tableRows; public LookupInputOutputTable(Composite parent, int style) { super(parent, style); @@ -43,7 +47,7 @@ public class LookupInputOutputTable extends Composite { TableColumn column1 = new TableColumn (table, SWT.LEFT); column1.setText (INPUT); column1.setWidth (85); - + TableColumn column2 = new TableColumn (table, SWT.LEFT); column2.setText (OUTPUT); column2.setWidth (85); @@ -53,6 +57,7 @@ public class LookupInputOutputTable extends Composite { tableViewer.setContentProvider (new ArrayContentProvider()); tableViewer.setLabelProvider (new InputOutputLabelProvider()); + tableViewer.setCellModifier(new InputOutputCellModifier()); tableRows = new ArrayList(); tableViewer.setInput(tableRows); @@ -62,7 +67,7 @@ public class LookupInputOutputTable extends Composite { editors[1] = new TextCellEditor(table); tableViewer.setCellEditors(editors); tableViewer.setColumnProperties(PROPS); - + } private class InputOutputLabelProvider extends LabelProvider implements ITableLabelProvider { @@ -93,25 +98,30 @@ public class LookupInputOutputTable extends Composite { } - public void removeLocation(Point2D location) { - for(InputOutput io : tableRows) { - if((Double)io.getInput(Double.class) == location.getX()) { - tableRows.remove(io); - tableViewer.getTable().getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - refresh(); - } - }); - break; + public void removeItem(int index) { + tableRows.remove(index); + tableViewer.getTable().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + refresh(); } - } + }); + } + + public void clearTable() { + this.tableRows.clear(); + tableViewer.getTable().getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + refresh(); + } + }); } - - class InputOutput { + + public class InputOutput { private double input, output; public InputOutput(double input, double output) { @@ -190,13 +200,60 @@ public class LookupInputOutputTable extends Composite { return 0; } } - + public TableViewer getTableViewer() { return this.tableViewer; } - + public void refresh() { tableViewer.setComparator(new InputOutputComparator()); tableViewer.refresh(); } + + class InputOutputCellModifier implements ICellModifier { + + public InputOutputCellModifier() {} + + public boolean canModify(Object element, String property) { + return true; + } + + public Object getValue(Object element, String property) { + InputOutput io = (InputOutput)element; + if (LookupInputOutputTable.INPUT.equals(property)) + return (String)io.getInput(String.class); + else if (LookupInputOutputTable.OUTPUT.equals(property)) + return (String)io.getOutput(String.class); + else + return null; + } + + public void modify(Object element, String property, Object value) { + if (element instanceof Item) element = ((Item) element).getData(); + + InputOutput io = (InputOutput)element; + + if (LookupInputOutputTable.INPUT.equals(property)) { + io.setInput((String)value); + } else if (LookupInputOutputTable.OUTPUT.equals(property)) { + io.setOutput((String)value); + } + tableModified(); + refresh(); + } + } + + private void tableModified() { + tableViewer.getTable().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + for (Listener listener : getListeners(SWT.Modify)) { + Event e = new Event(); + listener.handleEvent(e); + } + } + }); + + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupPopup.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupPopup.java deleted file mode 100644 index c7d3cfdd..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupPopup.java +++ /dev/null @@ -1,435 +0,0 @@ -package org.simantics.sysdyn.ui.properties.widgets.expressions; - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.GridLayout; -import java.awt.event.MouseEvent; -import java.awt.geom.Ellipse2D; -import java.awt.geom.Point2D; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.Iterator; -import javax.swing.JComponent; -import javax.swing.JPanel; -import org.eclipse.jface.dialogs.Dialog; -import org.eclipse.jface.dialogs.IDialogConstants; -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.jface.viewers.ICellModifier; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.FocusListener; -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.MouseListener; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Item; -import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.Table; -import org.eclipse.swt.widgets.TableItem; -import org.eclipse.swt.widgets.Text; -import org.eclipse.swt.widgets.Widget; -import org.jfree.chart.ChartFactory; -import org.jfree.chart.JFreeChart; -import org.jfree.chart.axis.ValueAxis; -import org.jfree.chart.plot.PlotOrientation; -import org.jfree.chart.plot.XYPlot; -import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; -import org.jfree.data.xy.XYDataItem; -import org.jfree.data.xy.XYDataset; -import org.jfree.data.xy.XYSeries; -import org.jfree.data.xy.XYSeriesCollection; -import org.simantics.g2d.chassis.SWTAWTComponent; -import org.simantics.sysdyn.tableParser.ParseException; -import org.simantics.sysdyn.tableParser.Token; -import org.simantics.sysdyn.tableParser.TableParser; -import org.simantics.sysdyn.ui.properties.widgets.expressions.LookupInputOutputTable.InputOutput; - -public class LookupPopup extends Dialog { - - JFreeChart chart; - LookupInputOutputTable table; - ArrayList dataPoints; - Text input, output; - Text minX, maxX, minY, maxY; - Label unit; - LookupChartInfo chartInfo; - - protected LookupPopup(Shell parentShell, LookupChartInfo chartInfo) { - super(parentShell); - this.chartInfo = chartInfo; - this.dataPoints = new ArrayList(); - TableParser parser = new TableParser(new StringReader("")); - parser.ReInit(new StringReader(chartInfo.lookupTable)); - try { - parser.table(); - ArrayList xTokens = parser.getXTokens(); - for(Token token : xTokens) { - dataPoints.add(new Point2D.Double( - Double.parseDouble(token.image), - Double.parseDouble(token.next.next.image))); - } - } catch (ParseException e1) { - } - - } - - @Override - protected Control createDialogArea(Composite parent) { - KeyListener enterListener = new KeyListener() { - - @Override - public void keyReleased(KeyEvent e) {} - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) - getButton(IDialogConstants.OK_ID).forceFocus(); - } - }; - - final LookupChartPanel chartPanel = new LookupChartPanel(createChart()); - chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); - chartPanel.setMouseZoomable(true, false); - chartPanel.setDomainZoomable(false); - chartPanel.setRangeZoomable(false); - - XYPlot plot = (XYPlot) chart.getPlot(); - ValueAxis rangeAxis = plot.getRangeAxis(); - ValueAxis domainAxis = plot.getDomainAxis(); - - Composite container = new Composite(parent, SWT.None); - GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(container); - - Composite yAxis = new Composite(container, SWT.NONE); - GridLayoutFactory.fillDefaults().applyTo(yAxis); - GridDataFactory.fillDefaults().grab(false, true).applyTo(yAxis); - maxY = new Text(yAxis, SWT.BORDER | SWT.RIGHT); - GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(maxY); - maxY.addKeyListener(enterListener); - maxY.addModifyListener(getAxisBoundModifyListener()); - maxY.setText("" + rangeAxis.getUpperBound()); - Composite fillY = new Composite(yAxis, SWT.NONE); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(false, true).applyTo(fillY); - GridLayoutFactory.fillDefaults().applyTo(fillY); - unit = new Label(fillY, SWT.RIGHT); - GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(unit); - unit.setText(""); //TODO: how to get and update units? - unit.addKeyListener(enterListener); - minY = new Text(yAxis, SWT.BORDER | SWT.RIGHT); - GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(minY); - minY.addKeyListener(enterListener); - minY.addModifyListener(getAxisBoundModifyListener()); - minY.setText("" + rangeAxis.getLowerBound()); - - SWTAWTComponent c = new SWTAWTComponent(container, SWT.BORDER) { - @Override - protected JComponent createSwingComponent() { - JPanel panel = new JPanel(); - panel.setLayout(new GridLayout(1, 1)); - panel.setPreferredSize(new java.awt.Dimension(500, 270)); - panel.add(chartPanel); - panel.doLayout(); - return panel; - } - }; - GridDataFactory.fillDefaults().hint(500, 300).applyTo(c); - c.populate(); - - - Composite valueTableComposite = new Composite(container, SWT.NONE); - GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(valueTableComposite); - GridLayoutFactory.fillDefaults().numColumns(3).applyTo(valueTableComposite); - - - table = new LookupInputOutputTable(valueTableComposite, SWT.NONE); - GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(table); - chartPanel.setTable(table); - table.getTableViewer().setCellModifier(new InputOutputCellModifier()); - table.getTableViewer().getTable().addMouseListener(new MouseListener() { - - @Override - public void mouseUp(org.eclipse.swt.events.MouseEvent e) { - if(e.button == MouseEvent.BUTTON3) { - Table table = (Table)e.widget; - TableItem item = (TableItem)table.getItem(new org.eclipse.swt.graphics.Point(e.x, e.y)); - chartPanel.removeItemFromSeries(table.indexOf(item)); - } - } - @Override - public void mouseDown(org.eclipse.swt.events.MouseEvent e) { } - @Override - public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) { } - }); - for(Point2D location : this.dataPoints) { - chartPanel.addLocationToSeries(location); - } - - input = new Text(valueTableComposite, SWT.BORDER | SWT.RIGHT); - GridDataFactory.fillDefaults().hint(60, SWT.DEFAULT).applyTo(input); - input.setText(""); - output = new Text(valueTableComposite, SWT.BORDER | SWT.RIGHT); - GridDataFactory.fillDefaults().hint(60, SWT.DEFAULT).applyTo(output); - output.setText(""); - - Button add = new Button(valueTableComposite, SWT.None); - add.setText("Add"); - add.addSelectionListener(new SelectionListener() { - - @Override - public void widgetSelected(SelectionEvent e) { - try { - Double in = Double.parseDouble(input.getText()); - Double out = Double.parseDouble(output.getText()); - chartPanel.addLocationToSeries(new Point2D.Double(in, out)); - } catch (NumberFormatException e1) { - input.setText(""); - output.setText(""); - } - } - - @Override - public void widgetDefaultSelected(SelectionEvent e) {} - }); - - FocusListener flistener = new FocusListener() { - @Override - public void focusGained(FocusEvent e) { - Text text = (Text)e.widget; - text.setSelection(0, text.getCharCount()); - } - @Override - public void focusLost(FocusEvent e) { } - }; - - - KeyListener listener = new KeyListener() { - - @Override - public void keyPressed(KeyEvent e) { - if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) { - try { - Double in = Double.parseDouble(input.getText()); - Double out = Double.parseDouble(output.getText()); - chartPanel.addLocationToSeries(new Point2D.Double(in, out)); - } catch (NumberFormatException e1) { - if(input.getText().isEmpty() && output.getText().isEmpty()) { - getButton(IDialogConstants.OK_ID).forceFocus(); - return; - } - } - input.setText(""); - output.setText(""); - input.setFocus(); - } - } - - @Override - public void keyReleased(KeyEvent e) { } - - }; - - input.addFocusListener(flistener); - input.addKeyListener(listener); - output.addFocusListener(flistener); - output.addKeyListener(listener); - - Label l = new Label(container, SWT.NONE); - l.setText(""); - - Composite xAxis = new Composite(container, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, false).applyTo(xAxis); - GridLayoutFactory.fillDefaults().numColumns(3).applyTo(xAxis); - minX = new Text(xAxis, SWT.BORDER | SWT.RIGHT); - GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(minX); - minX.addKeyListener(enterListener); - minX.addModifyListener(getAxisBoundModifyListener()); - minX.setText("" + domainAxis.getLowerBound()); - Composite fillX = new Composite(xAxis, SWT.CENTER); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, false).applyTo(fillX); - GridLayoutFactory.fillDefaults().applyTo(fillX); - l = new Label(fillX, SWT.NONE); - l.setText(chartInfo.expression); - maxX = new Text(xAxis, SWT.BORDER | SWT.RIGHT); - GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(maxX); - maxX.addKeyListener(enterListener); - maxX.addModifyListener(getAxisBoundModifyListener()); - maxX.setText("" + domainAxis.getUpperBound()); - - return null; - } - - protected void createButtonsForButtonBar(Composite parent) { - // create OK and Cancel buttons by default - createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, - false); - createButton(parent, IDialogConstants.CANCEL_ID, - IDialogConstants.CANCEL_LABEL, false); - } - - public LookupChartInfo open(Boolean bool) { - Shell shell = this.getShell(); - if (shell == null || shell.isDisposed()) { - shell = null; - create(); - shell = this.getShell(); - } - constrainShellSize(); - shell.open(); - getButton(IDialogConstants.OK_ID).setFocus(); - Display display = getParentShell().getDisplay(); - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) { - display.sleep(); - } - } - return chartInfo; - } - - @Override - public void okPressed() { - chartInfo.lookupTable = graphToModelicaTable(); - chartInfo.minX = Double.parseDouble(minX.getText()); - chartInfo.maxX = Double.parseDouble(maxX.getText()); - chartInfo.minY = Double.parseDouble(minY.getText()); - chartInfo.maxY = Double.parseDouble(maxY.getText()); - super.okPressed(); - } - - @SuppressWarnings("unchecked") - private String graphToModelicaTable() { - StringBuilder b = new StringBuilder(); - b.append("{"); - XYSeriesCollection collection = (XYSeriesCollection) ((XYPlot)chart.getPlot()).getDataset(); - XYSeries series = collection.getSeries(0); - if(series.isEmpty()) - return ""; - Iterator iterator = series.getItems().iterator(); - while(iterator.hasNext()){ - XYDataItem item = (XYDataItem)iterator.next(); - b.append("{" + item.getX() + "," + item.getY() + "}"); - if(iterator.hasNext()) - b.append(","); - } - b.append("}"); - return b.toString(); - } - - private JFreeChart createChart() { - XYDataset dataset = createDataset(); - chart = ChartFactory.createXYLineChart(null, null, null, - dataset, PlotOrientation.VERTICAL, false, true, false); - XYPlot plot = (XYPlot) chart.getPlot(); - XYLineAndShapeRenderer renderer - = (XYLineAndShapeRenderer) plot.getRenderer(); - renderer.setBaseShapesVisible(true); - renderer.setDrawOutlines(true); - renderer.setUseFillPaint(true); - renderer.setBaseFillPaint(Color.white); - renderer.setSeriesStroke(0, new BasicStroke(3.0f)); - renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f)); - renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); - - ValueAxis rangeAxis = plot.getRangeAxis(); - rangeAxis.setAutoRange(false); - rangeAxis.setRange(chartInfo.minY, chartInfo.maxY); - ValueAxis domainAxis = plot.getDomainAxis(); - domainAxis.setAutoRange(false); - domainAxis.setRange(chartInfo.minX, chartInfo.maxX); - return chart; - } - - public XYDataset createDataset() { - XYSeries series = new XYSeries("Series"); - XYSeriesCollection dataset = new XYSeriesCollection(); - dataset.addSeries(series); - return dataset; - } - - private ModifyListener getAxisBoundModifyListener() { - return new ModifyListener() { - - @Override - public void modifyText(ModifyEvent e) { - Widget widget = e.widget; - if(widget instanceof Text) { - Text text = (Text)widget; - Double value; - try { - value = Double.parseDouble(text.getText()); - } catch (NumberFormatException e1 ) { - return; - } - XYPlot plot = (XYPlot) chart.getPlot(); - ValueAxis rangeAxis = plot.getRangeAxis(); - ValueAxis domainAxis = plot.getDomainAxis(); - if(text == minX) { - domainAxis.setLowerBound(value); - } else if (text == maxX) { - domainAxis.setUpperBound(value); - } else if (text == minY) { - rangeAxis.setLowerBound(value); - } else if (text == maxY) { - rangeAxis.setUpperBound(value); - } - } - } - }; - } - - private class InputOutputCellModifier implements ICellModifier { - - XYSeries series; - - public InputOutputCellModifier() { - XYSeriesCollection collection = (XYSeriesCollection) ((XYPlot)chart.getPlot()).getDataset(); - series = collection.getSeries(0); - } - - public boolean canModify(Object element, String property) { - return true; - } - - public Object getValue(Object element, String property) { - InputOutput io = (InputOutput)element; - if (LookupInputOutputTable.INPUT.equals(property)) - return (String)io.getInput(String.class); - else if (LookupInputOutputTable.OUTPUT.equals(property)) - return (String)io.getOutput(String.class); - else - return null; - } - - public void modify(Object element, String property, Object value) { - if (element instanceof Item) element = ((Item) element).getData(); - - InputOutput io = (InputOutput)element; - Double x = (Double)io.getInput(Double.class); - int item = series.indexOf(x); - series.remove(item); - - if (LookupInputOutputTable.INPUT.equals(property)) { - Double newX = io.setInput((String)value); - // if has the same x-value, revert back - if(newX != null && series.indexOf(newX) >= 0) { - io.setInput(x); - } - } else if (LookupInputOutputTable.OUTPUT.equals(property)) { - io.setOutput((String)value); - } - - series.add((Double)io.getInput(Double.class), (Double)io.getOutput(Double.class)); - - table.refresh(); - } - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java index 44d69cf2..619aeaf1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java @@ -25,10 +25,7 @@ import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.jfree.chart.ChartFactory; @@ -44,8 +41,10 @@ import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; 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.representation.Auxiliary; import org.simantics.sysdyn.tableParser.ParseException; import org.simantics.sysdyn.tableParser.TableParser; import org.simantics.sysdyn.tableParser.Token; @@ -55,14 +54,13 @@ public class WithLookupExpression implements IExpression { private ExpressionField expression; private ExpressionField lookup; - private Button asGraph; private ExpressionField lastSelectedText = expression; - private Resource variable; - private LookupChartInfo chartInfo; private Timer updateChartTimer; + + private ChartPanel smallPanel; + private Frame smallFrame; - ChartPanel smallPanel; - Frame smallFrame; + private Resource variable; public WithLookupExpression(Resource variable) { this.variable = variable; @@ -71,9 +69,9 @@ public class WithLookupExpression implements IExpression { @Override public void createExpressionFields(Composite parent, final Map data) { GridLayoutFactory.fillDefaults().numColumns(3).applyTo(parent); - + updateChartTimer = new Timer(1000, new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { updateChart(); @@ -101,7 +99,7 @@ public class WithLookupExpression implements IExpression { Composite chartContainer = new Composite(parent, SWT.NONE); createChart(chartContainer, data); - + l = new Label(parent, SWT.NONE); l.setText("Lookup\ntable"); @@ -117,9 +115,9 @@ public class WithLookupExpression implements IExpression { lastSelectedText = lookup; } }); - + lookup.getSourceViewer().getTextWidget().addModifyListener(new ModifyListener() { - + @Override public void modifyText(ModifyEvent e) { if(!updateChartTimer.isRunning()) @@ -129,6 +127,48 @@ public class WithLookupExpression implements IExpression { } }); + + SimanticsUI.getSession().asyncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + String result = ""; + if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) { + result = graph.getRelatedValue(expression, sr.HasLookup); + } + return result; + } + }, new Listener() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(final String result) { + if(lookup != null) + lookup.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + lookup.setExpression(result); + } + }); + updateChart(); + } + + @Override + public boolean isDisposed() { + if(lookup != null && !lookup.isDisposed()) { + return false; + } + return true; + } + }); + updateChart(); } @@ -144,32 +184,37 @@ public class WithLookupExpression implements IExpression { @Override public void readData(final Resource variable, Map data) { - String[] results = null; + + class Auxiliary { + String equation, lookup; + } + + Auxiliary results = null; + if (variable != null && data.get("equation") == null) { try { - results = SimanticsUI.getSession().syncRequest(new Read() { + results = SimanticsUI.getSession().syncRequest(new Read() { @Override - public String[] perform(ReadGraph graph) throws DatabaseException { - String[] results = new String[2]; + public Auxiliary perform(ReadGraph graph) throws DatabaseException { + Auxiliary results = new Auxiliary(); SysdynResource sr = SysdynResource.getInstance(graph); Resource expression = graph.getPossibleObject(variable, sr.HasExpression); if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) { - results[0] = graph.getRelatedValue(expression, sr.HasEquation); - results[1] = graph.getRelatedValue(expression, sr.HasLookup); + results.equation = graph.getRelatedValue(expression, sr.HasEquation); + results.lookup = graph.getRelatedValue(expression, sr.HasLookup); } else { - results[0] = ""; - results[1] = ""; + results.equation = ""; + results.lookup = ""; } return results; } - }); } catch (DatabaseException e1) { e1.printStackTrace(); } - data.put("equation", results[0]); - data.put("lookup", results[1]); + data.put("equation", results.equation); + data.put("lookup", results.lookup); } } @@ -212,12 +257,14 @@ public class WithLookupExpression implements IExpression { g.claim(variable, sr.HasExpression, expression); g.claimValue(expression, sr.HasEquation, currentExpression); g.claimValue(expression, sr.HasLookup, currentLookupTable); - if(chartInfo != null) { - g.claimValue(expression, sr.HasMinX, chartInfo.minX); - g.claimValue(expression, sr.HasMaxX, chartInfo.maxX); - g.claimValue(expression, sr.HasMinY, chartInfo.minY); - g.claimValue(expression, sr.HasMaxY, chartInfo.maxY); - } + if(!g.hasStatement(expression, sr.HasMinX)) + g.claimValue(expression, sr.HasMinX, 0.0); + if(!g.hasStatement(expression, sr.HasMaxX)) + g.claimValue(expression, sr.HasMaxX, 10.0); + if(!g.hasStatement(expression, sr.HasMinY)) + g.claimValue(expression, sr.HasMinY, 0.0); + if(!g.hasStatement(expression, sr.HasMaxY)) + g.claimValue(expression, sr.HasMaxY, 10.0); } }); } @@ -229,13 +276,7 @@ public class WithLookupExpression implements IExpression { if(this.expression != null && this.expression.getExpression() != null) data.put("equation", this.expression.getExpression()); if(this.lookup != null && this.lookup.getExpression() != null) - data.put("lookup", this.lookup.getExpression()); - if(this.chartInfo != null) { - data.put("minX", chartInfo.minX); - data.put("maxX", chartInfo.maxX); - data.put("minY", chartInfo.minY); - data.put("maxY", chartInfo.maxY); - } + data.put("lookup", this.lookup.getExpression()); } @Override @@ -255,7 +296,7 @@ public class WithLookupExpression implements IExpression { this.expression.getSourceViewer().getTextWidget().addFocusListener(listener); this.lookup.getSourceViewer().getTextWidget().addFocusListener(listener); } - + private void createChart(Composite composite, final Map data) { GridLayoutFactory.fillDefaults().applyTo(composite); GridDataFactory.fillDefaults().span(1, 2).hint(150, SWT.DEFAULT).applyTo(composite); @@ -268,35 +309,19 @@ public class WithLookupExpression implements IExpression { JFreeChart chart = createChart(dataset); smallPanel = new ChartPanel(chart); smallFrame.add(smallPanel); - - asGraph = new Button(composite, SWT.NONE); - GridDataFactory.fillDefaults().align(SWT.END, SWT.BEGINNING).applyTo(asGraph); - asGraph.setText("Configure visually"); - asGraph.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { - if(chartInfo == null) chartInfo = new LookupChartInfo(expression.getExpression(), lookup.getExpression(), variable, data); - LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), chartInfo); - LookupChartInfo newInfo = pud.open(false); - if(pud.getReturnCode() == org.eclipse.jface.window.Window.OK) { - chartInfo = newInfo; - lookup.setExpression(chartInfo.lookupTable); - updateChart(); - lookup.focus(); - } - } - }); + } - + private static JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createXYLineChart( - null, - null, - null, - dataset, - PlotOrientation.VERTICAL, - true, - true, - false + null, + null, + null, + dataset, + PlotOrientation.VERTICAL, + true, + true, + false ); chart.removeLegend(); chart.getXYPlot().getDomainAxis().setTickLabelsVisible(false); @@ -308,7 +333,7 @@ public class WithLookupExpression implements IExpression { chart.getXYPlot().getRenderer().setSeriesStroke(0, new BasicStroke(3.0f)); return chart; } - + private void updateChart() { ArrayList dataPoints = new ArrayList(); TableParser parser = new TableParser(new StringReader("")); @@ -324,9 +349,9 @@ public class WithLookupExpression implements IExpression { } catch (ParseException e1) { return; } - + if(dataPoints.size() == 0) return; - + XYSeries series = new XYSeries("Lookup Table"); for(Point2D point : dataPoints) { series.add(point.getX(), point.getY()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java index 045bf160..7727d14e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java @@ -4,7 +4,6 @@ import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.management.ISessionContext; import org.simantics.utils.datastructures.Triple; public class DoublePropertyFactory extends ReadFactoryImpl { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyModifier.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyModifier.java new file mode 100644 index 00000000..7b800942 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyModifier.java @@ -0,0 +1,23 @@ +package org.simantics.sysdyn.ui.properties.widgets.factories; + +import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListenerImpl; +import org.simantics.databoard.binding.java.DoubleJavaBinding; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; + +public class DoublePropertyModifier extends TextModifyListenerImpl { + + final private String propertyURI; + + public DoublePropertyModifier(ISessionContext context, String propertyURI) { + this.propertyURI = propertyURI; + } + + @Override + public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException { + graph.claimValue(input, graph.getResource(propertyURI), Double.parseDouble(text), DoubleJavaBinding.INSTANCE); + } + +} -- 2.47.1