// The MIT License (MIT)
//
// Copyright (c) 2015, 2016 Arian Fornaris
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions: The above copyright notice and this permission
// notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
package phasereditor.canvas.ui.editors.grid;

import org.eclipse.jface.viewers.ICellEditorValidator;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;

/**
 * @author arian
 *
 */
public class NumberCellEditor extends TextCellEditor {

	public NumberCellEditor(Composite parent) {
		super(parent);
		setValidator(new ICellEditorValidator() {

			@Override
			public String isValid(Object value) {
				if (value instanceof String) {
					try {
						Double.parseDouble((String) value);
					} catch (NumberFormatException e) {
						return "Invalid number format.";
					}
				}
				return null;
			}
		});
	}

	@Override
	protected void doSetValue(Object value) {
		super.doSetValue(value == null ? null : value.toString());
	}

	@Override
	protected Object doGetValue() {
		String value = (String) super.doGetValue();
		return Double.valueOf(value);
	}

}

@@ -1185,16 +1185,129 @@
 .grid;%0A%0A
+import javax.script.ScriptEngine;%0Aimport javax.script.ScriptEngineManager;%0Aimport javax.script.ScriptException;%0A%0A
 import o
@@ -1529,16 +1529,164 @@
 itor %7B%0A%0A
+%09private static ScriptEngine _engine;%0A%0A%09static %7B%0A%09%09ScriptEngineManager m = new ScriptEngineManager();%0A%09%09_engine = m.getEngineByName(%22nashorn%22);%0A%09%7D%0A%0A
 %09public 
@@ -1882,81 +1882,381 @@
 %09%09%09%09
-try %7B%0A%09%09%09%09%09%09Double.parseDouble((String) value);%0A%09%09%09%09%09%7D catch (NumberForma
+String script = (String) value;%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09Double.parseDouble(script);%0A%09%09%09%09%09%7D catch (NumberFormatException e) %7B%0A%09%09%09%09%09%09// try a javascript expression%0A%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09@SuppressWarnings(%22synthetic-access%22)%0A%09%09%09%09%09%09%09Object result = _engine.eval(script);%0A%09%09%09%09%09%09%09if (!(result instanceof Number)) %7B%0A%09%09%09%09%09%09%09%09return %22Invalid expression result.%22;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D catch (Scrip
 tExc
@@ -2259,28 +2259,30 @@
 tException e
+1
 ) %7B%0A
+%09
 %09%09%09%09%09%09return
@@ -2298,16 +2298,26 @@
  number 
+or script 
 format.%22
@@ -2314,24 +2314,32 @@
 t format.%22;%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%7D
@@ -2580,16 +2580,25 @@
 alue();%0A
+%09%09try %7B%0A%09
 %09%09return
@@ -2621,14 +2621,240 @@
 value);%0A
+%09%09%7D catch (NumberFormatException e) %7B%0A%09%09%09try %7B%0A%09%09%09%09Object result = _engine.eval(value);%0A%09%09%09%09return Double.valueOf(((Number)result).doubleValue());%0A%09%09%09%7D catch (ScriptException e1) %7B%0A%09%09%09%09throw new RuntimeException(e1);%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

allow javascript expressions in the number editors.