Simplified ClickEventHandler/LayoutClickEventHandler and made them public so they are available to other components.
svn changeset:10054/svn branch:event-framework-3234
+++ /dev/null
-import com.vaadin.Application;
-import com.vaadin.event.FieldEvents.BlurEvent;
-import com.vaadin.event.FieldEvents.BlurListener;
-import com.vaadin.event.FieldEvents.FocusEvent;
-import com.vaadin.event.FieldEvents.FocusListener;
-import com.vaadin.event.LayoutEvents.LayoutClickEvent;
-import com.vaadin.event.LayoutEvents.LayoutClickListener;
-import com.vaadin.event.MouseEvents.ClickEvent;
-import com.vaadin.event.MouseEvents.ClickListener;
-import com.vaadin.terminal.ExternalResource;
-import com.vaadin.terminal.gwt.client.MouseEventDetails;
-import com.vaadin.ui.AbsoluteLayout;
-import com.vaadin.ui.Component;
-import com.vaadin.ui.Embedded;
-import com.vaadin.ui.GridLayout;
-import com.vaadin.ui.HorizontalLayout;
-import com.vaadin.ui.Label;
-import com.vaadin.ui.Layout;
-import com.vaadin.ui.TextField;
-import com.vaadin.ui.VerticalLayout;
-import com.vaadin.ui.Window;
-
-public class TestListeners extends Application implements LayoutClickListener,
- ClickListener, FocusListener {
-
- @Override
- public void init() {
- Window w = new Window("main window");
- setMainWindow(w);
- HorizontalLayout hl = new HorizontalLayout();
- w.setContent(hl);
-
- final AbsoluteLayout al = new AbsoluteLayout();
- al.setWidth("200px");
- al.setHeight("200px");
- al.addComponent(new TextField("This is its caption",
- "This is a textfield"), "top: 20px; left: 0px; width: 100px;");
- al.addComponent(new TextField("Antoerh caption",
- "This is another textfield"),
- "top: 120px; left: 0px; width: 100px;");
-
- final LayoutClickListener lcl = new LayoutClickListener() {
-
- public void layoutClick(LayoutClickEvent event) {
- System.out.println("AL: Click on " + event.getChildComponent());
- // al.removeListener(this);
-
- }
- };
- al.addListener(lcl);
-
- final GridLayout vl = new GridLayout();
- vl.addComponent(al);
- vl.setSpacing(true);
- for (int i = 0; i < 10; i++) {
- vl.addComponent(new Label("Component " + i));
- ExternalResource res = new ExternalResource(
- "http://vaadin.com/image/image_gallery?uuid=07c1f6d5-2e94-4f4d-a707-b548bf22279d&groupId=10919&t=1241012632062");
- Embedded e = new Embedded("an image", res);
- e.setType(Embedded.TYPE_IMAGE);
- e.addListener(new ClickListener() {
-
- public void click(ClickEvent event) {
- TestListeners.this.click(event);
-
- }
- });
- // e.addListener(this);
- vl.addComponent(e);
- TextField tf = new TextField("tf");
- tf.setInputPrompt("Please enter a value");
-
- // tf.addListener(this);
- tf.addListener(new BlurListener() {
-
- public void blur(BlurEvent event) {
- getMainWindow().showNotification(
- "Blurred " + event.getComponent());
-
- }
- });
- tf.addListener(new FocusListener() {
-
- public void focus(FocusEvent event) {
- getMainWindow().showNotification(
- "Focused " + event.getComponent());
-
- }
- });
- vl.addComponent(tf);
- }
-
- // vl.addListener(this);
- vl.addListener(new LayoutClickListener() {
-
- public void layoutClick(LayoutClickEvent event) {
- TestListeners.this.layoutClick(event);
- vl.removeListener(this);
- }
- });
- vl.setMargin(true);
-
- hl.addComponent(vl);
- hl.addComponent(createClickableGridLayout());
- hl.addComponent(createClickableVerticalLayout());
- }
-
- private Layout createClickableGridLayout() {
-
- GridLayout gl = new GridLayout(3, 3);
- addContent(gl, 5);
-
- gl.addListener(new LayoutClickListener() {
-
- public void layoutClick(LayoutClickEvent event) {
- getMainWindow().showNotification(
- "GL-click on " + event.getChildComponent());
-
- }
- });
-
- return wrap(gl, "Clickable GridLayout");
- }
-
- private Layout createClickableVerticalLayout() {
-
- VerticalLayout gl = new VerticalLayout();
- addContent(gl, 5);
-
- gl.addListener(new LayoutClickListener() {
-
- public void layoutClick(LayoutClickEvent event) {
- getMainWindow().showNotification(
- "VL-click on " + event.getChildComponent());
-
- }
- });
-
- return wrap(gl, "Clickable VerticalLayout");
- }
-
- private void addContent(Layout gl, int nr) {
- for (int i = 1; i < nr; i++) {
- Label l = new Label("This is label " + i);
- l.setWidth(null);
- gl.addComponent(l);
- }
- for (int i = nr; i < nr * 2; i++) {
- gl.addComponent(new TextField("This is tf" + i, "this is tf " + i));
- }
- }
-
- private Layout wrap(Component c, String caption) {
- VerticalLayout vl = new VerticalLayout();
- Label l = new Label(caption);
- l.setWidth(null);
- vl.addComponent(l);
- vl.addComponent(c);
-
- return vl;
- }
-
- public void layoutClick(LayoutClickEvent event) {
- if (event.getChildComponent() == null) {
- getMainWindow().showNotification("You missed!");
- } else {
- getMainWindow().showNotification(
- "Clicked on " + event.getChildComponent() + "!");
- // getMainWindow().removeComponent(event.getChildComponent());
- }
-
- }
-
- public void click(ClickEvent event) {
- getMainWindow().showNotification(
- "Clicked on " + event.getComponent() + " using "
- + getButton(event));
- }
-
- private String getButton(ClickEvent event) {
- if (event.getButton() == MouseEventDetails.BUTTON_LEFT) {
- return "left";
- } else if (event.getButton() == MouseEventDetails.BUTTON_RIGHT) {
- return "right";
- } else {
- return "middle";
- }
- }
-
- public void focus(FocusEvent event) {
- TextField tf = (TextField) event.getComponent();
- // tf.addStyleName("a");
- // tf.setValue("");
- getMainWindow().requestRepaintAll();
-
- }
-
-}
return instance;
}
+ public String getButtonName() {
+ if (button == BUTTON_LEFT) {
+ return "left";
+ } else if (button == BUTTON_RIGHT) {
+ return "right";
+ } else if (button == BUTTON_MIDDLE) {
+ return "middle";
+ }
+
+ return "";
+ }
+
public Class<MouseEventDetails> getType() {
return MouseEventDetails.class;
}
import com.vaadin.terminal.gwt.client.MouseEventDetails;\r
import com.vaadin.terminal.gwt.client.Paintable;\r
\r
-abstract class ClickEventHandler implements ClickHandler, DoubleClickHandler,\r
- ContextMenuHandler, MouseUpHandler {\r
+public abstract class ClickEventHandler implements ClickHandler,\r
+ DoubleClickHandler, ContextMenuHandler, MouseUpHandler {\r
\r
private HandlerRegistration clickHandlerRegistration;\r
private HandlerRegistration doubleClickHandlerRegistration;\r
\r
protected String clickEventIdentifier;\r
protected Paintable paintable;\r
+ private ApplicationConnection client;\r
\r
- ClickEventHandler(Paintable paintable, String clickEventIdentifier) {\r
+ public ClickEventHandler(Paintable paintable, String clickEventIdentifier) {\r
this.paintable = paintable;\r
this.clickEventIdentifier = clickEventIdentifier;\r
}\r
\r
- public void handleHandlerRegistration() {\r
+ public void handleEventHandlerRegistration(ApplicationConnection client) {\r
+ this.client = client;\r
// Handle registering/unregistering of click handler depending on if\r
// server side listeners have been added or removed.\r
if (hasEventListener()) {\r
protected abstract <H extends EventHandler> HandlerRegistration registerHandler(\r
final H handler, DomEvent.Type<H> type);\r
\r
- public abstract ApplicationConnection getApplicationConnection();\r
+ protected ApplicationConnection getApplicationConnection() {\r
+ return client;\r
+ }\r
\r
public boolean hasEventListener() {\r
return getApplicationConnection().hasEventListeners(paintable,\r
import com.vaadin.terminal.gwt.client.MouseEventDetails;
import com.vaadin.terminal.gwt.client.Paintable;
-abstract class LayoutClickEventHandler extends ClickEventHandler {
+public abstract class LayoutClickEventHandler extends ClickEventHandler {
LayoutClickEventHandler(Paintable paintable, String clickEventIdentifier) {
super(paintable, clickEventIdentifier);
private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
this, CLICK_EVENT_IDENTIFIER) {
- @Override
- public ApplicationConnection getApplicationConnection() {
- return client;
- }
-
@Override
protected Paintable getChildComponent(Element element) {
return getComponent(element);
return;
}
- clickEventHandler.handleHandlerRegistration();
+ clickEventHandler.handleEventHandlerRegistration(client);
HashSet<String> unrenderedPids = new HashSet<String>(
pidToComponentWrappper.keySet());
}
}
-
private Paintable getComponent(Element target) {
while (target != null && target != canvas) {
Paintable paintable = client.getPaintable(target);
private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
CLICK_EVENT_IDENTIFIER) {
- @Override
- public ApplicationConnection getApplicationConnection() {
- return client;
- }
-
@Override
protected <H extends EventHandler> HandlerRegistration registerHandler(
H handler, Type<H> type) {
boolean clearBrowserElement = true;
- clickEventHandler.handleHandlerRegistration();
+ clickEventHandler.handleEventHandlerRegistration(client);
if (uidl.hasAttribute("type")) {
final String type = uidl.getStringAttribute("type");
private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
this, CLICK_EVENT_IDENTIFIER) {
- @Override
- public ApplicationConnection getApplicationConnection() {
- return client;
- }
-
@Override
protected Paintable getChildComponent(Element element) {
return getComponent(element);
rendering = false;
return;
}
- clickEventHandler.handleHandlerRegistration();
+ clickEventHandler.handleEventHandlerRegistration(client);
canvas.setWidth("0px");
private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(\r
this, CLICK_EVENT_IDENTIFIER) {\r
\r
- @Override\r
- public ApplicationConnection getApplicationConnection() {\r
- return client;\r
- }\r
-\r
@Override\r
protected Paintable getChildComponent(Element element) {\r
return getComponent(element);\r
return;\r
}\r
\r
- clickEventHandler.handleHandlerRegistration();\r
+ clickEventHandler.handleEventHandlerRegistration(client);\r
\r
if (allowOrientationUpdate) {\r
handleOrientationUpdate(uidl);\r
private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
CLICK_EVENT_IDENTIFIER) {
- @Override
- public ApplicationConnection getApplicationConnection() {
- return client;
- }
-
@Override
protected <H extends EventHandler> HandlerRegistration registerHandler(
H handler, Type<H> type) {
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
rendering = true;
if (!uidl.hasAttribute("cached")) {
- clickEventHandler.handleHandlerRegistration();
+ clickEventHandler.handleEventHandlerRegistration(client);
// Handle caption displaying and style names, prior generics.
// Affects size
}
public void onChange(ChangeEvent event) {
+ valueChange(false);
+ }
+
+ /**
+ * Called when the field value might have changed and/or the field was
+ * blurred. These are combined so the blur event is sent in the same batch
+ * as a possible value change event (these are often connected).
+ *
+ * @param blurred
+ * true if the field was blurred
+ */
+ public void valueChange(boolean blurred) {
if (client != null && id != null) {
+ boolean sendBlurEvent = false;
+ boolean sendValueChange = false;
+
+ if (blurred
+ && client.hasEventListeners(this, BLUR_EVENT_IDENTIFIER)) {
+ sendBlurEvent = true;
+ client.updateVariable(id, BLUR_EVENT_IDENTIFIER, "", false);
+ }
+
String newText = getText();
if (!prompting && newText != null
&& !newText.equals(valueBeforeEdit)) {
- client.updateVariable(id, "text", getText(), immediate);
+ sendValueChange = immediate;
+ client.updateVariable(id, "text", getText(), false);
valueBeforeEdit = newText;
}
+
+ if (sendBlurEvent || sendValueChange) {
+ client.sendPendingVariableChanges();
+ }
}
}
setText(inputPrompt);
addStyleDependentName(CLASSNAME_PROMPT);
}
- onChange(null);
- if (client.hasEventListeners(this, BLUR_EVENT_IDENTIFIER)) {
- client.updateVariable(client.getPid(this), BLUR_EVENT_IDENTIFIER,
- "", true);
- }
+
+ valueChange(true);
}
private void setPrompting(boolean prompting) {
super.changeVariables(source, variables);
- if (variables.containsKey(FOCUS_EVENT)) {
- fireFocus(variables.get(FOCUS_EVENT));
- }
- if (variables.containsKey(BLUR_EVENT)) {
- fireBlur(variables.get(BLUR_EVENT));
- }
-
// Sets the text
if (variables.containsKey("text") && !isReadOnly()) {
}
}
+ if (variables.containsKey(FOCUS_EVENT)) {
+ fireFocus(variables.get(FOCUS_EVENT));
+ }
+ if (variables.containsKey(BLUR_EVENT)) {
+ fireBlur(variables.get(BLUR_EVENT));
+ }
+
}
/* Text field configuration */
--- /dev/null
+package com.vaadin.tests.components.textfield;
+
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.data.Property.ValueChangeListener;
+import com.vaadin.event.FieldEvents.BlurEvent;
+import com.vaadin.event.FieldEvents.BlurListener;
+import com.vaadin.event.FieldEvents.FocusEvent;
+import com.vaadin.event.FieldEvents.FocusListener;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.tests.layouts.TestLayoutClickListeners.Log;
+import com.vaadin.ui.TextField;
+
+public class TextFieldFocusAndBlurListeners extends TestBase implements
+ FocusListener, BlurListener, ValueChangeListener {
+ private Log log = new Log(5);
+
+ @Override
+ protected String getDescription() {
+ return "Tests the focus and blur functionality of TextField";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 3544;
+ }
+
+ @Override
+ public void setup() {
+ addComponent(log);
+ TextField tf1 = new TextField("TextField 1",
+ "Has focus and blur listeners");
+ tf1.setWidth("300px");
+ tf1.addListener((FocusListener) this);
+ tf1.addListener((BlurListener) this);
+
+ addComponent(tf1);
+
+ TextField tf2 = new TextField("TextField 2",
+ "Has focus, blur and valuechange listeners");
+ tf2.setWidth("300px");
+ tf2.addListener(new ValueChangeListener() {
+
+ public void valueChange(ValueChangeEvent event) {
+ TextFieldFocusAndBlurListeners.this.valueChange(event);
+ }
+ });
+ tf2.addListener(new FocusListener() {
+
+ public void focus(FocusEvent event) {
+ TextFieldFocusAndBlurListeners.this.focus(event);
+ }
+
+ });
+ tf2.addListener(new BlurListener() {
+
+ public void blur(BlurEvent event) {
+ TextFieldFocusAndBlurListeners.this.blur(event);
+ }
+ });
+
+ addComponent(tf2);
+
+ TextField tf3 = new TextField("TextField 3",
+ "Has non-immediate valuechange listener");
+ tf3.setWidth("300px");
+ tf3.addListener((ValueChangeListener) this);
+
+ addComponent(tf3);
+
+ TextField tf4 = new TextField("TextField 4",
+ "Has immediate valuechange listener");
+ tf4.setWidth("300px");
+ tf4.setImmediate(true);
+ tf4.addListener((ValueChangeListener) this);
+
+ addComponent(tf4);
+ }
+
+ public void focus(FocusEvent event) {
+ log.log(event.getComponent().getCaption() + ": Focus");
+
+ }
+
+ public void blur(BlurEvent event) {
+ TextField tf = (TextField) event.getComponent();
+ log.log(tf.getCaption() + ": Blur. Value is: "
+ + tf.getValue().toString());
+
+ }
+
+ public void valueChange(ValueChangeEvent event) {
+ TextField tf = (TextField) event.getProperty();
+ log.log(tf.getCaption() + ": ValueChange: " + tf.getValue().toString());
+ }
+}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="" />
+<title>TestLayoutClickListeners</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">TestLayoutClickListeners</td></tr>
+</thead><tbody>
+<tr>
+ <td>open</td>
+ <td>/run/com.vaadin.tests.layouts.TestLayoutClickListeners</td>
+ <td></td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>screenCapture</td>
+ <td></td>
+ <td>initial</td>
+</tr>
+<tr>
+ <td>mouseClick</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[1]/VGridLayout[0]/AbsolutePanel[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>43,11</td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:GridLayout: Click on This is label 1</td>
+</tr>
+<tr>
+ <td>mouseClick</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[1]/VGridLayout[0]/AbsolutePanel[0]/ChildComponentContainer[4]/VTextField[0]</td>
+ <td>82,14</td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:GridLayout: Click on This is tf5</td>
+</tr>
+<tr>
+ <td>mouseClick</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[5]/VTextField[0]</td>
+ <td>74,13</td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:VerticalLayout: Click on This is tf6</td>
+</tr>
+<tr>
+ <td>mouseClick</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VLabel[0]</td>
+ <td>53,13</td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:VerticalLayout: Click on This is label 3</td>
+</tr>
+<tr>
+ <td>click</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[2]/VAbsoluteLayout[0]/VAbsoluteLayout$AbsoluteWrapper[2]/VButton[0]/domChild[0]/domChild[0]</td>
+ <td></td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[1]/VLabel[0]</td>
+ <td>Button A button with its own click listener was clicked</td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:AbsoluteLayout: Click on A button with its own click listener</td>
+</tr>
+<tr>
+ <td>mouseClick</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[2]/VAbsoluteLayout[0]/VAbsoluteLayout$AbsoluteWrapper[0]/VTextField[0]</td>
+ <td>101,14</td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:AbsoluteLayout: Click on This is its caption</td>
+</tr>
+<tr>
+ <td>mouseClick</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[1]/VGridLayout[0]/domChild[0]/domChild[0]/domChild[1]</td>
+ <td>87,42</td>
+</tr>
+<tr>
+ <td>waitForVaadin</td>
+ <td></td>
+ <td></td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestslayoutsTestLayoutClickListeners::/VVerticalLayout[0]/ChildComponentContainer[0]/VVerticalLayout[0]/ChildComponentContainer[0]/VLabel[0]</td>
+ <td>exact:GridLayout: Click on <none></td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
--- /dev/null
+package com.vaadin.tests.layouts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.vaadin.event.LayoutEvents.LayoutClickEvent;
+import com.vaadin.event.LayoutEvents.LayoutClickListener;
+import com.vaadin.tests.components.AbstractTestCase;
+import com.vaadin.ui.AbsoluteLayout;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Layout;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.Window;
+
+public class TestLayoutClickListeners extends AbstractTestCase {
+
+ private Log log = new Log(5);
+
+ public static class Log extends VerticalLayout {
+ List<Label> eventLabels = new ArrayList<Label>();
+
+ public Log(int nr) {
+ for (int i = 0; i < nr; i++) {
+ Label l = createEventLabel();
+ eventLabels.add(l);
+ addComponent(l);
+ }
+
+ setCaption("Events:");
+ }
+
+ public void clear() {
+ for (Label l : eventLabels) {
+ l.setValue("");
+ }
+ }
+
+ public void log(String event) {
+ int nr = eventLabels.size();
+ for (int i = nr - 1; i > 0; i--) {
+ eventLabels.get(i).setValue(eventLabels.get(i - 1).getValue());
+ }
+ eventLabels.get(0).setValue(event);
+ }
+
+ private Label createEventLabel() {
+ Label l = new Label(" ", Label.CONTENT_XHTML);
+ l.setWidth(null);
+ return l;
+ }
+
+ }
+
+ @Override
+ public void init() {
+ Window w = new Window("main window");
+ setMainWindow(w);
+ setTheme("tests-tickets");
+
+ HorizontalLayout layoutsLayout = new HorizontalLayout();
+ layoutsLayout.setSpacing(true);
+ w.setContent(layoutsLayout);
+
+ layoutsLayout.addComponent(createClickableGridLayout());
+ layoutsLayout.addComponent(createClickableVerticalLayout());
+ layoutsLayout.addComponent(createClickableAbsoluteLayout());
+
+ VerticalLayout mainLayout = new VerticalLayout();
+ mainLayout.setMargin(true);
+ mainLayout.setSpacing(true);
+ w.setContent(mainLayout);
+ mainLayout.addComponent(log);
+ mainLayout.addComponent(layoutsLayout);
+ }
+
+ private Component createClickableAbsoluteLayout() {
+ final AbsoluteLayout al = new AbsoluteLayout();
+ al.setCaption("AbsoluteLayout");
+ al.setStyleName("borders");
+ al.setWidth("300px");
+ al.setHeight("500px");
+ al.addComponent(new TextField("This is its caption",
+ "This is a textfield"), "top: 60px; left: 0px; width: 100px;");
+ al.addComponent(new TextField("Another textfield caption",
+ "This is another textfield"),
+ "top: 120px; left: 20px; width: 100px;");
+
+ al.addComponent(new Button("A button with its own click listener",
+ new Button.ClickListener() {
+
+ public void buttonClick(
+ com.vaadin.ui.Button.ClickEvent event) {
+ log.log("Button " + event.getButton().getCaption()
+ + " was clicked");
+
+ }
+ }));
+ al.addListener(new LayoutClickListener() {
+
+ public void layoutClick(LayoutClickEvent event) {
+ logLayoutClick("AbsoluteLayout", event.getChildComponent());
+ }
+ });
+
+ return al;
+
+ }
+
+ private Layout createClickableGridLayout() {
+
+ GridLayout gl = new GridLayout(4, 4);
+ gl.setHeight("400px");
+ gl.setStyleName("borders");
+ gl.setSpacing(true);
+ addContent(gl, 4);
+ TextField largeTextarea = new TextField("Large textarea");
+ largeTextarea.setWidth("100%");
+ largeTextarea.setHeight("99%");
+ gl.addComponent(largeTextarea, 0, 3, 3, 3);
+
+ gl.addListener(new LayoutClickListener() {
+
+ public void layoutClick(LayoutClickEvent event) {
+ logLayoutClick("GridLayout", event.getChildComponent());
+ }
+ });
+ gl.setRowExpandRatio(3, 1);
+ return wrap(gl, "GridLayout");
+ }
+
+ protected void logLayoutClick(String layout, Component comp) {
+ String target = "<none>";
+ if (comp != null) {
+ target = comp.getCaption();
+ if (target == null && comp instanceof Label) {
+ target = ((Label) comp).getValue().toString();
+ }
+ }
+ log.log(layout + ": Click on " + target);
+
+ }
+
+ private Layout createClickableVerticalLayout() {
+
+ VerticalLayout gl = new VerticalLayout();
+ addContent(gl, 5);
+
+ gl.addListener(new LayoutClickListener() {
+
+ public void layoutClick(LayoutClickEvent event) {
+ logLayoutClick("VerticalLayout", event.getChildComponent());
+
+ }
+ });
+
+ return wrap(gl, "Clickable VerticalLayout");
+ }
+
+ private void addContent(Layout gl, int nr) {
+ for (int i = 1; i < nr; i++) {
+ Label l = new Label("This is label " + i);
+ l.setWidth(null);
+ gl.addComponent(l);
+ }
+ for (int i = nr; i < nr * 2; i++) {
+ gl.addComponent(new TextField("This is tf" + i, "this is tf " + i));
+ }
+ }
+
+ private Layout wrap(Component c, String caption) {
+ VerticalLayout vl = new VerticalLayout();
+ Label l = new Label(caption);
+ l.setWidth(null);
+ vl.addComponent(l);
+ vl.addComponent(c);
+
+ return vl;
+ }
+
+ @Override
+ protected String getDescription() {
+ return "All layouts have click listeners attached and the events are shown in the event log at the top";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 3541;
+ }
+
+}