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

276 lines
8.5 KiB

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;
}
}