monthSpinner.getStyles().put("sizeToContent", true);
monthSpinner.getSpinnerSelectionListeners().add(new SpinnerSelectionListener() {
@Override
public void selectedIndexChanged(Spinner spinner, int previousSelectedIndex) {
Calendar calendar = (Calendar)getComponent();
calendar.setMonth((Integer)spinner.getSelectedItem());
}
});
// Year spinner
yearSpinner = new Spinner();
yearSpinner.setSpinnerData(new NumericSpinnerData(0, Short.MAX_VALUE));
yearSpinner.getSpinnerSelectionListeners().add(new SpinnerSelectionListener() {
@Override
public void selectedIndexChanged(Spinner spinner, int previousSelectedIndex) {
Calendar calendar = (Calendar)getComponent();
calendar.setYear((Integer)spinner.getSelectedItem());
}
});
// Attach a listener to consume mouse clicks
ComponentMouseButtonListener spinnerMouseButtonListener = new ComponentMouseButtonListener.Adapter() {
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
return true;
}
};
monthSpinner.getComponentMouseButtonListeners().add(spinnerMouseButtonListener);
yearSpinner.getComponentMouseButtonListeners().add(spinnerMouseButtonListener);
// Add the month/year table pane
TablePane monthYearTablePane = new TablePane();
monthYearTablePane.getStyles().put("padding", 3);
monthYearTablePane.getStyles().put("horizontalSpacing", 4);
monthYearTablePane.getColumns().add(new TablePane.Column(1, true));
monthYearTablePane.getColumns().add(new TablePane.Column(-1));
TablePane.Row monthYearRow = new TablePane.Row(-1);
monthYearTablePane.getRows().add(monthYearRow);
monthYearRow.add(monthSpinner);
monthYearRow.add(yearSpinner);
TablePane.Row calendarRow = new TablePane.Row();
calendarRow.add(monthYearTablePane);
calendarTablePane.getRows().add(calendarRow);
TablePane.setColumnSpan(monthYearTablePane, 7);
// Add the day labels
calendarRow = new TablePane.Row();
Font labelFont = theme.getFont();
labelFont = labelFont.deriveFont(Font.BOLD);
for (int i = 0; i < 7; i++) {
Label label = new Label();
label.getStyles().put("font", labelFont);
label.getStyles().put("padding", new Insets(2, 2, 4, 2));
label.getStyles().put("horizontalAlignment", HorizontalAlignment.CENTER);
calendarRow.add(label);
}
calendarTablePane.getRows().add(calendarRow);
// Add the buttons
dateButtonGroup = new ButtonGroup();
dateButtonGroup.getButtonGroupListeners().add(new ButtonGroupListener.Adapter() {
@Override
public void selectionChanged(ButtonGroup buttonGroup, Button previousSelection) {
Calendar calendar = (Calendar)getComponent();
Button selection = buttonGroup.getSelection();
if (selection == null) {
CalendarDate selectedDate = calendar.getSelectedDate();
// If no date was selected, or the selection changed as a
// result of the user toggling the date button (as opposed
// to changing the month or year), clear the selection
if (selectedDate == null
|| (selectedDate.year == yearSpinner.getSelectedIndex()
&& selectedDate.month == monthSpinner.getSelectedIndex())) {
calendar.setSelectedDate((CalendarDate)null);
}
} else {
calendar.setSelectedDate((CalendarDate)selection.getButtonData());
}
}
});
for (int j = 0; j < 6; j++) {