You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.9 KiB
123 lines
3.9 KiB
package com.fr.solution.plugin.design.formula.ui.type.impl; |
|
|
|
import com.fanruan.api.util.TemplateKit; |
|
import com.fr.base.Parameter; |
|
import com.fr.base.ParameterHelper; |
|
import com.fr.base.ScriptFormula; |
|
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 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(); |
|
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 (StringUtils.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) { |
|
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 = TemplateKit.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(); |
|
if (scriptFormula == null) { |
|
return new Parameter[0]; |
|
} |
|
return ParameterHelper.analyze4Parameters(scriptFormula.getContent(), true); |
|
} |
|
|
|
@Override |
|
public String title4PopupWindow() { |
|
return Inter.getLocText("Plugin-Design_Group_Formula_Script_Title"); |
|
} |
|
}
|
|
|