diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-04-10 16:28:22 +0300 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-04-10 16:28:22 +0300 |
commit | 125d2a926c1bc7213e732da053cc7ea5cb019c50 (patch) | |
tree | b4f78aa32c5bd5e33e97f44fcd02f8ace254c1af | |
parent | 3cf3d1b564571f272e84dc152d8a606acf6ce808 (diff) | |
download | vaadin-framework-125d2a926c1bc7213e732da053cc7ea5cb019c50.tar.gz vaadin-framework-125d2a926c1bc7213e732da053cc7ea5cb019c50.zip |
Make Button component draggable (#9038)
* Make Button component draggable (#9037)
* Add own state to button drag source
5 files changed, 228 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/extensions/ButtonDragSourceConnector.java b/client/src/main/java/com/vaadin/client/extensions/ButtonDragSourceConnector.java new file mode 100644 index 0000000000..4c07d7a7e1 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/extensions/ButtonDragSourceConnector.java @@ -0,0 +1,58 @@ +/* + * Copyright 2000-2016 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.client.extensions; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.ui.VButton; +import com.vaadin.client.ui.button.ButtonConnector; +import com.vaadin.event.dnd.ButtonDragSource; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.dnd.ButtonDragSourceState; + +/** + * Extension to make Button a drag source for HTML5 drag and drop functionality. + * + * @author Vaadin Ltd. + * @since 8.1 + */ +@Connect(ButtonDragSource.class) +public class ButtonDragSourceConnector extends DragSourceExtensionConnector { + + private VButton buttonWidget; + + @Override + protected void extend(ServerConnector target) { + super.extend(target); + + buttonWidget = ((ButtonConnector) target).getWidget(); + + // Disable mouse event capturing to make the widget draggable + buttonWidget.setCapturingEnabled(false); + } + + @Override + public void onUnregister() { + super.onUnregister(); + + // Reset mouse event capturing + buttonWidget.setCapturingEnabled(true); + } + + @Override + public ButtonDragSourceState getState() { + return (ButtonDragSourceState) super.getState(); + } +} diff --git a/client/src/main/java/com/vaadin/client/ui/VButton.java b/client/src/main/java/com/vaadin/client/ui/VButton.java index d3bba9549c..6a32b32439 100644 --- a/client/src/main/java/com/vaadin/client/ui/VButton.java +++ b/client/src/main/java/com/vaadin/client/ui/VButton.java @@ -67,6 +67,13 @@ public class VButton extends FocusWidget implements ClickHandler { private int tabIndex = 0; + /** + * Switch for disabling mouse capturing. + * + * @see #isCapturing + */ + private boolean capturingEnabled = true; + /* * BELOW PRIVATE MEMBERS COPY-PASTED FROM GWT CustomButton */ @@ -199,7 +206,7 @@ public class VButton extends FocusWidget implements ClickHandler { clickPending = true; setFocus(true); DOM.setCapture(getElement()); - isCapturing = true; + isCapturing = isCapturingEnabled(); addStyleName(CLASSNAME_PRESSED); } break; @@ -412,6 +419,27 @@ public class VButton extends FocusWidget implements ClickHandler { } } + /** + * Enables or disables the widget's capturing of mouse events with the mouse + * held down. + * + * @param enabled + * {@literal true} if capturing enabled, {@literal false} otherwise + */ + public void setCapturingEnabled(boolean enabled) { + capturingEnabled = enabled; + } + + /** + * Returns if the widget's capturing of mouse events are enabled. + * + * @return {@literal true} if mouse capturing is enabled, {@literal false} + * otherwise + */ + public boolean isCapturingEnabled() { + return capturingEnabled; + } + private static native int getHorizontalBorderAndPaddingWidth(Element elem) /*-{ // THIS METHOD IS ONLY USED FOR INTERNET EXPLORER, IT DOESN'T WORK WITH OTHERS diff --git a/server/src/main/java/com/vaadin/event/dnd/ButtonDragSource.java b/server/src/main/java/com/vaadin/event/dnd/ButtonDragSource.java new file mode 100644 index 0000000000..4c681f5331 --- /dev/null +++ b/server/src/main/java/com/vaadin/event/dnd/ButtonDragSource.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2016 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.dnd; + +import com.vaadin.shared.ui.dnd.ButtonDragSourceState; +import com.vaadin.ui.Button; + +/** + * Extension to make Button a drag source for HTML5 drag and drop functionality. + * + * @author Vaadin Ltd. + * @since 8.1 + */ +public class ButtonDragSource extends DragSourceExtension<Button> { + + public ButtonDragSource(Button target) { + super(target); + } + + @Override + protected ButtonDragSourceState getState() { + return (ButtonDragSourceState) super.getState(); + } + + @Override + protected ButtonDragSourceState getState(boolean markAsDirty) { + return (ButtonDragSourceState) super.getState(markAsDirty); + } +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/ButtonDragSourceState.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/ButtonDragSourceState.java new file mode 100644 index 0000000000..d035c4d35c --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/ButtonDragSourceState.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2016 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.shared.ui.dnd; + +/** + * State class containing parameters for ButtonDragSourceExtension. + * + * @author Vaadin Ltd + * @since 8.1 + */ +public class ButtonDragSourceState extends DragSourceState { + +} diff --git a/uitest/src/main/java/com/vaadin/tests/dnd/DraggableButton.java b/uitest/src/main/java/com/vaadin/tests/dnd/DraggableButton.java new file mode 100644 index 0000000000..3a4db48eb5 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/dnd/DraggableButton.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2016 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.tests.dnd; + +import com.vaadin.event.dnd.ButtonDragSource; +import com.vaadin.event.dnd.DropTargetExtension; +import com.vaadin.server.Page; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Layout; + +public class DraggableButton extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + + Button draggableButton = new Button("Draggable Button"); + ButtonDragSource dragSourceExtension = new ButtonDragSource( + draggableButton); + dragSourceExtension.setDataTransferText( + "If you see this, the drop was successful"); + + Label dropTarget = new Label("Drop Here"); + dropTarget.addStyleName("drop-target"); + DropTargetExtension<Label> dropTargetExtension = new DropTargetExtension<>( + dropTarget); + dropTargetExtension + .addDropListener(event -> log(event.getDataTransferText())); + + Layout layout = new HorizontalLayout(); + layout.addComponents(draggableButton, dropTarget); + addComponent(layout); + + // Add styling + setStyle(); + } + + private void setStyle() { + Page.Styles styles = Page.getCurrent().getStyles(); + + styles.add(".drop-target {" + + "width: 150px;" + + "height: 100px;" + + "border: 1px solid black;" + + "border-radius: 4px;" + + "text-align: center;" + + "}"); + styles.add(".v-drag-over {" + + "border-style: dashed;" + + "}"); + } + + @Override + protected String getTestDescription() { + return "Test if Button is draggable"; + } +} |