⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datecombobox.java

📁 更方便的SWING
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * PSwing Utilities -- Nifty Swing Widgets
 * Copyright (C) 2002  Pallas Technology
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Pallas Technology
 * 1170 HOWELL MILL RD NW
 * SUITE 306
 * ATLANTA GEORGIA 30318
 * 
 * PHONE 404.983.0623
 * EMAIL info@pallastechnology.com
 * 
 * www.pallastechnology.com
 **************************************************************************
 * $Archive: SwingTools$
 * $FileName: DateComboBox.java$
 * $FileID: 8$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 2/27/03 6:37 PM$
 * $VerID: 80$
 * $Comment: Strip off the time component of date. $
 **************************************************************************/
//////////////////////////////////////////////////////////////
// DateComboBox.java
//////////////////////////////////////////////////////////////

package com.pallas.swing.date;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;

//////////////////////////////////////////////////////////////

/**
 * Title:   $FileName: DateComboBox.java$
 * @version $VerNum: 10$
 * @author $AuthorName: Rob MacGrogan$<br><br>
 * 
 * $Description: A date control that pops up a calendar.$<br>
 * $KeyWordsOff: $<br><br>
 * 
 * 
 * A date control that pops up a calendar. Derived from code posted at
 * http://softwaredev.earthweb.com/java/article/0,,12082_735291,00.html by
 * Paul Book.<br><br>
 * 
 * Works under java 1.3 and 1.4.1. A bug in 1.4.0 causes very odd behavior
 * under Windows LAF, but should work fine under other LAFs.
 */
public class DateComboBox extends JComboBox {

  //Used only for display.
  protected SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy");

  //Used for formatting what user types.
  private ArrayList formats = new ArrayList();

  /**
   * Defaults to null. And MMM d, yyyy pattern.
   */
  public DateComboBox() {
    super();
    initializeComponent();
  }

  /**
   * Sets initial date of combo box. Defaults pattern to MMM d, yyyy.
   */
  public DateComboBox(java.util.Date date) {
    super();
    initializeComponent();
    setDate(date);
  }

  public DateComboBox(java.util.Date date, String pattern) {
    super();
    initializeComponent();
    setDateFormat(pattern);
    setDate(date);
  }

  public DateComboBox(String pattern) {
    super();
    initializeComponent();
    setDateFormat(pattern);
  }

  public void setName(String s) {
    super.setName(s);
    getEditor().getEditorComponent().setName(s + "-- editor");
  }

  DateFormat getFormat() {
    return dateFormat;
  }

  public void addFormat(DateFormat format){
    formats.add(format);
  }

  public void addFormat(String format){
    formats.add(new SimpleDateFormat(format));
  }

  /**
   * Initializes the DateComboBox.
   */
  private void initializeComponent() {
    super.setEditable(true);

    JTextField field = (JTextField) getEditor().getEditorComponent();
    field.addFocusListener(new DateFocusListener(this));
    //setCalendarIcon();
  }
  
  /**
   * Attempts to parse sDate to a date using all available date formats.
   */
  java.util.Date parseDateString(String sDate)
          throws ParseException{
    java.util.Date value = null;
    //Now try all remaining formats.
    Iterator itr = formats.iterator();
    while(itr.hasNext()){
      DateFormat format = (DateFormat)itr.next();
      try{
        value = format.parse(sDate);
        //if we get here we got a value, so break.
        break;
      }
      catch (ParseException ex2){
        //just don't break.
      }
    }
    if (value == null){
      throw new ParseException("Can't format string " + sDate + " into a date.", 0);
    }
    return value;
  }

  private void setCalendarIcon() {
    Component[] components = getComponents();
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof JButton) {
        JButton btn = (JButton) components[i];
        ActionListener[] listeners = btn.getActionListeners();
        JButton calButton = new JButton(getButtonIcon());
        for (int j = 0; j < listeners.length; j++) {
          calButton.addActionListener(listeners[i]);
        }
        remove(btn);
        add(calButton);
        //btn.setIcon(getButtonIcon());
        break;
      }
    }

  }

  private Icon getButtonIcon() {
    java.net.URL imageURL = DateComboBox.class.getResource("calendar.gif");
    Image img = Toolkit.getDefaultToolkit().getImage(imageURL);
    Icon ic = new ImageIcon(img);
    return ic;
  }

  /**
   * This method does nothing. DateComboBox is always editable.
   */
  public void setEditable(boolean editable) {
    //Do nothing.
  }

  public void setDateFormat(SimpleDateFormat dateFormat) {
    this.dateFormat = dateFormat;
  }

  public void setDateFormat(String pattern) {
    dateFormat = new SimpleDateFormat(pattern);
  }

  /**
   * Returns a new Date with the same calendar date as the Date passed in, 
   * but with the time changed to 12:00 AM.
   */
  public static java.util.Date stripTime(java.util.Date dt){
    java.util.Date dtStripped = null;
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    
    Calendar calStripped = Calendar.getInstance();
    calStripped.clear();
    calStripped.set(year, month, day);
    
    return calStripped.getTime(); 
  }


  public void setDate(java.util.Date date) {
    if (date != null) {
      date = stripTime(date);
      CBDate cbDate = new CBDate();
      cbDate.setDate(date);
      cbDate.setFormat(dateFormat.toPattern());
      setSelectedItem(cbDate);
    }
    else {
      setSelectedItem(null);
    }
  }

  public java.util.Date getDate() {
    java.util.Date date = null;
    try {
      Object o = getSelectedItem();
      if (o instanceof CBDate) {
        CBDate cbDate = (CBDate) o;
        date = cbDate.getDate();
      }
      else {
        throw new java.text.ParseException("The value of the DateComboBox is not a Date.", 0);
      }
    }
    catch (ParseException ex) {
      date = null;
    }
    return date;
  }

  public void setSelectedItem(Object item) {
    // Could put extra logic here or in renderer when item is instanceof Date, Calendar, or String
    // Dont keep a list ... just the currently selected item
    removeAllItems(); // hides the popup if visible
    addItem(item);
    super.setSelectedItem(item);
  }

  public void updateUI() {
    ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
    if (cui instanceof MetalComboBoxUI) {
      cui = new MetalDateComboBoxUI();
    }
    else if (cui instanceof MotifComboBoxUI) {
      cui = new MotifDateComboBoxUI();
    }
    else if (cui instanceof WindowsComboBoxUI) {
      cui = new WindowsDateComboBoxUI();
    }
    setUI(cui);
    //super.updateUI();
    initializeComponent();
  }

  // Inner classes are used purely to keep DateComboBox component in one file
  //////////////////////////////////////////////////////////////
  // UI Inner classes -- one for each supported Look and Feel
  //////////////////////////////////////////////////////////////

  class MetalDateComboBoxUI extends MetalComboBoxUI {
    protected ComboPopup createPopup() {
      return new DatePopup((DateComboBox) comboBox);
    }
  }

  class WindowsDateComboBoxUI extends WindowsComboBoxUI {
    protected ComboPopup createPopup() {
      return new DatePopup((DateComboBox) comboBox);
    }
  }

  class MotifDateComboBoxUI extends MotifComboBoxUI {
    protected ComboPopup createPopup() {
      return new DatePopup((DateComboBox) comboBox);
    }
  }

  class CBDate {
    private java.util.Date date = null;
    private SimpleDateFormat format = null;

    public java.util.Date getDate() {
      return date;
    }
    public void setDate(java.util.Date dt) {
      date = dt;
    }
    public void setFormat(String s) {
      format = new SimpleDateFormat(s);
    }

    public String toString() {
      return format.format(date);
    }
  }

  //////////////////////////////////////////////////////////////
  // DatePopup inner class
  //////////////////////////////////////////////////////////////

  class DatePopup implements ComboPopup, MouseMotionListener, MouseListener, KeyListener, PopupMenuListener {

    protected DateComboBox comboBox;
    protected Calendar calendar;
    protected JPopupMenu popup;
    protected JLabel monthLabel;
    protected JPanel days = null;
    protected SimpleDateFormat monthFormat = new SimpleDateFormat("MMM yyyy");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -