Example usage for com.vaadin.server VaadinSession getCurrent

List of usage examples for com.vaadin.server VaadinSession getCurrent

Introduction

In this page you can find the example usage for com.vaadin.server VaadinSession getCurrent.

Prototype

public static VaadinSession getCurrent() 

Source Link

Document

Gets the currently used session.

Usage

From source file:io.github.jikuja.vaadin_yamapa.ui.views.PoiMap.java

License:Creative Commons License

private void setupLocationButton() {
    locate.addStyleName("locatebutton");
    locate.setIcon(FontAwesome.CROSSHAIRS);
    locate.addStyleName(ValoTheme.BUTTON_LARGE);
    locate.addStyleName(ValoTheme.BUTTON_ICON_ONLY);
    locate.setDescription("Locate me");
    locate.addClickListener(event -> {
        Notification.show("Attention", "No real geolocation support. Map center is used as coordinates",
                Notification.Type.HUMANIZED_MESSAGE);

        VaadinSession session = VaadinSession.getCurrent();
        session.setAttribute("lat", map.getCenter().getLat());
        session.setAttribute("lon", map.getCenter().getLon());
        MyUI.getInstance().getMenu().updateButtons();
    });//from  w w w  . ja  va 2  s .  c om
}

From source file:io.github.jikuja.vaadin_yamapa.ui.views.PoiMap.java

License:Creative Commons License

/**
 * Adds markers to the map/*  w  ww  .  j a  v  a2  s.  com*/
 */
private void addMarks() {
    for (Object iid : items.getItemIds()) {
        // new marker being added by someone
        if (iid instanceof TemporaryRowId) {
            Item item = items.getItem(iid);
            double lat = (Double) item.getItemProperty("LAT").getValue();
            double lon = (Double) item.getItemProperty("LONG").getValue();

            LMarker marker = new LMarker(lat, lon);
            marker.setIcon(FontAwesome.PLUS_CIRCLE);
            marker.addStyleName("temp-marker");
            map.addComponent(marker);
            // persistent / saved markers
        } else {
            Item item = items.getItem(iid);
            double lat = (Double) item.getItemProperty("LAT").getValue();
            double lon = (Double) item.getItemProperty("LONG").getValue();

            LMarker marker = new LMarker(lat, lon);
            marker.addClickListener(event -> {
                PoiForm form;
                if (Objects.equals(VaadinSession.getCurrent().getAttribute("userid"),
                        item.getItemProperty("USER_ID").getValue())) {
                    form = new PoiForm("Edit POI", items, iid, true, true);
                } else {
                    form = new PoiForm("POI Details", items, iid, false, false);
                }
                UI.getCurrent().addWindow(form);
            });
            map.addComponent(marker);
        }
    }
}

From source file:io.mateu.ui.vaadin.framework.MyUI.java

License:Apache License

/**
 * inicializacin del port, para saber si estamos con javafx o vaadin.
 *
 * tambin guarda nuestra app en la sesin de vaadin
 *
 * todo: comprobar que la sesin de vaadin y no la sesin http es donde queremos guardar la app
 *
 * @param app/*from  w  w  w . j a v a2s.c o m*/
 */
private void setApp(AbstractApplication app) {
    app.setPort(AbstractApplication.PORT_VAADIN);
    VaadinSession.getCurrent().setAttribute("app", app);
}

From source file:io.mateu.ui.vaadin.framework.MyUI.java

License:Apache License

/**
 * recupera la app de la sesin/*from   www .  j  ava  2  s  . c o m*/
 *
 * @return
 */
public AbstractApplication getApp() {
    return (AbstractApplication) VaadinSession.getCurrent().getAttribute("app");
}

From source file:io.mateu.ui.vaadin.framework.MyUI.java

License:Apache License

/**
 * actualiza los settings (la parte que muestra la sesin del usuario)
 *
 *
 *///from ww w.j  a  v  a2s. c  o  m
private void refreshSettings() {
    VaadinSession s = VaadinSession.getCurrent();

    settings.removeItems();

    if (s.getAttribute("usuario") == null) {

        System.out.println("***** NO AUTENTICADO.");

        if (getApp().isAuthenticationNeeded()) {
            MenuItem settingsItem = settings.addItem("Login", new MenuBar.Command() {
                @Override
                public void menuSelected(MenuItem menuItem) {

                    openLoginDialog(true);

                }
            });
        }

    } else {

        System.out.println("***** AUTENTICADO. USUARIO=" + s.getAttribute("usuario"));

        MenuItem settingsItem = settings.addItem("",
                foto = (getApp().getUserData().getPhoto() != null)
                        ? new ExternalResource(getApp().getUserData().getPhoto())
                        : sinfoto,
                null);

        settingsItem.addItem("Edit Profile", new MenuBar.Command() {
            @Override
            public void menuSelected(MenuItem menuItem) {
                editProfile();
            }
        });
        settingsItem.addItem("Change password", new MenuBar.Command() {
            @Override
            public void menuSelected(MenuItem menuItem) {
                changePassword();
            }
        });
        settingsItem.addItem("Change photo", new MenuBar.Command() {
            @Override
            public void menuSelected(MenuItem menuItem) {
                uploadFoto();
            }
        });
        settingsItem.addSeparator();
        settingsItem.addItem("Sign Out", new MenuBar.Command() {
            @Override
            public void menuSelected(MenuItem menuItem) {
                VaadinSession.getCurrent().setAttribute("usuario", null);
                getApp().setUserData(null);
                getViewDisplay().removeAllComponents();
                refreshSettings();

                if (MateuUI.getApp().isFavouritesAvailable())
                    linkFavoritos.setVisible(false);
                if (MateuUI.getApp().isLastEditedAvailable())
                    linkUltimosRegistros.setVisible(false);
                if (MateuUI.getApp().isFavouritesAvailable())
                    linkNuevoFavorito.setVisible(false);

                refreshMenu(null, null);
                addView(MyUI.this, getApp().getPublicHome());
                if (!"".equals(navigator.getState()))
                    navigator.navigateTo("");
            }
        });

    }

}

From source file:io.mateu.ui.vaadin.framework.MyUI.java

License:Apache License

/**
 * construye el men//from   ww  w.  jav a  2  s .c om
 */
public void refreshMenu(AbstractArea area, MenuEntry menu) {

    if (area != null && area.equals(getAreaActual())) {

        // no cambia el area. Solo puede cambiar el menu

        if ((menu == null && getMenuActual() == null) || (menu != null && menu.equals(getMenuActual()))) {

            // no cambia nada. no hacemos nada

        } else {
            setMenuActual(menu);
        }

    } else {

        // cambia el area. debemos reconstruir el menu

        setAreaActual(area);

        VaadinSession s = VaadinSession.getCurrent();

        boolean autentico = s.getAttribute("usuario") != null;

        List<AbstractArea> areas = new ArrayList<>();

        for (AbstractArea a : getApp().getAreas()) {
            if (autentico) {
                if (!a.isPublicAccess()) {
                    if (area == null)
                        area = a;
                    areas.add(a);
                }
            } else {
                if (!getApp().isAuthenticationNeeded() || a.isPublicAccess()) {
                    if (area == null)
                        area = a;
                    areas.add(a);
                }
            }
        }

        menuItemsLayout.removeAllComponents();
        /**
         * area no puede ser null
         */
        if (area != null) {

            divIndicadorAreaActual = new VerticalLayout();
            divIndicadorAreaActual.setSpacing(false);
            divIndicadorAreaActual.addStyleName("divIndicadorAreaActual");

            if (areas.size() > 1) {

                Button b;
                AbstractArea finalArea = area;
                divIndicadorAreaActual
                        .addComponent(b = new Button("You are at " + ((area != null) ? area.getName() : "--"),
                                new ClickListener() {
                                    @Override
                                    public void buttonClick(ClickEvent clickEvent) {
                                        navigator.navigateTo(getApp().getAreaId(finalArea) + "/changearea");
                                    }
                                }));
                b.addStyleName(ValoTheme.BUTTON_LINK);
                b.addStyleName("linkotrasareas");

                menuItemsLayout.addComponent(divIndicadorAreaActual);
            }

            buildMenuOptions(area);
        }

        setMenuActual(menu);
    }

}

From source file:io.mateu.ui.vaadin.framework.MyUI.java

License:Apache License

/**
 * are el dilogo para autenticarse//from  w w w  . j  a va 2  s . c  o  m
 */
private void openLoginDialog(boolean gohome) {

    // Create a sub-window and set the content
    Window subWindow = new Window("Login");

    subWindow.setWidth("375px");

    FormLayout f = new FormLayout();
    f.setMargin(true);

    TextField l;
    f.addComponent(l = new TextField("Username"));
    PasswordField p;
    f.addComponent(p = new PasswordField("Password"));
    Label e;
    f.addComponent(e = new Label());

    VerticalLayout v = new VerticalLayout();
    v.addComponent(f);

    HorizontalLayout footer = new HorizontalLayout();
    footer.setWidth("100%");
    footer.setSpacing(true);
    footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);

    Label footerText = new Label("");
    footerText.setSizeUndefined();

    Button forgot = new Button("Forgot password", new ClickListener() {
        @Override
        public void buttonClick(ClickEvent clickEvent) {
            e.setValue("Asking for email...");
            io.mateu.ui.core.client.app.MateuUI.getBaseService().forgotPassword(l.getValue(),
                    new AsyncCallback<Void>() {

                        @Override
                        public void onFailure(Throwable caught) {
                            e.setValue("" + caught.getClass().getName() + ": " + caught.getMessage());
                        }

                        @Override
                        public void onSuccess(Void result) {
                            e.setValue("Email sent. Please check your inbox");
                        }
                    });
        }
    });
    //forgot.addStyleName(ValoTheme.BUTTON_);

    Button ok = new Button("Login", new ClickListener() {
        @Override
        public void buttonClick(ClickEvent clickEvent) {
            e.setValue("Authenticating...");
            io.mateu.ui.core.client.app.MateuUI.getBaseService().authenticate(l.getValue(), p.getValue(),
                    new AsyncCallback<UserData>() {

                        @Override
                        public void onFailure(Throwable caught) {
                            e.setValue("" + caught.getClass().getName() + ": " + caught.getMessage());
                        }

                        @Override
                        public void onSuccess(UserData result) {
                            e.setValue("OK!");
                            getApp().setUserData(result);
                            VaadinSession.getCurrent().setAttribute("usuario", "admin");
                            subWindow.close();

                            if (MateuUI.getApp().isFavouritesAvailable())
                                linkFavoritos.setVisible(true);
                            if (MateuUI.getApp().isLastEditedAvailable())
                                linkUltimosRegistros.setVisible(true);
                            if (MateuUI.getApp().isFavouritesAvailable())
                                linkNuevoFavorito.setVisible(true);

                            refreshSettings();
                            refreshMenu(null, null);
                            System.out.println("STATE:" + navigator.getState());
                            System.out.println("URIFRAGMENT:" + Page.getCurrent().getUriFragment());
                            navigator.navigateTo((gohome) ? "" : navigator.getState());
                        }
                    });
        }
    });
    ok.addStyleName(ValoTheme.BUTTON_PRIMARY);
    ok.setClickShortcut(ShortcutAction.KeyCode.ENTER);

    Button cancel = new Button("Cancel");

    footer.addComponents(footerText, forgot, ok); //, cancel);
    footer.setExpandRatio(footerText, 1);

    v.addComponent(footer);

    subWindow.setContent(v);

    // Center it in the browser window
    subWindow.center();

    subWindow.setModal(true);

    // Open it in the UI
    UI.getCurrent().addWindow(subWindow);

    l.focus();
}

From source file:makeithappen.vaadin.app.internal.servlet.VaadinOsgiUIProvider.java

License:Open Source License

@Override
public UI createInstance(UICreateEvent event) {
    final UI ui = super.createInstance(event);
    final WrappedSession session = VaadinSession.getCurrent().getSession();
    session.setMaxInactiveInterval(MAX_INACTIVE_INTERVAL);
    return ui;/*  ww  w. jav a 2s. c  o  m*/
}

From source file:me.uni.emuseo.service.AuthManager.java

License:Open Source License

public void login(String username, String password) {

    UsernamePasswordAuthenticationToken request = new UsernamePasswordAuthenticationToken(username, password);
    Authentication result = authenticate(request);
    // SecurityContext context = SecurityContextHolder.getContext();
    // System.out.println(Thread.currentThread().getName() +
    // " Using sec context: "+context.hashCode());
    // context.setAuthentication(result);
    // System.out.println(Thread.currentThread().getName() +
    // " Using sec context: "+context.hashCode());
    VaadinSession.getCurrent().setAttribute("authentication", result);
    Navigator navigator = UI.getCurrent().getNavigator();
    navigator.navigateTo(Permissions.MENU_VIEW);
}

From source file:me.uni.emuseo.service.AuthManager.java

License:Open Source License

public void logout() {
    // SecurityContextHolder.clearContext();
    // UI.getCurrent().close();
    VaadinSession.getCurrent().setAttribute("authentication", null);
    Navigator navigator = UI.getCurrent().getNavigator();
    navigator.navigateTo(Permissions.LOGIN_VIEW);
}