Browse Source

解决冲突

fix-lag
Hoky 3 years ago
parent
commit
b8ec812d9e
  1. 454
      designer-base/src/main/java/com/fr/design/formula/FormulaPane.java

454
designer-base/src/main/java/com/fr/design/formula/FormulaPane.java

@ -2,69 +2,36 @@ package com.fr.design.formula;
import com.fr.base.BaseFormula; import com.fr.base.BaseFormula;
import com.fr.base.BaseUtils; import com.fr.base.BaseUtils;
import com.fr.base.Parameter; import com.fr.base.FRContext;
import com.fr.base.ParameterMapNameSpace;
import com.fr.base.TableDataNameSpace;
import com.fr.base.io.IOFile;
import com.fr.data.TableDataSource;
import com.fr.design.actions.UpdateAction; import com.fr.design.actions.UpdateAction;
import com.fr.design.border.UIRoundedBorder; import com.fr.design.border.UIRoundedBorder;
import com.fr.design.constants.UIConstants; import com.fr.design.constants.UIConstants;
import com.fr.design.dialog.BasicDialog; import com.fr.design.dialog.BasicDialog;
import com.fr.design.dialog.BasicPane; import com.fr.design.dialog.BasicPane;
import com.fr.design.dialog.DialogActionAdapter;
import com.fr.design.dialog.FineJOptionPane;
import com.fr.design.file.HistoryTemplateListCache;
import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.ibutton.UIButton;
import com.fr.design.gui.icontainer.UIScrollPane; import com.fr.design.gui.icontainer.UIScrollPane;
import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.ilist.QuickList; import com.fr.design.gui.ilist.QuickList;
import com.fr.design.gui.itableeditorpane.ParameterTableModel;
import com.fr.design.gui.itableeditorpane.UITableEditorPane;
import com.fr.design.gui.itextarea.UITextArea; import com.fr.design.gui.itextarea.UITextArea;
import com.fr.design.gui.itextfield.UITextField; import com.fr.design.gui.itextfield.UITextField;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea; import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.SyntaxConstants; import com.fr.design.gui.syntax.ui.rsyntaxtextarea.SyntaxConstants;
import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.mainframe.DesignerContext; import com.fr.design.mainframe.DesignerContext;
import com.fr.design.mainframe.JTemplate;
import com.fr.design.parameter.ParameterInputPane;
import com.fr.design.utils.gui.GUICoreUtils; import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.ComparatorUtils;
import com.fr.general.FArray;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.parser.ArrayExpression;
import com.fr.parser.BlockIntervalLiteral;
import com.fr.parser.ColumnRowRangeInPage;
import com.fr.parser.FRLexer; import com.fr.parser.FRLexer;
import com.fr.parser.FRParser; import com.fr.parser.FRParser;
import com.fr.parser.SheetIntervalLiteral;
import com.fr.report.core.namespace.SimpleCellValueNameSpace;
import com.fr.script.Calculator;
import com.fr.script.ScriptConstants;
import com.fr.script.checker.FunctionCheckerDispatcher;
import com.fr.script.checker.exception.ConditionCheckWrongException;
import com.fr.script.checker.exception.FunctionCheckWrongException;
import com.fr.script.rules.FunctionParameterType;
import com.fr.script.rules.FunctionRule;
import com.fr.stable.EncodeConstants; import com.fr.stable.EncodeConstants;
import com.fr.stable.EssentialUtils; import com.fr.stable.ProductConstants;
import com.fr.stable.ParameterProvider;
import com.fr.stable.StringUtils; import com.fr.stable.StringUtils;
import com.fr.stable.UtilEvalError;
import com.fr.stable.script.ColumnRowRange;
import com.fr.stable.script.Expression; import com.fr.stable.script.Expression;
import com.fr.stable.script.Node;
import com.fr.stable.script.Tiny;
import com.fr.stable.script.TinyHunter;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -98,14 +65,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.Locale;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* 公式编辑面板 * 公式编辑面板
@ -115,24 +75,19 @@ import java.util.Set;
*/ */
public class FormulaPane extends BasicPane implements KeyListener, UIFormula { public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
public static final String VALID_FORMULA = Toolkit.i18nText("Fine-Design_Basic_FormulaD_Valid_Formula"); protected VariableTreeAndDescriptionArea variableTreeAndDescriptionArea;
public static final String INVALID_FORMULA = Toolkit.i18nText("Fine-Design_Basic_FormulaD_Invalid_Formula"); protected RSyntaxTextArea formulaTextArea;
public static final int DEFUAL_FOMULA_LENGTH = 103; protected UITextField keyWordTextField = new UITextField(18);
public static final String ELLIPSIS = "..."; protected int currentPosition = 0;
private VariableTreeAndDescriptionArea variableTreeAndDescriptionArea; protected int beginPosition = 0;
private RSyntaxTextArea formulaTextArea; protected int insertPosition = 0;
private UITextField keyWordTextField = new UITextField(18); protected JList tipsList;
private int currentPosition = 0;
private int beginPosition = 0;
private int insertPosition = 0;
private JList tipsList;
protected DefaultListModel listModel = new DefaultListModel(); protected DefaultListModel listModel = new DefaultListModel();
private int ifHasBeenWriten = 0; protected int ifHasBeenWriten = 0;
private DefaultListModel functionTypeListModel = new DefaultListModel(); protected DefaultListModel functionTypeListModel = new DefaultListModel();
private QuickList functionTypeList; protected QuickList functionTypeList;
private DefaultListModel functionNameModel; protected DefaultListModel functionNameModel;
private JList functionNameList; protected JList functionNameList;
private UITableEditorPane<ParameterProvider> editor4CalPane;
public FormulaPane() { public FormulaPane() {
initComponents(); initComponents();
@ -193,12 +148,10 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
keyWordTextField.addKeyListener(new KeyListener() { keyWordTextField.addKeyListener(new KeyListener() {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
//do nothing
} }
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
//do nothing
} }
@Override @Override
@ -243,20 +196,7 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
protected void initComponents() { protected void initComponents() {
this.setLayout(new BorderLayout(4, 4)); this.setLayout(new BorderLayout(4, 4));
initTextPane();
initTipsPane();
initVariableTreeAndDescriptionArea();
}
private void initVariableTreeAndDescriptionArea() {
variableTreeAndDescriptionArea = new VariableTreeAndDescriptionArea();
this.add(variableTreeAndDescriptionArea, BorderLayout.SOUTH);
}
private void initTextPane() {
// text // text
JPanel textPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); JPanel textPane = FRGUIPaneFactory.createBorderLayout_S_Pane();
this.add(textPane, BorderLayout.CENTER); this.add(textPane, BorderLayout.CENTER);
JPanel checkBoxandbuttonPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); JPanel checkBoxandbuttonPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane();
@ -271,29 +211,26 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
textPane.add(formulaTextAreaScrollPane, BorderLayout.CENTER); textPane.add(formulaTextAreaScrollPane, BorderLayout.CENTER);
textPane.add(checkBoxandbuttonPane, BorderLayout.SOUTH); textPane.add(checkBoxandbuttonPane, BorderLayout.SOUTH);
initTipsPane();
UIButton checkValidButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_FormulaD_Check_Valid")); UIButton checkValidButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_FormulaD_Check_Valid"));
UIButton calButton = new UIButton(Toolkit.i18nText("Fine-Design_Basic_Formula_Cal_Button"));
checkValidButton.addActionListener(checkValidActionListener); checkValidButton.addActionListener(checkValidActionListener);
calButton.addActionListener(calculateActionListener);
JPanel checkBoxPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); JPanel checkBoxPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane();
checkBoxPane.setPreferredSize(new Dimension(450, 30)); checkBoxPane.setPreferredSize(new Dimension(450, 30));
checkBoxandbuttonPane.add(checkBoxPane, BorderLayout.WEST); checkBoxandbuttonPane.add(checkBoxPane, BorderLayout.WEST);
checkBoxandbuttonPane.add(checkValidButton, BorderLayout.EAST); checkBoxandbuttonPane.add(checkValidButton, BorderLayout.EAST);
checkBoxandbuttonPane.add(calButton, BorderLayout.EAST);
extendCheckBoxPane(checkBoxPane); extendCheckBoxPane(checkBoxPane);
variableTreeAndDescriptionArea = new VariableTreeAndDescriptionArea();
ParameterTableModel model = new ParameterTableModel(0); this.add(variableTreeAndDescriptionArea, BorderLayout.SOUTH);
editor4CalPane = new UITableEditorPane<>(model);
} }
protected void extendCheckBoxPane(JPanel checkBoxPane) { protected void extendCheckBoxPane(JPanel checkBoxPane) {
// do nothing
} }
private void configFormulaArea() { protected void configFormulaArea() {
formulaTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_FORMULA); formulaTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_FORMULA);
formulaTextArea.setAnimateBracketMatching(true); formulaTextArea.setAnimateBracketMatching(true);
formulaTextArea.setAntiAliasingEnabled(true); formulaTextArea.setAntiAliasingEnabled(true);
@ -308,45 +245,13 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
} }
public class DoubleClick extends MouseAdapter { public class DoubleClick extends MouseAdapter {
String singlePressContent;
String doublePressContent;
@Override
public void mousePressed(MouseEvent e) {
int index = tipsList.getSelectedIndex();
if (index != -1) {
if (e.getClickCount() == 1) {
singlePressContent = (String) listModel.getElementAt(index);
} else if (e.getClickCount() == 2) {
doublePressContent = (String) listModel.getElementAt(index);
}
}
}
@Override @Override
public void mouseReleased(MouseEvent e) { public void mouseClicked(MouseEvent e) {
int index = tipsList.getSelectedIndex(); int index = tipsList.getSelectedIndex();
if (index != -1) { if (index != -1) {
if (e.getClickCount() == 1) { String currentLineContent = (String) listModel.getElementAt(index);
if (ComparatorUtils.equals((String) listModel.getElementAt(index), singlePressContent)) { if (e.getClickCount() == 2) {
singleClickActuator(singlePressContent);
}
} else if (e.getClickCount() == 2) {
if (ComparatorUtils.equals((String) listModel.getElementAt(index), doublePressContent)) {
doubleClickActuator(doublePressContent);
}
}
}
}
private void singleClickActuator(String currentLineContent) {
refreshDescriptionTextArea(currentLineContent);
formulaTextArea.requestFocusInWindow();
fixFunctionNameList();
}
private void doubleClickActuator(String currentLineContent) {
if (ifHasBeenWriten == 0) { if (ifHasBeenWriten == 0) {
formulaTextArea.setForeground(Color.black); formulaTextArea.setForeground(Color.black);
formulaTextArea.setText(""); formulaTextArea.setText("");
@ -370,6 +275,13 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
insertPosition = position; insertPosition = position;
ifHasBeenWriten = 1; ifHasBeenWriten = 1;
listModel.removeAllElements(); listModel.removeAllElements();
} else if (e.getClickCount() == 1) {
refreshDescriptionTextArea(currentLineContent);
formulaTextArea.requestFocusInWindow();
fixFunctionNameList();
}
}
} }
} }
@ -413,7 +325,7 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
} }
} }
private void fixFunctionNameList() { protected void fixFunctionNameList() {
if (tipsList.getSelectedValue() != null) { if (tipsList.getSelectedValue() != null) {
int signOfContinue = 1; int signOfContinue = 1;
int indexOfFunction = 0; int indexOfFunction = 0;
@ -446,7 +358,7 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
} }
private int getBeginPosition() { protected int getBeginPosition() {
int i = currentPosition; int i = currentPosition;
String textArea = formulaTextArea.getText(); String textArea = formulaTextArea.getText();
for (; i > 0; i--) { for (; i > 0; i--) {
@ -461,7 +373,7 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
return i; return i;
} }
private void firstStepToFindTips(int theBeginPosition) { protected void firstStepToFindTips(int theBeginPosition) {
String textArea = formulaTextArea.getText(); String textArea = formulaTextArea.getText();
if (currentPosition > 0 && theBeginPosition < currentPosition) { if (currentPosition > 0 && theBeginPosition < currentPosition) {
@ -491,7 +403,6 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
// do nothing
} }
protected void search(String keyWord, boolean findDescription) { protected void search(String keyWord, boolean findDescription) {
@ -501,18 +412,13 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
if (keyWord.length() != 0) { if (keyWord.length() != 0) {
NameAndDescription[] descriptions = FunctionConstants.ALL.getDescriptions(); NameAndDescription[] descriptions = FunctionConstants.ALL.getDescriptions();
int lengthOfDes = descriptions.length; int lengthOfDes = descriptions.length;
List<String> list = new ArrayList<>();
for (int i = 0; i < lengthOfDes; i++) { for (int i = 0; i < lengthOfDes; i++) {
NameAndDescription and = descriptions[i]; NameAndDescription and = descriptions[i];
String functionName = and.searchResult(keyWord, findDescription); String functionName = and.searchResult(keyWord, findDescription);
if (StringUtils.isNotBlank(functionName)) { if (StringUtils.isNotBlank(functionName)) {
list.add(functionName); listModel.addElement(functionName);
}
} }
Collections.sort(list, new SimilarComparator(keyWord));
for (String name : list) {
listModel.addElement(name);
} }
if (!listModel.isEmpty()) { if (!listModel.isEmpty()) {
@ -549,7 +455,7 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
/** /**
* Apply text. * Apply text.
*/ */
private void applyText(String text) { public void applyText(String text) {
if (text == null || text.length() <= 0) { if (text == null || text.length() <= 0) {
return; return;
} }
@ -647,234 +553,45 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
} }
// check valid // check valid
private final ActionListener checkValidActionListener = new ActionListener() { protected ActionListener checkValidActionListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// Execute Formula default cell element. // Execute Formula default cell element.
String formulaText = formulaTextArea.getText().trim(); String formulaText = formulaTextArea.getText().trim();
String formulaValidMessage = getFormulaValidMessage(formulaText);
FineJOptionPane.showMessageDialog(
FormulaPane.this,
formulaValidMessage + ".",
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Tool_Tips"),
JOptionPane.INFORMATION_MESSAGE);
}
};
private static String getFormulaValidMessage(String formulaText) { if (formulaText != null && formulaText.length() > 0) {
StringReader in = new StringReader(formulaText); StringReader in = new StringReader(formulaText);
FRLexer lexer = new FRLexer(in); FRLexer lexer = new FRLexer(in);
FRParser parser = new FRParser(lexer); FRParser parser = new FRParser(lexer);
Expression expression = null;
try { try {
Expression expression = parser.parse(); expression = parser.parse();
Node node = expression.getConditionalExpression();
return FunctionCheckerDispatcher.getInstance()
.getFunctionChecker(node)
.checkFunction(node) ? VALID_FORMULA : INVALID_FORMULA;
} catch (ConditionCheckWrongException cce) {
String functionName = cce.getFunctionName();
StringBuilder errorMsg = new StringBuilder(functionName + com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_Condition_Tips") + ":");
return errorMsg.toString();
} catch (FunctionCheckWrongException ce) {
List<FunctionRule> rules = ce.getRules();
String functionName = ce.getFunctionName();
StringBuilder errorMsg = new StringBuilder(functionName + com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_Error_Tips") + ":");
for (int i = 0; i < rules.size(); i++) {
errorMsg.append("(");
if (rules.get(i).getParameterList().isEmpty()) {
errorMsg.append(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_No_Param"));
}
for (FunctionParameterType functionParameterType : rules.get(i).getParameterList()) {
errorMsg.append(getTypeString(functionParameterType)).append(",");
}
if (",".equals(errorMsg.charAt(errorMsg.length() - 1) + "")) {
errorMsg.deleteCharAt(errorMsg.length() - 1);
}
errorMsg.append(")");
if (i != rules.size() - 1) {
errorMsg.append(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_Or"));
}
}
return errorMsg.toString();
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e); FRContext.getLogger().error(e.getMessage(), e);
return INVALID_FORMULA;
// alex:继续往下面走,expression为null时告知不合法公式 // alex:继续往下面走,expression为null时告知不合法公式
} }
}
private static String getTypeString(FunctionParameterType type) { JOptionPane.showMessageDialog(
switch (type) {
case NUMBER:
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_ParamType_Number");
case STRING:
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_ParamType_String");
case ANY:
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_ParamType_Any");
case DATE:
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_ParamType_Date");
case BOOLEAN:
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_ParamType_Boolean");
case ARRAY:
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Check_ParamType_Array");
}
return "";
}
private final ActionListener calculateActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String formulaText = formulaTextArea.getText().trim();
String formulaValidMessage = getFormulaValidMessage(formulaText);
String unSupportFormula = containsUnsupportedSimulationFormulas(formulaText);
if (unSupportFormula != null) {
FineJOptionPane.showMessageDialog(
FormulaPane.this, FormulaPane.this,
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Unsupported_Formulas") + ":" + unSupportFormula, /*
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Tool_Tips"), * alex:仅仅只需要根据expression是否为null作合法性判断
* 不需要eval
* TODO 但有个问题,有些函数的参数个数是有规定的,何以判别之
*/
(expression != null ? com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_FormulaD_Valid_Formula") : com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_FormulaD_Invalid_Formula")) + ".", ProductConstants.PRODUCT_NAME,
JOptionPane.INFORMATION_MESSAGE); JOptionPane.INFORMATION_MESSAGE);
return;
} }
String messageTips;
if (formulaValidMessage.equals(INVALID_FORMULA)) {
messageTips = INVALID_FORMULA;
} else {
messageTips = formulaValidMessage.equals(VALID_FORMULA) ? "" : formulaValidMessage + "\n";
Map<String, Object> paramsMap = setParamsIfExist(formulaText);
Calculator calculator = Calculator.createCalculator();
ParameterMapNameSpace parameterMapNameSpace = ParameterMapNameSpace.create(paramsMap);
calculator.pushNameSpace(parameterMapNameSpace);
JTemplate<?, ?> currentEditingTemplate = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate();
if (currentEditingTemplate != null) {
IOFile file = (IOFile) currentEditingTemplate.getTarget();
calculator.setAttribute(TableDataSource.KEY, file);
calculator.pushNameSpace(TableDataNameSpace.getInstance());
calculator.pushNameSpace(SimpleCellValueNameSpace.getInstance());
}
BaseFormula baseFormula = BaseFormula.createFormulaBuilder().build(formulaText);
try {
Object value = calculator.evalValue(baseFormula);
String objectToString = EssentialUtils.objectToString(value);
String result = objectToString.length() > DEFUAL_FOMULA_LENGTH ?
objectToString.substring(0, DEFUAL_FOMULA_LENGTH - ELLIPSIS.length()) + ELLIPSIS : objectToString;
messageTips = messageTips +
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula_Cal_Result") + ":" + result;
FineLoggerFactory.getLogger().info("value:{}", value);
} catch (UtilEvalError utilEvalError) {
FineLoggerFactory.getLogger().error("", utilEvalError);
}
}
FineJOptionPane.showMessageDialog(
FormulaPane.this,
messageTips,
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Tool_Tips"),
JOptionPane.INFORMATION_MESSAGE);
} }
}; };
private String containsUnsupportedSimulationFormulas(String formulaText) {
try {
Expression expression = Calculator.createCalculator().parse(formulaText);
UnsupportedFormulaScanner scanner = new UnsupportedFormulaScanner();
if (!scanner.travelFormula(expression.getConditionalExpression())) {
return scanner.getUnSupportFormula();
}
UnsupportedSimulationFormulaHunter unsupportedSimulationFormulaHunter = new UnsupportedSimulationFormulaHunter();
expression.traversal4Tiny(unsupportedSimulationFormulaHunter);
return unsupportedSimulationFormulaHunter.isSupported() ? null : unsupportedSimulationFormulaHunter.getUnSupportFormula();
} catch (Exception e) {
FineLoggerFactory.getLogger().error("", e);
}
return null;
}
private Map<String, Object> setParamsIfExist(String formulaText) {
Map<String, Object> parameterMap = new HashMap<>();
try {
Calculator calculator = Calculator.createCalculator();
Expression expression = calculator.parse(formulaText);
ParameterCellHunter parameterCellHunter = new ParameterCellHunter();
expression.traversal4Tiny(parameterCellHunter);
Parameter[] parameters = parameterCellHunter.getParameterBooty();
if (parameters.length < 1 && editor4CalPane.update().size() < 1) {
return parameterMap;
}
ParameterInputPane pPane = new ParameterInputPane(parameters);
pPane.showSmallWindow(new JFrame(), new DialogActionAdapter() {
@Override
public void doOk() {
parameterMap.putAll(pPane.update());
}
}).setVisible(true);
//过滤出数组参数,如:[1,2]
for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
if (entry.getValue().toString().startsWith("[") && entry.getValue().toString().endsWith("]")) {
Expression parse = calculator.parse(entry.getValue());
ArrayExpression arrayExpression = (ArrayExpression) parse.getConditionalExpression();
FArray<Node> fArray = new FArray<>(arrayExpression.getArrays());
parameterMap.put(entry.getKey(), fArray);
}
}
} catch (Exception e) {
FineLoggerFactory.getLogger().error("", e);
}
return parameterMap;
}
private static class ParameterCellHunter extends TinyHunter {
private final Set<Parameter> parameterList = new HashSet<>();
Parameter[] getParameterBooty() {
return parameterList.toArray(new Parameter[0]);
}
public void hunter4Tiny(Tiny tiny) {
String statement = tiny.getStatement();
if (StringUtils.isNotBlank(statement) && statement.startsWith(ScriptConstants.DETAIL_TAG)) {
parameterList.add(new Parameter(statement.substring(1)));
} else if (tiny.getClass() == ColumnRowRange.class ||
tiny.getClass() == SheetIntervalLiteral.class || tiny.getClass() == BlockIntervalLiteral.class) {
parameterList.add(new Parameter(tiny.toString()));
}
}
}
private static class UnsupportedSimulationFormulaHunter extends TinyHunter {
private boolean supported = true;
private String unSupportFormula;
public boolean isSupported() {
return supported;
}
public String getUnSupportFormula() {
return unSupportFormula;
}
public void hunter4Tiny(Tiny tiny) {
if (tiny.getClass() == ColumnRowRangeInPage.class) {
supported = false;
unSupportFormula = tiny.toString();
}
}
}
public class VariableTreeAndDescriptionArea extends JPanel { public class VariableTreeAndDescriptionArea extends JPanel {
private JTree variablesTree; private JTree variablesTree;
private UITextArea descriptionTextArea; private UITextArea descriptionTextArea;
VariableTreeAndDescriptionArea() { public VariableTreeAndDescriptionArea() {
this.initComponents(); this.initComponents();
} }
@ -1053,31 +770,31 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
private void initVariablesTreeSelectionListener() { private void initVariablesTreeSelectionListener() {
variablesTree.addTreeSelectionListener(new TreeSelectionListener() { variablesTree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) { public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selectedTreeNode = (DefaultMutableTreeNode) variablesTree.getLastSelectedPathComponent(); Object selectedValue = ((DefaultMutableTreeNode) variablesTree.getLastSelectedPathComponent()).getUserObject();
Object selectedValue = selectedTreeNode.getUserObject();
Object selectedParentValue = ((DefaultMutableTreeNode) selectedTreeNode.getParent()).getUserObject();
if (selectedValue == null) { if (selectedValue == null) {
return; return;
} }
StringBuilder desBuf = new StringBuilder();
if (selectedValue instanceof TextUserObject) { try {
//有公式说明的条件:1.属于TextUserObject 2.parent是系统参数 String path;
if (ComparatorUtils.equals(((TextFolderUserObject) selectedParentValue).getText(), Locale locale = FRContext.getLocale();
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_FormulaPane_Variables"))) { if (locale.equals(Locale.CHINA)) {
descriptionTextArea.setText(com.fr.design.i18n.Toolkit.i18nText(FormulaConstants.getValueByKey(((TextUserObject) selectedValue).getText()))); path = "/com/fr/design/insert/formula/variable/cn/";
} else { } else {
descriptionTextArea.setText(StringUtils.EMPTY); path = "/com/fr/design/insert/formula/variable/en/";
}
if (selectedValue instanceof TextUserObject) {
desBuf = getText((TextUserObject) selectedValue, path);
} }
} else if (selectedValue instanceof TextFolderUserObject) { } catch (IOException exp) {
descriptionTextArea.setText(StringUtils.EMPTY); FineLoggerFactory.getLogger().error(exp.getMessage(), exp);
} }
descriptionTextArea.setText(desBuf.toString());
descriptionTextArea.moveCaretPosition(0); descriptionTextArea.moveCaretPosition(0);
} }
}); });
} }
private void initVariablesTree() { private void initVariablesTree() {
// vairable. // vairable.
variablesTree = new JTree(); variablesTree = new JTree();
@ -1097,15 +814,13 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
private void initComponents() { private void initComponents() {
this.setLayout(new BorderLayout(4, 4)); this.setLayout(new BorderLayout(4, 4));
initVariablesTree(); // Function
initFunctionPane();
}
private void initFunctionPane() {
JPanel functionPane = new JPanel(new BorderLayout(4, 4)); JPanel functionPane = new JPanel(new BorderLayout(4, 4));
this.add(functionPane, BorderLayout.WEST); this.add(functionPane, BorderLayout.WEST);
initFunctionTypeList(functionPane); initFunctionTypeList(functionPane);
initFunctionNameList(functionPane); initFunctionNameList(functionPane);
initVariablesTree();
// 选择:
functionTypeList.setSelectedIndex(0); functionTypeList.setSelectedIndex(0);
} }
@ -1250,42 +965,13 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
} }
} }
public static class SimilarComparator implements Comparator<String> {
private String searchKey;
public SimilarComparator(String searchKey) {
this.searchKey = searchKey.toLowerCase();
}
/**
* 把以关键词开头的和不以关键词开头的分别按照字母表顺序排序
*
* @param o1 待比较对象1
* @param o2 待比较对象2
* @return 比较结果1表示 o1 > o2, -1表示 o1 < o2, 0表示 o1 = o2
*/
@Override
public int compare(String o1, String o2) {
int result;
boolean o1StartWith = o1.toLowerCase().startsWith(searchKey);
boolean o2StartWith = o2.toLowerCase().startsWith(searchKey);
if (o1StartWith) {
result = o2StartWith ? o1.compareTo(o2) : -1;
} else {
result = o2StartWith ? 1 : o1.compareTo(o2);
}
return result;
}
}
public static class TextFolderUserObject { public static class TextFolderUserObject {
private String text; private String text;
private Icon icon; private Icon icon;
private String[] subNodes = new String[0]; private String[] subNodes = new String[0];
TextFolderUserObject(String text, Icon icon, String[] subNodes) { public TextFolderUserObject(String text, Icon icon, String[] subNodes) {
this.text = text; this.text = text;
this.icon = icon; this.icon = icon;
this.subNodes = subNodes; this.subNodes = subNodes;
@ -1312,11 +998,11 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
public static class TextUserObject { public static class TextUserObject {
TextUserObject(String text) { public TextUserObject(String text) {
this(text, text); this(text, text);
} }
TextUserObject(String text, String displayText) { public TextUserObject(String text, String displayText) {
this.text = text; this.text = text;
this.displayText = displayText; this.displayText = displayText;
} }
@ -1325,7 +1011,7 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
return this.text; return this.text;
} }
String getDisplayText() { public String getDisplayText() {
return this.displayText; return this.displayText;
} }
@ -1350,6 +1036,6 @@ public class FormulaPane extends BasicPane implements KeyListener, UIFormula {
buffer.append("|"); buffer.append("|");
buffer.append("\n"); buffer.append("\n");
} }
FineLoggerFactory.getLogger().debug(buffer.toString()); FRContext.getLogger().debug(buffer.toString());
} }
} }

Loading…
Cancel
Save