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

Advanced Web Application Topics

11.11.6. Dropping on a Component

Drops on a component are enabled by wrapping the component in a DragAndDropWrapper. The wrapper is an ordinary component; the constructor takes the wrapped component as a parameter.You just need to define the DropHandler for the wrapper with setDropHandler().

In the following example, we allow moving components in an absolute layout. Details on the drop handler are given later.

//A layout that allows moving its contained components

//by dragging and dropping them

final AbsoluteLayout absLayout = new AbsoluteLayout(); absLayout.setWidth("100%"); absLayout.setHeight("400px");

... put some (wrapped) components in the layout ...

//Wrap the layout to allow handling drops DragAndDropWrapper layoutWrapper =

new DragAndDropWrapper(absLayout);

//Handle moving components within the AbsoluteLayout layoutWrapper.setDropHandler(new DropHandler() {

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

}

public void drop(DragAndDropEvent event) {

...

}

});

Target Details for Wrapped Components

The drop handler receives the drop target details in a WrapperTargetDetails object, which implements the TargetDetails interface.

public void drop(DragAndDropEvent event) { WrapperTransferable t =

(WrapperTransferable) event.getTransferable(); WrapperTargetDetails details =

(WrapperTargetDetails) event.getTargetDetails();

The wrapper target details include a MouseEventDetails object, which you can get with getMouseEvent(). You can use it to get the mouse coordinates for the position where the mouse button was released and the drag ended. Similarly, you can find out the drag start position from the transferable object (if it is a WrapperTransferable) with getMouseDownEvent().

// Calculate the drag coordinate difference

int xChange = details.getMouseEvent().getClientX() - t.getMouseDownEvent().getClientX(); int yChange = details.getMouseEvent().getClientY()

-t.getMouseDownEvent().getClientY();

//Move the component in the absolute layout ComponentPosition pos =

absLayout.getPosition(t.getSourceComponent()); pos.setLeftValue(pos.getLeftValue() + xChange); pos.setTopValue(pos.getTopValue() + yChange);

You can get the absolute x and y coordinates of the target wrapper with getAbsoluteLeft() and getAbsoluteTop(), which allows you to translate the absolute mouse coordinates to co-

Dropping on a Component

311

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