package gui;
/**
* ClassName:DateChooser
* Copyright: copyright (c)2012
* @author Deng Zhiguang
* http://zgdeng.iteye.com
* @Date 2012-2-7
* Modification History:
* Date Author Version Reason
* ------------------------------------------
*/
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
/**
* 日期选择器,可以指定日期的显示格式
*/
public class DateChooser extends JPanel {
private static final long serialVersionUID = 4529266044762990227L;
private Date initDate;
private Calendar now = Calendar.getInstance();
private Calendar select;
private JPanel monthPanel;//月历
private JP1 jp1;//四块面板,组成
private JP2 jp2;
private JP3 jp3;
private JP4 jp4;
private Font font = new Font("宋体", Font.PLAIN, 12);
private final LabelManager lm = new LabelManager();
private SimpleDateFormat sdf;
private boolean isShow = false;
private Popup pop;
private JComponent showDate;
public static DateChooser getInstance() {
return new DateChooser();
}
public static DateChooser getInstance(Date date) {
return new DateChooser(date);
}
public static DateChooser getInstance(String format) {
return new DateChooser(format);
}
public static DateChooser getInstance(Date date, String format) {
return new DateChooser(date, format);
}
/**
* Creates a new instance of DateChooser
*/
private DateChooser() {
this(new Date());
}
private DateChooser(Date date) {
this(date, "yyyy年MM月dd日");
}
private DateChooser(String format) {
this(new Date(), format);
}
private DateChooser(Date date, String format) {
initDate = date;
sdf = new SimpleDateFormat(format);
select = Calendar.getInstance();
select.setTime(initDate);
initPanel();
}
/**
* 是否允许用户选择
*/
public void setEnabled(boolean b) {
super.setEnabled(b);
showDate.setEnabled(b);
}
/**
*得到当前选择框的日期
*/
public Date getDate() {
return select.getTime();
}
public String getStrDate() {
return sdf.format(select.getTime());
}
public String getStrDate(String format) {
sdf = new SimpleDateFormat(format);
return sdf.format(select.getTime());
}
//根据初始化的日期,初始化面板
private void initPanel() {
monthPanel = new JPanel(new BorderLayout());
monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JPanel up = new JPanel(new BorderLayout());
up.add(jp1 = new JP1(), BorderLayout.NORTH);
up.add(jp2 = new JP2(), BorderLayout.CENTER);
monthPanel.add(jp3 = new JP3(), BorderLayout.CENTER);
monthPanel.add(up, BorderLayout.NORTH);
monthPanel.add(jp4 = new JP4(), BorderLayout.SOUTH);
this.addAncestorListener(new AncestorListener() {
public void ancestorAdded(AncestorEvent event) {
}
public void ancestorRemoved(AncestorEvent event) {
}
//只要祖先组件一移动,马上就让popup消失
public void ancestorMoved(AncestorEvent event) {
hidePanel();
}
});
}
public void register(final JComponent showDate) {
this.showDate = showDate;
showDate.setRequestFocusEnabled(true);
showDate.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
showDate.requestFocusInWindow();
}
});
this.setBackground(Color.WHITE);
this.add(showDate, BorderLayout.CENTER);
this.setPreferredSize(new Dimension(90, 25));
this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
showDate.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
if (showDate.isEnabled()) {
showDate.setCursor(new Cursor(Cursor.HAND_CURSOR));
showDate.setForeground(Color.RED);
}
}
public void mouseExited(MouseEvent me) {
if (showDate.isEnabled()) {
showDate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
showDate.setForeground(Color.BLACK);
}
}
public void mousePressed(MouseEvent me) {
if (showDate.isEnabled()) {
showDate.setForeground(Color.CYAN);
if (isShow) {
hidePanel();
} else {
showPanel(showDate);
}
}
}
public void mouseReleased(MouseEvent me) {
if (showDate.isEnabled()) {
showDate.setForeground(Color.BLACK);
}
}
});
showDate.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
hidePanel();
}
public void focusGained(FocusEvent e) {
}
});
}
//根据新的日期刷新
private void refresh() {
jp1.updateDate();
jp2.updateDate();
jp3.updateDate();
jp4.updateDate();
SwingUtilities.updateComponentTreeUI(this);
}
//提交日期
private void commit() {
//TODO add other components here
if (showDate instanceof JTextField) {
((JTextField) showDate).setText(sdf.format(select.getTime()));
}else if (showDate instanceof JLabel) {
((JLabel) showDate).setText(sdf.format(select.getTime()));
}
hidePanel();
}
//隐藏日期选择面板
private void hidePanel() {
if (pop != null) {
isShow = false;
pop.hide();
pop = null;
}
}
//显示日期选择面板
private void showPanel(Component owner) {
if (pop != null) {
pop.hide();
}
Point show = new Point(0, showDate.getHeight());
SwingUtilities.convertPointToScreen(show, showDate);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
int x = show.x;
int y = show.y;
if (x < 0) {
x = 0;
}
if (x > size.width - 295) {
x = size.width - 295;
}
if (y < size.height - 170) {
} else {
y -= 188;
}
pop = PopupFactory.getSharedInstance().getPopup(owner, monthPanel, x, y);
pop.show();
isShow = true;
}
/**
* 最上面的面板用来显示月份的增减
*/
private class JP1 extends JPanel {
private static final long serialVersionUID = -5638853772805561174L;
JLabel yearleft, yearright, monthleft, monthright, center, centercontainer;
public JP1() {
super(new BorderLayout());
this.setBackground(new Color(160, 185, 215));
initJP1();
}
private void initJP1() {
yearleft = new JLabel(" <<", JLabel.CENTER);
yearleft.setToolTipText("上一年");
yearright = new JLabel(">> ", JLabel.CENTER);
yearright.setToolTipText("下一年");
yearleft.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
yearright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
monthleft = new JLabel(" <", JLabel.RIGHT);
monthleft.setToolTipText("上一月");
monthright = new JLabel("> ", JLabel.LEFT);
monthright.setToolTipText("下一月");
monthleft.setBorder(BorderFactory.createEmptyBorder(2, 30, 0, 0));
monthright.setBorder(BorderFactory.createEmptyBorder(