Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<name>Daniel Kurka</name>
<url>http://www.daniel-kurka.de</url>
</contributor>
<contributor>
<name>Lasse Loevdahl</name>
</contributor>
</contributors>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2012 Lasse Løvdahl
*
* 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.googlecode.mgwt.dom.client.recognizer.swipe;


/**
* A Aggregate handler for {@link SwipeStartEvent}, {@link SwipeEndEvent} and {@link SwipeMoveEvent}s
*
* @author Lasse Loevdahl
*
*/
public interface SwipeHandler extends SwipeMoveHandler, SwipeStartHandler, SwipeEndHandler {
}
79 changes: 76 additions & 3 deletions src/main/java/com/googlecode/mgwt/ui/client/widget/CellList.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,27 @@
import com.googlecode.mgwt.dom.client.event.touch.TouchMoveEvent;
import com.googlecode.mgwt.dom.client.event.touch.TouchStartEvent;
import com.googlecode.mgwt.dom.client.recognizer.EventPropagator;
import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapEvent;
import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapHandler;
import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEndEvent;
import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEvent;
import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEvent.DIRECTION;
import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeHandler;
import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeMoveEvent;
import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeStartEvent;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.theme.base.ListCss;
import com.googlecode.mgwt.ui.client.widget.celllist.Cell;
import com.googlecode.mgwt.ui.client.widget.celllist.CellLongTapEvent;
import com.googlecode.mgwt.ui.client.widget.celllist.CellLongTapHandler;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedHandler;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSwipedEvent;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSwipedHandler;
import com.googlecode.mgwt.ui.client.widget.celllist.HasCellLongTapHandler;
import com.googlecode.mgwt.ui.client.widget.celllist.HasCellSelectedHandler;
import com.googlecode.mgwt.ui.client.widget.celllist.HasCellSwipedHandler;
import com.googlecode.mgwt.ui.client.widget.touch.TouchWidget;

/**
Expand Down Expand Up @@ -77,7 +91,7 @@
* @author Daniel Kurka
* @param <T> the type of the model to render
*/
public class CellList<T> extends Composite implements HasCellSelectedHandler {
public class CellList<T> extends Composite implements HasCellSelectedHandler, HasCellSwipedHandler, HasCellLongTapHandler {

protected static final EventPropagator EVENT_PROPAGATOR = GWT.create(EventPropagator.class);

Expand Down Expand Up @@ -115,9 +129,10 @@ public UlTouchWidget() {

}

private class InternalTouchHandler implements TouchHandler {
private class InternalTouchHandler implements TouchHandler, SwipeHandler, LongTapHandler {

private boolean moved;
private boolean longTapped;
private int index;
private Element node;
private int x;
Expand Down Expand Up @@ -151,7 +166,7 @@ public void onTouchEnd(TouchEndEvent event) {
node.removeClassName(css.selected());
stopTimer();
}
if (started && !moved && index != -1) {
if (started && !moved && index != -1 && !longTapped) {
fireSelectionAtIndex(index, originalElement);
}
node = null;
Expand All @@ -170,6 +185,7 @@ public void onTouchStart(TouchStartEvent event) {
node.removeClassName(css.selected());
}
moved = false;
longTapped = false;
index = -1;
// Get the event target.
EventTarget eventTarget = event.getNativeEvent().getEventTarget();
Expand Down Expand Up @@ -223,6 +239,40 @@ public void onTouchStart(TouchStartEvent event) {
}

}

private int startedAt(SwipeEvent<?> event) {
switch (event.getDirection()) {
case BOTTOM_TO_TOP:
case TOP_TO_BOTTOM:
return y;
case LEFT_TO_RIGHT:
case RIGHT_TO_LEFT:
return x;
default:
return 0;
}
}

@Override
public void onSwipeMove(SwipeMoveEvent event) {
fireSwipedAtIndex(index, originalElement, startedAt(event), event.getDirection(), event.getDistance());
}

@Override
public void onSwipeStart(SwipeStartEvent event) {

}

@Override
public void onSwipeEnd(SwipeEndEvent event) {
fireSwipedAtIndex(index, originalElement, startedAt(event), event.getDirection(), event.getDistance());
}

@Override
public void onLongTap(LongTapEvent event) {
longTapped = true;
fireLongTapAtIndex(index, originalElement);
}
}

private UlTouchWidget main;
Expand Down Expand Up @@ -288,6 +338,16 @@ public HandlerRegistration addCellSelectedHandler(CellSelectedHandler cellSelect
return addHandler(cellSelectedHandler, CellSelectedEvent.getType());
}

@Override
public HandlerRegistration addCellSwipedHandler(CellSwipedHandler cellSwipedHandler) {
return addHandler(cellSwipedHandler, CellSwipedEvent.getType());
}

@Override
public HandlerRegistration addCellLongTapHandler(CellLongTapHandler cellLongTapHandler) {
return addHandler(cellLongTapHandler, CellLongTapEvent.getType());
}

/*
* (non-Javadoc)
*
Expand All @@ -304,6 +364,11 @@ protected void onAttach() {
handlers.add(main.addTouchStartHandler(internalTouchHandler));
handlers.add(main.addTouchMoveHandler(internalTouchHandler));

handlers.add(main.addSwipeEndHandler(internalTouchHandler));
handlers.add(main.addSwipeStartHandler(internalTouchHandler));
handlers.add(main.addSwipeMoveHandler(internalTouchHandler));

handlers.add(main.addLongTapHandler(internalTouchHandler));
}

/*
Expand Down Expand Up @@ -427,6 +492,14 @@ protected void fireSelectionAtIndex(int index, Element element) {
EVENT_PROPAGATOR.fireEvent(this, new CellSelectedEvent(index, element));
}

protected void fireSwipedAtIndex(int index, Element element, int startedAt, DIRECTION direction, int distance) {
EVENT_PROPAGATOR.fireEvent(this, new CellSwipedEvent(index, element, startedAt, direction, distance));
}

protected void fireLongTapAtIndex(int index, Element element) {
EVENT_PROPAGATOR.fireEvent(this, new CellLongTapEvent(index, element));
}

protected void startTimer(final Element node) {
if (timer != null) {
timer.cancel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.googlecode.mgwt.ui.client.widget.base;

import com.google.gwt.resources.client.ImageResource;

/**
* Widgets that implement this interface can change their icon
*
* @author Lasse Løvdahl
* @version $Id: $
*/
public interface HasImageResource {

/**
* @return the {@link com.google.gwt.resources.client.ImageResource} of the icon for this widget
*/
public ImageResource getIcon();

/**
* Sets the icon of this widget.
*
* @param the {@link com.google.gwt.resources.client.ImageResource} of the icon to be shown by this widget
*/
public void setIcon(ImageResource icon);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.theme.base.ButtonBarButtonCss;
import com.googlecode.mgwt.ui.client.widget.base.ButtonBase;
import com.googlecode.mgwt.ui.client.widget.base.HasImageResource;

/**
* <h1>Base class for all buttons in the button bar</h1>
Expand All @@ -53,8 +54,7 @@
* @author Daniel Kurka
*
*/
public class ButtonBarButtonBase extends ButtonBase {

public class ButtonBarButtonBase extends ButtonBase implements HasImageResource {
protected final static IconHandler ICON_HANDLER = GWT.create(IconHandler.class);

public interface IconHandler {
Expand Down Expand Up @@ -90,6 +90,8 @@ public void setIcons(Element element, ImageResource icon, ImageResource highligh
}
}

private ImageResource icon;

public ButtonBarButtonBase(ImageResource icon) {
this(MGWTStyle.getTheme().getMGWTClientBundle().getButtonBarButtonCss(), icon, MGWTStyle.getTheme().getMGWTClientBundle().getButtonBarHighlightImage());
}
Expand All @@ -98,22 +100,22 @@ public ButtonBarButtonBase(ImageResource icon, ImageResource highlight) {
this(MGWTStyle.getTheme().getMGWTClientBundle().getButtonBarButtonCss(), icon, highlight);
}

public ButtonBarButtonBase(ButtonBarButtonCss css, final ImageResource icon, final ImageResource highlight) {
public ButtonBarButtonBase(ButtonBarButtonCss css, ImageResource icon, final ImageResource highlight) {
super(css);
ICON_HANDLER.setIcons(getElement(), icon, highlight, false);
setIcon(icon);
addStyleName(css.barButton());

addTouchHandler(new TouchHandler() {

@Override
public void onTouchCanceled(TouchCancelEvent event) {
ICON_HANDLER.setIcons(getElement(), icon, highlight, false);
ICON_HANDLER.setIcons(getElement(), ButtonBarButtonBase.this.icon, highlight, false);

}

@Override
public void onTouchEnd(TouchEndEvent event) {
ICON_HANDLER.setIcons(getElement(), icon, highlight, false);
ICON_HANDLER.setIcons(getElement(), ButtonBarButtonBase.this.icon, highlight, false);

}

Expand All @@ -124,10 +126,26 @@ public void onTouchMove(TouchMoveEvent event) {

@Override
public void onTouchStart(TouchStartEvent event) {
ICON_HANDLER.setIcons(getElement(), icon, highlight, true);
ICON_HANDLER.setIcons(getElement(), ButtonBarButtonBase.this.icon, highlight, true);

}
});
}


/**
* @return the {@link com.google.gwt.resources.client.ImageResource} of the icon for this button
*/
public ImageResource getIcon() {
return icon;
}

/**
* Sets the icon of the button
* @param the {@link com.google.gwt.resources.client.ImageResource} of the icon to be shown by this button
*/
public void setIcon(ImageResource icon) {
this.icon = icon;
ICON_HANDLER.setIcons(getElement(), icon, null, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2010 Lasse Løvdahl
*
* 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.googlecode.mgwt.ui.client.widget.celllist;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.GwtEvent;

/**
* This event is fired when a cell {@link Cell} is long tapped
*
* @author Lasse Loevdahl
* @version $Id: $
*/
public class CellLongTapEvent extends GwtEvent<CellLongTapHandler> {

private static final GwtEvent.Type<CellLongTapHandler> TYPE = new GwtEvent.Type<CellLongTapHandler>();
private final int index;
private final Element targetElement;

/**
* Construct a cell long tapped event
*
* @param index
* the index of the cell that was selected
* @param targetElement
*/
public CellLongTapEvent(int index, Element targetElement) {
this.index = index;
this.targetElement = targetElement;
}



/** {@inheritDoc} */
@Override
public com.google.gwt.event.shared.GwtEvent.Type<CellLongTapHandler> getAssociatedType() {
return TYPE;
}

/*
* (non-Javadoc)
*
* @see
* com.google.gwt.event.shared.GwtEvent#dispatch(com.google.gwt.event.shared
* .EventHandler)
*/
/** {@inheritDoc} */
@Override
protected void dispatch(CellLongTapHandler handler) {
handler.onLongTap(this);
}

/**
* <p>
* getType
* </p>
*
* @return a {@link com.google.gwt.event.shared.GwtEvent.Type} object.
*/
public static GwtEvent.Type<CellLongTapHandler> getType() {
return TYPE;
}

/**
* get the index of the selected cell
*
* @return the index of the tapped cell
*/
public int getIndex() {
return index;
}

/**
* The DOM element that was swiped
*
* @return the {@link com.google.gwt.dom.client.Element} that fired the swipe
*/
public Element getTargetElement() {
return targetElement;
}

}
Loading