Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/copy paste #2

Merged
merged 3 commits into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Version 3.1.0 - TBD ([javadoc](http://diffplug.github.io/durian-swt/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/durian/durian-swt/))

- `Shells` now detects the monitor using the center of the proposed bounds, rather than the top-left.
- Added `StructuredDrag.copyToClipboard()` and `StructuredDrop.pasteFromClipboard()` [(#2)](https://github.com/diffplug/durian-swt/pull/2).

### Version 3.0.0 - August 1st 2018 ([javadoc](http://diffplug.github.io/durian-swt/javadoc/3.0.0/), [jcenter](https://bintray.com/diffplug/opensource/durian-swt/3.0.0/view))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,30 @@ protected String[] getTypeNames() {
return new String[]{TYPE_NAME};
}

@SuppressWarnings("unchecked")
@Override
public void javaToNative(Object object, TransferData transferData) {
if (object != null) {
set((T) object);
}
byte[] check = TYPE_NAME.getBytes(StandardCharsets.UTF_8);
super.javaToNative(check, transferData);
}

@Override
public Object nativeToJava(TransferData transferData) {
Object result = super.nativeToJava(transferData);

// check result
Preconditions.checkArgument(result instanceof byte[], "%s should have been byte[]", result.getClass());
String resultStr = new String((byte[]) result, StandardCharsets.UTF_8);
Preconditions.checkArgument(TYPE_NAME.equals(resultStr), "%s should have been %s", resultStr, TYPE_NAME);

// return this transfer object itself
return this;
if (obj == null) {
return null;
} else {
// check result
Object result = super.nativeToJava(transferData);
Preconditions.checkArgument(result instanceof byte[], "%s should have been byte[]", result.getClass());
String resultStr = new String((byte[]) result, StandardCharsets.UTF_8);
Preconditions.checkArgument(TYPE_NAME.equals(resultStr), "%s should have been %s", resultStr, TYPE_NAME);

// return this transfer object itself
return get();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.annotation.Nullable;

import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
Expand All @@ -42,6 +43,7 @@
import com.diffplug.common.collect.ImmutableMap;
import com.diffplug.common.rx.RxBox;
import com.diffplug.common.rx.RxGetter;
import com.diffplug.common.swt.SwtMisc;

/**
* Typed mechanism for implementing drag listeners.
Expand All @@ -55,7 +57,8 @@ public enum DropResult {

@FunctionalInterface
public interface TypedDragHandler<T> {
T dragStartData(DragSourceEvent e);
/** DragSourceEvent will be null if it's starting from a 'Ctrl+C' copy. */
T dragStartData(@Nullable DragSourceEvent e);

default void dropped(DragSourceEvent e, T value, DropResult result) {}

Expand Down Expand Up @@ -246,13 +249,29 @@ public Listener(RxBox<Boolean> dragInProgress, ImmutableMap<Transfer, Handler> m
transfers = map.keySet().toArray(new Transfer[map.size()]);
}

public void copyToClipboard() {
Clipboard clipboard = new Clipboard(SwtMisc.assertUI());
try {
populateData(null);
Object[] dataPer = new Object[data.size()];
Transfer[] transferPer = new Transfer[data.size()];
int i = 0;
for (Map.Entry<Transfer, Object> entry : data.entrySet()) {
dataPer[i] = entry.getValue();
transferPer[i] = entry.getKey();
++i;
}
clipboard.setContents(dataPer, transferPer);
} finally {
clipboard.dispose();
}
}

public Transfer[] transferArray() {
return Arrays.copyOf(transfers, transfers.length);
}

@Override
public void dragStart(DragSourceEvent event) {
dragInProgress.set(true);
private void populateData(@Nullable DragSourceEvent event) {
data.clear();
handlers.forEach((transfer, handler) -> {
// get the drag data
Expand All @@ -261,6 +280,12 @@ public void dragStart(DragSourceEvent event) {
data.put(transfer, value);
}
});
}

@Override
public void dragStart(DragSourceEvent event) {
dragInProgress.set(true);
populateData(event);
event.doit = data.size() > 0;
if (event.doit) {
Transfer[] validTransfers = data.keySet().stream().toArray(Transfer[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.function.Function;
import java.util.function.Predicate;

import javax.annotation.Nullable;

import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
Expand All @@ -40,6 +43,7 @@
import com.diffplug.common.base.Predicates;
import com.diffplug.common.collect.ImmutableList;
import com.diffplug.common.collect.ImmutableMap;
import com.diffplug.common.swt.SwtMisc;

/**
* Typed mechanism for implementing drop listeners.
Expand All @@ -53,7 +57,8 @@ public enum DropMethod {

@FunctionalInterface
public interface TypedDropHandler<T> {
void onEvent(DropMethod method, DropTargetEvent e, T value);
/** If it's the result of a "paste", then DropTargetEvent will be null. */
void onEvent(DropMethod method, @Nullable DropTargetEvent e, T value);

default <R> TypedDropHandler<R> map(Function<? super R, ? extends T> mapper) {
return new MappedTypedDropHandler<R, T>(this, mapper);
Expand Down Expand Up @@ -225,6 +230,21 @@ public Listener(ImmutableMap<Transfer, Handler> map) {
transfers = map.keySet().toArray(new Transfer[map.size()]);
}

public void pasteFromClipboard() {
Clipboard clipboard = new Clipboard(SwtMisc.assertUI());
try {
for (Transfer transfer : transfers) {
Object data = clipboard.getContents(transfer);
if (data != null) {
handlers.get(transfer).handler.onEvent(DropMethod.drop, null, data);
break;
}
}
} finally {
clipboard.dispose();
}
}

public Transfer[] transferArray() {
return Arrays.copyOf(transfers, transfers.length);
}
Expand Down Expand Up @@ -279,16 +299,21 @@ public AbstractTypedDropHandler(DndOp operation) {
}

@Override
public void onEvent(DropMethod method, DropTargetEvent e, T value) {
public void onEvent(DropMethod method, @Nullable DropTargetEvent e, T value) {
if (method == DropMethod.dragLeave) {
return;
} else {
if (value == null) {
// this means that we don't know what the data is yet
operation.trySetDetail(e);
if (e != null) {
operation.trySetDetail(e);
}
} else if (accept(value)) {
if (operation.trySetDetail(e) && method == DropMethod.drop) {
drop(e, value, e.detail == DND.DROP_MOVE);
if (e == null) {
drop(null, value, false);
} else if (operation.trySetDetail(e) && method == DropMethod.drop) {
boolean moved = e.detail == DND.DROP_MOVE;
drop(e, value, moved);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package com.diffplug.common.swt.dnd;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
Expand Down Expand Up @@ -75,6 +77,18 @@ <T> void addGroup(String name, T value, Playground<T> coat) {
dndCtl.setText("Drag and drop");
drag.applyTo(dndCtl, DndOp.COPY);
drop.applyTo(dndCtl);

Composite btnCmp = new Composite(txtGrp, SWT.NONE);
Layouts.setGridData(btnCmp).grabHorizontal();
Layouts.setGrid(btnCmp).margin(0).numColumns(2);
BiConsumer<String, Runnable> addBtn = (lbl, toRun) -> {
Button btn = new Button(btnCmp, SWT.PUSH | SWT.FLAT);
Layouts.setGridData(btn).grabHorizontal();
btn.setText(lbl);
btn.addListener(SWT.Selection, e -> toRun.run());
};
addBtn.accept("Copy", () -> drag.getListener().copyToClipboard());
addBtn.accept("Paste", () -> drop.getListener().pasteFromClipboard());
}
}
Adder adder = new Adder();
Expand Down