Search in sources :

Example 1 with CloseListener

use of com.vaadin.ui.Window.CloseListener in project VaadinUtils by rlsutton1.

the class DateTimePickerInline method getChangeYearClickListener.

private ClickListener getChangeYearClickListener() {
    return new ClickListener() {

        /**
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(ClickEvent event) {
            final Window window = new Window();
            GridLayout yearGrid = new GridLayout(5, 20);
            int currentYear = DateTime.now().getYear();
            int endYear = DateTime.now().getYear() + 10;
            int startYear = (endYear - 135);
            for (int year = endYear; year > startYear; year--) {
                final int buttonYear = year;
                Button yearButton = new Button("" + year);
                yearButton.setStyleName(ValoTheme.BUTTON_TINY);
                if (year == currentYear) {
                    yearButton.addStyleName(ValoTheme.BUTTON_FRIENDLY);
                }
                yearButton.addClickListener(new ClickListener() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void buttonClick(ClickEvent event) {
                        setValue(new DateTime(getValue().getTime()).withYear(buttonYear).toDate());
                        window.close();
                    }
                });
                yearGrid.addComponent(yearButton);
            }
            Panel scrollPanel = new Panel();
            VerticalLayout container = new VerticalLayout();
            container.addComponent(new Label("Select a year"));
            container.setMargin(true);
            container.addComponent(scrollPanel);
            scrollPanel.setContent(yearGrid);
            scrollPanel.setHeight("200");
            window.setResizable(false);
            window.setContent(container);
            // position window over year button
            int x = event.getClientX();
            int y = event.getClientY();
            int wh = UI.getCurrent().getPage().getBrowserWindowHeight();
            y = Math.max(0, y);
            int heightOfDatePicker = 300;
            y = Math.min(y, wh - heightOfDatePicker);
            int ww = UI.getCurrent().getPage().getBrowserWindowWidth();
            x = Math.max(0, x - 50);
            int widthOfDatePicker = 290;
            x = Math.min(x, ww - widthOfDatePicker);
            // window.setModal(true);
            window.setPosition(x, y);
            window.setClosable(true);
            UI.getCurrent().addWindow(window);
            // add mouse click listener to close window when the user
            // clicks outside of the window
            final MouseEvents.ClickListener listener = new MouseEvents.ClickListener() {

                private static final long serialVersionUID = 1L;

                @Override
                public void click(MouseEvents.ClickEvent event) {
                    window.close();
                }
            };
            UI.getCurrent().addClickListener(listener);
            // tidy up: remove mouse click listener
            window.addCloseListener(new CloseListener() {

                private static final long serialVersionUID = 1L;

                @Override
                public void windowClose(CloseEvent e) {
                    UI.getCurrent().removeClickListener(listener);
                }
            });
        }
    };
}
Also used : Window(com.vaadin.ui.Window) CloseEvent(com.vaadin.ui.Window.CloseEvent) ClickEvent(com.vaadin.ui.Button.ClickEvent) Label(com.vaadin.ui.Label) DateTime(org.joda.time.DateTime) MouseEvents(com.vaadin.event.MouseEvents) Panel(com.vaadin.ui.Panel) GridLayout(com.vaadin.ui.GridLayout) Button(com.vaadin.ui.Button) CloseListener(com.vaadin.ui.Window.CloseListener) VerticalLayout(com.vaadin.ui.VerticalLayout) ClickListener(com.vaadin.ui.Button.ClickListener)

Aggregations

MouseEvents (com.vaadin.event.MouseEvents)1 Button (com.vaadin.ui.Button)1 ClickEvent (com.vaadin.ui.Button.ClickEvent)1 ClickListener (com.vaadin.ui.Button.ClickListener)1 GridLayout (com.vaadin.ui.GridLayout)1 Label (com.vaadin.ui.Label)1 Panel (com.vaadin.ui.Panel)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 Window (com.vaadin.ui.Window)1 CloseEvent (com.vaadin.ui.Window.CloseEvent)1 CloseListener (com.vaadin.ui.Window.CloseListener)1 DateTime (org.joda.time.DateTime)1