* Make Button component draggable (#9037) * Add own state to button drag sourcetags/8.1.0.alpha5
@@ -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(); | |||
} | |||
} |
@@ -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 |
@@ -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); | |||
} | |||
} |
@@ -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 { | |||
} |
@@ -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"; | |||
} | |||
} |