Example usage for com.google.gwt.user.client Cookies setCookie

List of usage examples for com.google.gwt.user.client Cookies setCookie

Introduction

In this page you can find the example usage for com.google.gwt.user.client Cookies setCookie.

Prototype

public static void setCookie(String name, String value, Date expires) 

Source Link

Document

Sets a cookie.

Usage

From source file:com.dawg6.web.dhcalc.client.BasePanel.java

License:Open Source License

protected void saveCookie(String value, String name) {

    Date date = new Date();

    Cookies.setCookie(name, value, new Date(date.getTime() + ONE_YEAR));

}

From source file:com.dingziran.effective.client.ShowcaseShell.java

License:Apache License

/**
 * Initialize the {@link ListBox} used for locale selection.
 *//*from  w  w w  .j  a va2  s.  c  om*/
private void initializeLocaleBox() {
    final String cookieName = LocaleInfo.getLocaleCookieName();
    final String queryParam = LocaleInfo.getLocaleQueryParam();
    if (cookieName == null && queryParam == null) {
        // if there is no way for us to affect the locale, don't show the selector
        localeSelectionCell.getStyle().setDisplay(Display.NONE);
        return;
    }
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    if (currentLocale.equals("default")) {
        currentLocale = "en";
    }
    String[] localeNames = LocaleInfo.getAvailableLocaleNames();
    for (String localeName : localeNames) {
        if (!localeName.equals("default")) {
            String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
            localeBox.addItem(nativeName, localeName);
            if (localeName.equals(currentLocale)) {
                localeBox.setSelectedIndex(localeBox.getItemCount() - 1);
            }
        }
    }
    localeBox.addChangeHandler(new ChangeHandler() {
        @SuppressWarnings("deprecation")
        public void onChange(ChangeEvent event) {
            String localeName = localeBox.getValue(localeBox.getSelectedIndex());
            if (cookieName != null) {
                // expire in one year
                Date expires = new Date();
                expires.setYear(expires.getYear() + 1);
                Cookies.setCookie(cookieName, localeName, expires);
            }
            if (queryParam != null) {
                UrlBuilder builder = Location.createUrlBuilder().setParameter(queryParam, localeName);
                Window.Location.replace(builder.buildString());
            } else {
                // If we are using only cookies, just reload
                Window.Location.reload();
            }
        }
    });
}

From source file:com.dotweblabs.friendscube.app.client.local.LoginPage.java

License:Apache License

@EventHandler("loginButton")
public void login(ClickEvent event) {
    event.preventDefault();/* w ww . j a v  a  2 s. c  om*/
    String uname = username.getText();
    String pass = password.getText();
    if ((uname.isEmpty() || pass.isEmpty())) {
        $("#login-alert").removeClass("uk-hidden").text("Please enter username/password.");
        return;
    }

    TokensResourceProxy tokensResource = GWT.create(TokensResourceProxy.class);
    tokensResource.getClientResource()
            .setReference(ClientProxyHelper.restRootPath() + TokensResourceProxy.TOKENS_URI);
    // TODO: This is not really a good practice!
    tokensResource.getClientResource().addQueryParameter("domain", UserResourceProxy.DOMAIN);
    tokensResource.getClientResource().addQueryParameter("email", uname);
    tokensResource.getClientResource().addQueryParameter("password", Base64.byteArrayToBase64(pass.getBytes()));

    tokensResource.retrieve(new Result<User>() {
        @Override
        public void onFailure(Throwable throwable) {
            $("#login-alert").removeClass("uk-hidden").text("Server error when logging in. Please try again.");
        }

        @Override
        public void onSuccess(User user) {
            if (user != null) {
                loggedInUser.setUser(user);
                Multimap<String, String> state = ArrayListMultimap.create();
                state.put("token", String.valueOf(user.getClientToken()));
                long oneDay = 24 * 60 * 60 * 1000;
                Date expiration = new Date(new Date().getTime() + oneDay);
                Cookies.setCookie("token", String.valueOf(user.getClientToken()), expiration);
                welcomePage.go(state);
            } else {
                $("#login-alert").removeClass("uk-hidden")
                        .text("Incorrect username/password. Please try again.");
            }
        }
    });
}

From source file:com.ephesoft.gxt.rv.client.widget.DLFMultilineBox.java

License:Open Source License

private void storeSizeInCookies() {
    int width = getOffsetWidth();
    int height = getOffsetHeight();
    String heightCookie = CookieUtil.getCookieNameForHeight(elementCookieIdentifier);
    String widthCookie = CookieUtil.getCookieNameForWidth(elementCookieIdentifier);
    Cookies.setCookie(heightCookie, String.valueOf(height), new Date(Long.MAX_VALUE / 2));
    Cookies.setCookie(widthCookie, String.valueOf(width), new Date(Long.MAX_VALUE / 2));
}

From source file:com.fullmetalgalaxy.client.game.GameEngine.java

License:Open Source License

/**
 * This method save HMI option in cookies to be restored later
 *//*from ww  w.  j  a v a 2  s  .com*/
private void backupHMIFlags() {
    Cookies.setCookie("HMIFlags", buildHMIFlags(), new Date(Long.MAX_VALUE));
}

From source file:com.goodow.web.ui.client.search.DefaultHeader.java

License:Apache License

/**
 * Initialize the {@link ListBox} used for locale selection.
 *///w  ww .  ja  v a 2s  .c  o  m
private void initializeLocaleBox() {
    final String cookieName = LocaleInfo.getLocaleCookieName();
    final String queryParam = LocaleInfo.getLocaleQueryParam();
    if (cookieName == null && queryParam == null) {
        // if there is no way for us to affect the locale, don't show the selector
        localeSelectionCell.getStyle().setDisplay(Display.NONE);
        return;
    }
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    if (currentLocale.equals("default")) {
        currentLocale = "zh";
    }
    String[] localeNames = LocaleInfo.getAvailableLocaleNames();
    for (String localeName : localeNames) {
        if (!localeName.equals("default")) {
            String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
            localeBox.addItem(nativeName, localeName);
            if (localeName.equals(currentLocale)) {
                localeBox.setSelectedIndex(localeBox.getItemCount() - 1);
            }
        }
    }
    localeBox.addChangeHandler(new ChangeHandler() {
        @Override
        @SuppressWarnings("deprecation")
        public void onChange(final ChangeEvent event) {
            String localeName = localeBox.getValue(localeBox.getSelectedIndex());
            if (cookieName != null) {
                // expire in one year
                Date expires = new Date();
                expires.setYear(expires.getYear() + 1);
                Cookies.setCookie(cookieName, localeName, expires);
            }
            if (queryParam != null) {
                UrlBuilder builder = Location.createUrlBuilder().setParameter(queryParam, localeName);
                Window.Location.replace(builder.buildString());
            } else {
                // If we are using only cookies, just reload
                Window.Location.reload();
            }
        }
    });
}

From source file:com.google.gerrit.client.change.NewChangeScreenBar.java

License:Apache License

private void save(ChangeScreen sel) {
    removeFromParent();/*  ww w . j a  v a2  s  . c  o m*/
    Dispatcher.changeScreen2 = sel == ChangeScreen.CHANGE_SCREEN2;

    if (Gerrit.isSignedIn()) {
        Gerrit.getUserAccount().getGeneralPreferences().setChangeScreen(sel);

        Prefs in = Prefs.createObject().cast();
        in.change_screen(sel.name());
        AccountApi.self().view("preferences").background().post(in, new AsyncCallback<JavaScriptObject>() {
            @Override
            public void onFailure(Throwable caught) {
            }

            @Override
            public void onSuccess(JavaScriptObject result) {
            }
        });
    } else {
        Cookies.setCookie(Dispatcher.COOKIE_CS2, Dispatcher.changeScreen2 ? "1" : "0",
                new Date(System.currentTimeMillis() + 7 * 24 * 3600 * 1000));
    }
}

From source file:com.google.gerrit.client.MessageOfTheDayBar.java

License:Apache License

@UiHandler("dismiss")
void onDismiss(@SuppressWarnings("unused") ClickEvent e) {
    removeFromParent();//from w w w.jav a2  s .  c  om

    for (HostPageData.Message m : motd) {
        Cookies.setCookie(cookieName(m), "1", m.redisplay);
    }
}

From source file:com.google.gwt.sample.showcase.client.content.other.CwCookies.java

License:Apache License

/**
 * Initialize this example./*from w  w w . jav  a  2 s .  c o m*/
 */
@ShowcaseSource
@Override
public Widget onInitialize() {
    // Create the panel used to layout the content
    Grid mainLayout = new Grid(3, 3);

    // Display the existing cookies
    existingCookiesBox = new ListBox();
    Button deleteCookieButton = new Button(constants.cwCookiesDeleteCookie());
    deleteCookieButton.addStyleName("sc-FixedWidthButton");
    mainLayout.setHTML(0, 0, "<b>" + constants.cwCookiesExistingLabel() + "</b>");
    mainLayout.setWidget(0, 1, existingCookiesBox);
    mainLayout.setWidget(0, 2, deleteCookieButton);

    // Display the name of the cookie
    cookieNameBox = new TextBox();
    mainLayout.setHTML(1, 0, "<b>" + constants.cwCookiesNameLabel() + "</b>");
    mainLayout.setWidget(1, 1, cookieNameBox);

    // Display the name of the cookie
    cookieValueBox = new TextBox();
    Button setCookieButton = new Button(constants.cwCookiesSetCookie());
    setCookieButton.addStyleName("sc-FixedWidthButton");
    mainLayout.setHTML(2, 0, "<b>" + constants.cwCookiesValueLabel() + "</b>");
    mainLayout.setWidget(2, 1, cookieValueBox);
    mainLayout.setWidget(2, 2, setCookieButton);

    // Add a handler to set the cookie value
    setCookieButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            String name = cookieNameBox.getText();
            String value = cookieValueBox.getText();
            Date expires = new Date((new Date()).getTime() + COOKIE_TIMEOUT);

            // Verify the name is valid
            if (name.length() < 1) {
                Window.alert(constants.cwCookiesInvalidCookie());
                return;
            }

            // Set the cookie value
            Cookies.setCookie(name, value, expires);
            refreshExistingCookies(name);
        }
    });

    // Add a handler to select an existing cookie
    existingCookiesBox.addChangeHandler(new ChangeHandler() {
        public void onChange(ChangeEvent event) {
            updateExstingCookie();
        }
    });

    // Add a handler to delete an existing cookie
    deleteCookieButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            int selectedIndex = existingCookiesBox.getSelectedIndex();
            if (selectedIndex > -1 && selectedIndex < existingCookiesBox.getItemCount()) {
                String cookieName = existingCookiesBox.getValue(selectedIndex);
                Cookies.removeCookie(cookieName);
                existingCookiesBox.removeItem(selectedIndex);
                updateExstingCookie();
            }
        }
    });

    // Return the main layout
    refreshExistingCookies(null);
    return mainLayout;
}

From source file:com.google.livingstories.client.lsp.LivingStory.java

License:Apache License

@Override
public void onModuleLoad() {
    // Inject the contents of the CSS file
    Resources.INSTANCE.css().ensureInjected();

    AjaxLoader.init();/*from w  w w .  java 2s  . c  om*/

    String cookieName = Constants.getCookieName(LivingStoryData.getLivingStoryUrl());
    String cookieValue = Cookies.getCookie(cookieName);

    if (cookieValue != null) {
        try {
            LivingStoryData.setCookieBasedLastVisitDate(new Date(Long.valueOf(cookieValue)));
        } catch (NumberFormatException e) {
        }
    }

    // note the visit.
    Date now = new Date();
    Date cookieExpiry = new Date(now.getTime() + SIXTY_DAYS_IN_MILLISECONDS);
    Cookies.setCookie(cookieName, String.valueOf(now.getTime()), cookieExpiry);

    RootPanel.get("storyBody").add(new LivingStoryPage());

    HistoryManager.initialize();

    // Also set appropriate i18n text for a couple of constants:
    Document doc = Document.get();
    doc.getElementById("rssLink").setAttribute("title",
            LspMessageHolder.msgs.rssFeedTitle(LivingStoryData.getLivingStoryTitle()));
    doc.getElementById("readOtherStories").setInnerText(LspMessageHolder.consts.otherStories());
}