|
|
|
package com.fr.solution.plugin.design.formula.ui.type;
|
|
|
|
|
|
|
|
import com.fr.base.BaseFormula;
|
|
|
|
import com.fr.design.beans.FurtherBasicBeanPane;
|
|
|
|
import com.fr.design.gui.icontainer.UIScrollPane;
|
|
|
|
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea;
|
|
|
|
import com.fr.general.FRLogger;
|
|
|
|
import com.fr.general.Inter;
|
|
|
|
import com.fr.log.FineLoggerFactory;
|
|
|
|
import com.fr.parser.FRLexer;
|
|
|
|
import com.fr.parser.FRParser;
|
|
|
|
import com.fr.script.Calculator;
|
|
|
|
import com.fr.solution.plugin.design.formula.ui.evt.OperateListener;
|
|
|
|
import com.fr.stable.ProductConstants;
|
|
|
|
import com.fr.stable.StringUtils;
|
|
|
|
import com.fr.stable.script.Expression;
|
|
|
|
import com.fr.third.antlr.ANTLRException;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import java.awt.event.KeyListener;
|
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
import java.io.StringReader;
|
|
|
|
|
|
|
|
public abstract class BaseFormulaPane<T extends BaseFormula> extends FurtherBasicBeanPane<T> implements CommonEvent, KeyListener {
|
|
|
|
|
|
|
|
private static final int KEY_10 = 10;
|
|
|
|
//上下左右
|
|
|
|
private static final int KEY_37 = 37;
|
|
|
|
private static final int KEY_38 = 38;
|
|
|
|
private static final int KEY_39 = 39;
|
|
|
|
private static final int KEY_40 = 40;
|
|
|
|
|
|
|
|
|
|
|
|
private RSyntaxTextArea formulaTextArea;
|
|
|
|
private int currentPosition = 0;
|
|
|
|
private int beginPosition = 0;
|
|
|
|
private int insertPosition = 0;
|
|
|
|
private int ifHasBeenWritten = 0;
|
|
|
|
|
|
|
|
private OperateListener operateListener;
|
|
|
|
|
|
|
|
public BaseFormulaPane() {
|
|
|
|
setLayout(new BorderLayout());
|
|
|
|
initFormulaTextArea();
|
|
|
|
UIScrollPane formulaTextAreaScrollPane = new UIScrollPane(formulaTextArea);
|
|
|
|
formulaTextAreaScrollPane.setBorder(null);
|
|
|
|
add(formulaTextAreaScrollPane, BorderLayout.CENTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initFormulaTextArea() {
|
|
|
|
formulaTextArea = createContentEditor();
|
|
|
|
initFormulaTextAreaKeyListener();
|
|
|
|
initFormulaTextAreaMouseListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initFormulaTextAreaKeyListener() {
|
|
|
|
formulaTextArea.addKeyListener(this);
|
|
|
|
formulaTextArea.addKeyListener(new KeyAdapter() {
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
formulaTextArea.setForeground(Color.black);
|
|
|
|
String text = formulaTextArea.getText();
|
|
|
|
// 判断在中文输入状态是否还包含提示符 要删掉
|
|
|
|
String tip = "\n\n\n" + Inter.getLocText("Plugin-Design_Basic_FormulaPane_Tips");
|
|
|
|
if (text.contains(tip)) {
|
|
|
|
text = text.substring(0, text.indexOf(tip));
|
|
|
|
insertPosition = 0;
|
|
|
|
formulaTextArea.setText(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initFormulaTextAreaMouseListener() {
|
|
|
|
formulaTextArea.addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
insertPosition = formulaTextArea.getCaretPosition();
|
|
|
|
if (ifHasBeenWritten == 0) {
|
|
|
|
formulaTextArea.setText("");
|
|
|
|
ifHasBeenWritten = 1;
|
|
|
|
formulaTextArea.setForeground(Color.black);
|
|
|
|
insertPosition = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
if (currentPosition == insertPosition) {
|
|
|
|
beginPosition = getBeginPosition();
|
|
|
|
insertPosition = beginPosition;
|
|
|
|
firstStepToFindTips(beginPosition);
|
|
|
|
operateListener.fixFunctionNameList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getBeginPosition() {
|
|
|
|
int i = currentPosition;
|
|
|
|
String textArea = formulaTextArea.getText();
|
|
|
|
for (; i > 0; i--) {
|
|
|
|
String tested = textArea.substring(i - 1, i).toUpperCase();
|
|
|
|
char[] testedChar = tested.toCharArray();
|
|
|
|
if (isChar(testedChar[0]) || isNum(testedChar[0])) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isNum(char tested) {
|
|
|
|
return tested >= '0' && tested <= '9';
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isChar(char tested) {
|
|
|
|
return tested >= 'A' && tested <= 'Z';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void firstStepToFindTips(int theBeginPosition) {
|
|
|
|
String textArea = formulaTextArea.getText();
|
|
|
|
if (currentPosition > 0 && theBeginPosition < currentPosition) {
|
|
|
|
String next = textArea.substring(theBeginPosition, theBeginPosition + 1);
|
|
|
|
char[] nextChar = next.toCharArray();
|
|
|
|
if (!isNum(nextChar[0])) {
|
|
|
|
String toFind = textArea.substring(theBeginPosition, currentPosition);
|
|
|
|
operateListener.search(toFind, false);
|
|
|
|
formulaTextArea.requestFocusInWindow();
|
|
|
|
} else {
|
|
|
|
operateListener.clearMathListModel();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String toFind = textArea.substring(theBeginPosition, currentPosition);
|
|
|
|
operateListener.search(toFind, false);
|
|
|
|
formulaTextArea.requestFocusInWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reset() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract RSyntaxTextArea createContentEditor();
|
|
|
|
|
|
|
|
protected void fixContent(String content) {
|
|
|
|
if (content.trim().equals("=")) {
|
|
|
|
this.formulaTextArea.setForeground(Color.gray);
|
|
|
|
this.formulaTextArea.setText("\n\n\n" + Inter.getLocText("Plugin-Design_Basic_FormulaPane_Tips"));
|
|
|
|
this.formulaTextArea.setCaretPosition(0);
|
|
|
|
ifHasBeenWritten = 0;
|
|
|
|
operateListener.clearMathListModel();
|
|
|
|
} else if (content.trim().charAt(0) == '=') {
|
|
|
|
this.formulaTextArea.setText(content.trim().substring(1));
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
beginPosition = getBeginPosition();
|
|
|
|
insertPosition = beginPosition;
|
|
|
|
firstStepToFindTips(beginPosition);
|
|
|
|
operateListener.fixFunctionNameList();
|
|
|
|
ifHasBeenWritten = 1;
|
|
|
|
} else {
|
|
|
|
this.formulaTextArea.setText(content);
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
beginPosition = getBeginPosition();
|
|
|
|
insertPosition = beginPosition;
|
|
|
|
firstStepToFindTips(beginPosition);
|
|
|
|
operateListener.fixFunctionNameList();
|
|
|
|
ifHasBeenWritten = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected String getText() {
|
|
|
|
return formulaTextArea.getText();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T updateBean() {
|
|
|
|
String content = getText();
|
|
|
|
if (ifHasBeenWritten == 0) {
|
|
|
|
return createWrittenFormula(content);
|
|
|
|
} else {
|
|
|
|
return createFormula(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract T createWrittenFormula(String content);
|
|
|
|
|
|
|
|
protected abstract T createFormula(String content);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyTyped(KeyEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
if (ifHasBeenWritten == 0) {
|
|
|
|
this.formulaTextArea.setText(StringUtils.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
int key = e.getKeyCode();
|
|
|
|
// 如果是删除符号 ,为了可读性 没有和其他按键的程序相融合
|
|
|
|
if (key == KEY_38 || key == KEY_40 || key == KEY_37 || key == KEY_39 || key == KEY_10) {
|
|
|
|
operateListener.clearMathListModel();
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
insertPosition = currentPosition;
|
|
|
|
beginPosition = getBeginPosition();
|
|
|
|
} else {
|
|
|
|
if (this.formulaTextArea.getText().trim().length() == 0) {
|
|
|
|
insertPosition = 0;
|
|
|
|
operateListener.clearMathListModel();
|
|
|
|
} else {
|
|
|
|
this.formulaTextArea.setForeground(Color.black);
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
beginPosition = getBeginPosition();
|
|
|
|
insertPosition = beginPosition;
|
|
|
|
firstStepToFindTips(beginPosition);
|
|
|
|
operateListener.fixFunctionNameList();
|
|
|
|
ifHasBeenWritten = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply text.
|
|
|
|
*/
|
|
|
|
public void applyText(String text) {
|
|
|
|
if (text == null || text.length() <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ifHasBeenWritten == 0) {
|
|
|
|
formulaTextArea.setForeground(Color.black);
|
|
|
|
formulaTextArea.setText("");
|
|
|
|
ifHasBeenWritten = 1;
|
|
|
|
insertPosition = 0;
|
|
|
|
}
|
|
|
|
text = wrapText(text);
|
|
|
|
String textAll = formulaTextArea.getText();
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
int insert = 0;
|
|
|
|
int current = 0;
|
|
|
|
if (insertPosition <= currentPosition) {
|
|
|
|
insert = insertPosition;
|
|
|
|
current = currentPosition;
|
|
|
|
} else {
|
|
|
|
insert = currentPosition;
|
|
|
|
current = insertPosition;
|
|
|
|
}
|
|
|
|
String beforeIndexOfInsertString = textAll.substring(0, insert);
|
|
|
|
String afterIndexofInsertString = textAll.substring(current);
|
|
|
|
formulaTextArea.setText(beforeIndexOfInsertString + text + afterIndexofInsertString);
|
|
|
|
formulaTextArea.getText();
|
|
|
|
if (text.indexOf("()") != -1) {
|
|
|
|
formulaTextArea.setCaretPosition(insert + text.length() - 1);
|
|
|
|
}
|
|
|
|
formulaTextArea.requestFocus();
|
|
|
|
insertPosition = formulaTextArea.getCaretPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String wrapText(String text) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onDoubleClick(String currentLineContent) {
|
|
|
|
if (ifHasBeenWritten == 0) {
|
|
|
|
formulaTextArea.setForeground(Color.black);
|
|
|
|
formulaTextArea.setText("");
|
|
|
|
}
|
|
|
|
formulaTextArea.setForeground(Color.black);
|
|
|
|
currentPosition = formulaTextArea.getCaretPosition();
|
|
|
|
String output = currentLineContent + "()";
|
|
|
|
String textAll = formulaTextArea.getText();
|
|
|
|
String textReplaced;
|
|
|
|
int position = 0;
|
|
|
|
if (insertPosition <= currentPosition) {
|
|
|
|
textReplaced = textAll.substring(0, insertPosition) + output + textAll.substring(currentPosition);
|
|
|
|
position = insertPosition + output.length() - 1;
|
|
|
|
} else {
|
|
|
|
textReplaced = textAll.substring(0, currentPosition) + output + textAll.substring(insertPosition);
|
|
|
|
position = currentPosition + output.length() - 1;
|
|
|
|
}
|
|
|
|
formulaTextArea.setText(textReplaced);
|
|
|
|
formulaTextArea.requestFocusInWindow();
|
|
|
|
formulaTextArea.setCaretPosition(position);
|
|
|
|
insertPosition = position;
|
|
|
|
ifHasBeenWritten = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSingleClick() {
|
|
|
|
formulaTextArea.requestFocusInWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkContentValid() {
|
|
|
|
// Execute Formula default cell element.
|
|
|
|
String formulaText = formulaTextArea.getText().trim();
|
|
|
|
if (StringUtils.isNotEmpty(formulaText)) {
|
|
|
|
JOptionPane.showMessageDialog(
|
|
|
|
BaseFormulaPane.this,
|
|
|
|
(isValidFormula(formulaText) ? Inter.getLocText("Plugin-Design_Basic_FormulaD_Valid_Formula") : Inter.getLocText("Plugin-Design_Basic_FormulaD_Invalid_Formula")) + ".", ProductConstants.PRODUCT_NAME,
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract boolean isValidFormula(String formulaText);
|
|
|
|
|
|
|
|
public void onSearch() {
|
|
|
|
formulaTextArea.requestFocusInWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setOperateListener(OperateListener operateListener) {
|
|
|
|
this.operateListener = operateListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String title4PopupWindow() {
|
|
|
|
return Inter.getLocText("Plugin-Design_Group_Formula_Basic_Title");
|
|
|
|
}
|
|
|
|
}
|