import java.io.StringReader;\r
import java.util.ArrayList;\r
import java.util.Iterator;\r
-\r
import javax.swing.JComponent;\r
import javax.swing.JPanel;\r
-\r
import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.dialogs.IDialogConstants;\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.jface.viewers.ICellModifier;\r
import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.FocusEvent;\r
+import org.eclipse.swt.events.FocusListener;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
import org.eclipse.swt.events.ModifyEvent;\r
import org.eclipse.swt.events.ModifyListener;\r
import org.eclipse.swt.events.MouseListener;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.widgets.Button;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Control;\r
import org.eclipse.swt.widgets.Display;\r
import org.jfree.data.xy.XYDataset;\r
import org.jfree.data.xy.XYSeries;\r
import org.jfree.data.xy.XYSeriesCollection;\r
-import org.simantics.db.Resource;\r
import org.simantics.g2d.chassis.SWTAWTComponent;\r
import org.simantics.sysdyn.tableParser.ParseException;\r
import org.simantics.sysdyn.tableParser.Token;\r
import org.simantics.sysdyn.tableParser.TableParser;\r
import org.simantics.sysdyn.ui.equation.expressions.LookupInputOutputTable.InputOutput;\r
\r
-/**\r
- * should do something in format {{0,1},{1,2},{2,3}}\r
- * @author TLTEEMU\r
- *\r
- */\r
-\r
public class LookupPopup extends Dialog {\r
\r
JFreeChart chart;\r
- LookupChartPanel chartPanel;\r
- SWTAWTComponent c;\r
- JPanel panel;\r
- Resource variable;\r
LookupInputOutputTable table;\r
ArrayList<Point2D> dataPoints;\r
- \r
+ Text input, output;\r
Text minX, maxX, minY, maxY;\r
+ Label unit;\r
+ LookupChartInfo chartInfo;\r
\r
- protected LookupPopup(Shell parentShell, Resource variable, String tableAsString) {\r
+ protected LookupPopup(Shell parentShell, LookupChartInfo chartInfo) {\r
super(parentShell);\r
- this.variable = variable;\r
+ this.chartInfo = chartInfo;\r
this.dataPoints = new ArrayList<Point2D>();\r
- \r
TableParser parser = new TableParser(new StringReader(""));\r
- parser.ReInit(new StringReader(tableAsString));\r
+ parser.ReInit(new StringReader(chartInfo.lookupTable));\r
try {\r
parser.table();\r
ArrayList<Token> xTokens = parser.getXTokens();\r
Double.parseDouble(token.next.next.image)));\r
}\r
} catch (ParseException e1) {\r
- \r
}\r
- \r
+\r
}\r
\r
@Override\r
protected Control createDialogArea(Composite parent) {\r
+ KeyListener enterListener = new KeyListener() {\r
+\r
+ @Override\r
+ public void keyReleased(KeyEvent e) {}\r
\r
- chartPanel = new LookupChartPanel(createChart());\r
+ @Override\r
+ public void keyPressed(KeyEvent e) {\r
+ if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR)\r
+ getButton(IDialogConstants.OK_ID).forceFocus();\r
+ }\r
+ };\r
+\r
+ final LookupChartPanel chartPanel = new LookupChartPanel(createChart());\r
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));\r
chartPanel.setMouseZoomable(true, false);\r
chartPanel.setDomainZoomable(false);\r
GridDataFactory.fillDefaults().grab(false, true).applyTo(yAxis);\r
maxY = new Text(yAxis, SWT.BORDER | SWT.RIGHT);\r
GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(maxY);\r
+ maxY.addKeyListener(enterListener);\r
maxY.addModifyListener(getAxisBoundModifyListener());\r
maxY.setText("" + rangeAxis.getUpperBound());\r
Composite fillY = new Composite(yAxis, SWT.NONE);\r
- GridDataFactory.fillDefaults().grab(false, true).applyTo(fillY);\r
+ GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(false, true).applyTo(fillY);\r
+ GridLayoutFactory.fillDefaults().applyTo(fillY);\r
+ unit = new Label(fillY, SWT.RIGHT);\r
+ GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(unit);\r
+ unit.setText(""); //TODO: how to get and update units?\r
+ unit.addKeyListener(enterListener);\r
minY = new Text(yAxis, SWT.BORDER | SWT.RIGHT);\r
GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(minY);\r
+ minY.addKeyListener(enterListener);\r
minY.addModifyListener(getAxisBoundModifyListener());\r
minY.setText("" + rangeAxis.getLowerBound());\r
\r
- c = new SWTAWTComponent(container, SWT.BORDER) {\r
+ SWTAWTComponent c = new SWTAWTComponent(container, SWT.BORDER) {\r
@Override\r
protected JComponent createSwingComponent() {\r
- panel = new JPanel();\r
+ JPanel panel = new JPanel();\r
panel.setLayout(new GridLayout(1, 1));\r
panel.setPreferredSize(new java.awt.Dimension(500, 270));\r
panel.add(chartPanel);\r
\r
\r
Composite valueTableComposite = new Composite(container, SWT.NONE);\r
- GridDataFactory.fillDefaults().grab(true, true).applyTo(valueTableComposite);\r
- GridLayoutFactory.fillDefaults().applyTo(valueTableComposite);\r
+ GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(valueTableComposite);\r
+ GridLayoutFactory.fillDefaults().numColumns(3).applyTo(valueTableComposite);\r
\r
\r
table = new LookupInputOutputTable(valueTableComposite, SWT.NONE);\r
- GridDataFactory.fillDefaults().grab(true, true).applyTo(table);\r
+ GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(table);\r
chartPanel.setTable(table);\r
table.getTableViewer().setCellModifier(new InputOutputCellModifier());\r
table.getTableViewer().getTable().addMouseListener(new MouseListener() {\r
- \r
+\r
@Override\r
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {\r
if(e.button == MouseEvent.BUTTON3) {\r
chartPanel.addLocationToSeries(location);\r
}\r
\r
+ input = new Text(valueTableComposite, SWT.BORDER | SWT.RIGHT);\r
+ GridDataFactory.fillDefaults().hint(60, SWT.DEFAULT).applyTo(input);\r
+ input.setText("");\r
+ output = new Text(valueTableComposite, SWT.BORDER | SWT.RIGHT);\r
+ GridDataFactory.fillDefaults().hint(60, SWT.DEFAULT).applyTo(output);\r
+ output.setText("");\r
+\r
+ Button add = new Button(valueTableComposite, SWT.None);\r
+ add.setText("Add");\r
+ add.addSelectionListener(new SelectionListener() {\r
+\r
+ @Override\r
+ public void widgetSelected(SelectionEvent e) {\r
+ try {\r
+ Double in = Double.parseDouble(input.getText());\r
+ Double out = Double.parseDouble(output.getText());\r
+ chartPanel.addLocationToSeries(new Point2D.Double(in, out));\r
+ } catch (NumberFormatException e1) {\r
+ input.setText("");\r
+ output.setText("");\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void widgetDefaultSelected(SelectionEvent e) {}\r
+ });\r
+\r
+ FocusListener flistener = new FocusListener() {\r
+ @Override\r
+ public void focusGained(FocusEvent e) {\r
+ Text text = (Text)e.widget;\r
+ text.setSelection(0, text.getCharCount());\r
+ }\r
+ @Override\r
+ public void focusLost(FocusEvent e) { }\r
+ };\r
+\r
+\r
+ KeyListener listener = new KeyListener() {\r
+\r
+ @Override\r
+ public void keyPressed(KeyEvent e) {\r
+ if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
+ try {\r
+ Double in = Double.parseDouble(input.getText());\r
+ Double out = Double.parseDouble(output.getText());\r
+ chartPanel.addLocationToSeries(new Point2D.Double(in, out));\r
+ } catch (NumberFormatException e1) {\r
+ if(input.getText().isEmpty() && output.getText().isEmpty()) {\r
+ getButton(IDialogConstants.OK_ID).forceFocus();\r
+ return;\r
+ }\r
+ }\r
+ input.setText("");\r
+ output.setText("");\r
+ input.setFocus();\r
+ } \r
+ }\r
+\r
+ @Override\r
+ public void keyReleased(KeyEvent e) { }\r
+\r
+ };\r
+\r
+ input.addFocusListener(flistener);\r
+ input.addKeyListener(listener);\r
+ output.addFocusListener(flistener);\r
+ output.addKeyListener(listener);\r
\r
Label l = new Label(container, SWT.NONE);\r
l.setText("");\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(xAxis);\r
GridLayoutFactory.fillDefaults().numColumns(3).applyTo(xAxis);\r
minX = new Text(xAxis, SWT.BORDER | SWT.RIGHT);\r
- GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(minX);\r
+ GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(minX); \r
+ minX.addKeyListener(enterListener);\r
minX.addModifyListener(getAxisBoundModifyListener());\r
minX.setText("" + domainAxis.getLowerBound());\r
- Composite fillX = new Composite(xAxis, SWT.NONE);\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(fillX);\r
+ Composite fillX = new Composite(xAxis, SWT.CENTER);\r
+ GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, false).applyTo(fillX);\r
GridLayoutFactory.fillDefaults().applyTo(fillX);\r
l = new Label(fillX, SWT.NONE);\r
+ l.setText(chartInfo.expression);\r
maxX = new Text(xAxis, SWT.BORDER | SWT.RIGHT);\r
GridDataFactory.fillDefaults().hint(40, SWT.DEFAULT).applyTo(maxX);\r
+ maxX.addKeyListener(enterListener);\r
maxX.addModifyListener(getAxisBoundModifyListener());\r
maxX.setText("" + domainAxis.getUpperBound());\r
\r
return null;\r
}\r
\r
+ protected void createButtonsForButtonBar(Composite parent) {\r
+ // create OK and Cancel buttons by default\r
+ createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,\r
+ false);\r
+ createButton(parent, IDialogConstants.CANCEL_ID,\r
+ IDialogConstants.CANCEL_LABEL, false);\r
+ }\r
\r
- public String open(boolean moi) {\r
+ public LookupChartInfo open(Boolean bool) {\r
Shell shell = this.getShell();\r
if (shell == null || shell.isDisposed()) {\r
shell = null;\r
shell = this.getShell();\r
}\r
constrainShellSize();\r
-\r
shell.open();\r
+ getButton(IDialogConstants.OK_ID).setFocus();\r
Display display = getParentShell().getDisplay();\r
while (!shell.isDisposed()) {\r
if (!display.readAndDispatch()) {\r
display.sleep();\r
}\r
}\r
- return graphToModelicaTable();\r
+ return chartInfo;\r
}\r
-\r
+ \r
+ @Override\r
+ public void okPressed() {\r
+ chartInfo.lookupTable = graphToModelicaTable();\r
+ chartInfo.minX = Double.parseDouble(minX.getText());\r
+ chartInfo.maxX = Double.parseDouble(maxX.getText());\r
+ chartInfo.minY = Double.parseDouble(minY.getText());\r
+ chartInfo.maxY = Double.parseDouble(maxY.getText());\r
+ super.okPressed();\r
+ }\r
+ \r
@SuppressWarnings("unchecked")\r
private String graphToModelicaTable() {\r
StringBuilder b = new StringBuilder();\r
\r
private JFreeChart createChart() {\r
XYDataset dataset = createDataset();\r
- chart = ChartFactory.createXYLineChart("Lookup", "X", "Y",\r
+ chart = ChartFactory.createXYLineChart(null, null, null,\r
dataset, PlotOrientation.VERTICAL, false, true, false);\r
XYPlot plot = (XYPlot) chart.getPlot();\r
XYLineAndShapeRenderer renderer\r
\r
ValueAxis rangeAxis = plot.getRangeAxis();\r
rangeAxis.setAutoRange(false);\r
- rangeAxis.setRange(0, 40);\r
-\r
+ rangeAxis.setRange(chartInfo.minY, chartInfo.maxY);\r
ValueAxis domainAxis = plot.getDomainAxis();\r
domainAxis.setAutoRange(false);\r
- domainAxis.setRange(0, 30);\r
+ domainAxis.setRange(chartInfo.minX, chartInfo.maxX);\r
return chart;\r
}\r
\r
}\r
};\r
}\r
- \r
+\r
private class InputOutputCellModifier implements ICellModifier {\r
- \r
+\r
XYSeries series;\r
\r
public InputOutputCellModifier() {\r
\r
public void modify(Object element, String property, Object value) {\r
if (element instanceof Item) element = ((Item) element).getData();\r
- \r
+\r
InputOutput io = (InputOutput)element;\r
Double x = (Double)io.getInput(Double.class);\r
int item = series.indexOf(x);\r
series.remove(item);\r
- \r
+\r
if (LookupInputOutputTable.INPUT.equals(property)) {\r
Double newX = io.setInput((String)value);\r
// if has the same x-value, revert back\r
} else if (LookupInputOutputTable.OUTPUT.equals(property)) {\r
io.setOutput((String)value);\r
}\r
- \r
+\r
series.add((Double)io.getInput(Double.class), (Double)io.getOutput(Double.class));\r
- \r
- table.refresh();\r
\r
+ table.refresh();\r
}\r
}\r
\r
-\r
-\r
-\r
-\r
}\r
private Button asGraph;\r
private ExpressionField lastSelectedText = expression;\r
private Resource variable;\r
+ private LookupChartInfo chartInfo;\r
\r
+ public WithLookupExpressionViewFactor(Resource variable) {\r
+ super();\r
+ this.variable = variable;\r
+ }\r
+ \r
@Override\r
- public void createView(Composite parent, Map<String, Object> data) {\r
-\r
- final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
- final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";\r
+ public void createView(Composite parent, final Map<String, Object> data) {\r
+ String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
+ String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";\r
\r
GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
\r
asGraph.addSelectionListener(new SelectionAdapter() {\r
public void widgetSelected(SelectionEvent e) {\r
if(e.widget == asGraph) {\r
- LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), variable, lookup.getExpression());\r
- lookup.setExpression(pud.open(false));\r
+ if(chartInfo == null) chartInfo = new LookupChartInfo(expression.getExpression(), lookup.getExpression(), variable, data);\r
+ LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), chartInfo);\r
+ LookupChartInfo newInfo = pud.open(false);\r
+ if(pud.getReturnCode() == org.eclipse.jface.window.Window.OK) {\r
+ chartInfo = newInfo;\r
+ lookup.setExpression(chartInfo.lookupTable);\r
+ }\r
}\r
}\r
});\r
+\r
}\r
\r
@Override\r
}\r
data.put("equation", results[0]);\r
data.put("lookup", results[1]);\r
- this.variable = variable;\r
}\r
}\r
\r
final String currentLookupTable = lookup.getExpression();\r
\r
if(currentExpression != null && currentLookupTable != null) {\r
+ data.putAll(data);\r
data.put("equation", currentExpression);\r
data.put("lookup", currentLookupTable);\r
SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
g.claim(variable, sr.HasExpression, expression);\r
g.claimValue(expression, sr.HasEquation, currentExpression);\r
g.claimValue(expression, sr.HasLookup, currentLookupTable);\r
+ g.claimValue(expression, sr.HasMinX, chartInfo.minX);\r
+ g.claimValue(expression, sr.HasMaxX, chartInfo.maxX);\r
+ g.claimValue(expression, sr.HasMinY, chartInfo.minY);\r
+ g.claimValue(expression, sr.HasMaxY, chartInfo.maxY);\r
}\r
-\r
});\r
}\r
}\r
data.put("equation", this.expression.getExpression());\r
if(this.lookup != null && this.lookup.getExpression() != null)\r
data.put("lookup", this.lookup.getExpression());\r
+ if(this.chartInfo != null) {\r
+ data.put("minX", chartInfo.minX);\r
+ data.put("maxX", chartInfo.maxX);\r
+ data.put("minY", chartInfo.minY);\r
+ data.put("maxY", chartInfo.maxY);\r
+ }\r
}\r
\r
@Override\r