/* * $Id: CalendarDemo.java,v 1.23 2005/11/21 17:04:10 zfq Exp $ * * Copyright (C) 2001-2003 Extreme Component, Inc. All rights reserved. * Use is subject to license terms. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.plaf.basic.*; import javax.swing.event.*; import java.util.Date; import java.text.*; import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import com.zfqjava.swing.JCalendar; import com.zfqjava.swing.cell.*; import com.zfqjava.swing.AComboBox; import com.zfqjava.swing.JBean; /** * JCalendar Demo * * @author $Author: zfq $ * @version $Revision: 1.23 $ $Date: 2005/11/21 17:04:10 $ */ public class CalendarDemo extends JPanel { private JCalendar calendar; private static boolean filterSelection; public CalendarDemo() { setLayout(new BorderLayout()); calendar = new JCalendar() { public boolean canSelect(int year, int month, int day) { if(filterSelection) { if(day == 13) { return false; } } return true; } }; calendar.setCellProvider(new DefaultAttributesProvider()); calendar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("action fires: " + e.getActionCommand() + " date:" + calendar.getTime()); } }); add(calendar, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(createPropertyPanel(), BorderLayout.NORTH); panel.add(createControlPanel(), BorderLayout.CENTER); panel.add(createDialogPanel(), BorderLayout.SOUTH); add(panel, BorderLayout.EAST); } private JCalendar createCalendar() { JCalendar calendar = new JCalendar(); calendar.putClientProperty("JCalendar.headerStyle", this.calendar.getClientProperty("JCalendar.headerStyle")); calendar.putClientProperty("JCalendar.centerStyle", this.calendar.getClientProperty("JCalendar.centerStyle")); calendar.putClientProperty("JCalendar.footerStyle", this.calendar.getClientProperty("JCalendar.footerStyle")); calendar.setTime(this.calendar.getTime()); calendar.setCellProvider(new DefaultAttributesProvider()); calendar.setSelectionLabelVisible(this.calendar.isSelectionLabelVisible()); return calendar; } private static class DefaultAttributesProvider implements AttributesProvider { private SimpleAttributeSet a; private SimpleAttributeSet disabledA; public DefaultAttributesProvider() { a = new SimpleAttributeSet(); StyleConstants.setForeground(a, Color.red); StyleConstants.setBackground(a, Color.yellow); disabledA = new SimpleAttributeSet(); StyleConstants.setForeground(disabledA, Color.WHITE); StyleConstants.setBackground(disabledA, Color.LIGHT_GRAY); } public AttributeSet getAttributes(Object value) { if(value != null) { int day = ((Integer)value).intValue(); switch(day) { case 1: case 3: case 5: case 7: case 23: return a; case 13: if(CalendarDemo.filterSelection) { return disabledA; } } } return null; } } private JPanel createPropertyPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Change Property"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); c.weightx = 0.5; c.weighty = 0.5; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(0, 0, 5, 0); JLabel label = new JLabel("Header Style: "); addPanel(panel, c, label); Object[] headStyles = { "Classic_Arrow", "Modern_Arrow", "ComboBox_Spinner", "None" }; final JComboBox headStyleCB = new JComboBox(headStyles); headStyleCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object item = headStyleCB.getSelectedItem(); calendar.putClientProperty("JCalendar.headerStyle", item); } }); label.setLabelFor(headStyleCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, headStyleCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Center Style: "); addPanel(panel, c, label); Object[] centerStyles = { "MonthView", "YearView" }; final JComboBox centerStyleCB = new JComboBox(centerStyles); centerStyleCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object item = centerStyleCB.getSelectedItem(); calendar.putClientProperty("JCalendar.centerStyle", item); } }); label.setLabelFor(centerStyleCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, centerStyleCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Footer Style: "); addPanel(panel, c, label); Object[] footerStyles = { "Today", "None" }; final JComboBox footerStyleCB = new JComboBox(footerStyles); footerStyleCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object item = footerStyleCB.getSelectedItem(); calendar.putClientProperty("JCalendar.footerStyle", item); } }); label.setLabelFor(footerStyleCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, footerStyleCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Header Rollover Mode: "); addPanel(panel, c, label); Object[] headerRolloverModes = { "JToolBar", "JButton" }; final JComboBox headerRolloverModeCB = new JComboBox(headerRolloverModes); headerRolloverModeCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object item = headerRolloverModeCB.getSelectedItem(); calendar.putClientProperty("JCalendar.headerRolloverMode", item); } }); label.setLabelFor(headerRolloverModeCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, headerRolloverModeCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Enable selection filter: "); addPanel(panel, c, label); final JCheckBox box = new JCheckBox(); box.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { filterSelection = box.isSelected(); calendar.repaint(); } }); label.setLabelFor(box); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, box); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Toggle label visible: "); addPanel(panel, c, label); final JCheckBox box2 = new JCheckBox(); box2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calendar.setSelectionLabelVisible(box2.isSelected()); } }); label.setLabelFor(box2); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, box2); return panel; } private static void addPanel(Container container, GridBagConstraints c, Component component) { GridBagLayout gridbag = (GridBagLayout)container.getLayout(); gridbag.setConstraints(component, c); container.add(component); } private JPanel createControlPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Combine Control"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); c.weightx = 0.5; c.weighty = 0.5; c.fill = GridBagConstraints.HORIZONTAL; // another combobox button c.gridwidth = GridBagConstraints.RELATIVE; c.anchor = GridBagConstraints.WEST; JLabel label = new JLabel("ComboBox: "); addPanel(panel, c, label); AComboBox comboBox = createAComboBox(); comboBox.setValue(new Date()); DateCellRenderer renderer = (DateCellRenderer)comboBox.getRenderer(); DateCellEditor editor = (DateCellEditor)comboBox.getEditor(); renderer.setFormat(DateFormat.getDateInstance()); editor.setFormat(DateFormat.getDateInstance()); c.anchor = GridBagConstraints.EAST; c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, comboBox); c.anchor = GridBagConstraints.WEST; c.insets = new Insets(0, 0, 5, 0); label = new JLabel("Editable: "); addPanel(panel, c, label); // Todo: add comboBox AComboBox editableComboBox = createAComboBox(); editableComboBox.setEditable(true); editableComboBox.setValue(new Date()); renderer = (DateCellRenderer)editableComboBox.getRenderer(); editor = (DateCellEditor)editableComboBox.getEditor(); renderer.setFormat(DateFormat.getDateInstance()); editor.setFormat(DateFormat.getDateInstance()); editableComboBox.setValue(new Date()); c.anchor = GridBagConstraints.EAST; c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, editableComboBox); c.gridwidth = GridBagConstraints.RELATIVE; c.anchor = GridBagConstraints.WEST; label = new JLabel("DropDown Button: "); addPanel(panel, c, label); // Todo: add DropDownButton DropDownButtonPanel buttonPanel = new DropDownButtonPanel(); c.anchor = GridBagConstraints.EAST; c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, buttonPanel); JPanel fakePanel = new JPanel(); fakePanel.setLayout(new BorderLayout()); fakePanel.add(panel, BorderLayout.NORTH); return fakePanel; } private JPanel createDialogPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Dialog and Frame"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); // c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; c.insets = new Insets(0, 0, 5, 0); c.gridwidth = GridBagConstraints.REMAINDER; JButton b = new JButton("Show Frame"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame("JCalendar"); frame.getContentPane().add(createCalendar()); frame.pack(); centerOnScreen(frame); frame.show(); } }); addPanel(panel, c, b); return panel; } private AComboBox createAComboBox() { final AComboBox comboBox = new AComboBox(); comboBox.setRenderer(new DateCellRenderer(DateFormat.getDateInstance())); final JCalendar calendar = new JCalendar(); calendar.setCellProvider(new DefaultAttributesProvider()); calendar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { comboBox.hidePopup(); } }); calendar.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { comboBox.setValue(calendar.getTime()); } }); comboBox.setPopupComponent(calendar); comboBox.setValue(calendar.getTime()); return comboBox; } /** * DropDownButtonPanel provides a panel and add JCalendar component as Popup. * Todo: need enhance */ private static class DropDownButtonPanel extends JPanel { private JButton button; private JButton arrowButton; private JCalendar calendar; private JPopupMenu popup; public DropDownButtonPanel() { setLayout(new BorderLayout()); button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showPopup(); } }); add(button, BorderLayout.CENTER); arrowButton = new BasicArrowButton(SwingConstants.SOUTH); arrowButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showPopup(); } }); add(arrowButton, BorderLayout.EAST); calendar = new JCalendar(); calendar.setCellProvider(new DefaultAttributesProvider()); calendar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setValue(calendar.getTime()); hidePopup(); } }); calendar.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { setValue(calendar.getTime()); } }); popup = new JPopupMenu(); popup.add(calendar); setValue(calendar.getTime()); } /** * Resets the UI property with a value from the current look and feel. * * @see JComponent#updateUI */ public void updateUI() { super.updateUI(); if(calendar != null) { calendar.updateUI(); } if(popup != null) { popup.updateUI(); } } public JCalendar getJCalendar() { return calendar; } protected void setValue(Date date) { button.setText(formatDate(date)); } private String formatDate(Date date) { DateFormat format = DateFormat.getDateInstance(); return format.format(date); } private void hidePopup() { popup.setVisible(false); } private int getPopupX() { int width = popup.getPreferredSize().width; return (getWidth() - width); } private int getPopupY() { return getHeight(); } private void showPopup() { popup.show(this, getPopupX(), getPopupY()); //popup.setVisible(true); } } public static void main(String[] args) { DemoPanel demoPanel = new DemoPanel(); demoPanel.addDemo(new CalendarDemo()); demoPanel.setTitle("JCalendar Demo"); demoPanel.setDefaultCloseOperation(JBean.EXIT_ON_CLOSE); demoPanel.showFrame(); } private static void centerOnScreen(Window window) { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); window.setLocation((d.width - window.getWidth())/2, (d.height - window.getHeight())/2); } }