package com.vci.client.ui.swing.components; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.EventListener; import com.vci.client.ui.date.PopupCalendarPanel; import com.vci.client.ui.swing.VCISwingUtil; /** * *

* Title: *

*

* Description: *

*

* Copyright: Copyright (c) 2012 *

*

* Company: VCI *

* * @author xchao * @time 2012-5-15 * @version 1.0 */ public class VCIJCalendarPanel extends VCIJPanel implements ActionListener, EventListener { /** * */ private static final long serialVersionUID = 2746179740066575026L; /** * 是否是必须的 */ private boolean required = false; private boolean hasDate = true; /** * 是否包含时间 */ private boolean hasTime = false; /** * 自定义日期格式字符串 */ private String format = ""; private SimpleDateFormat sf = null; public String itemStyle = null;// add by caill 2016.2.19 增加用来传递附加属性内容的变量 private VCIJComboBox cbxDate = new VCIJComboBox(); private VCIJButton btnShowCalendar = VCISwingUtil.createVCIJButton("ShowDateCalendar", "", "", "date.png", this); /** * 构造日期&时间选择对象 */ public VCIJCalendarPanel() { this(false); } /** * 构造日期&时间选择对象 * * @param giveDefaultValue 是否自动填写日期&时间 */ public VCIJCalendarPanel(boolean giveDefaultValue) { this(giveDefaultValue ? "yyyy-MM-dd" : "", false, giveDefaultValue); } /** * 构造日期&时间选择对象 * * @param format 日期格式化字符串 * @param required 是否是必须的 * @param giveDefaultValue 是否自动填写日期&时间 */ public VCIJCalendarPanel(String format, boolean required, boolean giveDefaultValue) { this(format, required, giveDefaultValue, false); } /** * 构造日期&时间选择对象 * * @param format 日期格式化字符串 * @param required 是否是必须的 * @param giveDefaultValue 是否自动填写日期&时间 * @param hasTime 是否包含时间 */ @Deprecated public VCIJCalendarPanel(String format, boolean required, boolean giveDefaultValue, boolean hasTime) { this(required, giveDefaultValue, true, hasTime, format); } /** * 构造日期&时间选择对象 * * @param format 日期格式化字符串 * @param required 是否是必须的 * @param giveDefaultValue 是否自动填写日期&时间 * @param hasTime 是否包含时间 */ public VCIJCalendarPanel(boolean required, boolean giveDefaultValue, boolean hasDate, boolean hasTime) { this(required, giveDefaultValue, hasDate, hasTime, ""); } public VCIJCalendarPanel(boolean required, boolean giveDefaultValue, boolean hasDate, boolean hasTime, String specFormat) { super(); String format = ""; if (hasDate && hasTime) { format = "yyyy-MM-dd HH:mm:ss"; } else if (hasDate) { format = "yyyy-MM-dd"; } else if (hasTime) { format = "HH:mm:ss"; } if (specFormat != null && !"".equals(specFormat)) { format = specFormat; } setFormat(format); setRequired(required); setHasDate(hasDate); setHasTime(hasTime); if (required) { giveDefaultValue = true; } init(); if (giveDefaultValue && isValidDateFormat(getFormat())) { SimpleDateFormat sdf = new SimpleDateFormat(getFormat()); String dateString = sdf.format(Calendar.getInstance().getTime()); setDateString(dateString); } cbxDate.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue()); } }); } private void init() { initConmponents(); } private void initConmponents() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1; c.weightx = 1; c.weighty = 0; c.anchor = GridBagConstraints.NORTHWEST; c.fill = GridBagConstraints.BOTH; // cbxDate.setPreferredSize(new Dimension(100, 23)); add(cbxDate, c); c.gridx = 1; c.gridheight = 1; c.gridwidth = 1; c.weightx = 0; c.weighty = 0; c.anchor = GridBagConstraints.EAST; c.fill = GridBagConstraints.BOTH; btnShowCalendar.setMinimumSize(new Dimension(27, 23)); btnShowCalendar.setMaximumSize(new Dimension(27, 23)); btnShowCalendar.setPreferredSize(new Dimension(27, 23)); add(btnShowCalendar, c); btnShowCalendar.setToolTipText(getToolTipString()); c.gridx = 2; c.gridheight = 1; c.gridwidth = 1; c.weightx = 0; c.weighty = 0; c.anchor = GridBagConstraints.EAST; c.fill = GridBagConstraints.BOTH; VCIJLabel lbl = new VCIJLabel("");// 为显示 lbl.setMinimumSize(new Dimension(9, 23)); lbl.setPreferredSize(new Dimension(9, 23)); add(lbl, c); } private String getToolTipString() { return isHasTime() ? "点击选择日期及时间" : "点击选择日期"; } /** * 返回自定义日期格式字符串 *

* Description: *

* * @author xchao * @time 2012-11-21 * @return */ public String getFormat() { return format; } /** * 设置 自定义日期格式字符串 *

* Description: *

* * @author xchao * @time 2012-11-21 * @param format */ public void setFormat(String format) { this.format = format; this.sf = new SimpleDateFormat(this.format); } /** * 返回是否是必须的 *

* Description: *

* * @author xchao * @time 2012-11-21 * @return */ public boolean isRequired() { return required; } /** * 设置是否是必须的 *

* Description: *

* * @author xchao * @time 2012-11-21 * @param required */ public void setRequired(boolean required) { this.required = required; } public boolean isHasDate() { return hasDate; } public void setHasDate(boolean hasDate) { this.hasDate = hasDate; } /** * 返回是否包含时间 *

* Description: *

* * @author xchao * @time 2012-11-21 * @return */ public boolean isHasTime() { return hasTime; } /** * 设置是否包含时间 *

* Description: *

* * @author xchao * @time 2012-11-21 * @param hasTime */ public void setHasTime(boolean hasTime) { this.hasTime = hasTime; } /** * 返回日期&时间字符串 *

* Description: *

* * @author xchao * @time 2012-11-21 * @return */ public String getDateString() { String res = null; Object item = cbxDate.getSelectedItem(); if (item != null) { res = item.toString().trim(); } if ("".equals(res)) { res = null; } return res; } public Date getDate() { String res = null; Object item = cbxDate.getSelectedItem(); if (item != null) { res = item.toString().trim(); } if ("".equals(res)) { res = null; } try { Date dt = sf.parse(res); return dt; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } // add by caill start 2016.2.19 添加方法用来传递附加条件内容 public void setItemStyleString(String itemStyleString) { itemStyle = itemStyleString; } // add by caill end public void setDate(Date date){ cbxDate.removeAllItems(); if(!isRequired()){ cbxDate.addItem(" "); } String itemobj = sf.format(date); cbxDate.addItem(itemobj); cbxDate.setSelectedItem(itemobj); } /** * 设置日期格式字符串的值 *

* Description: *

* * @author xchao * @time 2012-11-21 * @param dateString */ public void setDateString(String dateString) { if (!isDateFormatString(dateString)) return; String itemobj = dateString; cbxDate.removeAllItems(); if (!isRequired()) { cbxDate.addItem(" "); } cbxDate.addItem(itemobj); cbxDate.setSelectedItem(itemobj); } private boolean isDateFormatString(String dateString) { boolean res = true; if (getFormat() != null) { if (isValidDateFormat(getFormat())) { SimpleDateFormat sdf = new SimpleDateFormat(getFormat()); try { sdf.parse(dateString); } catch (ParseException e) { res = false; } } else { res = false; } } return res; } private boolean isValidDateFormat(String format) { boolean res = true; try { new SimpleDateFormat(getFormat()); } catch (Exception e) { res = false; } return res; } @Override public void paint(Graphics g) { super.paint(g); if (isRequired()) { drawRequiredFlag(g); } } private void drawRequiredFlag(Graphics g) { int x = getWidth() - 9; int y = 1; g.setColor(Color.RED); g.fillArc(x, y, 7, 7, 0, 360); // int x = getWidth() - 10; // int y = 10; // g.setColor(Color.RED); // g.drawString("*", x, y); } @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); cbxDate.setEnabled(enabled); if (enabled) { cbxDate.setBackground(Color.WHITE); } btnShowCalendar.setEnabled(enabled); } public void popupCaneldar() { btnShowCalendar.doClick(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("ShowDateCalendar")) { new PopupCalendarPanel(cbxDate, isHasDate(), isHasTime(), isRequired(), itemStyle).show();// add by caill // 2016.2.19增加了ietemStyle参数用来传递附加属性的内容 } } /** * 将日期清空 *

* Description: *

* * @author ligang * @time 2013-3-5 * @param dateString */ public void setDateString() { cbxDate.removeAllItems(); cbxDate.addItem(" "); } public static final String DATE_TIME_VALUE = "DateTimeValue"; }