|
|
|
package com.fr.solution.plugin.design.formula.ui.type.impl;
|
|
|
|
;
|
|
|
|
//import com.fanruan.api.base.ScriptFormula;
|
|
|
|
import com.fanruan.api.cal.CalculatorKit;
|
|
|
|
import com.fanruan.api.cal.ParameterKit;
|
|
|
|
import com.fanruan.api.design.ui.component.code.UISyntaxTextArea;
|
|
|
|
import com.fanruan.api.i18n.I18nKit;
|
|
|
|
import com.fanruan.api.script.ScriptKit;
|
|
|
|
import com.fanruan.api.util.StringKit;
|
|
|
|
import com.fanruan.api.util.RenderKit;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fr.base.ScriptFormula;
|
|
|
|
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.SyntaxConstants;
|
|
|
|
import com.fr.general.FRLogger;
|
|
|
|
import com.fr.solution.plugin.design.formula.ui.type.BaseFormulaPane;
|
|
|
|
import com.fr.stable.ParameterProvider;
|
|
|
|
import com.fr.stable.script.CalculatorProvider;
|
|
|
|
|
|
|
|
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 UISyntaxTextArea createContentEditor() {
|
|
|
|
UISyntaxTextArea formulaTextArea = new UISyntaxTextArea();
|
|
|
|
formulaTextArea.setCloseCurlyBraces(true);
|
|
|
|
formulaTextArea.setLineWrap(true);
|
|
|
|
formulaTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
|
|
|
|
formulaTextArea.setCodeFoldingEnabled(true);
|
|
|
|
formulaTextArea.setAntiAliasingEnabled(true);
|
|
|
|
return formulaTextArea;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(Object ob) {
|
|
|
|
return ob instanceof ScriptFormula;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reset() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void populateBean(ScriptFormula formula) {
|
|
|
|
if (formula != null) {
|
|
|
|
String content = formula.getContent();
|
|
|
|
fixContent(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ScriptFormula createFormula(String content) {
|
|
|
|
ScriptFormula formula;
|
|
|
|
if (StringKit.isEmpty(content) || content.trim().charAt(0) == '=') {
|
|
|
|
formula = new ScriptFormula(content);
|
|
|
|
} else {
|
|
|
|
formula = new ScriptFormula("=" + content);
|
|
|
|
}
|
|
|
|
return formula;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ScriptFormula createWrittenFormula(String content) {
|
|
|
|
return createFormula(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDoubleClick(String currentLineContent) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String wrapText(String text) {
|
|
|
|
return "'${" + text + "}'";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isValidFormula(String formulaText) {
|
|
|
|
CalculatorProvider calculator = CalculatorKit.createCalculator();
|
|
|
|
ScriptFormula formula = createFormula(formulaText);
|
|
|
|
ParameterProvider[] parameters = analyze4Parameters(calculator);
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
for (ParameterProvider 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 = RenderKit.renderParameter4Tpl(formula.getPureContent(), map);
|
|
|
|
ScriptKit.newScriptEngine().eval(String.format(FUN_TPL, content));
|
|
|
|
} catch (Exception e) {
|
|
|
|
FRLogger.getLogger().error(e.getMessage(), e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ParameterProvider[] analyze4Parameters(CalculatorProvider calculator) {
|
|
|
|
ScriptFormula scriptFormula = updateBean();
|
|
|
|
if (scriptFormula == null) {
|
|
|
|
return new ParameterProvider[0];
|
|
|
|
}
|
|
|
|
return ParameterKit.analyze4Parameters(scriptFormula.getContent(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String title4PopupWindow() {
|
|
|
|
return I18nKit.getLocText("Plugin-Design_Group_Formula_Script_Title");
|
|
|
|
}
|
|
|
|
}
|