插件开发工具库,推荐依赖该工具库。
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.

161 lines
4.3 KiB

package com.fanruan.api.design.ui.editor;
import com.fanruan.api.design.ui.component.UITextField;
import com.fanruan.api.util.StringKit;
import com.fr.base.BaseFormula;
import com.fr.design.dialog.DialogActionAdapter;
import com.fr.design.formula.FormulaFactory;
import com.fr.design.formula.UIFormula;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* @author richie
* @version 10.0
* Created by richie on 2019/11/1
* 用于编辑和展示公式的编辑器
* TODO:待补充文档
*/
public class TypeFormulaEditor extends BaseEditor<BaseFormula> {
private BaseFormula formula = BaseFormula.createFormulaBuilder().build();
private UITextField currentTextField;
private TypeFormulaEditor.ShowPaneListener listener = new TypeFormulaEditor.ShowPaneListener();
public TypeFormulaEditor() {
this(StringKit.EMPTY);
}
public TypeFormulaEditor(String name) {
this(name, null);
}
public TypeFormulaEditor(String name, BaseFormula formula) {
if (formula != null) {
this.formula = formula;
}
this.setLayout(new BorderLayout());
JPanel editPane = new JPanel(new BorderLayout());
currentTextField = new UITextField(28);
currentTextField.setText(this.formula.getContent());
editPane.add(currentTextField, BorderLayout.CENTER);
currentTextField.setEditable(false);
currentTextField.addMouseListener(listener);
this.add(editPane, BorderLayout.CENTER);
this.setName(name);
}
private class ShowPaneListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (currentTextField.isEnabled()) {
showFormulaPane();
}
}
}
/**
* 选中时弹出公式编辑框
*/
public void selected() {
showFormulaPane();
}
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
currentTextField.setEnabled(enabled);
}
private void showFormulaPane() {
final UIFormula formulaPane = FormulaFactory.createFormulaPaneWhenReserveFormula();
formulaPane.populate(formula);
formulaPane.showLargeWindow(SwingUtilities.getWindowAncestor(TypeFormulaEditor.this), new DialogActionAdapter() {
@Override
public void doOk() {
formula = formulaPane.update();
setValue(formula);
fireStateChanged();
}
}).setVisible(true);
}
public BaseFormula getFormula() {
return formula;
}
@Override
public UITextField getSwingComponent() {
return currentTextField;
}
/**
* object是否是公式类型对象
*
* @param object 需判断的对象
* @return 是公式类型则返回true
*/
public boolean accept(Object object) {
return object instanceof BaseFormula;
}
@Override
public BaseFormula getValue() {
if (formula != null && "=".equals(formula.getContent())) {
return null;
}
return formula;
}
@Override
public void setValue(BaseFormula value) {
if (value == null) {
value = BaseFormula.createFormulaBuilder().build();
}
this.formula = value;
currentTextField.setText(value.toString());
}
public String getIconName() {
return "type_formula";
}
/**
* 重置
*/
public void reset() {
currentTextField.setText("=");
formula = BaseFormula.createFormulaBuilder().build();
}
/**
* 清楚数据
*/
public void clearData() {
reset();
}
/**
* 是否可用
*
* @param flag 为true代表可用
*/
public void enableEditor(boolean flag) {
this.setEnabled(flag);
this.currentTextField.setEnabled(flag);
if (!flag) {
this.currentTextField.removeMouseListener(listener);
} else {
int listenerSize = this.currentTextField.getMouseListeners().length;
for (int i = 0; i < listenerSize; i++) {
this.currentTextField.removeMouseListener(listener);
}
this.currentTextField.addMouseListener(listener);
}
}
}