Example usage for com.google.gwt.user.client.ui MenuBar setFocusOnHoverEnabled

List of usage examples for com.google.gwt.user.client.ui MenuBar setFocusOnHoverEnabled

Introduction

In this page you can find the example usage for com.google.gwt.user.client.ui MenuBar setFocusOnHoverEnabled.

Prototype

public void setFocusOnHoverEnabled(boolean enabled) 

Source Link

Document

Enable or disable auto focus when the mouse hovers over the MenuBar.

Usage

From source file:org.unitime.timetable.gwt.client.curricula.CourseCurriculaTable.java

License:Apache License

private MenuBar createMenu(final PopupPanel popup, final boolean showHeader) {
    MenuBar menu = new MenuBar(true);
    MenuItem showHide = new MenuItem(
            CurriculumCookie.getInstance().getCurriculaCoursesDetails() ? MESSAGES.opHideDetails()
                    : MESSAGES.opShowDetails(),
            true, new Command() {
                @Override/*ww w. j  av a2  s  .c o  m*/
                public void execute() {
                    popup.hide();
                    CurriculumCookie.getInstance().setCurriculaCoursesDetails(
                            !CurriculumCookie.getInstance().getCurriculaCoursesDetails());
                    iHeader.setCollapsible(
                            showHeader ? CurriculumCookie.getInstance().getCurriculaCoursesDetails() : null);
                    if (iCurricula.getRowCount() > 2) {
                        for (int row = 1; row < iCurricula.getRowCount() - 1; row++) {
                            int rowType = iRowTypes.get(row);
                            if (CurriculumCookie.getInstance().getCurriculaCoursesDetails()
                                    && (rowType == sRowTypeCurriculum || rowType == sRowTypeOtherArea))
                                continue;
                            iCurricula.getRowFormatter().setVisible(row,
                                    CurriculumCookie.getInstance().getCurriculaCoursesDetails());
                        }
                        for (int col = 0; col < iClassifications.size() + 2; col++) {
                            iCurricula.getCellFormatter().setStyleName(iCurricula.getRowCount() - 1, col,
                                    CurriculumCookie.getInstance().getCurriculaCoursesDetails()
                                            ? "unitime-TotalRow"
                                            : null);
                        }
                    }
                }
            });
    showHide.getElement().getStyle().setCursor(Cursor.POINTER);
    menu.addItem(showHide);
    if (iCurricula.getRowCount() > 2 && CurriculumCookie.getInstance().getCurriculaCoursesDetails()) {
        boolean canExpand = false, canCollapse = false;
        for (int row = 1; row < iCurricula.getRowCount() - 1; row++) {
            int rowType = iRowTypes.get(row);
            if (rowType == sRowTypeArea || rowType == sRowTypeOther) {
                if (iCurricula.getRowFormatter().isVisible(row))
                    canExpand = true;
                else
                    canCollapse = true;
            }
        }
        if (canExpand) {
            MenuItem expandAll = new MenuItem(MESSAGES.opExpandAll(), true, new Command() {
                @Override
                public void execute() {
                    popup.hide();
                    for (int row = 1; row < iCurricula.getRowCount() - 1; row++) {
                        int rowType = iRowTypes.get(row);
                        boolean visible = (rowType != sRowTypeArea && rowType != sRowTypeOther);
                        iCurricula.getRowFormatter().setVisible(row, visible);
                        iExpandedAreas.clear();
                        iExpandedAreas.addAll(iAllAreas);
                    }
                }
            });
            expandAll.getElement().getStyle().setCursor(Cursor.POINTER);
            menu.addItem(expandAll);
        }
        if (canCollapse) {
            MenuItem collapseAll = new MenuItem(MESSAGES.opCollapseAll(), true, new Command() {
                @Override
                public void execute() {
                    popup.hide();
                    for (int row = 1; row < iCurricula.getRowCount() - 1; row++) {
                        int rowType = iRowTypes.get(row);
                        boolean visible = (rowType != sRowTypeCurriculum && rowType != sRowTypeOtherArea);
                        iCurricula.getRowFormatter().setVisible(row, visible);
                        iExpandedAreas.clear();
                    }
                }
            });
            collapseAll.getElement().getStyle().setCursor(Cursor.POINTER);
            menu.addItem(collapseAll);
        }
    }
    menu.addSeparator();
    for (final Type t : Type.values()) {
        if (!isAvailable(t))
            continue;
        MenuItem item = new MenuItem(MESSAGES.opShowEnrollmentByType(t.getName()), true, new Command() {
            @Override
            public void execute() {
                popup.hide();
                CurriculumCookie.getInstance().setCourseCurriculaTableType(t);
                iHint.setText(MESSAGES.hintEnrollmentOfType(t.getName()));
                if (iCurricula.getRowCount() > 1) {
                    for (int row = 1; row < iCurricula.getRowCount(); row++) {
                        int hc = getHeaderCols(row);
                        for (int col = 0; col <= iClassifications.size(); col++) {
                            ((MyLabel) iCurricula.getWidget(row, hc + col)).refresh();
                        }
                    }
                    //((MyLabel)iCurricula.getWidget(iCurricula.getRowCount() - 1, 1)).refresh();
                    ((Label) iCurricula.getWidget(iCurricula.getRowCount() - 1, 0))
                            .setText(MESSAGES.totalEnrollmentOfType(t.getName()));
                }
            }
        });
        if (t == CurriculumCookie.getInstance().getCourseCurriculaTableType())
            item.getElement().getStyle().setColor("#666666");
        item.getElement().getStyle().setCursor(Cursor.POINTER);
        menu.addItem(item);
    }
    menu.addSeparator();
    MenuItem populateProjectedDemands = new MenuItem(MESSAGES.opPopulateCourseProjectedDemands(), true,
            new Command() {
                @Override
                public void execute() {
                    popup.hide();
                    LoadingWidget.getInstance().show(MESSAGES.waitPopulateCourseProjectedDemands());
                    iCurriculaService.populateCourseProjectedDemands(false, iOfferingId,
                            new AsyncCallback<Boolean>() {

                                @Override
                                public void onFailure(Throwable caught) {
                                    iHeader.setErrorMessage(
                                            MESSAGES.failedPopulateCourseProjectedDemands(caught.getMessage()));
                                    UniTimeNotifications.error(
                                            MESSAGES.failedPopulateCourseProjectedDemands(caught.getMessage()),
                                            caught);
                                    LoadingWidget.getInstance().hide();
                                }

                                @Override
                                public void onSuccess(Boolean result) {
                                    ToolBox.open(GWT.getHostPageBaseURL() + "instructionalOfferingDetail.do?io="
                                            + iOfferingId);
                                }

                            });
                }
            });
    populateProjectedDemands.getElement().getStyle().setCursor(Cursor.POINTER);
    menu.addItem(populateProjectedDemands);
    MenuItem populateProjectedDemands2 = new MenuItem(MESSAGES.opPopulateCourseProjectedDemandsIncludeOther(),
            true, new Command() {
                @Override
                public void execute() {
                    popup.hide();
                    LoadingWidget.getInstance().show(MESSAGES.waitPopulateCourseProjectedDemands());
                    iCurriculaService.populateCourseProjectedDemands(true, iOfferingId,
                            new AsyncCallback<Boolean>() {
                                @Override
                                public void onFailure(Throwable caught) {
                                    iHeader.setErrorMessage(
                                            MESSAGES.failedPopulateCourseProjectedDemands(caught.getMessage()));
                                    UniTimeNotifications.error(
                                            MESSAGES.failedPopulateCourseProjectedDemands(caught.getMessage()),
                                            caught);
                                    LoadingWidget.getInstance().hide();
                                }

                                @Override
                                public void onSuccess(Boolean result) {
                                    ToolBox.open(GWT.getHostPageBaseURL() + "instructionalOfferingDetail.do?io="
                                            + iOfferingId);
                                }

                            });
                }
            });
    populateProjectedDemands2.getElement().getStyle().setCursor(Cursor.POINTER);
    menu.addItem(populateProjectedDemands2);
    menu.setVisible(true);
    menu.setFocusOnHoverEnabled(true);
    return menu;
}

From source file:org.unitime.timetable.gwt.client.events.AddMeetingsDialog.java

License:Apache License

public AddMeetingsDialog(AcademicSessionProvider session, EventPropertiesProvider propeties,
        AsyncCallback<List<MeetingInterface>> callback) {
    super(true, true);
    setAnimationEnabled(false);/*from w  w w.  ja v a2s.c  om*/

    iCallback = callback;
    iSession = session;
    iProperties = propeties;

    setText(MESSAGES.dialogAddMeetings());
    setEscapeToHide(true);

    iDatesForm = new SimpleForm();

    iDatesHeader = new UniTimeHeaderPanel();
    iDatesHeader.setRotateFocus(true);
    iDatesHeader.addButton("next", MESSAGES.buttonNext(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            if (iDates.getSelectedDaysCount() == 0) {
                iDatesHeader.setErrorMessage(MESSAGES.errorNoDateSelected());
                AriaStatus.getInstance().setText(MESSAGES.errorNoDateSelected());
                return;
            } else if (getStartSlot() == null) {
                iDatesHeader.setErrorMessage(MESSAGES.errorNoStartTime());
                AriaStatus.getInstance().setText(MESSAGES.errorNoStartTime());
                return;
            } else if (getEndSlot() == null) {
                iDatesHeader.setErrorMessage(MESSAGES.errorNoEndTime());
                AriaStatus.getInstance().setText(MESSAGES.errorNoEndTime());
                return;
            }
            LoadingWidget.getInstance().show(MESSAGES.waitCheckingRoomAvailability());
            iRooms.getElements(new AsyncCallback<List<Entity>>() {
                @Override
                public void onFailure(Throwable caught) {
                    LoadingWidget.getInstance().hide();
                    iDatesHeader.setErrorMessage(caught.getMessage());
                    AriaStatus.getInstance().setText(caught.getMessage());
                }

                @Override
                public void onSuccess(List<Entity> result) {
                    iMatchingRooms = result;
                    if (result == null || result.isEmpty()) {
                        LoadingWidget.getInstance().hide();
                        iDatesHeader.setErrorMessage(MESSAGES.errorNoMatchingRooms());
                        AriaStatus.getInstance().setText(MESSAGES.errorNoMatchingRooms());
                    } else if (iDates.getSelectedDaysCount() > 0) {
                        iDatesHeader.clearMessage();
                        if (iProperties != null && iProperties.isTooEarly(getStartSlot(), getEndSlot())) {
                            iAvailabilityHeader.setWarningMessage(
                                    MESSAGES.warnMeetingTooEarly(TimeUtils.slot2time(getStartSlot()) + " - "
                                            + TimeUtils.slot2time(getEndSlot())));
                            AriaStatus.getInstance()
                                    .setText(MESSAGES.warnMeetingTooEarly(TimeUtils.slot2aria(getStartSlot())
                                            + " - " + TimeUtils.slot2aria(getEndSlot())));
                        } else {
                            iAvailabilityHeader.clearMessage();
                        }
                        RPC.execute(
                                EventRoomAvailabilityRpcRequest.checkAvailability(getStartSlot(), getEndSlot(),
                                        getDates(), getRooms(), iEventId, iSession.getAcademicSessionId()),
                                new AsyncCallback<EventRoomAvailabilityRpcResponse>() {
                                    @Override
                                    public void onFailure(Throwable caught) {
                                        LoadingWidget.getInstance().hide();
                                        iDatesHeader.setErrorMessage(caught.getMessage());
                                        AriaStatus.getInstance().setText(caught.getMessage());
                                    }

                                    @Override
                                    public void onSuccess(EventRoomAvailabilityRpcResponse result) {
                                        LoadingWidget.getInstance().hide();
                                        iResponse = result;
                                        populate(0, EventCookie.getInstance().getRoomsSortBy());
                                        setWidget(iAvailabilityForm);
                                        recenter();
                                        iText.setFocus(true);
                                    }
                                });
                    }
                }
            });
        }
    });

    iDatesForm.addHeaderRow(iDatesHeader);

    SimpleForm form = new SimpleForm();
    form.removeStyleName("unitime-NotPrintableBottomLine");

    iDates = new SessionDatesSelector(session);
    form.addRow(MESSAGES.propDates(), iDates);

    iTimes = new StartEndTimeSelector();
    form.addRow(MESSAGES.propTimes(), iTimes);

    iRooms = new RoomFilterBox(session);
    form.addRow(MESSAGES.propLocations(), iRooms);

    iScrollDates = new ScrollPanel(form);
    iScrollDates.setStyleName("unitime-VerticalScrollPanel");

    iDatesForm.addRow(iScrollDates);

    iDatesForm.addBottomRow(iDatesHeader.clonePanel());

    iAvailabilityForm = new SimpleForm();

    iAvailabilityHeader = new UniTimeHeaderPanel();
    iAvailabilityHeader.setRotateFocus(true);

    iAvailabilityHeader.addButton("dates", MESSAGES.buttonPrevious(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            setWidget(iDatesForm);
            recenter();
            iResponse = null;
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                @Override
                public void execute() {
                    iDates.setFocus(true);
                }
            });
        }
    });
    iAvailabilityHeader.addButton("prev", MESSAGES.buttonLeft(), new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            populate(iIndex - iStep, null);
            recenter();
        }
    });
    iAvailabilityHeader.addButton("next", MESSAGES.buttonRight(), new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            populate(iIndex + iStep, null);
            recenter();
        }
    });
    iAvailabilityHeader.addButton("more", MESSAGES.buttonMoreOperations(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            final PopupPanel popup = new PopupPanel(true);
            MenuBar menu = new MenuBar(true);
            for (final SortRoomsBy sortBy : SortRoomsBy.values()) {
                if (sortBy == SortRoomsBy.DISTANCE && !iRooms.hasChip(new Chip("flag", "Nearby")))
                    continue;
                MenuItem item = new MenuItem(
                        (sortBy.ordinal() == EventCookie.getInstance().getRoomsSortBy() ? "&uarr; "
                                : (sortBy.ordinal() + SortRoomsBy.values().length == EventCookie.getInstance()
                                        .getRoomsSortBy()) ? "&darr; " : "")
                                + MESSAGES.opSortBy(getSortRoomsByName(sortBy)),
                        true, new Command() {
                            @Override
                            public void execute() {
                                popup.hide();
                                if (sortBy.ordinal() == EventCookie.getInstance().getRoomsSortBy()) {
                                    EventCookie.getInstance()
                                            .setSortRoomsBy(SortRoomsBy.values().length + sortBy.ordinal());
                                } else {
                                    EventCookie.getInstance().setSortRoomsBy(sortBy.ordinal());
                                }
                                populate(0, EventCookie.getInstance().getRoomsSortBy());
                                recenter();
                            }
                        });
                item.getElement().getStyle().setCursor(Cursor.POINTER);
                menu.addItem(item);
            }
            menu.addSeparator();
            MenuItem swapItem = new MenuItem(MESSAGES.opSwapAxes(), true, new Command() {
                @Override
                public void execute() {
                    popup.hide();
                    EventCookie.getInstance()
                            .setRoomsHorizontal(!EventCookie.getInstance().areRoomsHorizontal());
                    populate(0, EventCookie.getInstance().getRoomsSortBy());
                    recenter();
                }
            });
            swapItem.getElement().getStyle().setCursor(Cursor.POINTER);
            menu.addItem(swapItem);
            MenuItem expandOrCollapseAll = new MenuItem(
                    EventCookie.getInstance().isExpandRoomConflicts() ? MESSAGES.opCollapseAll()
                            : MESSAGES.opExpandAll(),
                    true, new Command() {
                        @Override
                        public void execute() {
                            popup.hide();
                            EventCookie.getInstance()
                                    .setExpandRoomConflicts(!EventCookie.getInstance().isExpandRoomConflicts());
                            populate(iIndex, EventCookie.getInstance().getRoomsSortBy());
                            recenter();
                        }
                    });
            expandOrCollapseAll.getElement().getStyle().setCursor(Cursor.POINTER);
            menu.addItem(expandOrCollapseAll);
            menu.setVisible(true);
            menu.setFocusOnHoverEnabled(true);
            popup.add(menu);
            popup.showRelativeTo((UIObject) event.getSource());
            if (event.getSource() instanceof Focusable) {
                final Focusable focusable = (Focusable) event.getSource();
                popup.addCloseHandler(new CloseHandler<PopupPanel>() {
                    @Override
                    public void onClose(CloseEvent<PopupPanel> event) {
                        focusable.setFocus(true);
                    }
                });
            }
            menu.focus();
        }
    });
    iAvailabilityHeader.addButton("select", MESSAGES.buttonSelect(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            hide();
            List<MeetingInterface> meetings = new ArrayList<MeetingInterface>();

            for (Integer date : getDates()) {
                for (Entity room : getRooms()) {
                    if (isSelected(date, room)) {
                        MeetingInterface meeting = new MeetingInterface();

                        meeting.setDayOfYear(date);
                        meeting.setStartSlot(getStartSlot());
                        meeting.setEndSlot(getEndSlot());
                        meeting.setStartOffset(0);
                        meeting.setEndOffset(-Integer.parseInt(room.getProperty("breakTime", "0")));

                        meeting.setLocation(new ResourceInterface(room));

                        meeting.setConflicts(getConflicts(date, room));

                        meetings.add(meeting);
                    }
                }
            }

            iCallback.onSuccess(meetings);
        }
    });

    iAvailabilityForm.addHeaderRow(iAvailabilityHeader);

    iRoomAvailability = new P("unitime-MeetingSelection");

    iText = new AriaTextBox();
    iText.setStyleName("text");

    iScrollRooms = new ScrollPanel(iRoomAvailability);

    iAvailabilityForm.addRow(iScrollRooms);

    iAvailabilityForm.addNotPrintableBottomRow(iAvailabilityHeader.clonePanel());

    setWidget(iDatesForm);

    addCloseHandler(new CloseHandler<PopupPanel>() {
        @Override
        public void onClose(CloseEvent<PopupPanel> event) {
            RootPanel.getBodyElement().getStyle().setOverflow(Overflow.AUTO);
        }
    });

    iText.addKeyUpHandler(new KeyUpHandler() {
        @Override
        public void onKeyUp(KeyUpEvent event) {
            if (iResponse == null)
                return;
            if (EventCookie.getInstance().areRoomsHorizontal()) {
                int min = iIndex, max = Math.min(iIndex + iStep, getRooms().size()) - 1;
                switch (event.getNativeKeyCode()) {
                case KeyCodes.KEY_DOWN:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(0, getDates().indexOf(iHoverDate) + 1),
                                getDates().size() - 1);
                        iHoverDate = getDates().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(0);
                        iHoverLoc = getRooms().get(min);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    iScrollRooms.ensureVisible(iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()));
                    break;
                case KeyCodes.KEY_UP:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(0, getDates().indexOf(iHoverDate) - 1),
                                getDates().size() - 1);
                        iHoverDate = getDates().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(getDates().size() - 1);
                        iHoverLoc = getRooms().get(min);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    iScrollDates.ensureVisible(iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()));
                    break;
                case KeyCodes.KEY_RIGHT:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(min, getRooms().indexOf(iHoverLoc) + 1), max);
                        iHoverLoc = getRooms().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(0);
                        iHoverLoc = getRooms().get(min);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    break;
                case KeyCodes.KEY_LEFT:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(min, getRooms().indexOf(iHoverLoc) - 1), max);
                        iHoverLoc = getRooms().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(0);
                        iHoverLoc = getRooms().get(max);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    break;
                case KeyCodes.KEY_PAGEDOWN:
                    if (iIndex + iStep < getRooms().size())
                        populate(iIndex + iStep, null);
                    break;
                case KeyCodes.KEY_PAGEUP:
                    if (iIndex > 0)
                        populate(iIndex - iStep, null);
                    break;
                case 32:
                case KeyCodes.KEY_ENTER:
                    if (iHoverDate != null && iHoverLoc != null) {
                        setSelected(iHoverDate, iHoverLoc, !isSelected(iHoverDate, iHoverLoc));
                    }
                    break;
                }
            } else {
                int min = iIndex, max = Math.min(iIndex + iStep, getDates().size()) - 1;
                switch (event.getNativeKeyCode()) {
                case KeyCodes.KEY_RIGHT:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(min, getDates().indexOf(iHoverDate) + 1), max);
                        iHoverDate = getDates().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(min);
                        iHoverLoc = getRooms().get(0);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    iScrollRooms.ensureVisible(iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()));
                    break;
                case KeyCodes.KEY_LEFT:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(min, getDates().indexOf(iHoverDate) - 1), max);
                        iHoverDate = getDates().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(max);
                        iHoverLoc = getRooms().get(0);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    iScrollDates.ensureVisible(iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()));
                    break;
                case KeyCodes.KEY_DOWN:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(0, getRooms().indexOf(iHoverLoc) + 1),
                                getRooms().size() - 1);
                        iHoverLoc = getRooms().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(min);
                        iHoverLoc = getRooms().get(0);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    break;
                case KeyCodes.KEY_UP:
                    if (iHoverDate != null && iHoverLoc != null) {
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
                        int idx = Math.min(Math.max(0, getRooms().indexOf(iHoverLoc) - 1),
                                getRooms().size() - 1);
                        iHoverLoc = getRooms().get(idx);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    } else {
                        iHoverDate = getDates().get(min);
                        iHoverLoc = getRooms().get(getRooms().size() - 1);
                        iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
                    }
                    break;
                case KeyCodes.KEY_PAGEDOWN:
                    if (iIndex + iStep < getDates().size())
                        populate(iIndex + iStep, null);
                    break;
                case KeyCodes.KEY_PAGEUP:
                    if (iIndex > 0)
                        populate(iIndex - iStep, null);
                    break;
                case 32:
                case KeyCodes.KEY_ENTER:
                    if (iHoverDate != null && iHoverLoc != null) {
                        setSelected(iHoverDate, iHoverLoc, !isSelected(iHoverDate, iHoverLoc));
                    }
                    break;
                }
            }
            iText.setAriaLabel(toAriaLabel(true, true, false));
        }
    });
    iText.addKeyDownHandler(new KeyDownHandler() {
        @Override
        public void onKeyDown(KeyDownEvent event) {
            switch (event.getNativeEvent().getKeyCode()) {
            case KeyCodes.KEY_RIGHT:
            case KeyCodes.KEY_LEFT:
            case KeyCodes.KEY_UP:
            case KeyCodes.KEY_DOWN:
            case KeyCodes.KEY_PAGEDOWN:
            case KeyCodes.KEY_PAGEUP:
                event.preventDefault();
            }
        }
    });
    iText.addKeyPressHandler(new KeyPressHandler() {
        @Override
        public void onKeyPress(KeyPressEvent event) {
            switch (event.getNativeEvent().getKeyCode()) {
            case 32:
                if (iText.getValue().isEmpty())
                    event.preventDefault();
                return;
            case KeyCodes.KEY_ENTER:
                event.preventDefault();
                return;
            }
        }
    });
    iText.addFocusHandler(new FocusHandler() {
        @Override
        public void onFocus(FocusEvent event) {
            if (iHoverDate != null && iHoverLoc != null)
                iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).addStyleName("hover");
            iText.setAriaLabel(toAriaLabel(true, true, false));
        }
    });
    iText.addBlurHandler(new BlurHandler() {
        @Override
        public void onBlur(BlurEvent event) {
            if (iHoverDate != null && iHoverLoc != null) {
                iPanels.get(iHoverDate + ":" + iHoverLoc.getUniqueId()).removeStyleName("hover");
            }
        }
    });
}

From source file:org.unitime.timetable.gwt.client.events.EventRoomAvailability.java

License:Apache License

public EventRoomAvailability() {
    iHistoryToken = new HistoryToken(PageType.Availability);

    iFilter = new SimpleForm(2);
    iFilter.removeStyleName("unitime-NotPrintableBottomLine");
    iFilter.getColumnFormatter().setWidth(0, "120px");

    iFilterHeader = new UniTimeHeaderPanel(MESSAGES.sectFilter());
    iFilterHeader.addButton("add", MESSAGES.buttonAddEvent(), 75, new ClickHandler() {
        @Override//from  w w  w  . java 2  s.c  o  m
        public void onClick(ClickEvent event) {
            iEventAdd.setEvent(null);
            iEventAdd.show();
        }
    });
    iFilterHeader.setEnabled("add", false);
    iFilter.addHeaderRow(iFilterHeader);
    iFilterHeader.addButton("clear", MESSAGES.buttonClear(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            iDates.setValue(null);
            iTimes.setValue(null);
            iRooms.setValue(iHistoryToken.getDefaultParameter("rooms", ""), true);
            hideResults();
            changeUrl();
        }
    });
    iFilterHeader.addButton("search", MESSAGES.buttonSearch(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            reload(true);
        }
    });

    iSession = new AcademicSessionSelectionBox(iHistoryToken.getParameter("term"), "Events") {
        @Override
        protected void onInitializationSuccess(List<AcademicSession> sessions) {
            iFilter.setVisible(sessions != null && !sessions.isEmpty());
            UniTimePageHeader.getInstance().getRight().setVisible(false);
            UniTimePageHeader.getInstance().getRight().setPreventDefault(true);
        }

        @Override
        protected void onInitializationFailure(Throwable caught) {
            UniTimeNotifications.error(MESSAGES.failedLoadSessions(caught.getMessage()), caught);
        }
    };
    iSession.setFilter(this);
    iSessionRow = iFilter.addRow(MESSAGES.propAcademicSession(), iSession);

    iDates = new SessionDatesSelector(iSession);
    iDates.setCanSelectPast(true);
    ToolBox.setMaxWidth(iDates.getElement().getStyle(),
            Math.round(0.9 * ToolBox.getClientWidth() - 120) + "px");
    iFilter.addRow(MESSAGES.propDates(), iDates);

    iTimes = new StartEndTimeSelector();
    iTimes.setDiff(12);
    iFilter.addRow(MESSAGES.propTimes(), iTimes);

    iRooms = new RoomFilterBox(iSession);
    iFilter.addRow(MESSAGES.propLocations(), iRooms);

    iPanel = new SimpleForm(2);
    iPanel.removeStyleName("unitime-NotPrintableBottomLine");
    iPanel.addRow(iFilter);
    iHeader = new UniTimeHeaderPanel();
    iHeader.addButton("add", MESSAGES.buttonAddEvent(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            iEventAdd.setEvent(null);
            iEventAdd.show();
        }
    });
    iHeader.setEnabled("add", false);
    iHeader.addButton("print", MESSAGES.buttonPrint(), 75, new ClickHandler() {
        @Override
        public void onClick(ClickEvent clickEvent) {
            HashMap<Long, String> colors = new HashMap<Long, String>();
            List<Page> pages = new ArrayList<Page>();
            if (iSelectedDates.size() == 1) {
                final Date date = iDates.getDate(iSelectedDates.get(0));
                int year = Integer.parseInt(DateTimeFormat.getFormat("yyyy").format(date));
                int month = Integer.parseInt(DateTimeFormat.getFormat("MM").format(date));
                int dow = (SingleDateSelector.firstDayOfWeek(year, month)
                        + Integer.parseInt(DateTimeFormat.getFormat("dd").format(date)) - 1) % 7;
                int days[] = new int[] { dow };
                WeekInterface week = new WeekInterface();
                week.setDayOfYear(iSelectedDates.get(0) - dow);
                for (int i = 0; i < 7; i++) {
                    week.addDayName(
                            new DateInterface(sDateFormat.format(iDates.getDate(week.getDayOfYear() + i)),
                                    month, week.getDayOfYear() + i));
                }
                List<WeekInterface> weeks = new ArrayList<WeekInterface>();
                weeks.add(week);
                boolean past = !iDates.isEnabled(date) || iDates.hasFlag(date, Flag.PAST);
                for (int i = 0; i < iSelectedDates.size(); i++) {
                    int startHour = 7;
                    if (iSelectedTimes.getStart() != null) {
                        startHour = Math.max(0, (iSelectedTimes.getStart() - 6) / 12);
                    }
                    int endHour = 18;
                    if (iSelectedTimes.getEnd() != null) {
                        endHour = Math.min(24, (17 + iSelectedTimes.getEnd()) / 12);
                    }

                    final TimeGrid grid = new TimeGrid(colors, days, (int) (1000 / days.length), 55, true,
                            false, startHour, endHour, EventRoomAvailability.this);
                    grid.setResourceType(ResourceType.ROOM);
                    grid.setSelectedWeeks(weeks);
                    List<ResourceInterface> rooms = new ArrayList<EventInterface.ResourceInterface>(
                            iSelectedRooms);
                    grid.setRoomResources(rooms);
                    grid.setMode(TimeGrid.Mode.OVERLAP);
                    for (EventInterface event : sortedEvents()) {
                        List<MeetingInterface> meetings = new ArrayList<MeetingInterface>();
                        for (MeetingInterface meeting : event.getMeetings()) {
                            if (meeting.getApprovalStatus() != ApprovalStatus.Pending
                                    && meeting.getApprovalStatus() != ApprovalStatus.Approved)
                                continue;
                            if (iSelectedDates.get(i) == meeting.getDayOfYear() && meeting.hasLocation())
                                meetings.add(meeting);
                        }
                        if (!meetings.isEmpty())
                            grid.addEvent(event, meetings);
                    }
                    grid.labelDays(week, null);
                    grid.yellow(iSelectedTimes.getStart() == null ? 90 : iSelectedTimes.getStart(),
                            iSelectedTimes.getEnd() == null ? 210 : iSelectedTimes.getEnd());
                    if (past)
                        grid.gray(0, 1);
                    grid.showVerticalSplit();
                    grid.addMeetingClickHandler(iMeetingClickHandler);
                    pages.add(new Page() {
                        @Override
                        public String getName() {
                            return DateTimeFormat.getFormat(CONSTANTS.meetingDateFormat()).format(date);
                        }

                        @Override
                        public String getUser() {
                            return "";
                        }

                        @Override
                        public String getSession() {
                            return "";
                        }

                        @Override
                        public Element getBody() {
                            return grid.getElement();
                        }
                    });
                }
            } else {
                int[] days = new int[iSelectedDates.size()];
                WeekInterface week = new WeekInterface();
                week.setDayOfYear(iSelectedDates.get(0));
                List<String> dows = new ArrayList<String>();
                int lastPast = -1;
                for (int i = 0; i < iSelectedDates.size(); i++) {
                    Date date = iDates.getDate(iSelectedDates.get(i));
                    int year = Integer.parseInt(DateTimeFormat.getFormat("yyyy").format(date));
                    int month = Integer.parseInt(DateTimeFormat.getFormat("MM").format(date));
                    int dow = (SingleDateSelector.firstDayOfWeek(year, month)
                            + Integer.parseInt(DateTimeFormat.getFormat("dd").format(date)) - 1) % 7;
                    days[i] = i;
                    week.addDayName(new DateInterface(sDateFormat.format(date), month, iSelectedDates.get(i)));
                    dows.add(CONSTANTS.days()[dow]);
                    if (!iDates.isEnabled(date) || iDates.hasFlag(date, Flag.PAST))
                        lastPast = i;
                }
                List<WeekInterface> weeks = new ArrayList<WeekInterface>();
                weeks.add(week);
                for (final ResourceInterface room : iSelectedRooms) {
                    int startHour = 7;
                    if (iSelectedTimes.getStart() != null) {
                        startHour = Math.max(0, (iSelectedTimes.getStart() - 6) / 12);
                    }
                    int endHour = 18;
                    if (iSelectedTimes.getEnd() != null) {
                        endHour = Math.min(24, (17 + iSelectedTimes.getEnd()) / 12);
                    }

                    final TimeGrid grid = new TimeGrid(colors, days, (int) (1000 / days.length), 55, true,
                            false, startHour, endHour, EventRoomAvailability.this);
                    grid.setResourceType(ResourceType.ROOM);
                    grid.setSelectedWeeks(weeks);
                    List<ResourceInterface> rooms = new ArrayList<EventInterface.ResourceInterface>();
                    rooms.add(room);
                    grid.setRoomResources(rooms);
                    grid.setMode(TimeGrid.Mode.OVERLAP);
                    for (EventInterface event : sortedEvents()) {
                        List<MeetingInterface> meetings = new ArrayList<MeetingInterface>();
                        for (MeetingInterface meeting : event.getMeetings()) {
                            if (meeting.getApprovalStatus() != ApprovalStatus.Pending
                                    && meeting.getApprovalStatus() != ApprovalStatus.Approved)
                                continue;
                            if (meeting.getMeetingDate() != null && meeting.hasLocation()
                                    && meeting.getLocation().getId().equals(room.getId())) {
                                int idx = iSelectedDates.indexOf(meeting.getDayOfYear());
                                if (idx >= 0) {
                                    meeting.setGridIndex(idx);
                                    meetings.add(meeting);
                                }
                            }
                        }
                        if (!meetings.isEmpty())
                            grid.addEvent(event, meetings);
                    }
                    grid.labelDays(dows, week);
                    grid.yellow(iSelectedTimes.getStart() == null ? 90 : iSelectedTimes.getStart(),
                            iSelectedTimes.getEnd() == null ? 210 : iSelectedTimes.getEnd());
                    if (lastPast >= 0)
                        grid.gray(0, lastPast);
                    grid.addMeetingClickHandler(iMeetingClickHandler);
                    pages.add(new Page() {
                        @Override
                        public String getName() {
                            return room.getName();
                        }

                        @Override
                        public String getUser() {
                            return room.getRoomType();
                        }

                        @Override
                        public String getSession() {
                            return (room.hasSize() ? MESSAGES.hintRoomCapacity(room.getSize().toString()) : "");
                        }

                        @Override
                        public Element getBody() {
                            return grid.getElement();
                        }

                    });
                }
            }
            ToolBox.print(pages);
        }
    });
    iHeader.addButton("sort", MESSAGES.buttonSortBy(), new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            final PopupPanel popup = new PopupPanel(true);
            MenuBar menu = new MenuBar(true);
            for (final SortRoomsBy sortBy : SortRoomsBy.values()) {
                if (sortBy == SortRoomsBy.DISTANCE && !iRooms.hasChip(new Chip("flag", "Nearby")))
                    continue;
                MenuItem item = new MenuItem(
                        (sortBy.ordinal() == EventCookie.getInstance().getRoomsSortBy() ? "&uarr; "
                                : (sortBy.ordinal() + SortRoomsBy.values().length == EventCookie.getInstance()
                                        .getRoomsSortBy()) ? "&darr; " : "")
                                + AddMeetingsDialog.getSortRoomsByName(sortBy),
                        true, new Command() {
                            @Override
                            public void execute() {
                                popup.hide();
                                if (sortBy.ordinal() == EventCookie.getInstance().getRoomsSortBy()) {
                                    EventCookie.getInstance()
                                            .setSortRoomsBy(SortRoomsBy.values().length + sortBy.ordinal());
                                } else {
                                    EventCookie.getInstance().setSortRoomsBy(sortBy.ordinal());
                                }
                                populate(iData, EventCookie.getInstance().getRoomsSortBy());
                            }
                        });
                item.getElement().getStyle().setCursor(Cursor.POINTER);
                menu.addItem(item);
            }
            menu.setVisible(true);
            menu.setFocusOnHoverEnabled(true);
            popup.add(menu);
            popup.showRelativeTo((UIObject) event.getSource());
            ((MenuBar) popup.getWidget()).focus();
        }
    });
    iPanel.addHeaderRow(iHeader);

    iTables = new VerticalPanel();
    iPanel.addRow(iTables);

    iFooter = iHeader.clonePanel();
    iPanel.addRow(iFooter);
    iRootPanel = new SimplePanel(iPanel);
    initWidget(iRootPanel);

    hideResults();

    iSession.addAcademicSessionChangeHandler(new AcademicSessionChangeHandler() {
        @Override
        public void onAcademicSessionChange(AcademicSessionChangeEvent event) {
            loadProperties(new AsyncCallback<EventPropertiesRpcResponse>() {
                @Override
                public void onFailure(Throwable caught) {
                }

                @Override
                public void onSuccess(EventPropertiesRpcResponse result) {
                    if (!iInitialized)
                        setup(true);
                    else
                        reload(false);
                }
            });
        }
    });

    iEventDetail = new EventDetail(this) {
        @Override
        protected void onHide() {
            iRootPanel.setWidget(iPanel);
            UniTimePageLabel.getInstance().setPageName(MESSAGES.pageEventRoomAvailability());
            if (!isShowingResults())
                reload(true);
            changeUrl();
        }

        @Override
        protected void onShow() {
            iRootPanel.setWidget(iEventDetail);
            changeUrl();
        }

        @Override
        protected void edit() {
            hide();
            iEventAdd.setEvent(getEvent());
            iEventAdd.show();
        }

        @Override
        protected EventInterface getPrevious(Long eventId) {
            return null;
        }

        @Override
        protected EventInterface getNext(Long eventId) {
            return null;
        }

        @Override
        protected void previous(final EventInterface event) {
            LoadingWidget.execute(
                    EventDetailRpcRequest.requestEventDetails(iSession.getAcademicSessionId(), event.getId()),
                    new AsyncCallback<EventInterface>() {
                        @Override
                        public void onFailure(Throwable caught) {
                            UniTimeNotifications
                                    .error(MESSAGES.failedLoad(event.getName(), caught.getMessage()), caught);
                        }

                        @Override
                        public void onSuccess(EventInterface result) {
                            LoadingWidget.getInstance().hide();
                            setEvent(result);
                            changeUrl();
                        }
                    }, MESSAGES.waitLoading(event.getName()));
        }

        @Override
        protected void next(final EventInterface event) {
            previous(event);
        }

        @Override
        public void hide() {
            super.hide();
        }

        @Override
        protected void onApprovalOrReject(Long eventId, EventInterface event) {
            if (iData != null)
                populate(tinker(new GwtRpcResponseList<EventInterface>(iData), eventId, event), null);
        }
    };

    iEventAdd = new EventAdd(iSession, this) {
        @Override
        protected void onHide() {
            iSession.setFilter(EventRoomAvailability.this);
            iFilter.setWidget(iSessionRow, 1, iSession);
            final EventInterface modified = iEventAdd.getEvent(), detail = iEventDetail.getEvent(),
                    saved = iEventAdd.getSavedEvent();
            if (saved != null) {
                if (iData != null)
                    populate(tinker(new GwtRpcResponseList<EventInterface>(iData),
                            (saved.getId() == null ? modified.getId() : saved.getId()), saved), null);
                if (saved.getId() != null) {
                    iEventDetail.setEvent(saved);
                    iEventDetail.show();
                } else {
                    iRootPanel.setWidget(iPanel);
                    UniTimePageLabel.getInstance().setPageName(MESSAGES.pageEventRoomAvailability());
                }
            } else if (modified != null && detail != null && detail.getId().equals(modified.getId())) {
                LoadingWidget.execute(EventDetailRpcRequest.requestEventDetails(iSession.getAcademicSessionId(),
                        modified.getId()), new AsyncCallback<EventInterface>() {
                            @Override
                            public void onFailure(Throwable caught) {
                                UniTimeNotifications.error(
                                        MESSAGES.failedLoad(detail.getName(), caught.getMessage()), caught);
                            }

                            @Override
                            public void onSuccess(EventInterface result) {
                                LoadingWidget.getInstance().hide();
                                iEventDetail.setEvent(result);
                                iEventDetail.show();
                            }
                        }, MESSAGES.waitLoading(detail.getName()));
            } else {
                iRootPanel.setWidget(iPanel);
                UniTimePageLabel.getInstance().setPageName(MESSAGES.pageEventRoomAvailability());
            }
            changeUrl();
        }

        @Override
        protected void onShow() {
            iRootPanel.setWidget(iEventAdd);
            changeUrl();
        }
    };

    iMeetingClickHandler = new MeetingClickHandler() {
        @Override
        public void onMeetingClick(final MeetingClickEvent event) {
            if (!event.getEvent().isCanView())
                return;
            LoadingWidget.execute(EventDetailRpcRequest.requestEventDetails(iSession.getAcademicSessionId(),
                    event.getEvent().getId()), new AsyncCallback<EventInterface>() {
                        @Override
                        public void onFailure(Throwable caught) {
                            UniTimeNotifications.error(
                                    MESSAGES.failedLoad(event.getEvent().getName(), caught.getMessage()),
                                    caught);
                        }

                        @Override
                        public void onSuccess(EventInterface result) {
                            LoadingWidget.getInstance().hide();
                            iEventDetail.setEvent(result);
                            iEventDetail.show();
                        }
                    }, MESSAGES.waitLoading(event.getEvent().getName()));
        }
    };

    History.addValueChangeHandler(new ValueChangeHandler<String>() {
        @Override
        public void onValueChange(ValueChangeEvent<String> event) {
            if (iInitialized) {
                iHistoryToken.reset(event.getValue());
                setup(false);
                if (LoadingWidget.getInstance().isShowing())
                    LoadingWidget.getInstance().hide();
            }
        }
    });
}