package com.vci.client.ui.date; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.text.DateFormatSymbols; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JFormattedTextField; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; import javax.swing.UIManager; import javax.swing.border.AbstractBorder; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; import javax.swing.plaf.basic.BasicComboPopup; import org.apache.commons.lang3.StringUtils; import com.vci.client.ui.image.BundleImage; import com.vci.client.ui.swing.components.VCIJComboBox; /** *

Title:

*

Description: DatePopup 选择框弹出的日期选择面板

*

Copyright: Copyright (c) 2009

*

Company: VCI

* @author eddie * @time 2009-5-6 * @version 1.0 */ public class PopupCalendarPanel extends BasicComboPopup { /** * */ private static final long serialVersionUID = 4194644973069213593L; protected Calendar calendar; protected JLabel monthLabel; protected JPanel days = null; protected Color selectedBackground; protected Color selectedForeground; protected Color background; protected Color foreground; protected VCIJComboBox jcombobox = new VCIJComboBox(); ImageIcon todayIcon; /** * 年月日格式 */ public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); /** * 年月格式 */ public SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM"); /** * 日格式 */ public SimpleDateFormat dayFormat = new SimpleDateFormat("d"); public String itemStyleValue = null; private JSpinner spinnerHour = null; private JSpinner spinnerMinute = null; private JSpinner spinnerSecond = null; private boolean hasDate = true; private boolean hasTime = false; private boolean required = false; private String format = ""; private SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); private SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss"); public PopupCalendarPanel(VCIJComboBox box){ this(box, true, false, false,"");//add by caill 增加了参数"" } private SimpleDateFormat getSimpleDateFormatByInputValue(String inputValue) { SimpleDateFormat sdf = null; try { sdfDateTime.parse(inputValue); sdf = sdfDateTime; } catch (ParseException e) { try { sdfDate.parse(inputValue); sdf = sdfDate; } catch (ParseException e2) { try { sdfTime.parse(inputValue); sdf = sdfTime; } catch (ParseException e3) { } } } if(sdf == null){ sdf = sdfDateTime; } return sdf; } public PopupCalendarPanel(VCIJComboBox box, boolean hasDate, boolean hasTime, boolean required, String itemStyle) {//add by caill 2016.2.19增加itemStyle参数用来传递附加属性的内容 super(box); setHasDate(hasDate); setHasTime(hasTime); setRequired(required); jcombobox = box; todayIcon = new BundleImage().createImageIcon("today.gif"); calendar = Calendar.getInstance(); //add by caill start 2016.2.19 如果日期输入框中当前有值,就将此值赋给calendar;如果输入框中没有值,calendar就取当前时间为值 if(itemStyle != null && !itemStyle.trim().equals("")){ String[] str = itemStyle.split(";"); for(int i = 0;i < str.length;i++){ if(str[i].trim().equals("minseczeroFlag=1")){ //判断附加属性中是否存在minseczeroFlag=1的字符串,如果有,则把它赋给itemStyleValue itemStyleValue = str[i].trim(); break; } } } if(jcombobox.getSelectedItem() != null){ String preValue = jcombobox.getSelectedItem().toString(); if (StringUtils.isBlank(preValue)) { calendar.setTime(new Date()); } else { SimpleDateFormat formater = getSimpleDateFormatByInputValue(preValue); try { // monthLabel.setText(monthFormat.format(formater.parse(preValue))); calendar.setTime(formater.parse(preValue)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //add by caill end background = UIManager.getColor("ComboBox.background"); foreground = UIManager.getColor("ComboBox.foreground"); selectedBackground = UIManager.getColor( "ComboBox.selectionBackground"); selectedForeground = UIManager.getColor( "ComboBox.selectionForeground"); spinnerHour = getJSpinner("HH", 0, 23, 1); spinnerMinute = getJSpinner("mm", 0, 59, 1); spinnerSecond = getJSpinner("ss", 0, 59, 1); initializePopup(); } /** * 初始化弹出面板 * @param string * @param string */ protected void initializePopup() { //<< < 9999/99/99 > >> //add by caill start 2016.2.19 如果日期输入框中当前有值,就将此值赋给calendar;如果输入框中没有值,calendar就取当前时间为值 if(jcombobox.getSelectedItem() != null){ String preValue = jcombobox.getSelectedItem().toString(); if (StringUtils.isBlank(preValue)) { calendar.setTime(new Date()); } else { SimpleDateFormat formater = getSimpleDateFormatByInputValue(preValue); try { // monthLabel.setText(monthFormat.format(formater.parse(preValue))); calendar.setTime(formater.parse(preValue)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //add by caill end JPanel header = new JPanel(); header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS)); header.setBackground(new Color(0, 0, 128)); header.setForeground(Color.white); header.setPreferredSize(new Dimension(1, 25)); JLabel label; label = createUpdateButton(Calendar.YEAR, -1); label.setText("<<"); header.add(Box.createHorizontalStrut(12)); header.add(label); header.add(Box.createHorizontalStrut(12)); label = createUpdateButton(Calendar.MONTH, -1); label.setText("< "); header.add(label); monthLabel = new JLabel("", JLabel.CENTER); monthLabel.setBackground(new Color(0, 0, 128)); monthLabel.setForeground(Color.white); header.add(Box.createHorizontalGlue()); header.add(monthLabel); header.add(Box.createHorizontalGlue()); label = createUpdateButton(Calendar.MONTH, 1); label.setText(" >"); header.add(label); label = createUpdateButton(Calendar.YEAR, 1); label.setText(">>"); header.add(Box.createHorizontalStrut(12)); header.add(label); header.add(Box.createHorizontalStrut(12)); //星期日 星期一 星期二 星期三 星期四 星期五 星期六 JPanel pWeeks = new JPanel(new GridLayout(0, 7)); pWeeks.setBackground(background); pWeeks.setOpaque(true); DateFormatSymbols sy = new DateFormatSymbols(Locale.getDefault()); String strWeeks[] = sy.getShortWeekdays(); for (int i = 1; i <= 7; i++) { label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setForeground(foreground); label.setText(strWeeks[i]); pWeeks.add(label); } //中间放日期的面板 days = new JPanel(new GridLayout(0, 7)); days.setBorder(new TopBottomLineBorder(Color.black)); days.setBackground(background); days.setOpaque(true); JPanel pCenter = new JPanel(new BorderLayout()); pCenter.setBackground(background); pCenter.setOpaque(true); pCenter.add(pWeeks, BorderLayout.NORTH); pCenter.add(days, BorderLayout.CENTER); //显示今天的日期,直接单击图标跳到今天 String today = "Today:"; if(isHasTime()){ today = ""; } //add by caill start 2016.2.19 如果文本框内有值,将日期控件下端的日期设置为文本框内的值;如果没有值,则显示当前时间 JLabel lbToday =null; if(jcombobox.getSelectedItem() != null){ String preValue = jcombobox.getSelectedItem().toString(); if (StringUtils.isBlank(preValue)) { lbToday = new JDayLable(new Date(), today + dateFormat.format(new Date())); } else { SimpleDateFormat formater = getSimpleDateFormatByInputValue(preValue); try { lbToday = new JDayLable(formater.parse(preValue), today + dateFormat.format(formater.parse(preValue))); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }else{ lbToday = new JDayLable(new Date(), today + dateFormat.format(new Date())); } //add by caill end // JLabel lbToday = new JDayLable(new Date(), today + dateFormat.format(new Date())); lbToday.setIcon(todayIcon); lbToday.setHorizontalAlignment(JLabel.LEFT); JLabel lbClear = new JDayLable("清空"); lbClear.setHorizontalAlignment(JLabel.CENTER); JLabel lbOk = new JDayLable("确定"); lbClear.setHorizontalAlignment(JLabel.CENTER); JPanel pSouth = new JPanel(new BorderLayout()); pSouth.setBackground(background); pSouth.setForeground(foreground); if(isHasDate()){ pSouth.add(lbToday, BorderLayout.CENTER); } JPanel pal = new JPanel(new FlowLayout(FlowLayout.RIGHT)); if(isHasTime()){ pal.add(spinnerHour); pal.add(spinnerMinute); pal.add(spinnerSecond); } if(!isHasDate() && isHasTime()){ pal.add(lbOk); } if(!isRequired()){ pal.add(lbClear); } pSouth.add(pal, BorderLayout.EAST); setForeground(foreground); setBackground(background); setBorder(BorderFactory.createLineBorder(Color.black)); setLayout(new BorderLayout()); removeAll(); if(isHasDate()){ add(BorderLayout.NORTH, header); add(BorderLayout.CENTER, pCenter); } add(BorderLayout.SOUTH, pSouth); } private JSpinner getJSpinner(String initValueFormat, int minVal, int maxVal, int stepVal){ Date initDate = calendar.getTime(); Date newDate = calendar.getTime(); boolean flag = true; try { // 将 每个时间对象的时、分、秒设置为0 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); String dateTimeString = sdf.format(initDate); initDate = sdf.parse(dateTimeString); } catch (ParseException e) { } calendar.add(Calendar.YEAR, -500); Date earliestDate = calendar.getTime(); calendar.add(Calendar.YEAR, 1000); Date latestDate = calendar.getTime(); calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); //add by caill start 2016.2.19 将日期控件下端的时、分、秒按判断条件回显 SpinnerDateModel model = null; boolean equals = false; if(itemStyleValue != null){ equals = itemStyleValue.trim().equals("minseczeroFlag=1");//add by caill 判断附加属性内容从而设置分秒为0 } //JSpinner spinner = null; if(jcombobox.getSelectedItem() != null && !jcombobox.getSelectedItem().toString().trim().equals("") && !equals){ //如果当前文本框中有值,并且附加属性的内容不为“minseczeroFlag=1”,则回显当前文本框中的值 String preValue = jcombobox.getSelectedItem().toString(); SimpleDateFormat formater = getSimpleDateFormatByInputValue(preValue); try { model = new SpinnerDateModel(formater.parse(preValue), earliestDate, latestDate, Calendar.SECOND); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if(equals && jcombobox.getSelectedItem() != null && !jcombobox.getSelectedItem().toString().trim().equals("")){ //如果当前文本框中有值,并且附加属性的内容为“minseczeroFlag=1”,在回显当前文本框中的值的同时,将分钟和秒设置为0 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); String dateTimeString = sdf.format(newDate); try { newDate = sdf.parse(dateTimeString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } model = new SpinnerDateModel(newDate, earliestDate, latestDate, Calendar.SECOND); flag = false; }else if(equals){ //如果当前文本框中没有值,并且附加属性的内容为“minseczeroFlag=1”,则回显当前时间的值,但将分钟和秒设置为0 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); String dateTimeString = sdf.format(newDate); try { newDate = sdf.parse(dateTimeString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } model = new SpinnerDateModel(newDate, earliestDate, latestDate, Calendar.SECOND); flag = false; }else{ //如果当前文本框中没有值,并且附加属性的内容不为“minseczeroFlag=1”,则回显当前时间的值 model = new SpinnerDateModel(calendar.getTime(), earliestDate, latestDate, Calendar.SECOND); } //add by caill end //SpinnerModel model = new SpinnerDateModel(calendar.getTime(), earliestDate, latestDate, Calendar.SECOND); JSpinner spinner = new JSpinner(model); //add by caill start 2016.2.19 当将分钟和秒设置为0时,上下按钮处于不可编辑状态 if((initValueFormat.equals("mm") || initValueFormat.equals("ss")) && (flag == false)){ spinner.setEditor(new JSpinner.DateEditor(spinner, "00"));//此处将分钟和秒写死为00,从而使按钮处于无效状态,实现不可修改功能 }else{ spinner.setEditor(new JSpinner.DateEditor(spinner, initValueFormat)); } //add by caill end //spinner.setEditor(new JSpinner.DateEditor(spinner, initValueFormat)); ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setEditable(false); // ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setEnabled(false); ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setColumns(0); ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setHorizontalAlignment(JFormattedTextField.REVERT); return spinner; } 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; } public boolean isRequired() { return required; } public void setRequired(boolean required) { this.required = required; } /** * 返回日期格&时间格式字符串 *

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; } /** * 返回时间字符串 *

Description:

* * @author xchao * @time 2012-11-21 * @return */ private String getTimeString(){ String res = String.format("%s:%s:%s", ((JSpinner.DefaultEditor)spinnerHour.getEditor()).getTextField().getText(), ((JSpinner.DefaultEditor)spinnerMinute.getEditor()).getTextField().getText(), ((JSpinner.DefaultEditor) spinnerSecond.getEditor()).getTextField().getText()); return res; } /** * 显示弹出面板 */ public void show() { updatePopup(); super.show(); } public void destroy() { this.setVisible(false); } /** * 创建上一月,下一月,上一年,下一年"按钮" * @param field int * @param amount int * @return JLabel */ protected JLabel createUpdateButton(final int field, final int amount) { final JLabel label = new JLabel(); final Border selectedBorder = new LineBorder(Color.black); final Border unselectedBorder = new EmptyBorder(selectedBorder.getBorderInsets(new JLabel())); label.setBorder(unselectedBorder); label.setBackground(new Color(0, 0, 128)); label.setForeground(Color.white); label.setRequestFocusEnabled(false); label.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { calendar.add(field, amount); updatePopup(); } public void mouseEntered(MouseEvent e) { label.setBorder(selectedBorder); } public void mouseExited(MouseEvent e) { label.setBorder(unselectedBorder); } }); return label; } /** * 更新日期 */ protected void updatePopup() { monthLabel.setText(monthFormat.format(calendar.getTime())); days.removeAll(); Calendar setupCalendar = (Calendar) calendar.clone(); setupCalendar.set(Calendar.DAY_OF_MONTH, 1); int first = setupCalendar.get(Calendar.DAY_OF_WEEK); /** 1 2 3 4 5 6 7 2 3 4 5 6 7 8 9 10 11 12 13 8 9 10 11 12 13 14 15 16 17 18 19 14 15 16 17 18 19 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 31 26 27 28 29 30 31 */ setupCalendar.add(Calendar.DATE, -first); int flag = 0; for (int i = 0; i < 42; i++) { setupCalendar.add(Calendar.DATE, 1); String txt = dayFormat.format(setupCalendar.getTime()); JLabel label = new JDayLable(setupCalendar.getTime(), txt); days.add(label); if (txt.equals("1")) { flag++; } if (flag != 1) { label.setEnabled(false); } } days.repaint(); pack(); } /** *

Title:

*

Description: TopBottomLineBorder只带上下两条线的边界Border

*

Copyright: Copyright (c) 2004

*

Company:

* @author Title: JDatePicker

*

Description:JDayLable 带选取日期功能的JLabel

*

Copyright: Copyright (c) 2004

*

Company:

* @author * @version 1.0 */ class JDayLable extends JLabel implements MouseListener { /** * */ private static final long serialVersionUID = 4031255003084656843L; Date date; Image img; boolean isToday = false; public JDayLable(Date date, String text) { setHorizontalAlignment(JLabel.CENTER); setForeground(foreground); setPreferredSize(new Dimension(40, 20)); setToolTipText(dateFormat.format(date)); addMouseListener(this); this.date = date; setText(text); Date d = new Date(); //add by caill start 2016.2.19 如果日期输入框中当前有值,就将此值赋给d;如果输入框中没有值,d就取当前时间为值 if(jcombobox.getSelectedItem() != null && !jcombobox.getSelectedItem().toString().trim().equals("")){ String preValue = jcombobox.getSelectedItem().toString(); SimpleDateFormat formater = getSimpleDateFormatByInputValue(preValue); try { d=formater.parse(preValue); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } //add by caill end isToday = (text.length() < 3) && dateFormat.format(date).equals(dateFormat.format(d)); if (calendar.getTime().equals(date)) { setBorder(new LineBorder(selectedBackground, 1)); } } public JDayLable(String text) { setHorizontalAlignment(JLabel.CENTER); setForeground(foreground); setPreferredSize(new Dimension(40, 20)); addMouseListener(this); setText(text); } public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) { if (isEnabled()) { setOpaque(false); setBackground(background); setForeground(foreground); } Object itemobj = null; if(isHasDate() && isHasTime()){ calendar.setTime(date); itemobj = dateFormat.format(calendar.getTime()); // add by xhcao 2012.11.21 if(isHasTime()){ itemobj = itemobj.toString() + " " + getTimeString(); } } else if(isHasDate()){ if(date != null){ calendar.setTime(date); itemobj = dateFormat.format(calendar.getTime()); } else { itemobj = ""; } } else if(isHasTime()){ if(isHasTime()){ itemobj = getTimeString(); } } String oldValue = ""; if(jcombobox.getSelectedItem() != null){ oldValue = jcombobox.getSelectedItem().toString(); } String newValue = itemobj.toString(); jcombobox.removeAllItems(); if(!isRequired()){ jcombobox.addItem(""); } jcombobox.addItem(itemobj); jcombobox.setSelectedItem(itemobj); jcombobox.firePropertyChangeV2("valueChange", oldValue, newValue); destroy(); } public void mouseEntered(MouseEvent e) { if (isEnabled()) { setOpaque(true); setBackground(selectedBackground); setForeground(selectedForeground); } } public void mouseExited(MouseEvent e) { if (isEnabled()) { setOpaque(false); setBackground(background); setForeground(foreground); } } public void paint(Graphics g) { super.paint(g); if (isToday && todayIcon != null && isEnabled()) { int x = (this.getWidth() - todayIcon.getIconWidth()) / 2; int y = (this.getHeight() - todayIcon.getIconHeight()) / 2; todayIcon.paintIcon(this, g, x, y); } } } }