Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Advanced Web Application Topics

set. While the criterion is server-side, it is lazy-loading, so that the list of accepted target nodes is loaded only once from the server for each drag operation. See Section 11.11.4, “Accepting Drops” for an example.

In addition, the accept criteria defined in AbstractSelect are available for a Tree, as listed in Section 11.11.4, “Accepting Drops”.

11.11.3. Dropping Items On a Table

You can drag items from, to, or within a Table. Making table a drag source requires simply setting the drag mode with setDragMode(). Table supports dragging both single rows, with

TableDragMode.ROW, and multiple rows, with TableDragMode.MULTIROW. While dragging, the dragged node or nodes are referenced with a TreeTransferable object, which is a DataBoundTransferable. Tree nodes are identified by the item IDs of the container items.

When a transferable is dropped on a table, the drop location is stored in a AbstractSelectTargetDetails object, which identifies the target row by its item ID. You can get the item ID with getItemIdOver() method. A drop can occur directly on or above or below a row; the exact location is a VerticalDropLocation, which you can get with the getDropLocation() method from the details object.

Accept Criteria for Tables

Table defines one specialized accept criterion for tables.

TableDropCriterion (server-side)

Accepts drops only on (or above or below) items that are specified by a set of item IDs. You must extend the abstract class and implement the getAllowedItemIds() to return the set. While the criterion is server-side, it is lazy-loading, so that the list of accepted target items is loaded only once from the server for each drag operation.

11.11.4. Accepting Drops

You can not drop the objects you are dragging around just anywhere. Before a drop is possible, the specific drop location on which the mouse hovers must be accepted. Hovering a dragged object over an accepted location displays an accept indicator, which allows the user to position the drop properly. As such checks have to be done all the time when the mouse pointer moves around the drop targets, it is not feasible to send the accept requests to the server-side, so drops on a target are normally accepted by a client-side accept criterion.

A drop handler must define the criterion on the objects which it accepts to be dropped on the target.The criterion needs to be provided in the getAcceptCriterion() method of the DropHandler interface. A criterion is represented in an AcceptCriterion object, which can be a composite of multiple criteria that are evaluated using logical operations. There are two basic types of criteria: client-side and server-side criteria. The various built-in criteria allow accepting drops based on the identity of the source and target components, and on the data flavor of the dragged objects.

To allow dropping any transferable objects, you can return a universal accept criterion, which you can get with AcceptAll.get().

tree.setDropHandler(new DropHandler() {

public AcceptCriterion getAcceptCriterion() { return AcceptAll.get();

}

...

Dropping Items On a Table

307

Advanced Web Application Topics

Client-Side Criteria

The client-side criteria, which inherit the ClientSideCriterion, are verified on the client-side, so server requests are not needed for verifying whether each component on which the mouse pointer hovers would accept a certain object.

The following client-side criteria are define in com.vaadin.event.dd.acceptcriterion:

AcceptAll

Accepts all transferables and targets.

And

Logical AND operation on two client-side criterion; accepts the transferable if all the defined sub-criteria accept it.

ContainsDataFlavour

The transferable must contain the defined data flavour.

Not

Logical NOT operation on two client-side criterion; accepts the transferable if and only if the sub-criterion does not accept it.

Or

Logical OR operation on two client-side criterion; accepts the transferable if any of the defined sub-criteria accept it.

SourceIs

Accepts all transferables from any of the given source components

SourceIsTarget

Accepts the transferable only if the source component is the same as the target. This criterion is useful for ensuring that items are dragged only within a tree or a table, and not from outside it.

TargetDetailIs

Accepts any transferable if the target detail, such as the item of a tree node or table row, is of the given data flavor and has the given value.

In addition, target components such as Tree and Table define some component-specific clientside accept criteria. See Section 11.11.2, “Dropping Items On a Tree” for more details.

AbstractSelect defines the following criteria for all selection components, including Tree and

Table.

AcceptItem

Accepts only specific items from a specific selection component. The selection component, which must inherit AbstractSelect, is given as the first parameter for the constructor. It is followed by a list of allowed item identifiers in the drag source.

AcceptItem.ALL

Accepts all transferables as long as they are items.

TargetItemIs

Accepts all drops on the specified target items. The constructor requires the target component (AbstractSelect) followed by a list of allowed item identifiers.

308

Accepting Drops

Advanced Web Application Topics

VerticalLocationIs.MIDDLE, TOP, and BOTTOM

The three static criteria accepts drops on, above, or below an item. For example, you could accept drops only in between items with the following:

public AcceptCriterion getAcceptCriterion() { return new Not(VerticalLocationIs.MIDDLE);

}

Server-Side Criteria

The server-side criteria are verified on the server-side with the accept() method of the ServerSideCriterion class. This allows fully programmable logic for accepting drops, but the negative side is that it causes a very large amount of server requests. A request is made for every target position on which the pointer hovers. This problem is eased in many cases by the component-specific lazy loading criteria TableDropCriterion and TreeDropCriterion. They do the server visit once for each drag and drop operation and return all accepted rows or nodes for current Transferable at once.

The accept() method gets the drag event as a parameter so it can perform its logic much like in drop().

public AcceptCriterion getAcceptCriterion() {

//Server-side accept criterion that allows drops on any other

//location except on nodes that may not have children ServerSideCriterion criterion = new ServerSideCriterion() { public boolean accept(DragAndDropEvent dragEvent) {

TreeTargetDetails target = (TreeTargetDetails)

dragEvent.getTargetDetails();

//The tree item on which the load hovers Object targetItemId = target.getItemIdOver();

//On which side of the target the item is hovered VerticalDropLocation location = target.getDropLocation(); if (location == VerticalDropLocation.MIDDLE)

if (! tree.areChildrenAllowed(targetItemId)) return false; // Not accepted

return true; // Accept everything else

}

};

return criterion;

}

The server-side criteria base class ServerSideCriterion provides a generic accept() method. The more specific TableDropCriterion and TreeDropCriterion are conveniency extensions that allow definiting allowed drop targets as a set of items. They also provide some optimization by lazy loading, which reduces server communications significantly.

public AcceptCriterion getAcceptCriterion() {

//Server-side accept criterion that allows drops on any

//other tree node except on node that may not have children TreeDropCriterion criterion = new TreeDropCriterion() {

@Override

protected Set<Object> getAllowedItemIds( DragAndDropEvent dragEvent, Tree tree) {

HashSet<Object> allowed = new HashSet<Object>(); for (Iterator<Object> i =

tree.getItemIds().iterator(); i.hasNext();) { Object itemId = i.next();

if (tree.hasChildren(itemId)) allowed.add(itemId);

}

Accepting Drops

309

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]