gwtgwtbootstrap3

How to use gwtbootstrap3 calendar with UiBinder


Can we use gwtbootstrap3 fullcalendar mentioned at http://gwtbootstrap3.github.io/gwtbootstrap3-demo/#fullcalendar with UiBinder.

I am trying to use it with UiBinder and nothing is appearing on that page.

code to my UiBinder class

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui"
    xmlns:b="urn:import:org.gwtbootstrap3.client.ui.gwt">
    <g:HTMLPanel ui:field="calendarContainer">
    </g:HTMLPanel>
</ui:UiBinder> 

code to the corresponding view class

public class EventView extends ReverseCompositeView<IEventView.IEventPresenter> implements IEventView {

    private static EventViewUiBinder uiBinder = GWT.create( EventViewUiBinder.class );

    interface EventViewUiBinder extends UiBinder<Widget, EventView> {
    }

    @UiField
    HTMLPanel calendarContainer;

    @Override
    public void createView() {
        //don't create the view before to take advantage of the lazy loading mechanism
        initializeCalendar();
        initWidget( uiBinder.createAndBindUi( this ) );
    }

    private void initializeCalendar() {
        final FullCalendar fc = new FullCalendar("event-calendar", ViewOption.month, true);
        fc.addLoadHandler(new LoadHandler() {
            @Override
            public void onLoad(LoadEvent loadEvent) {
                addEvents();
            }

            private void addEvents() {
                for (int i = 0; i < 15; i++) {
                    Event calEvent = new Event("id "+ i, "this is event "+i);
                    int day = Random.nextInt(10);
                    Date start = new Date();
                    CalendarUtil.addDaysToDate(start, -1 * day);
                    calEvent.setStart(start);
                    if(i%3 ==0){
                        calEvent.setAllDay(true);
                    }else{
                        Date d = new Date(start.getTime());
                        d.setHours(d.getHours()+1);
                        calEvent.setEnd(d);
                    }
                    fc.addEvent(calEvent);
                }
            }

        });

        calendarContainer.add(fc);
    }


}

and I am using mvp4g framework.


Solution

  • initializeCalendar();
    initWidget( uiBinder.createAndBindUi( this ) );
    

    You should first init the widget and then initialize the calendar. This is because you are adding your FullCalendar to the calendarContainer HTMLPanel in initializeCalendar. And before the call to initWidget, calendarContainer is null.

    I think you can go a step further and do it like this:

    <!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
    <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
        xmlns:g="urn:import:com.google.gwt.user.client.ui"
        xmlns:b="urn:import:org.gwtbootstrap3.client.ui.gwt"
        xmlns:f="urn:import:org.gwtbootstrap3.extras.fullcalendar">
        <g:HTMLPanel>
            <f:FullCalendar ui:field="fullCalendar" />
        </g:HTMLPanel>
    </ui:UiBinder>
    

    And then in your EventView:

    @UiField(provided = true) FullCalendar fullCalendar;
    

    @UiField(provided = true) means it's up to you to initialize this widget before calling initWidget. So, in this case, the order:

    initializeCalendar();
    initWidget( uiBinder.createAndBindUi( this ) );
    

    is OK. Additionally, you don't have to add the FullCalendar to any panel anymore - it's already added, you just have to initialize it.