From 90ad1f38e1b5f406b0d9320d35d3b06d1224042d Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 1 Nov 2019 17:23:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/design/ui/component/UIDatePicker.java | 276 ++++++++++++++++++ .../api/design/ui/editor/BaseEditor.java | 20 ++ .../design/ui/editor/BaseNumberEditor.java | 75 +++++ .../api/design/ui/editor/NoneEditor.java | 65 +++++ .../api/design/ui/editor/TextEditor.java | 14 - .../design/ui/editor/TypeBooleanEditor.java | 89 ++++++ .../ui/editor/TypeColumnIndexEditor.java | 114 ++++++++ .../ui/editor/TypeColumnNameEditor.java | 78 +++++ .../api/design/ui/editor/TypeDateEditor.java | 150 ++++++++++ .../design/ui/editor/TypeDoubleEditor.java | 47 +++ .../api/design/ui/editor/TypeFloatEditor.java | 47 +++ .../design/ui/editor/TypeFormulaEditor.java | 161 ++++++++++ .../design/ui/editor/TypeIntegerEditor.java | 48 +++ .../api/design/ui/editor/TypeLongEditor.java | 43 +++ .../design/ui/editor/TypeParameterEditor.java | 63 ++++ .../api/design/ui/editor/TypeTextEditor.java | 146 +++++++++ .../design/work/component/IntComboBox.java | 57 ++++ .../work/component/ParameterComboBox.java | 84 ++++++ 18 files changed, 1563 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/fanruan/api/design/ui/component/UIDatePicker.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/BaseEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/BaseNumberEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/NoneEditor.java delete mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TextEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeBooleanEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeColumnIndexEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeColumnNameEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeDateEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeDoubleEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeFloatEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeFormulaEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeIntegerEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeLongEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeParameterEditor.java create mode 100644 src/main/java/com/fanruan/api/design/ui/editor/TypeTextEditor.java create mode 100644 src/main/java/com/fanruan/api/design/work/component/IntComboBox.java create mode 100644 src/main/java/com/fanruan/api/design/work/component/ParameterComboBox.java diff --git a/src/main/java/com/fanruan/api/design/ui/component/UIDatePicker.java b/src/main/java/com/fanruan/api/design/ui/component/UIDatePicker.java new file mode 100644 index 0000000..d92a27e --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/component/UIDatePicker.java @@ -0,0 +1,276 @@ +package com.fanruan.api.design.ui.component; + +import com.fanruan.api.log.LogKit; +import com.fanruan.api.util.AssistKit; +import com.fanruan.api.util.StringKit; +import com.fr.design.gui.date.JDateDocument; +import com.fr.design.gui.date.SingleObjectComboBoxModel; +import com.fr.design.gui.date.UICalendarPanel; +import com.fr.design.gui.icombobox.UIComboBoxUI; + +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.plaf.ComboBoxUI; +import javax.swing.plaf.basic.BasicComboPopup; +import javax.swing.plaf.basic.ComboPopup; +import java.awt.*; +import java.awt.event.MouseEvent; +import java.io.Serializable; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + */ +public class UIDatePicker extends UIComboBox implements Serializable { + /** + * 日期格式类型 + */ + public static final int STYLE_CN_DATE = 0; + public static final int STYLE_CN_DATE1 = 1; + public static final int STYLE_CN_DATETIME = 2; + public static final int STYLE_CN_DATETIME1 = 3; + public static final int STYLE_EN_DATE = 4; + public boolean isWillHide = false; + + /** + * 日期格式类型 + */ + private int formatStyle = STYLE_CN_DATETIME; + /** + * 当前设置日期格式 + */ + private SimpleDateFormat dateFormat = null; + + /** + * 只有一个值的ComboBoxModel + */ + private SingleObjectComboBoxModel model = new SingleObjectComboBoxModel(); + + private JDateDocument dateDocument = null; + + public UIDatePicker() throws UnsupportedOperationException { + this(STYLE_CN_DATE); + } + + public UIDatePicker(int formatStyle) throws UnsupportedOperationException { + this(formatStyle, new Date()); + } + + public UIDatePicker(int formatStyle, Date initialDatetime) throws UnsupportedOperationException { + this.setStyle(formatStyle); + //设置可编辑 + this.setEditable(true); + + this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + + //设置编辑器属性(只能输入正确日期) + JTextField textField = ((JTextField) getEditor().getEditorComponent()); + textField.setHorizontalAlignment(SwingConstants.CENTER); + dateDocument = new JDateDocument(textField, this.dateFormat); + textField.setDocument(dateDocument); + //设置Model为单值Model + this.setModel(model); + //设置当前选择日期 + this.setSelectedItem(initialDatetime == null ? new Date() : initialDatetime); + updateUI(); + } + + /** + * 设置日期格式 + * STYLE_CN_DATE + * STYLE_CN_DATE1 + * STYLE_CN_DATETIME + * STYLE_CN_DATETIME1 + * + * @param formatStyle int + */ + public void setStyle(int formatStyle) throws UnsupportedOperationException { + this.formatStyle = formatStyle; + dateFormat = getDateFormat(formatStyle); + model.setDateFormat(dateFormat); + if (dateDocument != null) { + dateDocument.setDateFormat(dateFormat); + } + } + + /** + * 取得指定类型的日期格式 + * + * @param formatStyle int + * @return SimpleDateFormat + * @throws UnsupportedOperationException + */ + private static SimpleDateFormat getDateFormat(int formatStyle) throws + UnsupportedOperationException { + switch (formatStyle) { + case STYLE_CN_DATE: + return new SimpleDateFormat("yyyy/MM/dd"); + case STYLE_CN_DATE1: + return new SimpleDateFormat("yyyy-MM-dd"); + case STYLE_CN_DATETIME: + return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + case STYLE_CN_DATETIME1: + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + case STYLE_EN_DATE: + return new SimpleDateFormat("MM/dd/yyyy"); + default: + throw new UnsupportedOperationException( + "invalid formatStyle parameter!"); + } + } + + /** + * 取得日期格式 + * STYLE_CN_DATE + * STYLE_CN_DATE1 + * STYLE_CN_DATETIME + * STYLE_CN_DATETIME1 + * + * @return int + */ + public int getStyle() { + return formatStyle; + } + + /** + * 取得当前选择的日期 + * + * @return Date + */ + public Date getSelectedDate() throws ParseException { + synchronized (this) { + return dateFormat.parse(getSelectedItem().toString()); + } + } + + /** + * 设置当前选择的日期 + */ + public synchronized void setSelectedDate(Date date) throws ParseException { + if (date == null) { + this.setSelectedItem(null); + } else { + this.setSelectedItem(dateFormat.format(date)); + } + } + + @Override + public void setSelectedItem(Object anObject) { + model.setSelectedItem(anObject); + super.setSelectedItem(anObject); + } + + class DatePopup extends BasicComboPopup implements ChangeListener { + UICalendarPanel calendarPanel = null; + + public DatePopup(JComboBox box) { + super(box); + + setLayout(new BorderLayout()); + calendarPanel = new UICalendarPanel(formatStyle > 1); + calendarPanel.addDateChangeListener(this); + add(calendarPanel, BorderLayout.CENTER); + setBorder(BorderFactory.createEmptyBorder()); + } + + @Override + public void hide() { + if (isWillHide) { + super.hide(); + } + } + + @Override + public void show() { + if (isWillHide || !UIDatePicker.this.isEnabled()) { + return; + } + if (calendarPanel != null) { + calendarPanel.resetHMSPaneSelectedNumberField(); + } + super.show(); + } + + /** + * 显示弹出面板 + */ + @Override + protected void firePropertyChange(String propertyName, + Object oldValue, + Object newValue) { + if (AssistKit.equals(propertyName, "visible")) { + if (oldValue == Boolean.FALSE && newValue == Boolean.TRUE) { //SHOW + try { + String strDate = comboBox.getSelectedItem().toString(); + synchronized (this) { + Date selectionDate = new Date(); + if (StringKit.isNotBlank(strDate)) { + selectionDate = dateFormat.parse(strDate); + } + calendarPanel.setSelectedDate(selectionDate); + calendarPanel.updateHMS(); + } + } catch (Exception e) { + LogKit.error(e.getMessage(), e); + } + } else if (oldValue == Boolean.TRUE + && newValue == Boolean.FALSE) { + } + } + super.firePropertyChange(propertyName, oldValue, newValue); + } + + public void stateChanged(ChangeEvent e) { + if (calendarPanel.getSelectedDate() != null && dateFormat != null) { + String strDate = dateFormat.format(calendarPanel.getSelectedDate()); + if (comboBox.isEditable() && comboBox.getEditor() != null) { + comboBox.configureEditor(comboBox.getEditor(), strDate); + } + comboBox.setSelectedItem(strDate); + } + comboBox.repaint(); + setVisible(false); + } + } + + @Override + protected ComboBoxUI getUIComboBoxUI() { + return new UIComboBoxUI() { + @Override + protected ComboPopup createPopup() { + return new UIDatePicker.DatePopup(comboBox); + } + + @Override + public void mousePressed(MouseEvent e) { + if (UIDatePicker.this.isPopupVisible()) { + isWillHide = true; + UIDatePicker.this.hidePopup(); + } else { + isWillHide = false; + UIDatePicker.this.showPopup(); + } + } + }; + } + + //设置dataFormat + public void setDateFormat(SimpleDateFormat format) { + this.dateFormat = format; + } + + //获取dateFormat + public SimpleDateFormat getDateFormat() { + return this.dateFormat; + } + + public JDateDocument getDateDocument() { + return this.dateDocument; + } + +} diff --git a/src/main/java/com/fanruan/api/design/ui/editor/BaseEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/BaseEditor.java new file mode 100644 index 0000000..a3e6ab2 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/BaseEditor.java @@ -0,0 +1,20 @@ +package com.fanruan.api.design.ui.editor; + +import com.fr.design.editor.editor.Editor; + +import javax.swing.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + */ +public abstract class BaseEditor extends Editor { + + /** + * 返回用于编辑和展示值的swing控件 + * + * @return swing控件 + */ + public abstract JComponent getSwingComponent(); +} diff --git a/src/main/java/com/fanruan/api/design/ui/editor/BaseNumberEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/BaseNumberEditor.java new file mode 100644 index 0000000..aa32500 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/BaseNumberEditor.java @@ -0,0 +1,75 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.ui.component.UINumberField; +import com.fanruan.api.util.StringKit; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示数值的编辑器,该类是一个抽象类 + */ +public abstract class BaseNumberEditor extends BaseEditor { + private static final long serialVersionUID = 1L; + + protected UINumberField numberField; + + protected String oldValue = StringKit.EMPTY; + + public BaseNumberEditor() { + this(null, StringKit.EMPTY); + } + + public BaseNumberEditor(T value, String name) { + this.setLayout(new BorderLayout()); + numberField = new UINumberField(); + this.add(numberField, BorderLayout.CENTER); + KeyListener textKeyListener = new KeyAdapter() { + + public void keyReleased(KeyEvent evt) { + int code = evt.getKeyCode(); + + if (code == KeyEvent.VK_ESCAPE) { + numberField.setText(oldValue); + } + if (code == KeyEvent.VK_ENTER) { + fireEditingStopped(); + } else { + fireStateChanged(); + } + } + }; + this.numberField.addKeyListener(textKeyListener); + this.numberField.setHorizontalAlignment(SwingConstants.RIGHT); + this.setValue(value); + this.setName(name); + } + + @Override + public UINumberField getSwingComponent() { + return numberField; + } + + public void setEnabled(boolean enabled) { + this.numberField.setEnabled(enabled); + } + + public void selected() { + this.requestFocus(); + } + + public String getIconName() { + return "type_double"; + } + + public void requestFocus() { + this.numberField.requestFocus(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/NoneEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/NoneEditor.java new file mode 100644 index 0000000..319bcdc --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/NoneEditor.java @@ -0,0 +1,65 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.ui.component.UITextField; +import com.fanruan.api.util.StringKit; + +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 这是一个占位符编辑器,表示不选择值类型 + */ +public class NoneEditor extends BaseEditor { + + private UITextField textField; + private String displayValue; + + public NoneEditor() { + this(null); + } + + public NoneEditor(String displayValue, String name) { + this.setLayout(new BorderLayout()); + this.displayValue = displayValue; + textField = new UITextField(); + this.add(textField, BorderLayout.CENTER); + if (displayValue != null) { + textField.setText(displayValue); + } + textField.setEditable(false); + this.setName(name); + } + + public NoneEditor(String displayValue) { + this(displayValue, StringKit.EMPTY); + } + + @Override + public UITextField getSwingComponent() { + return textField; + } + + @Override + public boolean accept(Object object) { + return false; + } + + @Override + public Object getValue() { + return null; + } + + @Override + public void setValue(Object value) { + if (displayValue != null) { + textField.setText(displayValue); + textField.setEditable(false); + } + } + + public String getIconName() { + return "type_none"; + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TextEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TextEditor.java deleted file mode 100644 index 4a08298..0000000 --- a/src/main/java/com/fanruan/api/design/ui/editor/TextEditor.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.fanruan.api.design.ui.editor; - -/** - * 文本编辑器 - * */ -public class TextEditor extends com.fr.design.editor.editor.TextEditor{ - public TextEditor(){ - - } - - public TextEditor(String value) { - super(value); - } -} diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeBooleanEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeBooleanEditor.java new file mode 100644 index 0000000..ab5e77e --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeBooleanEditor.java @@ -0,0 +1,89 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.design.ui.component.UICheckBox; + +import java.awt.*; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示布尔值的编辑器 + * TODO:待补充文档 + */ +public class TypeBooleanEditor extends BaseEditor { + + private UICheckBox booleanCheckBox; + + public TypeBooleanEditor() { + this(Boolean.TRUE); + } + + public TypeBooleanEditor(boolean b) { + this(Boolean.valueOf(b)); + } + + public TypeBooleanEditor(Boolean value) { + this.setLayout(new BorderLayout()); + booleanCheckBox = new UICheckBox("true"); + this.add(booleanCheckBox, BorderLayout.CENTER); + this.setValue(value); + this.setName(DesignKit.i18nText("Fine-Design_Basic_Parameter_Boolean")); + booleanCheckBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + fireStateChanged(); + } + }); + } + + public UICheckBox getSwingComponent() { + return booleanCheckBox; + } + + @Override + public Boolean getValue() { + return this.booleanCheckBox.isSelected(); + } + + + @Override + public void setValue(Boolean value) { + if (value == null) { + value = true; + } + this.booleanCheckBox.setSelected(value); + } + + + @Override + public void setEnabled(boolean enabled) { + this.booleanCheckBox.setEnabled(enabled); + } + + + @Override + public void requestFocus() { + this.booleanCheckBox.requestFocus(); + } + + + @Override + protected void fireEditingStopped() { + this.setValue(this.booleanCheckBox.isSelected()); + + super.fireEditingStopped(); + } + + public String getIconName() { + return "type_bool"; + } + + @Override + public boolean accept(Object object) { + return object instanceof Boolean; + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeColumnIndexEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeColumnIndexEditor.java new file mode 100644 index 0000000..e3ddde8 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeColumnIndexEditor.java @@ -0,0 +1,114 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.design.work.component.IntComboBox; + +import java.awt.*; +import java.awt.event.ActionListener; +import java.awt.event.ItemListener; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 列序号编辑器:里面是一排连续的整数(0-n) + */ +public class TypeColumnIndexEditor extends BaseEditor { + + IntComboBox valueColumnIndexComboBox; + + public TypeColumnIndexEditor() { + this(0); + } + + /** + * 默认名字是“列序号”,也可以通过第二个构造函数改变 + * + * @param value 边界值 + */ + public TypeColumnIndexEditor(int value) { + this(value, DesignKit.i18nText("Fine-Design_Basic_Datasource_Column_Index")); + } + + public TypeColumnIndexEditor(int value, String name) { + this.setLayout(new BorderLayout(0, 0)); + valueColumnIndexComboBox = new IntComboBox(); + for (int i = 1; i <= value; i++) { + valueColumnIndexComboBox.addItem(i); + } + + if (value > 0) { + valueColumnIndexComboBox.setSelectedInt(1); + } + this.add(valueColumnIndexComboBox, BorderLayout.CENTER); + this.setName(name); + valueColumnIndexComboBox.setBorder(null); + + } + + @Override + public IntComboBox getSwingComponent() { + return valueColumnIndexComboBox; + } + + @Override + public Integer getValue() { + return valueColumnIndexComboBox.getSelectedInt(); + } + + @Override + public void setValue(Object value) { + valueColumnIndexComboBox.setSelectedInt(value == null ? 0 : Integer.parseInt(value.toString())); + } + + public String getIconName() { + return "ds_column_index"; + } + + /** + * object参数是否是Integer + * + * @param object 传进来用于判断的参数 + * @return 返回是否是Index + */ + public boolean accept(Object object) { + return object instanceof Integer; + } + + /** + * 增加一个ItemListener + * + * @param l 用于增加的Listener + */ + public void addItemListener(ItemListener l) { + valueColumnIndexComboBox.addItemListener(l); + } + + /** + * 增加一个ActionListener + * + * @param l 用于增加的Listener + */ + public void addActionListener(ActionListener l) { + valueColumnIndexComboBox.addActionListener(l); + } + + /** + * 重置 + */ + public void reset() { + valueColumnIndexComboBox.setSelectedIndex(-1); + } + + /** + * 清除所有项 + */ + public void clearData() { + valueColumnIndexComboBox.removeAllItems(); + } + + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + valueColumnIndexComboBox.setEnabled(enabled); + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeColumnNameEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeColumnNameEditor.java new file mode 100644 index 0000000..7579187 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeColumnNameEditor.java @@ -0,0 +1,78 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.util.ArrayKit; +import com.fanruan.api.util.StringKit; +import com.fr.design.gui.icombobox.UIComboBoxRenderer; + +import javax.swing.*; +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 列编辑器,里面是数据集的列名 + */ +public class TypeColumnNameEditor extends TypeColumnIndexEditor { + + private String[] columnNames; + + public TypeColumnNameEditor() { + this(ArrayKit.EMPTY_STRING_ARRAY); + } + + public TypeColumnNameEditor(String[] columnNames) { + this(columnNames, DesignKit.i18nText("Fine-Design_Basic_Column_Name")); + } + + public TypeColumnNameEditor(final String[] columnNames, String name) { + super(columnNames.length, name); + this.columnNames = columnNames; + valueColumnIndexComboBox.setRenderer(new UIComboBoxRenderer() { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + + if (value == null) { + this.setText(""); + } else { + this.setText(columnNames[((Integer) value).intValue() - 1]); + } + + return this; + } + }); + } + + @Override + public void setValue(Object value) { + for (int i = 0; i < columnNames.length; i++) { + if (columnNames[i].equalsIgnoreCase(String.valueOf(value))) { + super.setValue(i + 1); + return; + } + } + + super.reset(); + } + + @Override + public boolean accept(Object object) { + return object instanceof String; + } + + public String getColumnName() { + int index = this.getValue() - 1; + return getColumnNameAtIndex(index); + } + + public String getColumnNameAtIndex(int index) { + return index >= 0 && columnNames.length > index ? columnNames[index] : StringKit.EMPTY; + } + + public String getIconName() { + return "ds_column_name"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeDateEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeDateEditor.java new file mode 100644 index 0000000..4279545 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeDateEditor.java @@ -0,0 +1,150 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.ui.component.UIDatePicker; +import com.fanruan.api.log.LogKit; +import com.fanruan.api.util.GeneralKit; + +import java.awt.*; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示日期值的编辑器 + * TODO:待补充文档 + */ +public class TypeDateEditor extends BaseEditor { + + private UIDatePicker uiDatePicker; + + public TypeDateEditor() { + this(null); + } + + public TypeDateEditor(boolean es) { + this(null, es); + } + + public TypeDateEditor(boolean es, String name) { + this(null, es, name); + } + + public TypeDateEditor(Date value) { + this(value, false); + } + + public TypeDateEditor(Date value, boolean format) { + this(value, format, ""); + } + + public TypeDateEditor(Date value, boolean format, String name) { + this.setLayout(new BorderLayout()); + uiDatePicker = new UIDatePicker(); + if (format) { + int dateStyle = (GeneralKit.getLocale() == Locale.ENGLISH + || GeneralKit.getLocale() == Locale.US + || GeneralKit.getLocale() == Locale.UK) ? UIDatePicker.STYLE_EN_DATE : UIDatePicker.STYLE_CN_DATE; + uiDatePicker.setStyle(dateStyle); + uiDatePicker.setEditable(false); + } + uiDatePicker.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + fireStateChanged(); + } + }); + this.uiDatePicker.setFocusTraversalKeysEnabled(false); + this.add(uiDatePicker, BorderLayout.CENTER); + + this.setValue(value); + this.setName(name); + } + + public TypeDateEditor(Date value, boolean format, String name, int dateFormat) { + this.setLayout(new BorderLayout()); + uiDatePicker = new UIDatePicker(dateFormat); + if (format) { + uiDatePicker.setEditable(false); + } + uiDatePicker.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + fireStateChanged(); + } + }); + this.uiDatePicker.setFocusTraversalKeysEnabled(false); + this.add(uiDatePicker, BorderLayout.CENTER); + + this.setValue(value); + this.setName(name); + } + + @Override + public UIDatePicker getSwingComponent() { + return uiDatePicker; + } + + @Override + public boolean accept(Object object) { + return object instanceof Date; + } + + @Override + public Date getValue() { + try { + return this.uiDatePicker.getSelectedDate(); + } catch (ParseException parseException) { + LogKit.error(parseException.getMessage(), parseException); + return new Date(); + } + } + + @Override + public void setValue(Date value) { + if (value == null) { + return; + } + + try { + this.uiDatePicker.setSelectedDate(value); + } catch (ParseException parseException) { + LogKit.error(parseException.getMessage(), parseException); + } + } + + @Override + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + + this.uiDatePicker.setEnabled(enabled); + } + + + public void selected() { + this.uiDatePicker.setSelectedItem(new Date()); + } + + @Override + public void requestFocus() { + this.uiDatePicker.requestFocus(); + } + + public String getIconName() { + return "type_date"; + } + + public void setUIDatePickerStyle(int style) { + this.uiDatePicker.setStyle(style); + } + + public SimpleDateFormat getUIDatePickerFormat() { + return this.uiDatePicker.getDateFormat(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeDoubleEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeDoubleEditor.java new file mode 100644 index 0000000..32b0d99 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeDoubleEditor.java @@ -0,0 +1,47 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fr.base.Utils; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示双精度浮点型数值的编辑器 + * TODO:待补充文档 + */ +public class TypeDoubleEditor extends BaseNumberEditor { + + public TypeDoubleEditor() { + this(0.0); + } + + public TypeDoubleEditor(Double value) { + super(value, DesignKit.i18nText("Fine-Design_Basic_Parameter_Double")); + } + + @Override + public boolean accept(Object object) { + return object instanceof Double; + } + + @Override + public Double getValue() { + return this.numberField.getValue(); + } + + @Override + public void setValue(Double value) { + if (value == null) { + value = (double) 0; + } + this.numberField.setInteger(false); + this.numberField.setValue(value); + oldValue = Utils.objectToString(value); + } + + public String getIconName() { + return "type_double"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeFloatEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeFloatEditor.java new file mode 100644 index 0000000..7632fd5 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeFloatEditor.java @@ -0,0 +1,47 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fr.base.Utils; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示单精度浮点型数值的编辑器 + * TODO:待补充文档 + */ +public class TypeFloatEditor extends BaseNumberEditor { + + public TypeFloatEditor() { + this(0.0f); + } + + public TypeFloatEditor(Float value) { + super(value, DesignKit.i18nText("Fine-Design_Basic_Parameter_Float")); + } + + @Override + public boolean accept(Object object) { + return object instanceof Float; + } + + @Override + public Float getValue() { + return (float) this.numberField.getValue(); + } + + @Override + public void setValue(Float value) { + if (value == null) { + value = (float) 0; + } + this.numberField.setInteger(false); + this.numberField.setValue(value.doubleValue()); + oldValue = Utils.objectToString(value); + } + + public String getIconName() { + return "type_double"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeFormulaEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeFormulaEditor.java new file mode 100644 index 0000000..6f7b6ed --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeFormulaEditor.java @@ -0,0 +1,161 @@ +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 { + 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); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeIntegerEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeIntegerEditor.java new file mode 100644 index 0000000..26ebaee --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeIntegerEditor.java @@ -0,0 +1,48 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.util.GeneralKit; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示整数值的编辑器 + * TODO:待补充文档 + */ +public class TypeIntegerEditor extends BaseNumberEditor { + + private static final long serialVersionUID = 1L; + + public TypeIntegerEditor() { + this(0); + } + + public TypeIntegerEditor(Integer value) { + super(value, DesignKit.i18nText("Fine-Design_Basic_Parameter_Integer")); + } + + @Override + public boolean accept(Object object) { + return object instanceof Integer; + } + + @Override + public Integer getValue() { + return (int) this.numberField.getValue(); + } + + @Override + public void setValue(Integer value) { + if (value == null) { + value = 0; + } + this.numberField.setInteger(true); + this.numberField.setValue(value); + oldValue = GeneralKit.objectToString(value); + } + + public String getIconName() { + return "type_int"; + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeLongEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeLongEditor.java new file mode 100644 index 0000000..04b9773 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeLongEditor.java @@ -0,0 +1,43 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.util.GeneralKit; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示长整型数值的编辑器 + * TODO:待补充文档 + */ +public class TypeLongEditor extends BaseNumberEditor { + + public TypeLongEditor() { + super(); + } + + public TypeLongEditor(Long value, String name) { + super(value, name); + } + + @Override + public boolean accept(Object object) { + return object instanceof Long; + } + + @Override + public Long getValue() { + return (long) (int) this.numberField.getValue(); + } + + @Override + public void setValue(Long value) { + if (value == null) { + value = 0L; + } + this.numberField.setInteger(true); + this.numberField.setValue(value.intValue()); + oldValue = GeneralKit.objectToString(value); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeParameterEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeParameterEditor.java new file mode 100644 index 0000000..3d766a3 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeParameterEditor.java @@ -0,0 +1,63 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.design.work.component.ParameterComboBox; +import com.fr.design.editor.editor.Editor; +import com.fr.stable.ParameterProvider; + +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于编辑和展示参数的编辑器 + * TODO:待补充文档 + */ +public class TypeParameterEditor extends Editor { + + private ParameterComboBox parameterCombobox; + + public TypeParameterEditor() { + this(null); + } + + public TypeParameterEditor(ParameterProvider parameter) { + parameterCombobox = new ParameterComboBox(); + parameterCombobox.setEditable(true); + this.setLayout(new BorderLayout()); + this.add(parameterCombobox, BorderLayout.CENTER); + this.setValue(parameter); + this.setName(DesignKit.i18nText("Fine-Design_Basic_Parameter")); + } + + @Override + public boolean accept(Object object) { + return object instanceof ParameterProvider; + } + + @Override + public ParameterProvider getValue() { + return parameterCombobox.getSelectedItem(); + } + + @Override + public void setValue(ParameterProvider value) { + parameterCombobox.setSelectedParameter(value); + } + + @Override + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + parameterCombobox.setEnabled(enabled); + } + + @Override + public void requestFocus() { + parameterCombobox.requestFocus(); + } + + public String getIconName() { + return "parameter"; + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/ui/editor/TypeTextEditor.java b/src/main/java/com/fanruan/api/design/ui/editor/TypeTextEditor.java new file mode 100644 index 0000000..46f63b8 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/editor/TypeTextEditor.java @@ -0,0 +1,146 @@ +package com.fanruan.api.design.ui.editor; + +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.design.ui.component.UITextField; +import com.fanruan.api.util.StringKit; + +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-27 + * 用于编辑和展示文本的编辑器 + * com.fr.design.editor.editor.Editor是一个可靠的API类 + */ +public class TypeTextEditor extends BaseEditor { + + private UITextField textField; + + private String oldValue = StringKit.EMPTY; + + /** + * 创建一个带空值的文本编辑器 + */ + public TypeTextEditor() { + this(null); + } + + /** + * 创建一个带指定文本值的编辑器 + * + * @param value 值 + */ + public TypeTextEditor(String value) { + this.setLayout(new BorderLayout()); + textField = new UITextField(); + textField.setBorder(null); + this.add(textField, BorderLayout.CENTER); + KeyListener textKeyListener = new KeyAdapter() { + + @Override + public void keyReleased(KeyEvent evt) { + int code = evt.getKeyCode(); + if (code == KeyEvent.VK_ESCAPE) { + textField.setText(oldValue); + } + if (code == KeyEvent.VK_ENTER) { + fireEditingStopped(); + } else { + fireStateChanged(); + } + } + }; + this.textField.addKeyListener(textKeyListener); + + this.setValue(value); + this.setName(DesignKit.i18nText("Fine-Design_Basic_Parameter_String")); + } + + /** + * 获取用于编辑和展示文本的源控件,是一个swing控件 + * + * @return swing控件 + */ + public final UITextField getSwingComponent() { + return this.textField; + } + + /** + * 判断编辑器是否接受指定类型的值 + * + * @param object 待接收的值 + * @return 文本编辑器只接收文本类型的值,如果该值为文本类型,则返回true,否则返回false + */ + @Override + public boolean accept(Object object) { + return object instanceof String; + } + + /** + * 获取编辑器中的文本内容 + * + * @return 编辑器的值 + */ + @Override + public final String getValue() { + return this.textField.getText(); + } + + /** + * 设置编辑器的文本内容 + * + * @param value 待设置的文本内容 + */ + @Override + public final void setValue(String value) { + if (value == null) { + value = StringKit.EMPTY; + } + + oldValue = value; + this.textField.setText(oldValue); + } + + /** + * 设置编辑器的可用性 + * + * @param enabled true表示可以使用,false表示被禁用(无法输入) + */ + @Override + public final void setEnabled(boolean enabled) { + super.setEnabled(enabled); + this.textField.setEnabled(enabled); + } + + @Override + public final void requestFocus() { + this.textField.requestFocus(); + } + + /** + * 被选中时文本输入框请求焦点 + */ + @Override + public void selected() { + this.textField.requestFocus(); + } + + @Override + public String getIconName() { + return "type_string"; + } + + /** + * 清楚编辑器中的所有值 + */ + @Override + public void clearData() { + super.clearData(); + this.textField.setText(StringKit.EMPTY); + this.oldValue = StringKit.EMPTY; + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/work/component/IntComboBox.java b/src/main/java/com/fanruan/api/design/work/component/IntComboBox.java new file mode 100644 index 0000000..b8c6274 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/work/component/IntComboBox.java @@ -0,0 +1,57 @@ +package com.fanruan.api.design.work.component; + +import com.fanruan.api.design.ui.component.UIComboBox; +import com.fr.design.gui.icombobox.UIComboBoxRenderer; + +import javax.swing.*; +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 用于选择和展示整数的下拉框 + */ +public class IntComboBox extends UIComboBox { + + public IntComboBox() { + init(); + } + + public IntComboBox(Integer[] data) { + ComboBoxModel model = new DefaultComboBoxModel<>(data); + setModel(model); + init(); + } + + private void init() { + this.setRenderer(new UIComboBoxRenderer() { + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + if (value instanceof Integer) { + this.setText(" " + value + " "); + } + return this; + } + }); + } + + public int getSelectedInt() { + if (this.getSelectedItem() == null) { + return -1; + } + return (Integer) this.getSelectedItem(); + } + + public void setSelectedInt(int selectedInt) { + int index = -1; + for (int i = 0; i < this.getItemCount(); i++) { + if ((Integer) this.getItemAt(i) == selectedInt) { + index = i; + break; + } + } + this.setSelectedIndex(index); + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/work/component/ParameterComboBox.java b/src/main/java/com/fanruan/api/design/work/component/ParameterComboBox.java new file mode 100644 index 0000000..addf257 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/work/component/ParameterComboBox.java @@ -0,0 +1,84 @@ +package com.fanruan.api.design.work.component; + +import com.fanruan.api.cal.ParameterKit; +import com.fanruan.api.design.ui.component.UIComboBox; +import com.fanruan.api.util.AssistKit; +import com.fr.design.DesignModelAdapter; +import com.fr.stable.ParameterProvider; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/1 + * 参数选择下拉框 + */ +public class ParameterComboBox extends UIComboBox { + + public ParameterComboBox() { + this.initComponents(); + } + + private void initComponents() { + this.setRenderer(new DefaultListCellRenderer() { + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + + if (value instanceof ParameterProvider) { + this.setText(value.toString()); + } + + return this; + } + }); + updateParaItems(); + } + + public void updateParaItems() { + + DesignModelAdapter designModel = DesignModelAdapter.getCurrentModelAdapter(); + + this.removeAllItems(); + if (designModel != null) { + ParameterProvider[] paras = designModel.getParameters(); + List paraNameList = new ArrayList(); + for (int i = 0; i < paras.length; i++) { + String paraName = paras[i].getName(); + if (!paraNameList.contains(paraName)) { + paraNameList.add(paraName); + this.addItem(paras[i]); + } + + } + } + } + + @Override + public ParameterProvider getSelectedItem() { + if (super.getSelectedItem() instanceof ParameterProvider) { + return (ParameterProvider) super.getSelectedItem(); + } else if (super.getSelectedItem() instanceof String && ((String) super.getSelectedItem()).startsWith("$")) { + return ParameterKit.newParameter(((String) super.getSelectedItem()).substring(1)); + } + return ParameterKit.newParameter(); + } + + public void setSelectedParameter(ParameterProvider parameter) { + if (parameter == null) { + return; + } + for (int i = 0; i < this.getItemCount(); i++) { + if (AssistKit.equals(((ParameterProvider) this.getItemAt(i)).getName(), parameter.getName())) { + this.setSelectedIndex(i); + return; + } + } + this.addItem(parameter); + this.setSelectedItem(parameter); + } +} \ No newline at end of file