|
|
|
@ -3,15 +3,27 @@ package com.fr.solution.plugin.design.formula.ui.type.impl;
|
|
|
|
|
import com.fr.base.Parameter; |
|
|
|
|
import com.fr.base.ParameterHelper; |
|
|
|
|
import com.fr.base.ScriptFormula; |
|
|
|
|
import com.fr.base.TemplateUtils; |
|
|
|
|
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea; |
|
|
|
|
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.SyntaxConstants; |
|
|
|
|
import com.fr.general.FRLogger; |
|
|
|
|
import com.fr.general.Inter; |
|
|
|
|
import com.fr.script.Calculator; |
|
|
|
|
import com.fr.script.ScriptFactory; |
|
|
|
|
import com.fr.solution.plugin.design.formula.ui.type.BaseFormulaPane; |
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
|
|
|
|
|
import javax.script.ScriptException; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
public class ScriptFormulaPane extends BaseFormulaPane<ScriptFormula> { |
|
|
|
|
|
|
|
|
|
private static final String FUN_TPL = "(function() {\n" + |
|
|
|
|
" var array = function(str) {return str ? str.split(\",\") : [];},int = function(str) {return parseInt(str);},number = function(str) {return parseFloat(str);};\n" + |
|
|
|
|
" %s\n" + |
|
|
|
|
"})();"; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected RSyntaxTextArea createContentEditor() { |
|
|
|
|
RSyntaxTextArea formulaTextArea = new RSyntaxTextArea(); |
|
|
|
@ -67,6 +79,35 @@ public class ScriptFormulaPane extends BaseFormulaPane<ScriptFormula> {
|
|
|
|
|
return "'${" + text + "}'"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected boolean isValidFormula(String formulaText) { |
|
|
|
|
Calculator calculator = Calculator.createCalculator(); |
|
|
|
|
ScriptFormula formula = createFormula(formulaText); |
|
|
|
|
Parameter[] parameters = analyze4Parameters(calculator); |
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
for (Parameter parameter : parameters) { |
|
|
|
|
String name = parameter.getName(); |
|
|
|
|
Object value = parameter.getValue(); |
|
|
|
|
if (value instanceof Boolean) { |
|
|
|
|
map.put(name, true); |
|
|
|
|
} else if (value instanceof Number) { |
|
|
|
|
map.put(name, 1); |
|
|
|
|
} else { |
|
|
|
|
map.put(name, "1"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
String content = TemplateUtils.renderParameter4Tpl(formula.getPureContent(), map); |
|
|
|
|
ScriptFactory.newSingletonScriptEngine().eval(String.format(FUN_TPL, content)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
FRLogger.getLogger().error(e.getMessage(), e); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Parameter[] analyze4Parameters(Calculator calculator) { |
|
|
|
|
ScriptFormula scriptFormula = updateBean(); |
|
|
|
|