aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Gottschaemmer2012-10-27 11:23:36 (EDT)
committerPatrick Gottschaemmer2012-10-27 11:23:36 (EDT)
commit81fcdb169da4e5b45ea9b7535829b45353ba6bde (patch)
tree8fa957641e1c95adac3aeb87570a6ead2d9afec0
parentd54022c7184e757cc74c9c43353c0ebc531c3d93 (diff)
downloadorg.eclipse.recommenders-81fcdb169da4e5b45ea9b7535829b45353ba6bde.zip
org.eclipse.recommenders-81fcdb169da4e5b45ea9b7535829b45353ba6bde.tar.gz
org.eclipse.recommenders-81fcdb169da4e5b45ea9b7535829b45353ba6bde.tar.bz2
[Extdoc] add Drag'n'Drop Support to ExtdocViewrefs/changes/73/8073/12
This commit adds Drag'n'Drop Support for the ExtdocProviders. A new DnD TransferType 'ExtdocProviderTransfer' is added for ensuring a proper validation between DnD supporting views and TransferTypes. Change-Id: Ie00ecfff4929d5be25fc305c26f90703070f7f69
-rw-r--r--plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocProviderTransfer.java72
-rw-r--r--plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java128
2 files changed, 195 insertions, 5 deletions
diff --git a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocProviderTransfer.java b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocProviderTransfer.java
new file mode 100644
index 0000000..db9c02a
--- /dev/null
+++ b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocProviderTransfer.java
@@ -0,0 +1,72 @@
+/**
+ * Copyright (c) 2010, 2011 Darmstadt University of Technology.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Patrick Gottschaemmer, Olav Lenz - add Drag'n'Drop support
+ */
+package org.eclipse.recommenders.internal.extdoc.rcp.ui;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.util.Policy;
+import org.eclipse.recommenders.extdoc.rcp.providers.ExtdocProvider;
+import org.eclipse.recommenders.rcp.RecommendersPlugin;
+import org.eclipse.swt.dnd.ByteArrayTransfer;
+import org.eclipse.swt.dnd.TransferData;
+
+public class ExtdocProviderTransfer extends ByteArrayTransfer {
+
+ private static final String TYPE_NAME = "extdoc-provider-transfer-format";
+ private static final int TYPEID = registerType(TYPE_NAME);
+ private static final ExtdocProviderTransfer INSTANCE = new ExtdocProviderTransfer();
+ private ExtdocProvider extDocProvider;
+
+ private ExtdocProviderTransfer() {
+ };
+
+ public static ExtdocProviderTransfer getInstance() {
+ return INSTANCE;
+ }
+
+ @Override
+ protected int[] getTypeIds() {
+ return new int[] { TYPEID };
+ }
+
+ @Override
+ protected String[] getTypeNames() {
+ return new String[] { TYPE_NAME };
+ }
+
+ public void javaToNative(final Object object, final TransferData transferData) {
+ final byte[] check = TYPE_NAME.getBytes();
+ super.javaToNative(check, transferData);
+ }
+
+ @Override
+ public Object nativeToJava(final TransferData transferData) {
+ final Object convert = super.nativeToJava(transferData);
+ if (isInvalidNativeType(convert)) {
+ RecommendersPlugin.log(new Status(IStatus.ERROR, Policy.JFACE, IStatus.ERROR, JFaceResources
+ .getString("ExtdocProviderTransfer.errorMessage"), null)); //$NON-NLS-1$
+ }
+ return getExtdocProvider();
+ }
+
+ private boolean isInvalidNativeType(final Object result) {
+ return !(result instanceof byte[]) || !TYPE_NAME.equals(new String((byte[]) result));
+ }
+
+ public ExtdocProvider getExtdocProvider() {
+ return extDocProvider;
+ }
+
+ public void setExtdocProvider(final ExtdocProvider extDocProvider) {
+ this.extDocProvider = extDocProvider;
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java
index 4217cbb..ab6f724 100644
--- a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java
+++ b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Sebastian Proksch - initial API and implementation
+ * Patrick Gottschaemmer, Olav Lenz - add Drag'n'Drop support
*/
package org.eclipse.recommenders.internal.extdoc.rcp.ui;
@@ -16,6 +17,7 @@ import static org.eclipse.recommenders.internal.extdoc.rcp.ui.ExtdocUtils.setInf
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
@@ -29,12 +31,15 @@ import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerComparator;
+import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.recommenders.extdoc.rcp.providers.ExtdocProvider;
-import org.eclipse.recommenders.internal.extdoc.rcp.wiring.ExtdocModule.Extdoc;
import org.eclipse.recommenders.rcp.RecommendersPlugin;
import org.eclipse.recommenders.rcp.events.JavaSelectionEvent;
import org.eclipse.recommenders.utils.rcp.PartListener2Adapter;
@@ -42,6 +47,12 @@ import org.eclipse.recommenders.utils.rcp.RCPUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DragSourceAdapter;
+import org.eclipse.swt.dnd.DragSourceEvent;
+import org.eclipse.swt.dnd.DropTargetEvent;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
@@ -52,6 +63,7 @@ import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.part.ViewPart;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import com.google.common.eventbus.AllowConcurrentEvents;
@@ -72,6 +84,8 @@ public class ExtdocView extends ViewPart {
| JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES
| JavaElementLabels.M_EXCEPTIONS | JavaElementLabels.F_PRE_TYPE_SIGNATURE
| JavaElementLabels.T_TYPE_PARAMETERS;
+ private static final int MOVE_AFTER = 1;
+ private static final int MOVE_BEFORE = 0;
private final JavaElementLabelProvider labelProvider = new JavaElementLabelProvider((int) LABEL_FLAGS);
@@ -79,6 +93,7 @@ public class ExtdocView extends ViewPart {
private ScrolledComposite scrollable;
private Composite content;
private TableViewer viewer;
+ private List<ExtdocProvider> providerRanking;
private boolean visible = true;
@@ -92,6 +107,7 @@ public class ExtdocView extends ViewPart {
this.subscriptionManager = subscriptionManager;
this.providers = providers;
this.preferences = preferences;
+ this.providerRanking = new LinkedList<ExtdocProvider>(providers);
}
@Override
@@ -116,7 +132,21 @@ public class ExtdocView extends ViewPart {
}
private void createProviderOverview() {
- viewer = new TableViewer(sash);
+ viewer = new TableViewer(sash, SWT.SINGLE);
+
+ addDnDSupport();
+
+ viewer.setComparator(new ViewerComparator() {
+
+ @Override
+ public int compare(Viewer viewer, Object first, Object second) {
+ int indexFirst = providerRanking.indexOf((ExtdocProvider) first);
+ int indexSecond = providerRanking.indexOf((ExtdocProvider) second);
+
+ return (indexFirst - indexSecond);
+ }
+ });
+
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new LabelProvider() {
@@ -155,6 +185,95 @@ public class ExtdocView extends ViewPart {
viewer.setSelection(new StructuredSelection(Iterables.getFirst(providers, null)));
}
+ private void addDnDSupport() {
+ final int operations = DND.DROP_MOVE;
+ final Transfer[] transferTypes = new Transfer[] { ExtdocProviderTransfer.getInstance() };
+
+ viewer.addDragSupport(operations, transferTypes, new DragSourceAdapter() {
+
+ @Override
+ public void dragSetData(final DragSourceEvent event) {
+ final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
+
+ if (ExtdocProviderTransfer.getInstance().isSupportedType(event.dataType)) {
+ final ExtdocProvider selectedProvider = (ExtdocProvider) selection.getFirstElement();
+ ExtdocProviderTransfer.getInstance().setExtdocProvider(selectedProvider);
+ }
+ }
+ });
+
+ viewer.addDropSupport(operations, transferTypes, new ViewerDropAdapter(viewer) {
+
+ private int newIndex;
+ private int currentFeedback;
+
+ @Override
+ public boolean validateDrop(final Object target, final int operation, final TransferData transferType) {
+ return ExtdocProviderTransfer.getInstance().isSupportedType(transferType);
+ }
+
+ @Override
+ public void dragOver(final DropTargetEvent event) {
+ if (determineLocation(event) == ViewerDropAdapter.LOCATION_BEFORE && isFirstProvider(event)) {
+ event.feedback = DND.FEEDBACK_INSERT_BEFORE;
+ } else {
+ event.feedback = DND.FEEDBACK_INSERT_AFTER;
+ }
+ currentFeedback = event.feedback;
+ }
+
+ private boolean isFirstProvider(final DropTargetEvent event) {
+ return providerRanking.indexOf((ExtdocProvider) determineTarget(event)) == 0;
+ }
+
+ @Override
+ public void drop(final DropTargetEvent event) {
+ if (event.item != null) {
+ newIndex = providerRanking.indexOf((ExtdocProvider) event.item.getData());
+ } else {
+ newIndex = providerRanking.size() - 1;
+ }
+ performDrop(event.data);
+ }
+
+ @Override
+ public boolean performDrop(final Object data) {
+ final ExtdocProvider provider = (ExtdocProvider) data;
+ final int oldIndex = providerRanking.indexOf(provider);
+
+ if (currentFeedback == DND.FEEDBACK_INSERT_AFTER) {
+ moveAfter(oldIndex, newIndex);
+ } else {
+ moveBefore(oldIndex, newIndex);
+ }
+ viewer.refresh();
+ return true;
+ }
+ });
+ }
+
+ private void move(int oldIndex, int newIndex, int moveStyle) {
+ if (newIndex == oldIndex) {
+ return;
+ } else if (newIndex < oldIndex) {
+ ExtdocProvider tmp = providerRanking.remove(oldIndex);
+ providerRanking.add(newIndex + moveStyle, tmp);
+ } else {
+ ExtdocProvider tmp = providerRanking.remove(oldIndex);
+ providerRanking.add(newIndex - 1 + moveStyle, tmp);
+ }
+ }
+
+ @VisibleForTesting
+ public void moveAfter(int oldIndex, int newIndex) {
+ move(oldIndex, newIndex, MOVE_AFTER);
+ }
+
+ @VisibleForTesting
+ public void moveBefore(int oldIndex, int newIndex) {
+ move(oldIndex, newIndex, MOVE_BEFORE);
+ }
+
private void createContentArea() {
scrollable = new ScrolledComposite(sash, SWT.H_SCROLL | SWT.V_SCROLL);
scrollable.getVerticalBar().setIncrement(20);
@@ -260,9 +379,8 @@ public class ExtdocView extends ViewPart {
text = element.getElementName();
break;
case IJavaElement.LOCAL_VARIABLE:
- text =
- JavaElementLabels.getElementLabel(element, JavaElementLabels.F_PRE_TYPE_SIGNATURE
- | JavaElementLabels.F_POST_QUALIFIED);
+ text = JavaElementLabels.getElementLabel(element, JavaElementLabels.F_PRE_TYPE_SIGNATURE
+ | JavaElementLabels.F_POST_QUALIFIED);
break;
default:
text = JavaElementLabels.getElementLabel(element, LABEL_FLAGS);