1

I have a com.google.gwt.user.datepicker.client.DatePicker and I don't want the user to be able to select the previous month or the next month in certain cases.

For example I don't want the user to be able to select a day before today.

I figured out how to disable the actual days in the calendar, but the user can still click on the << and go back a month, which all the days are disabled.

I can't find any way to get a reference to the << and >> buttons/links to preferably hide them or alternatively disable them.

I am using GWT 2.1.1 and can go to GWT 2.2.0 if that matters.

Does anyone know how to get a reference to these widgets?

Community
  • 1
  • 1

1 Answers1

1

The DefaultDateMonthSelector (com.google.gwt.user.datepicker.client) used by the DatePicker does not expose these widgets and hence it is not possible to disable these buttons directly.

To implement this feature, write a new DateMonthSelector (you could start with the source of DefaultMonthSelector http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/user/datepicker/client/DefaultMonthSelector.java ). For e.g.,

public final class LimitedMonthSelector extends MonthSelector {
   // Keep the PushButtons disabled
   // Modified from the original DefaultMonthSelector
  @Override
  protected void setup() {
    // Set up backwards.
    backwards = new PushButton();
    backwards.getUpFace().setHTML("&laquo;");
    backwards.setStyleName(css().previousButton());
    backwards.setEnabled(false);   // Disable the back button

    forwards = new PushButton();
    forwards.getUpFace().setHTML("&raquo;");
    forwards.setStyleName(css().nextButton());
    forwards.setEnabled(false);   // Disable the forward button

    // Set up grid.
    grid = new Grid(1, 3);
    grid.setWidget(0, 0, backwards);
    grid.setWidget(0, 2, forwards);

    CellFormatter formatter = grid.getCellFormatter();
    formatter.setStyleName(0, 1, css().month());
    formatter.setWidth(0, 0, "1");
    formatter.setWidth(0, 1, "100%");
    formatter.setWidth(0, 2, "1");
    grid.setStyleName(css().monthSelector());
    initWidget(grid);
  }
}

And then use this MonthSelector implementation in a DatePicker implementation. Like,

public class LimitedDatePicker extends DatePicker {

  public MonthYearDatePicker() {
    super(new LimitedMonthSelector(), new DefaultCalendarView(), new CalendarModel());
  }
} 

This new LimitedDatePicker would have its back/forward buttons disabled.

krishnakumarp
  • 8,967
  • 3
  • 49
  • 55