package jfxtras.scene.control; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import javafx.beans.property.BooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.control.ToggleButton; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.Priority; /** * This skin uses regular JavaFX controls * @author Tom Eugelink * */ public class CalendarPickerXControlSkin2 extends CalendarPickerXMonthlySkinAbstract { // ================================================================================================================== // CONSTRUCTOR /** * */ public CalendarPickerXControlSkin2(CalendarPickerX control) { super(control); // setup component createNodes(); // update the data refreshDayButtons(); } // ================================================================================================================== // PROPERTIES // ================================================================================================================== // DRAW /** * construct the nodes */ private void createNodes() { // the result GridPane lGridPane = new GridPane(); lGridPane.setVgap(2.0); lGridPane.setHgap(2.0); // setup the grid so all weekday togglebuttons will grow, but the weeknumbers do not ColumnConstraints lColumnConstraintsAlwaysGrow = new ColumnConstraints(); lColumnConstraintsAlwaysGrow.setHgrow(Priority.ALWAYS); ColumnConstraints lColumnConstraintsNeverGrow = new ColumnConstraints(); lColumnConstraintsNeverGrow.setHgrow(Priority.NEVER); lGridPane.getColumnConstraints().addAll(lColumnConstraintsNeverGrow, lColumnConstraintsAlwaysGrow, lColumnConstraintsAlwaysGrow, lColumnConstraintsAlwaysGrow, lColumnConstraintsAlwaysGrow, lColumnConstraintsAlwaysGrow, lColumnConstraintsAlwaysGrow, lColumnConstraintsAlwaysGrow); // year spinner yearXSpinner = new SpinnerX(SpinnerXDataProviderFactory.integerRange(), Calendar.getInstance().get(Calendar.YEAR)); // if the value changed, update the displayed calendar yearXSpinner.valueProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Object arg1, Object arg2) { setDisplayedCalendarFromSpinners(); } }); lGridPane.add(yearXSpinner, 4, 0, 4, 1); // col, row, hspan, vspan // setup: 6 rows of 7 days per week (which is the maximum number of buttons required in the worst case layout) for (int i = 0; i < 6 * 7; i++) { // create buttons ToggleButton lToggleButton = new ToggleButton("" + i); // add it lGridPane.add(lToggleButton, (i % 7) + 1, (i / 7) + 2); // col, row lToggleButton.setMaxWidth(Double.MAX_VALUE); // make the button grow to fill a GridPane's cell lToggleButton.setAlignment(Pos.BASELINE_CENTER); // CULPRIT!!! <-------------------------------------------------------------- // remember it dayButtons.add(lToggleButton); } // add to self this.getStyleClass().add(this.getClass().getSimpleName()); // always add self as style class, because CSS should relate to the skin not the control getChildren().add(lGridPane); } private SpinnerX yearXSpinner = null; final private List dayButtons = new ArrayList(); /* * */ private void setDisplayedCalendarFromSpinners() { // get spinner values int lYear = yearXSpinner.getValue().intValue(); int lMonth = 7; // get new calendar to display Calendar lCalendar = (Calendar)getDisplayedCalendar().clone(); lCalendar.set(Calendar.YEAR, lYear); lCalendar.set(Calendar.MONTH, lMonth); // set it setDisplayedCalendar(lCalendar); refreshDayButtons(); } /* * */ private void refreshDayButtons() { // setup the buttons [0..(6*7)-1] // displayed calendar always points to the 1st of the month int lFirstOfMonthIdx = determineFirstOfMonthDayOfWeek(); // set the month buttons int lDaysInMonth = determineDaysInMonth(); Calendar lCalendar = (Calendar)getDisplayedCalendar().clone(); for (int i = 1; i <= lDaysInMonth; i++) { // set the date lCalendar.set(java.util.Calendar.DATE, i); // determine the index in the buttons int lIdx = lFirstOfMonthIdx + i - 1; // update the button ToggleButton lToggleButton = dayButtons.get(lIdx); lToggleButton.setText("" + i); } } }