diff options
Diffstat (limited to 'server/src/com/vaadin/event/dd')
17 files changed, 1109 insertions, 0 deletions
diff --git a/server/src/com/vaadin/event/dd/DragAndDropEvent.java b/server/src/com/vaadin/event/dd/DragAndDropEvent.java new file mode 100644 index 0000000000..d7d2b24f94 --- /dev/null +++ b/server/src/com/vaadin/event/dd/DragAndDropEvent.java @@ -0,0 +1,62 @@ +/* + * Copyright 2011 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.event.dd; + +import java.io.Serializable; + +import com.vaadin.event.Transferable; +import com.vaadin.event.dd.acceptcriteria.AcceptCriterion; + +/** + * DragAndDropEvent wraps information related to drag and drop operation. It is + * passed by terminal implementation for + * {@link DropHandler#drop(DragAndDropEvent)} and + * {@link AcceptCriterion#accept(DragAndDropEvent)} methods. + * <p> + * DragAndDropEvent instances contains both the dragged data in + * {@link Transferable} (generated by {@link DragSource} and details about the + * current drop event in {@link TargetDetails} (generated by {@link DropTarget}. + * + * @since 6.3 + * + */ +public class DragAndDropEvent implements Serializable { + private Transferable transferable; + private TargetDetails dropTargetDetails; + + public DragAndDropEvent(Transferable transferable, + TargetDetails dropTargetDetails) { + this.transferable = transferable; + this.dropTargetDetails = dropTargetDetails; + } + + /** + * @return the Transferable instance representing the data dragged in this + * drag and drop event + */ + public Transferable getTransferable() { + return transferable; + } + + /** + * @return the TargetDetails containing drop target related details of drag + * and drop operation + */ + public TargetDetails getTargetDetails() { + return dropTargetDetails; + } + +} diff --git a/server/src/com/vaadin/event/dd/DragSource.java b/server/src/com/vaadin/event/dd/DragSource.java new file mode 100644 index 0000000000..f42fd8b61b --- /dev/null +++ b/server/src/com/vaadin/event/dd/DragSource.java @@ -0,0 +1,64 @@ +/* + * Copyright 2011 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.event.dd; + +import java.util.Map; + +import com.vaadin.event.Transferable; +import com.vaadin.event.dd.acceptcriteria.AcceptCriterion; +import com.vaadin.ui.Component; +import com.vaadin.ui.Tree; + +/** + * DragSource is a {@link Component} that builds a {@link Transferable} for a + * drag and drop operation. + * <p> + * In Vaadin the drag and drop operation practically starts from client side + * component. The client side component initially defines the data that will be + * present in {@link Transferable} object on server side. If the server side + * counterpart of the component implements this interface, terminal + * implementation lets it create the {@link Transferable} instance from the raw + * client side "seed data". This way server side implementation may translate or + * extend the data that will be available for {@link DropHandler}. + * + * @since 6.3 + * + */ +public interface DragSource extends Component { + + /** + * DragSource may convert data added by client side component to meaningful + * values for server side developer or add other data based on it. + * + * <p> + * For example Tree converts item identifiers to generated string keys for + * the client side. Vaadin developer don't and can't know anything about + * these generated keys, only about item identifiers. When tree node is + * dragged client puts that key to {@link Transferable}s client side + * counterpart. In {@link Tree#getTransferable(Map)} the key is converted + * back to item identifier that the server side developer can use. + * <p> + * + * @since 6.3 + * @param rawVariables + * the data that client side initially included in + * {@link Transferable}s client side counterpart. + * @return the {@link Transferable} instance that will be passed to + * {@link DropHandler} (and/or {@link AcceptCriterion}) + */ + public Transferable getTransferable(Map<String, Object> rawVariables); + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/DropHandler.java b/server/src/com/vaadin/event/dd/DropHandler.java new file mode 100644 index 0000000000..36d25e5da1 --- /dev/null +++ b/server/src/com/vaadin/event/dd/DropHandler.java @@ -0,0 +1,73 @@ +/* + * Copyright 2011 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.event.dd; + +import java.io.Serializable; + +import com.vaadin.event.Transferable; +import com.vaadin.event.dd.acceptcriteria.AcceptAll; +import com.vaadin.event.dd.acceptcriteria.AcceptCriterion; +import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion; + +/** + * DropHandlers contain the actual business logic for drag and drop operations. + * <p> + * The {@link #drop(DragAndDropEvent)} method is used to receive the transferred + * data and the {@link #getAcceptCriterion()} method contains the (possibly + * client side verifiable) criterion whether the dragged data will be handled at + * all. + * + * @since 6.3 + * + */ +public interface DropHandler extends Serializable { + + /** + * Drop method is called when the end user has finished the drag operation + * on a {@link DropTarget} and {@link DragAndDropEvent} has passed + * {@link AcceptCriterion} defined by {@link #getAcceptCriterion()} method. + * The actual business logic of drag and drop operation is implemented into + * this method. + * + * @param event + * the event related to this drop + */ + public void drop(DragAndDropEvent event); + + /** + * Returns the {@link AcceptCriterion} used to evaluate whether the + * {@link Transferable} will be handed over to + * {@link DropHandler#drop(DragAndDropEvent)} method. If client side can't + * verify the {@link AcceptCriterion}, the same criteria may be tested also + * prior to actual drop - during the drag operation. + * <p> + * Based on information from {@link AcceptCriterion} components may display + * some hints for the end user whether the drop will be accepted or not. + * <p> + * Vaadin contains a variety of criteria built in that can be composed to + * more complex criterion. If the build in criteria are not enough, + * developer can use a {@link ServerSideCriterion} or build own custom + * criterion with client side counterpart. + * <p> + * If developer wants to handle everything in the + * {@link #drop(DragAndDropEvent)} method, {@link AcceptAll} instance can be + * returned. + * + * @return the {@link AcceptCriterion} + */ + public AcceptCriterion getAcceptCriterion(); + +} diff --git a/server/src/com/vaadin/event/dd/DropTarget.java b/server/src/com/vaadin/event/dd/DropTarget.java new file mode 100644 index 0000000000..9a4ef05cae --- /dev/null +++ b/server/src/com/vaadin/event/dd/DropTarget.java @@ -0,0 +1,54 @@ +/* + * Copyright 2011 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.event.dd; + +import java.util.Map; + +import com.vaadin.ui.Component; + +/** + * DropTarget is an interface for components supporting drop operations. A + * component that wants to receive drop events should implement this interface + * and provide a {@link DropHandler} which will handle the actual drop event. + * + * @since 6.3 + */ +public interface DropTarget extends Component { + + /** + * @return the drop hanler that will receive the dragged data or null if + * drops are not currently accepted + */ + public DropHandler getDropHandler(); + + /** + * Called before the {@link DragAndDropEvent} is passed to + * {@link DropHandler}. Implementation may for example translate the drop + * target details provided by the client side (drop target) to meaningful + * server side values. If null is returned the terminal implementation will + * automatically create a {@link TargetDetails} with raw client side data. + * + * @see DragSource#getTransferable(Map) + * + * @param clientVariables + * data passed from the DropTargets client side counterpart. + * @return A DropTargetDetails object with the translated data or null to + * use a default implementation. + */ + public TargetDetails translateDropTargetDetails( + Map<String, Object> clientVariables); + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/TargetDetails.java b/server/src/com/vaadin/event/dd/TargetDetails.java new file mode 100644 index 0000000000..eb67b49090 --- /dev/null +++ b/server/src/com/vaadin/event/dd/TargetDetails.java @@ -0,0 +1,49 @@ +/* + * Copyright 2011 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.event.dd; + +import java.io.Serializable; + +import com.vaadin.ui.Tree.TreeTargetDetails; + +/** + * TargetDetails wraps drop target related information about + * {@link DragAndDropEvent}. + * <p> + * When a TargetDetails object is used in {@link DropHandler} it is often + * preferable to cast the TargetDetails to an implementation provided by + * DropTarget like {@link TreeTargetDetails}. They often provide a better typed, + * drop target specific API. + * + * @since 6.3 + * + */ +public interface TargetDetails extends Serializable { + + /** + * Gets target data associated with the given string key + * + * @param key + * @return The data associated with the key + */ + public Object getData(String key); + + /** + * @return the drop target on which the {@link DragAndDropEvent} happened. + */ + public DropTarget getTarget(); + +} diff --git a/server/src/com/vaadin/event/dd/TargetDetailsImpl.java b/server/src/com/vaadin/event/dd/TargetDetailsImpl.java new file mode 100644 index 0000000000..7c0c98bb79 --- /dev/null +++ b/server/src/com/vaadin/event/dd/TargetDetailsImpl.java @@ -0,0 +1,58 @@ +/* + * Copyright 2011 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.event.dd; + +import java.util.HashMap; +import java.util.Map; + +/** + * A HashMap backed implementation of {@link TargetDetails} for terminal + * implementation and for extension. + * + * @since 6.3 + * + */ +@SuppressWarnings("serial") +public class TargetDetailsImpl implements TargetDetails { + + private HashMap<String, Object> data = new HashMap<String, Object>(); + private DropTarget dropTarget; + + protected TargetDetailsImpl(Map<String, Object> rawDropData) { + data.putAll(rawDropData); + } + + public TargetDetailsImpl(Map<String, Object> rawDropData, + DropTarget dropTarget) { + this(rawDropData); + this.dropTarget = dropTarget; + } + + @Override + public Object getData(String key) { + return data.get(key); + } + + public Object setData(String key, Object value) { + return data.put(key, value); + } + + @Override + public DropTarget getTarget() { + return dropTarget; + } + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/AcceptAll.java b/server/src/com/vaadin/event/dd/acceptcriteria/AcceptAll.java new file mode 100644 index 0000000000..a8ef49c21c --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/AcceptAll.java @@ -0,0 +1,48 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.dd.DragAndDropEvent; + +/** + * Criterion that accepts all drops anywhere on the component. + * <p> + * Note! Class is singleton, use {@link #get()} method to get the instance. + * + * + * @since 6.3 + * + */ +public final class AcceptAll extends ClientSideCriterion { + + private static final long serialVersionUID = 7406683402153141461L; + private static AcceptCriterion singleton = new AcceptAll(); + + private AcceptAll() { + } + + public static AcceptCriterion get() { + return singleton; + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + return true; + } +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/AcceptCriterion.java b/server/src/com/vaadin/event/dd/acceptcriteria/AcceptCriterion.java new file mode 100644 index 0000000000..7b04efc4b3 --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/AcceptCriterion.java @@ -0,0 +1,87 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import java.io.Serializable; + +import com.vaadin.event.Transferable; +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.event.dd.DropHandler; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * Criterion that can be used create policy to accept/discard dragged content + * (presented by {@link Transferable}). + * + * The drag and drop mechanism will verify the criteria returned by + * {@link DropHandler#getAcceptCriterion()} before calling + * {@link DropHandler#drop(DragAndDropEvent)}. + * + * The criteria can be evaluated either on the client (browser - see + * {@link ClientSideCriterion}) or on the server (see + * {@link ServerSideCriterion}). If no constraints are needed, an + * {@link AcceptAll} can be used. + * + * In addition to accepting or rejecting a possible drop, criteria can provide + * additional hints for client side painting. + * + * @see DropHandler + * @see ClientSideCriterion + * @see ServerSideCriterion + * + * @since 6.3 + */ +public interface AcceptCriterion extends Serializable { + + /** + * Returns whether the criteria can be checked on the client or whether a + * server request is needed to check the criteria. + * + * This requirement may depend on the state of the criterion (e.g. logical + * operations between criteria), so this cannot be based on a marker + * interface. + */ + public boolean isClientSideVerifiable(); + + public void paint(PaintTarget target) throws PaintException; + + /** + * This needs to be implemented iff criterion does some lazy server side + * initialization. The UIDL painted in this method will be passed to client + * side drop handler implementation. Implementation can assume that + * {@link #accept(DragAndDropEvent)} is called before this method. + * + * @param target + * @throws PaintException + */ + public void paintResponse(PaintTarget target) throws PaintException; + + /** + * Validates the data in event to be appropriate for the + * {@link DropHandler#drop(DragAndDropEvent)} method. + * <p> + * Note that even if your criterion is validated on client side, you should + * always validate the data on server side too. + * + * @param dragEvent + * @return + */ + public boolean accept(DragAndDropEvent dragEvent); +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/And.java b/server/src/com/vaadin/event/dd/acceptcriteria/And.java new file mode 100644 index 0000000000..3d11ecf7bf --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/And.java @@ -0,0 +1,66 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * A compound criterion that accepts the drag if all of its criteria accepts the + * drag. + * + * @see Or + * + * @since 6.3 + * + */ +public class And extends ClientSideCriterion { + + private static final long serialVersionUID = -5242574480825471748L; + protected ClientSideCriterion[] criteria; + + /** + * + * @param criteria + * criteria of which the And criterion will be composed + */ + public And(ClientSideCriterion... criteria) { + this.criteria = criteria; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + for (ClientSideCriterion crit : criteria) { + crit.paint(target); + } + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + for (ClientSideCriterion crit : criteria) { + if (!crit.accept(dragEvent)) { + return false; + } + } + return true; + } + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/ClientSideCriterion.java b/server/src/com/vaadin/event/dd/acceptcriteria/ClientSideCriterion.java new file mode 100644 index 0000000000..be7e2d4033 --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/ClientSideCriterion.java @@ -0,0 +1,73 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import java.io.Serializable; + +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * Parent class for criteria that can be completely validated on client side. + * All classes that provide criteria that can be completely validated on client + * side should extend this class. + * + * It is recommended that subclasses of ClientSideCriterion re-validate the + * condition on the server side in + * {@link AcceptCriterion#accept(com.vaadin.event.dd.DragAndDropEvent)} after + * the client side validation has accepted a transfer. + * + * @since 6.3 + */ +public abstract class ClientSideCriterion implements Serializable, + AcceptCriterion { + + /* + * All criteria that extend this must be completely validatable on client + * side. + * + * (non-Javadoc) + * + * @see + * com.vaadin.event.dd.acceptCriteria.AcceptCriterion#isClientSideVerifiable + * () + */ + @Override + public final boolean isClientSideVerifiable() { + return true; + } + + @Override + public void paint(PaintTarget target) throws PaintException { + target.startTag("-ac"); + target.addAttribute("name", getIdentifier()); + paintContent(target); + target.endTag("-ac"); + } + + protected void paintContent(PaintTarget target) throws PaintException { + } + + protected String getIdentifier() { + return getClass().getCanonicalName(); + } + + @Override + public final void paintResponse(PaintTarget target) throws PaintException { + // NOP, nothing to do as this is client side verified criterion + } + +} diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/ContainsDataFlavor.java b/server/src/com/vaadin/event/dd/acceptcriteria/ContainsDataFlavor.java new file mode 100644 index 0000000000..55ee17fea9 --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/ContainsDataFlavor.java @@ -0,0 +1,65 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.Transferable; +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * A Criterion that checks whether {@link Transferable} contains given data + * flavor. The developer might for example accept the incoming data only if it + * contains "Url" or "Text". + * + * @since 6.3 + */ +public class ContainsDataFlavor extends ClientSideCriterion { + + private String dataFlavorId; + + /** + * Constructs a new instance of {@link ContainsDataFlavor}. + * + * @param dataFlawor + * the type of data that will be checked from + * {@link Transferable} + */ + public ContainsDataFlavor(String dataFlawor) { + dataFlavorId = dataFlawor; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + target.addAttribute("p", dataFlavorId); + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + return dragEvent.getTransferable().getDataFlavors() + .contains(dataFlavorId); + } + + @Override + protected String getIdentifier() { + // extending classes use client side implementation from this class + return ContainsDataFlavor.class.getCanonicalName(); + } +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/Not.java b/server/src/com/vaadin/event/dd/acceptcriteria/Not.java new file mode 100644 index 0000000000..b3f73699ea --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/Not.java @@ -0,0 +1,51 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * Criterion that wraps another criterion and inverts its return value. + * + * @since 6.3 + * + */ +public class Not extends ClientSideCriterion { + + private static final long serialVersionUID = 1131422338558613244L; + private AcceptCriterion acceptCriterion; + + public Not(ClientSideCriterion acceptCriterion) { + this.acceptCriterion = acceptCriterion; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + acceptCriterion.paint(target); + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + return !acceptCriterion.accept(dragEvent); + } + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/Or.java b/server/src/com/vaadin/event/dd/acceptcriteria/Or.java new file mode 100644 index 0000000000..42d1c3293d --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/Or.java @@ -0,0 +1,64 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * A compound criterion that accepts the drag if any of its criterion accepts + * it. + * + * @see And + * + * @since 6.3 + * + */ +public class Or extends ClientSideCriterion { + private static final long serialVersionUID = 1L; + private AcceptCriterion criteria[]; + + /** + * @param criteria + * the criteria of which the Or criteria will be composed + */ + public Or(ClientSideCriterion... criteria) { + this.criteria = criteria; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + for (AcceptCriterion crit : criteria) { + crit.paint(target); + } + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + for (AcceptCriterion crit : criteria) { + if (crit.accept(dragEvent)) { + return true; + } + } + return false; + } + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/ServerSideCriterion.java b/server/src/com/vaadin/event/dd/acceptcriteria/ServerSideCriterion.java new file mode 100644 index 0000000000..b9c2855021 --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/ServerSideCriterion.java @@ -0,0 +1,69 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import java.io.Serializable; + +import com.vaadin.event.Transferable; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * Parent class for criteria which are verified on the server side during a drag + * operation to accept/discard dragged content (presented by + * {@link Transferable}). + * <p> + * Subclasses should implement the + * {@link AcceptCriterion#accept(com.vaadin.event.dd.DragAndDropEvent)} method. + * <p> + * As all server side state can be used to make a decision, this is more + * flexible than {@link ClientSideCriterion}. However, this does require + * additional requests from the browser to the server during a drag operation. + * + * @see AcceptCriterion + * @see ClientSideCriterion + * + * @since 6.3 + */ +public abstract class ServerSideCriterion implements Serializable, + AcceptCriterion { + + private static final long serialVersionUID = 2128510128911628902L; + + @Override + public final boolean isClientSideVerifiable() { + return false; + } + + @Override + public void paint(PaintTarget target) throws PaintException { + target.startTag("-ac"); + target.addAttribute("name", getIdentifier()); + paintContent(target); + target.endTag("-ac"); + } + + public void paintContent(PaintTarget target) { + } + + @Override + public void paintResponse(PaintTarget target) throws PaintException { + } + + protected String getIdentifier() { + return ServerSideCriterion.class.getCanonicalName(); + } +} diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/SourceIs.java b/server/src/com/vaadin/event/dd/acceptcriteria/SourceIs.java new file mode 100644 index 0000000000..cc1d586076 --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/SourceIs.java @@ -0,0 +1,79 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.vaadin.event.TransferableImpl; +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; +import com.vaadin.ui.Component; + +/** + * Client side criteria that checks if the drag source is one of the given + * components. + * + * @since 6.3 + */ +@SuppressWarnings("serial") +public class SourceIs extends ClientSideCriterion { + + private Component[] components; + + public SourceIs(Component... component) { + components = component; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + int paintedComponents = 0; + for (int i = 0; i < components.length; i++) { + Component c = components[i]; + if (c.getApplication() != null) { + target.addAttribute("component" + paintedComponents++, c); + } else { + Logger.getLogger(SourceIs.class.getName()) + .log(Level.WARNING, + "SourceIs component {0} at index {1} is not attached to the component hierachy and will thus be ignored", + new Object[] { c.getClass().getName(), + Integer.valueOf(i) }); + } + } + target.addAttribute("c", paintedComponents); + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + if (dragEvent.getTransferable() instanceof TransferableImpl) { + Component sourceComponent = ((TransferableImpl) dragEvent + .getTransferable()).getSourceComponent(); + for (Component c : components) { + if (c == sourceComponent) { + return true; + } + } + } + + return false; + } + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/SourceIsTarget.java b/server/src/com/vaadin/event/dd/acceptcriteria/SourceIsTarget.java new file mode 100644 index 0000000000..a4b5f24619 --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/SourceIsTarget.java @@ -0,0 +1,63 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.Transferable; +import com.vaadin.event.TransferableImpl; +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.event.dd.DropTarget; +import com.vaadin.ui.Component; +import com.vaadin.ui.Table; +import com.vaadin.ui.Tree; + +/** + * + * A criterion that ensures the drag source is the same as drop target. Eg. + * {@link Tree} or {@link Table} could support only re-ordering of items, but no + * {@link Transferable}s coming outside. + * <p> + * Note! Class is singleton, use {@link #get()} method to get the instance. + * + * @since 6.3 + * + */ +public class SourceIsTarget extends ClientSideCriterion { + + private static final long serialVersionUID = -451399314705532584L; + private static SourceIsTarget instance = new SourceIsTarget(); + + private SourceIsTarget() { + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + if (dragEvent.getTransferable() instanceof TransferableImpl) { + Component sourceComponent = ((TransferableImpl) dragEvent + .getTransferable()).getSourceComponent(); + DropTarget target = dragEvent.getTargetDetails().getTarget(); + return sourceComponent == target; + } + return false; + } + + public static synchronized SourceIsTarget get() { + return instance; + } + +}
\ No newline at end of file diff --git a/server/src/com/vaadin/event/dd/acceptcriteria/TargetDetailIs.java b/server/src/com/vaadin/event/dd/acceptcriteria/TargetDetailIs.java new file mode 100644 index 0000000000..536ba8780e --- /dev/null +++ b/server/src/com/vaadin/event/dd/acceptcriteria/TargetDetailIs.java @@ -0,0 +1,84 @@ +/* + * Copyright 2011 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.event.dd.acceptcriteria; + +import com.vaadin.event.dd.DragAndDropEvent; +import com.vaadin.event.dd.TargetDetails; +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; + +/** + * Criterion for checking if drop target details contains the specific property + * with the specific value. Currently only String values are supported. + * + * @since 6.3 + * + * TODO add support for other basic data types that we support in UIDL. + * + */ +public class TargetDetailIs extends ClientSideCriterion { + + private static final long serialVersionUID = 763165450054331246L; + private String propertyName; + private Object value; + + /** + * Constructs a criterion which ensures that the value there is a value in + * {@link TargetDetails} that equals the reference value. + * + * @param dataFlavor + * the type of data to be checked + * @param value + * the reference value to which the drop target detail will be + * compared + */ + public TargetDetailIs(String dataFlavor, String value) { + propertyName = dataFlavor; + this.value = value; + } + + public TargetDetailIs(String dataFlavor, Boolean true1) { + propertyName = dataFlavor; + value = true1; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + target.addAttribute("p", propertyName); + if (value instanceof Boolean) { + target.addAttribute("v", ((Boolean) value).booleanValue()); + target.addAttribute("t", "b"); + } else if (value instanceof String) { + target.addAttribute("v", (String) value); + } + } + + @Override + public boolean accept(DragAndDropEvent dragEvent) { + Object data = dragEvent.getTargetDetails().getData(propertyName); + return value.equals(data); + } + + @Override + protected String getIdentifier() { + // sub classes by default use VDropDetailEquals a client implementation + return TargetDetailIs.class.getCanonicalName(); + } +}
\ No newline at end of file |