From 8545d39358d57c1503023ed6c890a8ef4f8033fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 18 Oct 2012 12:47:06 +0300 Subject: [PATCH] Update mini tutorial code Change-Id: Ib8114efc793f6df412cefddfcf7627d4812b1228 --- .../minitutorials/v7a3/RefresherTestUI.java | 11 ++- .../minitutorials/v7b5/HandlingLogout.java | 64 ++++++++++++++ .../SettingReadingSessionAttributesUI.java | 84 +++++++++++++++++++ .../minitutorials/v7a3/RefresherState.java | 2 +- 4 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7b5/HandlingLogout.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7b5/SettingReadingSessionAttributesUI.java diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7a3/RefresherTestUI.java b/uitest/src/com/vaadin/tests/minitutorials/v7a3/RefresherTestUI.java index 430da55fa1..c849cdce36 100644 --- a/uitest/src/com/vaadin/tests/minitutorials/v7a3/RefresherTestUI.java +++ b/uitest/src/com/vaadin/tests/minitutorials/v7a3/RefresherTestUI.java @@ -21,20 +21,29 @@ import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractTestUI; import com.vaadin.tests.minitutorials.v7a3.Refresher.RefreshListener; import com.vaadin.tests.widgetset.TestingWidgetSet; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; @Widgetset(TestingWidgetSet.NAME) public class RefresherTestUI extends AbstractTestUI { @Override protected void setup(VaadinRequest request) { - Refresher refresher = new Refresher(); + final Refresher refresher = new Refresher(); refresher.extend(this); + refresher.setInterval(1000); refresher.addListener(new RefreshListener() { @Override public void refresh(Refresher source) { System.out.println("Got refresh"); } }); + addComponent(new Button("Remove refresher", new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + removeExtension(refresher); + } + })); } @Override diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7b5/HandlingLogout.java b/uitest/src/com/vaadin/tests/minitutorials/v7b5/HandlingLogout.java new file mode 100644 index 0000000000..0356b4c5a8 --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7b5/HandlingLogout.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.tests.minitutorials.v7b5; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.VaadinServletService; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; + +public class HandlingLogout extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + addComponent(new Button("Logout", new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + logout(); + } + })); + } + + private void logout() { + // Close the VaadinServiceSession + getUI().getSession().close(); + + // Invalidate underlying session instead if login info is stored there + // VaadinService.getCurrentRequest().getWrappedSession().invalidate(); + + // Redirect to avoid keeping the removed UI open in the browser + getUI().getPage().setLocation(getLogoutPageLocation()); + } + + protected String getLogoutPageLocation() { + return VaadinServletService.getCurrentRequest().getContextPath() + + "logout.html"; + } + + @Override + protected String getTestDescription() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected Integer getTicketNumber() { + return Integer.valueOf(9646); + } + +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7b5/SettingReadingSessionAttributesUI.java b/uitest/src/com/vaadin/tests/minitutorials/v7b5/SettingReadingSessionAttributesUI.java new file mode 100644 index 0000000000..bc96152880 --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7b5/SettingReadingSessionAttributesUI.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.tests.minitutorials.v7b5; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.VaadinService; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Label; +import com.vaadin.ui.TextField; +import com.vaadin.ui.UI; +import com.vaadin.ui.VerticalLayout; + +//Remove comment to preserve UI value when reloading +//@PreserveOnRefresh +public class SettingReadingSessionAttributesUI extends UI { + + private String value; + + private VerticalLayout statusHolder = new VerticalLayout(); + private TextField textField = new TextField(); + + @Override + protected void init(VaadinRequest request) { + addComponent(statusHolder); + addComponent(textField); + addComponent(new Button("Set new values", new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + String value = textField.getValue(); + + saveValue(SettingReadingSessionAttributesUI.this, value); + } + })); + addComponent(new Button("Reload page", new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + getPage().setLocation(getPage().getLocation()); + } + })); + + showValue(this); + } + + private static void saveValue(SettingReadingSessionAttributesUI ui, + String value) { + // Save to UI instance + ui.value = value; + // Save to VaadinServiceSession + ui.getSession().setAttribute("myValue", value); + // Save to HttpSession + VaadinService.getCurrentRequest().getWrappedSession() + .setAttribute("myValue", value); + + // Show new values + showValue(ui); + } + + private static void showValue(SettingReadingSessionAttributesUI ui) { + ui.statusHolder.removeAllComponents(); + ui.statusHolder.addComponent(new Label("Value in UI: " + ui.value)); + ui.statusHolder.addComponent(new Label( + "Value in VaadinServiceSession: " + + ui.getSession().getAttribute("myValue"))); + ui.statusHolder.addComponent(new Label("Value in HttpSession: " + + VaadinService.getCurrentRequest().getWrappedSession() + .getAttribute("myValue"))); + } + +} diff --git a/uitest/src/com/vaadin/tests/widgetset/client/minitutorials/v7a3/RefresherState.java b/uitest/src/com/vaadin/tests/widgetset/client/minitutorials/v7a3/RefresherState.java index 769f6330a9..13e8d8bc1d 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/minitutorials/v7a3/RefresherState.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/minitutorials/v7a3/RefresherState.java @@ -3,5 +3,5 @@ package com.vaadin.tests.widgetset.client.minitutorials.v7a3; import com.vaadin.shared.communication.SharedState; public class RefresherState extends SharedState { - public int interval = 1000; + public int interval; } \ No newline at end of file -- 2.39.5