package com.fr.plugin.db.redis.ui.value; import com.fanruan.api.design.DesignKit; import com.fanruan.api.design.ui.component.UITextField; import com.fanruan.api.design.util.GUICoreKit; import com.fanruan.api.design.work.formula.FormulaUIKit; import com.fr.base.BaseFormula; import com.fr.design.dialog.DialogActionAdapter; import com.fr.design.editor.editor.Editor; import com.fr.design.formula.UIFormula; import com.fr.plugin.db.redis.core.order.impl.FormulaOrderValue; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class FormulaEditor extends Editor { private UITextField currentTextField; private FormulaEditor.ShowPaneListener listener = new FormulaEditor.ShowPaneListener(); private FormulaOrderValue orderValue = new FormulaOrderValue(BaseFormula.createFormulaBuilder().build()); /** * Constructor. */ public FormulaEditor() { this(DesignKit.i18nText("Plugin-Redis_Formula")); } public FormulaEditor(String name) { this(name, null); } public FormulaEditor(String name, FormulaOrderValue formula) { if (formula != null) { this.orderValue = formula; } this.setLayout(new BorderLayout()); JPanel editPane = GUICoreKit.createBorderLayoutPane(); currentTextField = new UITextField(28); currentTextField.setText(this.orderValue.getValue().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 setColumns(int i) { this.currentTextField.setColumns(i); } /** * 选中时弹出公式编辑框 */ public void selected() { showFormulaPane(); } public void setEnabled(boolean enabled) { super.setEnabled(enabled); currentTextField.setEnabled(enabled); } protected void showFormulaPane() { final UIFormula formulaPane = FormulaUIKit.createFormulaPaneWhenReserveFormula(); formulaPane.populate(orderValue.getValue()); formulaPane.showLargeWindow(SwingUtilities.getWindowAncestor(FormulaEditor.this), new DialogActionAdapter() { @Override public void doOk() { orderValue.setValue(formulaPane.update()); setValue(orderValue); fireStateChanged(); } }).setVisible(true); } /** * Return the value of the CellEditor. */ @Override public FormulaOrderValue getValue() { return orderValue; } /** * Set the value to the CellEditor. */ @Override public void setValue(FormulaOrderValue value) { if (value == null) { value = new FormulaOrderValue(BaseFormula.createFormulaBuilder().build()); } this.orderValue = value; currentTextField.setText(value.toString()); } public String getIconName() { return "type_formula"; } /** * object是否是公司类型对象 * * @param object 需判断的对象 * @return 是公式类型则返回true */ public boolean accept(Object object) { return object instanceof FormulaOrderValue; } /** * 重置 */ public void reset() { currentTextField.setText("="); orderValue = new FormulaOrderValue(BaseFormula.createFormulaBuilder().build()); } /** * 清楚数据 */ public void clearData() { reset(); } }