📄 calendarpanel.java
字号:
/* * NachoCalendar * * Project Info: http://nachocalendar.sf.net * * 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. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * Changes * ------- * 2005-03-30 Added circular scroll * 2005-03-25 Added moon print * 2005-03-23 Centers the panel of today * 2005-01-08 Cleanups * 2005-01-02 Added property change support. * 2004-12-24 Reimplemented layout functions. * Added keyboard navigation. * Reimplemented selection model, now supports multiple selection * 2004-12-12 Reimplemented the constructor. * 2004-10-22 setEnabled(boolean b) overriden, now works * 2004-10-17 Added keylistener support. * 2004-10-01 Checked with checkstyle * * ------- * * CalendarPanel.java * * Created on August 12, 2004, 4:05 PM */package net.sf.nachocalendar.components;import java.awt.Adjustable;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.text.ParseException;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import javax.swing.BoundedRangeModel;import javax.swing.DefaultBoundedRangeModel;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import net.sf.nachocalendar.event.DateSelectionEvent;import net.sf.nachocalendar.event.DateSelectionListener;import net.sf.nachocalendar.model.DataModel;import net.sf.nachocalendar.model.DateSelectionModel;import net.sf.nachocalendar.model.DefaultDateSelectionModel;/** * Panel used to show many months at once. * @author Ignacio Merani */public class CalendarPanel extends JPanel implements ChangeListener { private boolean antiAliased; private KeyListener klistener; private MouseListener mlistener; private DateSelectionListener listlistener; private DateSelectionModel dateSelectionModel; private DataModel datamodel; private HeaderRenderer headerrenderer; private DayRenderer dayrenderer; private Calendar navigation, calendar; private Date minDate, maxDate; private int minimalDaysInFirstWeek; private boolean printMoon; protected boolean eternalScroll; private boolean showToday; private int middle; //used to display the panel in the middle! /** Array with the panels. */ private MonthPanel[] months; /** Orientation. */ private int orientation; /** Scroll Position. */ private int scrollPosition; /** * Horizontal orientation. */ public static final int HORIZONTAL = 0; /** Vertical orientation. */ public static final int VERTICAL = 1; /** Left Position.*/ public static final int LEFT = 0; /** Right Position. */ public static final int RIGHT = 1; /** Up Position. */ public static final int UP = 0; /** Down Position. */ public static final int DOWN = 1; /** Scroll Bar. */ private JScrollBar scroll; /** Year Scroller. */ private YearScroller ys; /** Today Button */ private JButton today; /** Array representing the working days. */ private boolean[] workingdays = {false, true, true, true, true, true, true }; /** Calendar used to calc dates. */ private Calendar cal; private int showingyear, quantity; private Date date; private JPanel abajo; private boolean showWeekNumber; /** Utility field holding list of ChangeListeners. */ private transient java.util.ArrayList changeListenerList; /** Holds value of property yearPosition. */ private int yearPosition; /** Default constructor, constructs a vertical panel with 3 months. */ public CalendarPanel() { this(3, VERTICAL); } /** * Constructs a panel with 3 months and the provided orientation. * @param quantity quantity of months to show at once */ public CalendarPanel(int quantity) { this(quantity, VERTICAL); } /** * Constructrs a panel with 3 months, Vertical. * * @param showWeekNumbers */ public CalendarPanel(boolean showWeekNumbers) { this(3, VERTICAL, showWeekNumbers); } /** * Constructs a panel with the provided quantity and orientation. * @param quantity months to show at once * @param orientation orientation */ public CalendarPanel(int quantity, int orientation) { this(quantity, orientation, true); } //TODO: LeO: parameterized correctly public CalendarPanel(int quantity, int orientation, boolean showWeekNumber) { this(quantity, orientation, showWeekNumber, true); } /** * Creates a new instance of CalendarPanel. * @param showWeekNumber true to show the week numbers * @param quantity months to show at once * @param orientation the orientation */ public CalendarPanel(int quantity, int orientation, boolean showWeekNumber, boolean eternalScroll) { if (quantity < 1) { quantity = 1; } if (quantity > 12) { quantity = 12; } this.quantity = quantity; this.showWeekNumber = showWeekNumber; this.orientation = orientation; navigation = new GregorianCalendar(); calendar = new GregorianCalendar(); dateSelectionModel = new DefaultDateSelectionModel(); ys = new YearScroller(); middle=quantity/2; this.eternalScroll = eternalScroll; initScroll(); today = new JButton(CalendarUtils.getMessage("today")); today.setVisible(false); cal = new GregorianCalendar(); setLayout(new BorderLayout()); createListeners(); dateSelectionModel.addDateSelectionListener(listlistener); setQuantity(quantity); if (orientation == VERTICAL) { layoutVertical(); } else { layoutHorizontal(); } initDisplayPanel(); setValue(new Date()); setRenderer(new DefaultDayRenderer()); setHeaderRenderer(new DefaultHeaderRenderer()); } /** * init the ScrollBar with the Parameters required! */ private void initScroll() { int or = (orientation == HORIZONTAL ? Adjustable.HORIZONTAL : Adjustable.VERTICAL); scroll = new JScrollBar(or); scroll.setMaximum(17 - quantity); scroll.setBlockIncrement(3); scroll.setUnitIncrement(1); BoundedRangeModel model = new DefaultBoundedRangeModel() { public void setValue(int value) { super.setValue(value); if (eternalScroll) { if (value < getMinimum()) { goPreviousYear(); } if (value > (getMaximum() - getExtent())) { goNextYear(); } } } }; model.setMaximum(12 + 5 - quantity); model.setMinimum(0); model.setExtent(5); model.setValue(0); scroll.setModel(model); } private void goNextYear() { ys.setYear(ys.getYear() + 1); setShowingYear(ys.getYear()); scroll.setValue(0); } private void goPreviousYear() { ys.setYear(ys.getYear() - 1); setShowingYear(ys.getYear()); scroll.setValue(scroll.getMaximum() - scroll.getModel().getExtent()); } /** * Displays the panel right after the start * * @param quantity * @param eternalScroll */ private void initDisplayPanel() { int displayRange= cal.get(Calendar.MONTH); //first we have to retrieve the month setShowingYear(cal.get(Calendar.YEAR)); //next we set the year (and change the cal) if (0 < displayRange && (displayRange-middle) < 10){ scroll.setValue(displayRange -middle); } else if (displayRange < quantity) { scroll.setValue(displayRange); } else { scroll.setValue(displayRange - quantity -middle); } } /** Utility method used during initialization. */ private void createListeners() { scroll.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { setShowingMonth(e.getValue()); } }); ys.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { setShowingYear(ys.getYear()); } }); today.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cal.setTime(new Date()); initDisplayPanel(); } }); addMouseWheelListener(new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { if (!isEnabled()) return; int q = e.getWheelRotation(); int value = scroll.getValue(); value += q; if (value < 0) { value = 0; if (eternalScroll) { goPreviousYear(); return; } } if (value > 11) { value = 11; if (eternalScroll) { goNextYear(); return; } } scroll.setValue(value); } }); // listener to pass events klistener = new KeyListener() { public void keyPressed(KeyEvent e) { boolean changed = false; int keycode = e.getKeyCode(); navigation.setTime(calendar.getTime()); if ((keycode == KeyEvent.VK_LEFT) || (keycode == 226)) { navigation.add(Calendar.DAY_OF_YEAR, -1); changed = true; } if ((keycode == KeyEvent.VK_RIGHT) || (keycode == 227)) { navigation.add(Calendar.DAY_OF_YEAR, 1); changed = true; } if ((keycode == KeyEvent.VK_UP) || (keycode == 224)) { navigation.add(Calendar.DAY_OF_YEAR, -7); changed = true; } if ((keycode == KeyEvent.VK_DOWN) || (keycode == 225)) { navigation.add(Calendar.DAY_OF_YEAR, 7); changed = true; } if ((keycode == KeyEvent.VK_PAGE_UP)) { navigation.add(Calendar.MONTH, -1); scroll.setValue(navigation.get(Calendar.MONTH)); changed = true; } if ((keycode == KeyEvent.VK_PAGE_DOWN)) { navigation.add(Calendar.MONTH, 1); scroll.setValue(navigation.get(Calendar.MONTH)); changed = true; } if (changed) { if (!isShowing(navigation.getTime())) { showMonth(navigation.getTime()); } if ((!e.isControlDown()) && (!e.isShiftDown())) { dateSelectionModel.clearSelection(); if (e.isShiftDown()) { dateSelectionModel.addSelectionInterval( dateSelectionModel.getLeadSelectionDate(), navigation.getTime()); } else dateSelectionModel.addSelectionInterval(navigation .getTime(), navigation.getTime()); } else { if (e.isShiftDown()) { dateSelectionModel.addSelectionInterval( dateSelectionModel.getLeadSelectionDate(), navigation.getTime()); } else { if (dateSelectionModel.isSelectedDate(navigation .getTime())) { dateSelectionModel.removeSelectionInterval( navigation.getTime(), navigation .getTime()); } else dateSelectionModel.addSelectionInterval( navigation.getTime(), navigation .getTime());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -