/*
* @(#)MonthViewDemo.java
*
* Copyright (c) 2003-2004 Stand By Soft, Ltd. All rights reserved.
*
* This software is the proprietary information of Stand By Soft, Ltd.
* Use is subject to license terms.
*/
package com.standbysoft.demo.date;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Locale;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import com.standbysoft.component.date.swing.JMonthView;
/**
* Shows the operations that can be performed on a <code>JMonthView</code> component.
*/
public class MonthViewDemo extends JPanel {
/**
* JMonthView component used to show the features.
*/
private JMonthView monthView;
public MonthViewDemo() {
monthView = new JMonthView();
setLayout(new GridBagLayout());
add(monthView, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(8, 5, 0, 5), 0, 0));
add(createGeneralPanel(), new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 5, -2, 5), 0, 0));
add(new JLabel(), new GridBagConstraints(0, 3, 4, 1, 0.0, 2.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
}
/**
* Creates a panel that controls JMonthView specific features.
*
* @return actual panel
*/
private JComponent createGeneralPanel() {
JPanel generalPanel = new JPanel();
generalPanel.setBorder(new TitledBorder("General"));
generalPanel.setLayout(new GridBagLayout());
JLabel localeLabel = new JLabel("Locale:");
localeLabel.setDisplayedMnemonic('l');
final JComboBox localeComboBox = new JComboBox(Locale.getAvailableLocales());
localeLabel.setLabelFor(localeComboBox);
localeComboBox.setSelectedItem(monthView.getLocale());
localeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
Locale locale = (Locale) localeComboBox.getSelectedItem();
monthView.setLocale(locale);
}
}
});
localeComboBox.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel l = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Locale locale = (Locale) value;
l.setText(locale.getDisplayName());
return l;
}
});
final JCheckBox displayTodayCheckBox = new JCheckBox("Display Today Button");
displayTodayCheckBox.setMnemonic('t');
displayTodayCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
try {
monthView.setDisplayToday(displayTodayCheckBox.isSelected());
} catch (Exception e) {
displayTodayCheckBox.setSelected(monthView.isDisplayToday());
JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(MonthViewDemo.this), "You can switch off empty selection only if at least one date is selected.", "Empty Selection Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null);
}
}
});
displayTodayCheckBox.setSelected(monthView.isDisplayToday());
final JCheckBox showStatusBar = new JCheckBox("Show Status Bar");
showStatusBar.setMnemonic('s');
showStatusBar.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
try {
monthView.setStatusVisible(showStatusBar.isSelected());
} catch (Exception e) {
showStatusBar.setSelected(monthView.isStatusVisible());
JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(MonthViewDemo.this), "You can switch off empty selection only if at least one date is selected.", "Empty Selection Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null);
}
}
});
showStatusBar.setSelected(monthView.isStatusVisible());
final JCheckBox emptySelectionCheckBox = new JCheckBox("Allow Empty Selection");
emptySelectionCheckBox.setMnemonic('e');
emptySelectionCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
try {
monthView.setEmptySelectionAllowed(emptySelectionCheckBox.isSelected());
} catch (Exception e) {
emptySelectionCheckBox.setSelected(monthView.isEmptySelectionAllowed());
JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(MonthViewDemo.this), "You can switch off empty selection only if at least one date is selected.", "Empty Selection Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null);
}
}
});
emptySelectionCheckBox.setSelected(monthView.isEmptySelectionAllowed());
generalPanel.add(displayTodayCheckBox, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
generalPanel.add(showStatusBar, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
generalPanel.add(emptySelectionCheckBox, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
generalPanel.add(new JLabel(), new GridBagConstraints(0, 4, 3, 1, 2.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
generalPanel.add(localeLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
generalPanel.add(localeComboBox, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
return generalPanel;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("JMonthView Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
MonthViewDemo newContentPane = new MonthViewDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
|