diff options
author | Matti Tahvonen <matti.tahvonen@itmill.com> | 2010-01-29 08:12:00 +0000 |
---|---|---|
committer | Matti Tahvonen <matti.tahvonen@itmill.com> | 2010-01-29 08:12:00 +0000 |
commit | 9c57035c34e8228c602b61672587c30b3be2fe1f (patch) | |
tree | e5b9fbb58abc9aa74e5eb12d5c8131887cd3a244 /src/com/vaadin/event | |
parent | 810017204ef10e2d5fbea15ed17b0c1e8a6b0daa (diff) | |
download | vaadin-framework-9c57035c34e8228c602b61672587c30b3be2fe1f.tar.gz vaadin-framework-9c57035c34e8228c602b61672587c30b3be2fe1f.zip |
dd
svn changeset:11054/svn branch:6.3_dd
Diffstat (limited to 'src/com/vaadin/event')
-rw-r--r-- | src/com/vaadin/event/AbstractDropHandler.java | 246 | ||||
-rw-r--r-- | src/com/vaadin/event/ComponentTransferrable.java | 12 | ||||
-rw-r--r-- | src/com/vaadin/event/DataBindedTransferrable.java | 9 | ||||
-rw-r--r-- | src/com/vaadin/event/DragRequest.java | 46 | ||||
-rw-r--r-- | src/com/vaadin/event/DropHandler.java | 7 | ||||
-rw-r--r-- | src/com/vaadin/event/HasDropHandler.java | 13 | ||||
-rw-r--r-- | src/com/vaadin/event/Transferable.java | 13 |
7 files changed, 346 insertions, 0 deletions
diff --git a/src/com/vaadin/event/AbstractDropHandler.java b/src/com/vaadin/event/AbstractDropHandler.java new file mode 100644 index 0000000000..da15dc61b6 --- /dev/null +++ b/src/com/vaadin/event/AbstractDropHandler.java @@ -0,0 +1,246 @@ +package com.vaadin.event; + +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; +import com.vaadin.terminal.gwt.client.ui.dd.DragAndDropManager.DragEventType; +import com.vaadin.ui.Component; + +/** + * An implementation of DropHandler interface. + * + * AcceptCriterion may be used to configure accept rules. Using them can result + * client side verifiable accept rules for quick feedback in UI. Still rules are + * also validate on server so implementor don't need to double check validity on + * {@link #receive(Transferable)} method. + * + */ +public abstract class AbstractDropHandler implements DropHandler { + /** + * Criterion that can be used create policy to accept/discard dragged + * content (presented by {@link Transferable}). + * + * TODO figure out how this can be done partly on client at least in some + * cases. isClientSideFilterable() tms. + * + */ + public interface AcceptCriterion { + public boolean accepts(Transferable transferable); + } + + public interface ClientSideVerifiable extends AcceptCriterion { + + /** + * May depend on state, like in OR or AND, so to be really + * ClientSideVerifiable needs to return true here (instead of just + * implementing marker interface). + */ + public boolean isClientSideVerifiable(); + + public void paint(PaintTarget target) throws PaintException; + + } + + private static final class AcceptAll implements ClientSideVerifiable { + public boolean accepts(Transferable transferable) { + return true; + } + + public boolean isClientSideVerifiable() { + return true; + } + + public void paint(PaintTarget target) throws PaintException { + target.startTag("acceptCriterion"); + target.addAttribute("name", "acceptAll"); + target.endTag("acceptCriterion"); + } + } + + public static class And implements ClientSideVerifiable { + private AcceptCriterion f1; + private AcceptCriterion f2; + + public And(AcceptCriterion f1, AcceptCriterion f2) { + this.f1 = f1; + this.f2 = f2; + } + + public boolean accepts(Transferable transferable) { + return f1.accepts(transferable) && f2.accepts(transferable); + } + + public boolean isClientSideVerifiable() { + boolean a1 = f1 instanceof ClientSideVerifiable ? ((ClientSideVerifiable) f1) + .isClientSideVerifiable() + : false; + boolean a2 = f2 instanceof ClientSideVerifiable ? ((ClientSideVerifiable) f2) + .isClientSideVerifiable() + : false; + return a1 && a2; + } + + public void paint(PaintTarget target) throws PaintException { + target.startTag("acceptCriterion"); + target.addAttribute("name", "and"); + ((ClientSideVerifiable) f1).paint(target); + ((ClientSideVerifiable) f2).paint(target); + target.endTag("acceptCriterion"); + } + } + + public static class ComponentFilter implements ClientSideVerifiable { + private Component component; + + public ComponentFilter(Component component) { + this.component = component; + } + + public boolean accepts(Transferable transferable) { + if (transferable instanceof ComponentTransferrable) { + return ((ComponentTransferrable) transferable) + .getSourceComponent() == component; + } else { + return false; + } + } + + public boolean isClientSideVerifiable() { + return true; + } + + public void paint(PaintTarget target) throws PaintException { + target.startTag("acceptCriterion"); + target.addAttribute("name", "component"); + target.addAttribute("component", component); + target.endTag("acceptCriterion"); + } + } + + private static final class IsDataBinded implements ClientSideVerifiable { + public boolean accepts(Transferable transferable) { + if (transferable instanceof DataBindedTransferrable) { + return ((DataBindedTransferrable) transferable).getItemId() != null; + } + return false; + } + + public boolean isClientSideVerifiable() { + return true; + } + + public void paint(PaintTarget target) throws PaintException { + target.startTag("acceptCriterion"); + target.addAttribute("name", "needsItemId"); + target.endTag("acceptCriterion"); + } + } + + public class Not implements AcceptCriterion { + private AcceptCriterion acceptCriterion; + + public Not(AcceptCriterion acceptCriterion) { + this.acceptCriterion = acceptCriterion; + } + + public boolean accepts(Transferable transferable) { + return !acceptCriterion.accepts(transferable); + } + + } + + public class Or implements AcceptCriterion { + private AcceptCriterion f1; + private AcceptCriterion f2; + + Or(AcceptCriterion f1, AcceptCriterion f2) { + this.f1 = f1; + this.f2 = f2; + } + + public boolean accepts(Transferable transferable) { + return f1.accepts(transferable) || f2.accepts(transferable); + } + } + + public static class OverTreeNode implements ClientSideVerifiable { + + public boolean accepts(Transferable transferable) { + try { + return transferable.getData("detail").toString().toLowerCase() + .equals("center"); + } catch (Exception e) { + return false; + } + } + + public boolean isClientSideVerifiable() { + return true; + } + + public void paint(PaintTarget target) throws PaintException { + target.startTag("acceptCriterion"); + target.addAttribute("name", "overTreeNode"); + target.endTag("acceptCriterion"); + } + + } + + public static final AcceptCriterion CRITERION_ACCEPT_ALL = new AcceptAll(); + + public static final AcceptCriterion CRITERION_HAS_ITEM_ID = new IsDataBinded(); + + private AcceptCriterion acceptCriterion = CRITERION_ACCEPT_ALL; + + /* + * (non-Javadoc) + * + * @seecom.vaadin.event.DropHandler#acceptTransferrable(com.vaadin.event. + * Transferable) + */ + public boolean acceptTransferrable(Transferable transferable) { + return acceptCriterion.accepts(transferable); + } + + private boolean clientSideVerifiable() { + if (acceptCriterion instanceof ClientSideVerifiable) { + return ((ClientSideVerifiable) acceptCriterion) + .isClientSideVerifiable(); + } + return false; + } + + public void handleDragRequest(DragRequest event) { + boolean acceptTransferrable = acceptTransferrable(event + .getTransferrable()); + if (acceptTransferrable) { + if (event.getType() == DragEventType.DROP) { + receive(event.getTransferrable()); + } else { + event.setResponseParameter("accepted", true); + } + + } + } + + public void paint(PaintTarget target) throws PaintException { + target.startTag("dh"); + if (!clientSideVerifiable()) { + target.addAttribute("serverValidate", true); + } else { + ((ClientSideVerifiable) acceptCriterion).paint(target); + } + target.endTag("dh"); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.event.DropHandler#receive(com.vaadin.event.Transferable) + */ + public abstract void receive(Transferable transferable); + + public void setAcceptCriterion(AcceptCriterion acceptCriterion) { + this.acceptCriterion = acceptCriterion; + } + +} diff --git a/src/com/vaadin/event/ComponentTransferrable.java b/src/com/vaadin/event/ComponentTransferrable.java new file mode 100644 index 0000000000..e0048d933d --- /dev/null +++ b/src/com/vaadin/event/ComponentTransferrable.java @@ -0,0 +1,12 @@ +package com.vaadin.event; + +import com.vaadin.ui.Component; + +public interface ComponentTransferrable extends Transferable { + + /** + * @return the component where the drag operation started + */ + public Component getSourceComponent(); + +} diff --git a/src/com/vaadin/event/DataBindedTransferrable.java b/src/com/vaadin/event/DataBindedTransferrable.java new file mode 100644 index 0000000000..8d131eb741 --- /dev/null +++ b/src/com/vaadin/event/DataBindedTransferrable.java @@ -0,0 +1,9 @@ +package com.vaadin.event; + +public interface DataBindedTransferrable extends ComponentTransferrable { + + public Object getItemId(); + + public Object getPropertyId(); + +} diff --git a/src/com/vaadin/event/DragRequest.java b/src/com/vaadin/event/DragRequest.java new file mode 100644 index 0000000000..b1bd2a6316 --- /dev/null +++ b/src/com/vaadin/event/DragRequest.java @@ -0,0 +1,46 @@ +package com.vaadin.event; + +import java.util.HashMap; +import java.util.Map; + +import com.vaadin.terminal.gwt.client.ui.dd.DragAndDropManager.DragEventType; + +public class DragRequest { + + private DragEventType dragEventType; + private Transferable transferable; + private Map<String, Object> responseData; + + public DragRequest(DragEventType dragEventType, Transferable transferable) { + this.dragEventType = dragEventType; + this.transferable = transferable; + } + + public Transferable getTransferrable() { + return transferable; + } + + public DragEventType getType() { + return dragEventType; + } + + public Map<String, Object> getResponseData() { + return responseData; + } + + /** + * DropHanler can pass simple parameters back to client side. + * + * TODO define which types are supported (most likely the same as in UIDL) + * + * @param key + * @param value + */ + public void setResponseParameter(String key, Object value) { + if (responseData == null) { + responseData = new HashMap<String, Object>(); + } + responseData.put(key, value); + } + +} diff --git a/src/com/vaadin/event/DropHandler.java b/src/com/vaadin/event/DropHandler.java new file mode 100644 index 0000000000..f44ca15569 --- /dev/null +++ b/src/com/vaadin/event/DropHandler.java @@ -0,0 +1,7 @@ +package com.vaadin.event; + +public interface DropHandler { + + public void handleDragRequest(DragRequest event); + +}
\ No newline at end of file diff --git a/src/com/vaadin/event/HasDropHandler.java b/src/com/vaadin/event/HasDropHandler.java new file mode 100644 index 0000000000..73ce74c392 --- /dev/null +++ b/src/com/vaadin/event/HasDropHandler.java @@ -0,0 +1,13 @@ +package com.vaadin.event; + +import com.vaadin.ui.Component; + +/** + * Implementing component most commonly has also setDropHandler method, but + * not polluting interface here as component might also have internal + * AbstractDropHandler implementation. + * + */ +public interface HasDropHandler extends Component { + public DropHandler getDropHandler(); +}
\ No newline at end of file diff --git a/src/com/vaadin/event/Transferable.java b/src/com/vaadin/event/Transferable.java new file mode 100644 index 0000000000..5ce4adfb8f --- /dev/null +++ b/src/com/vaadin/event/Transferable.java @@ -0,0 +1,13 @@ +package com.vaadin.event; + +import java.util.Collection; + +public interface Transferable { + + public Object getData(String dataFlawor); + + public void setData(String dataFlawor, Object value); + + public Collection<String> getDataFlawors(); + +} |