*/
package com.vaadin.terminal.gwt.client;
+import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.Event;
private boolean metaKey;
private boolean shiftKey;
private int type;
+ private int relativeX = -1;
+ private int relativeY = -1;
public int getButton() {
return button;
return shiftKey;
}
+ public int getRelativeX() {
+ return relativeX;
+ }
+
+ public int getRelativeY() {
+ return relativeY;
+ }
+
public MouseEventDetails(NativeEvent evt) {
+ this(evt, null);
+ }
+
+ public MouseEventDetails(NativeEvent evt, Element relativeToElement) {
button = evt.getButton();
clientX = evt.getClientX();
clientY = evt.getClientY();
metaKey = evt.getMetaKey();
shiftKey = evt.getShiftKey();
type = Event.getTypeInt(evt.getType());
+ if (relativeToElement != null) {
+ relativeX = getRelativeX(evt, relativeToElement);
+ relativeY = getRelativeY(evt, relativeToElement);
+ }
}
private MouseEventDetails() {
public String serialize() {
return "" + button + DELIM + clientX + DELIM + clientY + DELIM + altKey
+ DELIM + ctrlKey + DELIM + metaKey + DELIM + shiftKey + DELIM
- + type;
+ + type + DELIM + relativeX + DELIM + relativeY;
}
public static MouseEventDetails deSerialize(String serializedString) {
instance.metaKey = Boolean.valueOf(fields[5]).booleanValue();
instance.shiftKey = Boolean.valueOf(fields[6]).booleanValue();
instance.type = Integer.parseInt(fields[7]);
+ instance.relativeX = Integer.parseInt(fields[8]);
+ instance.relativeY = Integer.parseInt(fields[9]);
return instance;
}
return type == Event.ONDBLCLICK;
}
+ private static int getRelativeX(NativeEvent evt, Element target) {
+ return evt.getClientX() - target.getAbsoluteLeft()
+ + target.getScrollLeft()
+ + target.getOwnerDocument().getScrollLeft();
+ }
+
+ private static int getRelativeY(NativeEvent evt, Element target) {
+ return evt.getClientY() - target.getAbsoluteTop()
+ + target.getScrollTop()
+ + target.getOwnerDocument().getScrollTop();
+ }
+
}
/*
@ITMillApache2LicenseForJavaFiles@
*/
-package com.vaadin.terminal.gwt.client.ui;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import com.google.gwt.dom.client.NativeEvent;\r
-import com.google.gwt.event.dom.client.ContextMenuEvent;\r
-import com.google.gwt.event.dom.client.ContextMenuHandler;\r
-import com.google.gwt.event.dom.client.DomEvent;\r
-import com.google.gwt.event.dom.client.DoubleClickEvent;\r
-import com.google.gwt.event.dom.client.DoubleClickHandler;\r
-import com.google.gwt.event.dom.client.MouseUpEvent;\r
-import com.google.gwt.event.dom.client.MouseUpHandler;\r
-import com.google.gwt.event.shared.EventHandler;\r
-import com.google.gwt.event.shared.HandlerRegistration;\r
-import com.vaadin.terminal.gwt.client.ApplicationConnection;\r
-import com.vaadin.terminal.gwt.client.MouseEventDetails;\r
-import com.vaadin.terminal.gwt.client.Paintable;\r
-\r
-public abstract class ClickEventHandler implements DoubleClickHandler,\r
- ContextMenuHandler, MouseUpHandler {\r
-\r
- private HandlerRegistration doubleClickHandlerRegistration;\r
- private HandlerRegistration mouseUpHandlerRegistration;\r
- private HandlerRegistration contextMenuHandlerRegistration;\r
-\r
- protected String clickEventIdentifier;\r
- protected Paintable paintable;\r
- private ApplicationConnection client;\r
-\r
- public ClickEventHandler(Paintable paintable, String clickEventIdentifier) {\r
- this.paintable = paintable;\r
- this.clickEventIdentifier = clickEventIdentifier;\r
- }\r
-\r
- public void handleEventHandlerRegistration(ApplicationConnection client) {\r
- this.client = client;\r
+package com.vaadin.terminal.gwt.client.ui;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.event.dom.client.ContextMenuEvent;
+import com.google.gwt.event.dom.client.ContextMenuHandler;
+import com.google.gwt.event.dom.client.DomEvent;
+import com.google.gwt.event.dom.client.DoubleClickEvent;
+import com.google.gwt.event.dom.client.DoubleClickHandler;
+import com.google.gwt.event.dom.client.MouseUpEvent;
+import com.google.gwt.event.dom.client.MouseUpHandler;
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.Element;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.terminal.gwt.client.ApplicationConnection;
+import com.vaadin.terminal.gwt.client.MouseEventDetails;
+import com.vaadin.terminal.gwt.client.Paintable;
+
+public abstract class ClickEventHandler implements DoubleClickHandler,
+ ContextMenuHandler, MouseUpHandler {
+
+ private HandlerRegistration doubleClickHandlerRegistration;
+ private HandlerRegistration mouseUpHandlerRegistration;
+ private HandlerRegistration contextMenuHandlerRegistration;
+
+ protected String clickEventIdentifier;
+ protected Paintable paintable;
+ private ApplicationConnection client;
+
+ public ClickEventHandler(Paintable paintable, String clickEventIdentifier) {
+ this.paintable = paintable;
+ this.clickEventIdentifier = clickEventIdentifier;
+ }
+
+ public void handleEventHandlerRegistration(ApplicationConnection client) {
+ this.client = client;
// Handle registering/unregistering of click handler depending on if\r
// server side listeners have been added or removed.\r
- if (hasEventListener()) {\r
- if (mouseUpHandlerRegistration == null) {\r
- mouseUpHandlerRegistration = registerHandler(this, MouseUpEvent\r
- .getType());\r
- contextMenuHandlerRegistration = registerHandler(this,\r
- ContextMenuEvent.getType());\r
- doubleClickHandlerRegistration = registerHandler(this,\r
- DoubleClickEvent.getType());\r
- }\r
- } else {\r
- if (mouseUpHandlerRegistration != null) {\r
+ if (hasEventListener()) {
+ if (mouseUpHandlerRegistration == null) {
+ mouseUpHandlerRegistration = registerHandler(this, MouseUpEvent
+ .getType());
+ contextMenuHandlerRegistration = registerHandler(this,
+ ContextMenuEvent.getType());
+ doubleClickHandlerRegistration = registerHandler(this,
+ DoubleClickEvent.getType());
+ }
+ } else {
+ if (mouseUpHandlerRegistration != null) {
// Remove existing handlers\r
- doubleClickHandlerRegistration.removeHandler();\r
- mouseUpHandlerRegistration.removeHandler();\r
- contextMenuHandlerRegistration.removeHandler();\r
-\r
- contextMenuHandlerRegistration = null;\r
- mouseUpHandlerRegistration = null;\r
- doubleClickHandlerRegistration = null;\r
-\r
- }\r
- }\r
-\r
- }\r
-\r
- protected abstract <H extends EventHandler> HandlerRegistration registerHandler(\r
- final H handler, DomEvent.Type<H> type);\r
-\r
- protected ApplicationConnection getApplicationConnection() {\r
- return client;\r
- }\r
-\r
- public boolean hasEventListener() {\r
- return getApplicationConnection().hasEventListeners(paintable,\r
- clickEventIdentifier);\r
- }\r
-\r
- protected void fireClick(NativeEvent event) {\r
- ApplicationConnection client = getApplicationConnection();\r
- String pid = getApplicationConnection().getPid(paintable);\r
-\r
- MouseEventDetails mouseDetails = new MouseEventDetails(event);\r
-\r
- Map<String, Object> parameters = new HashMap<String, Object>();\r
- parameters.put("mouseDetails", mouseDetails.serialize());\r
- client.updateVariable(pid, clickEventIdentifier, parameters, true);\r
-\r
- }\r
-\r
- public void onContextMenu(ContextMenuEvent event) {\r
- if (hasEventListener()) {\r
+ doubleClickHandlerRegistration.removeHandler();
+ mouseUpHandlerRegistration.removeHandler();
+ contextMenuHandlerRegistration.removeHandler();
+
+ contextMenuHandlerRegistration = null;
+ mouseUpHandlerRegistration = null;
+ doubleClickHandlerRegistration = null;
+
+ }
+ }
+
+ }
+
+ protected abstract <H extends EventHandler> HandlerRegistration registerHandler(
+ final H handler, DomEvent.Type<H> type);
+
+ protected ApplicationConnection getApplicationConnection() {
+ return client;
+ }
+
+ public boolean hasEventListener() {
+ return getApplicationConnection().hasEventListeners(paintable,
+ clickEventIdentifier);
+ }
+
+ protected void fireClick(NativeEvent event) {
+ ApplicationConnection client = getApplicationConnection();
+ String pid = getApplicationConnection().getPid(paintable);
+
+ MouseEventDetails mouseDetails = new MouseEventDetails(event,
+ getRelativeToElement());
+
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("mouseDetails", mouseDetails.serialize());
+ client.updateVariable(pid, clickEventIdentifier, parameters, true);
+
+ }
+
+ public void onContextMenu(ContextMenuEvent event) {
+ if (hasEventListener()) {
// Prevent showing the browser's context menu when there is a right\r
// click listener.\r
- event.preventDefault();\r
- }\r
-\r
- }\r
-\r
- public void onMouseUp(MouseUpEvent event) {\r
+ event.preventDefault();
+ }
+
+ }
+
+ public void onMouseUp(MouseUpEvent event) {
// TODO For perfect accuracy we should check that a mousedown has\r
// occured on this element before this mouseup and that no mouseup\r
// has occured anywhere after that.\r
- if (hasEventListener()) {\r
+ if (hasEventListener()) {
// "Click" with left, right or middle button\r
- fireClick(event.getNativeEvent());\r
- }\r
- }\r
-\r
- public void onDoubleClick(DoubleClickEvent event) {\r
- if (hasEventListener()) {\r
- fireClick(event.getNativeEvent());\r
- }\r
- }\r
-\r
+ fireClick(event.getNativeEvent());
+ }
+ }
+
+ public void onDoubleClick(DoubleClickEvent event) {
+ if (hasEventListener()) {
+ fireClick(event.getNativeEvent());
+ }
+ }
+
+ /**
+ * Click event calculates and returns coordinates relative to the element
+ * returned by this method. Default implementation uses the root element of
+ * the widget. Override to provide a different relative element.
+ *
+ * @return The Element used for calculating relative coordinates for a click
+ * or null if no relative coordinates can be calculated.
+ */
+ protected Element getRelativeToElement() {
+ if (paintable instanceof Widget) {
+ return ((Widget) paintable).getElement();
+ }
+
+ return null;
+ }
+
}
\ No newline at end of file
import com.vaadin.event.Action.Handler;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.event.ItemClickEvent.ItemClickSource;
+import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DragSource;
import com.vaadin.event.dd.DropHandler;
* the column which header was pressed and details about the mouse event
* itself.
*/
- public static class HeaderClickEvent extends Component.Event {
+ public static class HeaderClickEvent extends ClickEvent {
public static final Method HEADER_CLICK_METHOD;
static {
// The property id of the column which header was pressed
private Object columnPropertyId;
- // The mouse details
- private MouseEventDetails details;
-
public HeaderClickEvent(Component source, Object propertyId,
MouseEventDetails details) {
- super(source);
- this.details = details;
+ super(source, details);
columnPropertyId = propertyId;
}
public Object getPropertyId() {
return columnPropertyId;
}
-
- /**
- * Returns the details of the mouse event like the mouse coordinates,
- * button pressed etc.
- *
- * @return The mouse details
- */
- public MouseEventDetails getEventDetails() {
- return details;
- }
}
/**
* the column which header was pressed and details about the mouse event
* itself.
*/
- public static class FooterClickEvent extends Component.Event {
+ public static class FooterClickEvent extends ClickEvent {
public static final Method FOOTER_CLICK_METHOD;
static {
// The property id of the column which header was pressed
private Object columnPropertyId;
- // The mouse details
- private MouseEventDetails details;
-
/**
* Constructor
*
*/
public FooterClickEvent(Component source, Object propertyId,
MouseEventDetails details) {
- super(source);
+ super(source, details);
columnPropertyId = propertyId;
- this.details = details;
}
/**
public Object getPropertyId() {
return columnPropertyId;
}
-
- /**
- * Returns the details of the mouse event like the mouse coordinates,
- * button pressed etc.
- *
- * @return The mouse details
- */
- public MouseEventDetails getEventDetails() {
- return details;
- }
}
/**