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

📄 calendar.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.grid.ed;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.event.*;
import org.compiere.plaf.*;
import org.compiere.swing.*;
import org.compiere.util.*;

/**
 *  Pop up Calendar & Time
 *
 *  @author 	Jorg Janke
 *  @version 	$Id: Calendar.java,v 1.18 2005/12/27 06:18:36 jjanke Exp $
 */
public class Calendar extends CDialog
	implements ActionListener, MouseListener, ChangeListener, KeyListener
{
	/**
	 *  Mimimum Constructor for Date editor
	 *  @param frame frame
	 */
	public Calendar (Frame frame)
	{
		this (frame, Msg.getMsg(Env.getCtx(), "Calendar"), null, DisplayType.Date);
	}   //  Calendar

	/**
	 *  Constructor
	 *  @param frame frame
	 *  @param title title
	 *  @param startTS start date/time
	 *  @param displayType DisplayType (Date, DateTime, Time)
	 */
	public Calendar (Frame frame, String title, Timestamp startTS, int displayType)
	{
		super (frame, title, true);
		log.info(startTS==null ? "null" : startTS.toString() + " - DT=" + displayType);
		m_displayType = displayType;
		//
		try
		{
			jbInit();
			setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		}
		catch(Exception ex)
		{
			log.log(Level.SEVERE, "Calendar", ex);
		}
		//
		loadData(startTS);
	}	//	Calendar

	/** Display Type				*/
	private int					m_displayType;
	/**	The Date					*/
	private GregorianCalendar	m_calendar;
	/** Is there a PM format		*/
	private boolean 			m_hasAM_PM = false;
	//
	private CButton[] 			m_days;
	private CButton				m_today;
	/**	First Dat of week			*/
	private int 				m_firstDay;
	//
	private int					m_currentDay;
	private int					m_currentMonth;
	private int					m_currentYear;
	private int					m_current24Hour = 0;
	private int					m_currentMinute = 0;
	//
	private boolean				m_setting = true;
	/** Abort = ignore				*/
	private boolean             m_abort = true;
	/** Cancel = set null			*/
	private boolean				m_cancel = false;
	//
	private long				m_lastClick = System.currentTimeMillis();
	private int					m_lastDay = -1;
	//
	private static final Insets	ZERO_INSETS = new Insets(0,0,0,0);
	/**	Logger			*/
	private static CLogger log = CLogger.getCLogger(Calendar.class);
	//
	private CPanel mainPanel = new CPanel();
	private CPanel monthPanel = new CPanel();
	private CComboBox cMonth = new CComboBox();
	private JSpinner cYear = new JSpinner(new SpinnerNumberModel(2000, 1900,2100,1));
	private BorderLayout mainLayout = new BorderLayout();
	private CPanel dayPanel = new CPanel();
	private GridLayout dayLayout = new GridLayout();
	private GridBagLayout monthLayout = new GridBagLayout();
	private CButton bNext = new CButton();
	private CButton bBack = new CButton();
	private CPanel timePanel = new CPanel();
	private CComboBox fHour = new CComboBox(getHours());
	private CLabel lTimeSep = new CLabel();
	private JSpinner fMinute = new JSpinner(new MinuteModel(5));	//	5 minute snap size
	private JCheckBox cbPM = new JCheckBox();
	private JLabel lTZ = new JLabel();
	private CButton bOK = new CButton();
	private GridBagLayout timeLayout = new GridBagLayout();

	/**
	 *	Static init
	 *  @throws Exception
	 */
	private void jbInit() throws Exception
	{
		this.addKeyListener(this);
		//
		mainPanel.setLayout(mainLayout);
		mainLayout.setHgap(2);
		mainLayout.setVgap(2);
		mainPanel.setBorder(BorderFactory.createLoweredBevelBorder());
		getContentPane().add(mainPanel);

		//	Month Panel
		monthPanel.setLayout(monthLayout);
		monthPanel.add(bBack,        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		monthPanel.add(cYear,           new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0
			,GridBagConstraints.SOUTHEAST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
		monthPanel.add(bNext,    new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		monthPanel.add(cMonth,  new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
			,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		mainPanel.add(monthPanel, BorderLayout.NORTH);
		cMonth.addActionListener(this);
		cYear.addChangeListener(this);
		bBack.setIcon(Env.getImageIcon("Parent16.gif"));   //  <
		bBack.setMargin(new Insets(0,0,0,0));
		bBack.addActionListener(this);
		bNext.setIcon(Env.getImageIcon("Detail16.gif"));   //   >
		bNext.setMargin(new Insets(0,0,0,0));
		bNext.addActionListener(this);

		//	Day Panel
		dayPanel.setLayout(dayLayout);
		dayLayout.setColumns(7);
		dayLayout.setHgap(2);
		dayLayout.setRows(7);
		dayLayout.setVgap(2);
		mainPanel.add(dayPanel, BorderLayout.CENTER);

		//	Time Panel
		timePanel.setLayout(timeLayout);
		lTimeSep.setText(" : ");
		timePanel.add(fHour,     new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 6, 0, 0), 0, 0));
		timePanel.add(lTimeSep,     new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0
			,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		timePanel.add(fMinute,    new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		timePanel.add(cbPM,     new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0));
		timePanel.add(lTZ,      new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 4, 0, 0), 0, 0));
		timePanel.add(bOK,      new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 6, 0, 2), 0, 0));
		mainPanel.add(timePanel,  BorderLayout.SOUTH);
		fHour.addKeyListener(this);		//	Enter returns
		// JSpinner ignores KeyListener
		((JSpinner.DefaultEditor)fMinute.getEditor()).getTextField().addKeyListener(this);
		fMinute.addChangeListener(this);
		cbPM.addActionListener(this);
		cbPM.addKeyListener(this);
		bOK.setIcon(Env.getImageIcon("Ok16.gif"));
		bOK.setMargin(new Insets(0,1,0,1));
		bOK.addActionListener(this);
	}	//	jbInit

	/**
	 *  Window Events - requestFocus
	 *  @param e event
	 */
	protected void processWindowEvent(WindowEvent e)
	{
	//	log.config( "Calendar.processWindowEvent", e);
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_OPENED)
		{
			if (m_displayType == DisplayType.Time)
				fHour.requestFocus();
			else if (m_today != null)
				m_today.requestFocus();
		}
	}   //  processWindowEvent

	
	/**************************************************************************
	 *	Load Data.
	 * 	- Years
	 *	- Month names
	 *	- Day Names
	 *  @param startTS time stamp
	 */
	private void loadData (Timestamp startTS)
	{
		m_calendar = new GregorianCalendar(Language.getLoginLanguage().getLocale());
		if (startTS == null)
			m_calendar.setTimeInMillis(System.currentTimeMillis());
		else
			m_calendar.setTime(startTS);
		m_firstDay = m_calendar.getFirstDayOfWeek();
		//
		Locale loc = Language.getLoginLanguage().getLocale();
		SimpleDateFormat formatDate = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.LONG, loc);
		//	Short: h:mm a - HH:mm 	Long: h:mm:ss a z - HH:mm:ss z
		SimpleDateFormat formatTime = (SimpleDateFormat)DateFormat.getTimeInstance(DateFormat.SHORT, loc);
		m_hasAM_PM = formatTime.toPattern().indexOf('a') != -1;
		if (m_hasAM_PM)
			cbPM.setText(formatTime.getDateFormatSymbols().getAmPmStrings()[1]);
		else
			cbPM.setVisible(false);

		//	Years
		m_currentYear = m_calendar.get(java.util.Calendar.YEAR);
		cYear.setEditor(new JSpinner.NumberEditor(cYear, "0000"));
		cYear.setValue(new Integer(m_currentYear));

		//	Months		-> 0=Jan 12=_
		String[] months = formatDate.getDateFormatSymbols().getMonths();
		for (int i = 0; i < months.length; i++)
		{
			KeyNamePair p = new KeyNamePair(i+1, months[i]);
			if (!months[i].equals(""))
				cMonth.addItem(p);
		}
		m_currentMonth = m_calendar.get(java.util.Calendar.MONTH) + 1;	//	Jan=0
		cMonth.setSelectedIndex(m_currentMonth-1);

		//	Week Days	-> 0=_  1=Su  .. 7=Sa
		String[] days = formatDate.getDateFormatSymbols().getShortWeekdays();	//	0 is blank, 1 is Sunday
		for (int i = m_firstDay; i < 7 + m_firstDay; i++)
		{
			int index = i > 7 ? i -7 : i;
			dayPanel.add(createWeekday(days[index]), null);
		}

		//	Days
		m_days = new CButton[6*7];
		m_currentDay = m_calendar.get(java.util.Calendar.DATE);
		for (int i = 0; i < 6; i++)		//	six weeks a month maximun
			for (int j = 0; j < 7; j++)	//	seven days
			{
				int index = i*7 + j;
				m_days[index] = createDay();
				dayPanel.add(m_days[index], null);
			}

		//	Today button
		m_days[m_days.length-1].setBackground(Color.green);
		m_days[m_days.length-1].setText("*");
		m_days[m_days.length-1].setToolTipText(Msg.getMsg(Env.getCtx(), "Today"));
		//	Cancel
		m_days[m_days.length-2].setBackground(Color.red);
		m_days[m_days.length-2].setText("x");
		m_days[m_days.length-2].setToolTipText(Msg.getMsg(Env.getCtx(), "Cancel"));

		//	Date/Time
		m_current24Hour = m_calendar.get(java.util.Calendar.HOUR_OF_DAY);
		m_currentMinute = m_calendar.get(java.util.Calendar.MINUTE);

		//	What to show
		timePanel.setVisible(m_displayType == DisplayType.DateTime || m_displayType == DisplayType.Time);
		monthPanel.setVisible(m_displayType != DisplayType.Time);
		dayPanel.setVisible(m_displayType != DisplayType.Time);

		//	update UI from m_current...
		m_setting = false;
		setCalendar();
	}	//	loadData

	/**
	 *	Create Week Day Label
	 *  @param title Weedkay Title
	 *  @return week day
	 */
	private JLabel createWeekday (String title)
	{
		JLabel label = new JLabel(title);
		label.setBorder(BorderFactory.createRaisedBevelBorder());
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		label.setRequestFocusEnabled(false);
		return label;
	}	//	createWeekday

	/**
	 *	Create Day Label
	 *  @return button
	 */
	private CButton createDay()
	{
		CButton button = new CButton();
		button.setBorder(BorderFactory.createLoweredBevelBorder());
		button.setHorizontalTextPosition(SwingConstants.CENTER);
		button.setMargin(ZERO_INSETS);
		button.addActionListener(this);
		button.addMouseListener(this);
		button.addKeyListener(this);
		return button;
	}	//	createWeekday

	/**
	 * 	Create 12/25 hours
	 *  @return Array with hours as String
	 */
	private Object[] getHours()
	{
		Locale loc = Language.getLoginLanguage().getLocale();
		//	Short: h:mm a - HH:mm 	Long: h:mm:ss a z - HH:mm:ss z
		SimpleDateFormat formatTime = (SimpleDateFormat)DateFormat.getTimeInstance(DateFormat.SHORT, loc);
		m_hasAM_PM = formatTime.toPattern().indexOf('a') != -1;
		//
		Object[] retValue = new Object[m_hasAM_PM ? 12 : 24];
		if (m_hasAM_PM)
		{
			retValue[0] = "12";
			for (int i = 1; i < 10; i++)
				retValue[i] = " " + String.valueOf(i);
			for (int i = 10; i < 12; i++)
				retValue[i] = String.valueOf(i);
		}
		else
		{
			for (int i = 0; i < 10; i++)
				retValue[i] = "0" + String.valueOf(i);
			for (int i = 10; i < 24; i++)
				retValue[i] = String.valueOf(i);
		}
		return retValue;
	}	//	getHours


	/**************************************************************************
	 *	Set Calandar from m_current variables and update UI
	 */
	private void setCalendar()
	{
		if (m_setting)
			return;
	//	log.config( "Calendar.setCalendar");

		//  --- Set Month & Year
		m_setting = true;
		cMonth.setSelectedIndex(m_currentMonth-1);
		cYear.setValue(new Integer(m_currentYear));
		m_setting = false;

		//  --- Set Day
		//	what is the first day in the selected month?
		m_calendar.set(m_currentYear, m_currentMonth-1, 1);		//	Month is zero based
		int dayOne = m_calendar.get(java.util.Calendar.DAY_OF_WEEK);
		int lastDate = m_calendar.getActualMaximum(java.util.Calendar.DATE);

		//	convert to index
		dayOne -= m_firstDay;
		if (dayOne < 0)

⌨️ 快捷键说明

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