package demo;
/*
* Demo.java
*
* This demo shows you how to use a MultiDateChooser
* and a MultiDateButton.
*
* It displays a MultiDateChooser on the frame.
* The Button "Show selected Dates" displays the selections made
* on that date chooser.
*
* The Button "Datechooser Dialog" is a MultiDateButton.
* It displays a Dialog with another MultiDateChooser.
* The selections made there are displayed immediately in an OptionPane
* that pops up via the "DateSelectionListener" which is implemented
* directly by this "Demo" class.
*
* The Button "Clear All" clears the selections in both "MultiDateChooser"s
* (the one in the frame and the one in the dialog).
*/
import chooser.*;
import event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
public class Demo extends JFrame implements ActionListener, DateSelectionListener {
private JButton btShow, btClearAll;
private MultiDateButton btChooserDialog;
private JPanel mainpanel;
private JToolBar toolbar;
private MultiDateChooser chooser;
private SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
public Demo() {
super("Demo");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainpanel = new JPanel();
toolbar = new JToolBar();
btShow = new JButton("Show selected Dates");
btChooserDialog = new MultiDateButton("Datechooser Dialog");
btClearAll = new JButton("Clear All");
getContentPane().add(mainpanel, BorderLayout.CENTER);
btShow.addActionListener(this);
toolbar.add(btShow);
toolbar.add(btChooserDialog);
btClearAll.addActionListener(this);
toolbar.add(btClearAll);
getContentPane().add(toolbar, BorderLayout.PAGE_START);
chooser = new MultiDateChooser();
mainpanel.add(chooser);
//do some selections programmatically:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -2);
chooser.add(cal.getTime());
cal.add(Calendar.DATE, 5);
chooser.add(cal.getTime());
//listen to selection changes in chooser dialog:
btChooserDialog.addSelectionListener(this);
}
private void showSelectedDates(MultiDateChooser source) {
Set<Date> dates = source.getSelectedDates();
String str = "";
for (Date date : dates) {
str += df.format(date) + "\n";
}
str += "\n";
JOptionPane.showMessageDialog(this, str);
}
public void actionPerformed(final ActionEvent e) {
Object source = e.getSource();
if (source == btShow) {
showSelectedDates(chooser);
} else if (source == btClearAll) {
chooser.clearSelections();
btChooserDialog.clearSelections();
}
}
public void dateSelectionChanged(DateSelectionEvent e) {
showSelectedDates((MultiDateChooser) e.getSource());
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
public void run() {
new Demo().setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
}