Skip to content

Commit

Permalink
Used Typed new Generics for selection in TreeViewer DnD Classes #155
Browse files Browse the repository at this point in the history
As we now have more typed lists the handling of selections and the
resulting DnD actions can be implemented more nicely.

Addresses: #155
  • Loading branch information
azoitl authored and ptziegler committed Sep 30, 2024
1 parent 89fae40 commit 84c468d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -60,6 +60,7 @@ protected String[] getTypeNames() {
*
* @return The viewer where the drag started
*/
@SuppressWarnings("static-method")
public EditPartViewer getViewer() {
return viewer;
}
Expand All @@ -69,6 +70,7 @@ public EditPartViewer getViewer() {
*
* @param epv The viewer
*/
@SuppressWarnings("static-method")
public void setViewer(EditPartViewer epv) {
viewer = epv;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.gef.ui.parts;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.dnd.DragSourceEvent;
Expand All @@ -26,7 +25,7 @@

class TreeViewerTransferDragListener extends AbstractTransferDragSourceListener {

private List modelSelection;
private List<Object> modelSelection;

public TreeViewerTransferDragListener(EditPartViewer viewer) {
super(viewer, TreeViewerTransfer.getInstance());
Expand All @@ -48,7 +47,7 @@ public void dragSetData(DragSourceEvent event) {
@Override
public void dragStart(DragSourceEvent event) {
TreeViewerTransfer.getInstance().setViewer(getViewer());
List selection = getViewer().getSelectedEditParts();
List<? extends EditPart> selection = getViewer().getSelectedEditParts();
TreeViewerTransfer.getInstance().setObject(selection);
saveModelSelection(selection);
}
Expand All @@ -65,24 +64,13 @@ public void dragFinished(DragSourceEvent event) {
}

protected void revertModelSelection() {
List list = new ArrayList();
Object editpart;
for (int i = 0; i < modelSelection.size(); i++) {
editpart = getViewer().getEditPartForModel(modelSelection.get(i));
if (editpart != null) {
list.add(editpart);
}
}
List<EditPart> list = modelSelection.stream().map(m -> getViewer().getEditPartForModel(m)).toList();
getViewer().setSelection(new StructuredSelection(list));
modelSelection = null;
}

protected void saveModelSelection(List editPartSelection) {
modelSelection = new ArrayList();
for (int i = 0; i < editPartSelection.size(); i++) {
EditPart editpart = (EditPart) editPartSelection.get(i);
modelSelection.add(editpart.getModel());
}
protected void saveModelSelection(List<? extends EditPart> editPartSelection) {
modelSelection = editPartSelection.stream().map(EditPart::getModel).toList();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -14,7 +14,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.dnd.DND;
Expand All @@ -40,23 +39,17 @@ public TreeViewerTransferDropListener(EditPartViewer viewer) {
@Override
protected Request createTargetRequest() {
ChangeBoundsRequest request = new ChangeBoundsRequest(RequestConstants.REQ_MOVE);
request.setEditParts((List) TreeViewerTransfer.getInstance().getObject());
request.setEditParts(getTransferedEditParts());
return request;
}

@Override
protected Command getCommand() {
CompoundCommand command = new CompoundCommand();

Iterator iter = ((List) TreeViewerTransfer.getInstance().getObject()).iterator();

Request request = getTargetRequest();
final CompoundCommand command = new CompoundCommand();
final Request request = getTargetRequest();
request.setType(isMove() ? RequestConstants.REQ_MOVE : RequestConstants.REQ_ORPHAN);

while (iter.hasNext()) {
EditPart editPart = (EditPart) iter.next();
command.add(editPart.getCommand(request));
}
getTransferedEditParts().stream().forEach(ep -> command.add(ep.getCommand(request)));

// If reparenting, add all editparts to target editpart.
if (!isMove()) {
Expand Down Expand Up @@ -85,6 +78,12 @@ protected Collection<EditPart> getExclusionSet() {
return exclude;
}

@SuppressWarnings("unchecked") // we are putting a List<? extends EditPart> into the object of the transfer so
// casting is save
private static List<? extends EditPart> getTransferedEditParts() {
return (List<? extends EditPart>) TreeViewerTransfer.getInstance().getObject();
}

@Override
protected void handleDragOver() {
if (TreeViewerTransfer.getInstance().getViewer() != getViewer()) {
Expand All @@ -95,18 +94,19 @@ protected void handleDragOver() {
super.handleDragOver();
}

@SuppressWarnings("static-method")
protected EditPart getSourceEditPart() {
List selection = (List) TreeViewerTransfer.getInstance().getObject();
if (selection == null || selection.isEmpty() || !(selection.get(0) instanceof EditPart)) {
List<? extends EditPart> selection = getTransferedEditParts();
if (selection == null || selection.isEmpty()) {
return null;
}
return (EditPart) selection.get(0);
return selection.get(0);
}

protected List includeChildren(List list) {
protected List<EditPart> includeChildren(List<? extends EditPart> list) {
List<EditPart> result = new ArrayList<>();
for (Object element : list) {
List<? extends EditPart> children = ((EditPart) element).getChildren();
for (EditPart element : list) {
List<? extends EditPart> children = element.getChildren();
result.addAll(children);
result.addAll(includeChildren(children));
}
Expand All @@ -123,9 +123,7 @@ public boolean isEnabled(DropTargetEvent event) {

protected boolean isMove() {
EditPart source = getSourceEditPart();
List selection = (List) TreeViewerTransfer.getInstance().getObject();
for (Object element : selection) {
EditPart ep = (EditPart) element;
for (EditPart ep : getTransferedEditParts()) {
if (ep.getParent() != source.getParent()) {
return false;
}
Expand Down

0 comments on commit 84c468d

Please sign in to comment.