Skip to content

Commit

Permalink
[Modernize Code] Use the new typed GroupRequest to clean-up code (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl authored Nov 5, 2023
1 parent af41b91 commit 3485608
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
*******************************************************************************/
package org.eclipse.gef.examples.flow.policies;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.ContainerEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;

import org.eclipse.gef.examples.flow.model.Activity;
import org.eclipse.gef.examples.flow.model.StructuredActivity;
import org.eclipse.gef.examples.flow.model.commands.OrphanChildCommand;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;

/**
* ActivityContainerEditPolicy
Expand All @@ -44,11 +43,10 @@ protected Command getCreateCommand(CreateRequest request) {
*/
@Override
protected Command getOrphanChildrenCommand(GroupRequest request) {
List parts = request.getEditParts();
CompoundCommand result = new CompoundCommand();
for (int i = 0; i < parts.size(); i++) {
for (EditPart child : request.getEditParts()) {
OrphanChildCommand orphan = new OrphanChildCommand();
orphan.setChild((Activity) ((EditPart) parts.get(i)).getModel());
orphan.setChild((Activity) child.getModel());
orphan.setParent((StructuredActivity) getHost().getModel());
result.add(orphan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gef.editpolicies.ContainerEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;

import org.eclipse.gef.examples.flow.model.Activity;
import org.eclipse.gef.examples.flow.model.StructuredActivity;
import org.eclipse.gef.examples.flow.model.commands.AddAndAssignSourceCommand;
import org.eclipse.gef.examples.flow.model.commands.CreateAndAssignSourceCommand;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;

/**
* @author Daniel Lee
Expand All @@ -36,11 +37,11 @@ public class ActivitySourceEditPolicy extends ContainerEditPolicy {
@Override
protected Command getAddCommand(GroupRequest request) {
CompoundCommand cmd = new CompoundCommand();
for (int i = 0; i < request.getEditParts().size(); i++) {
for (EditPart ep : request.getEditParts()) {
AddAndAssignSourceCommand add = new AddAndAssignSourceCommand();
add.setParent((StructuredActivity) getHost().getParent().getModel());
add.setSource((Activity) getHost().getModel());
add.setChild(((Activity) ((EditPart) request.getEditParts().get(i)).getModel()));
add.setChild((Activity) ep.getModel());
cmd.add(add);
}
return cmd;
Expand All @@ -63,12 +64,15 @@ protected Command getCreateCommand(CreateRequest request) {
*/
@Override
public EditPart getTargetEditPart(Request request) {
if (REQ_CREATE.equals(request.getType()))
if (REQ_CREATE.equals(request.getType())) {
return getHost();
if (REQ_ADD.equals(request.getType()))
}
if (REQ_ADD.equals(request.getType())) {
return getHost();
if (REQ_MOVE.equals(request.getType()))
}
if (REQ_MOVE.equals(request.getType())) {
return getHost();
}
return super.getTargetEditPart(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
*******************************************************************************/
package org.eclipse.gef.examples.flow.policies;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import org.eclipse.gef.examples.flow.model.Activity;
import org.eclipse.gef.examples.flow.model.StructuredActivity;
import org.eclipse.gef.examples.flow.model.commands.AddCommand;
import org.eclipse.gef.examples.flow.model.commands.CreateCommand;
import org.eclipse.gef.examples.flow.parts.SimpleActivityPart;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

/**
* @author Daniel Lee
Expand All @@ -47,20 +46,17 @@ protected Command createAddCommand(EditPart child) {
*/
@Override
protected EditPolicy createChildEditPolicy(EditPart child) {
if (child instanceof SimpleActivityPart)
if (child instanceof SimpleActivityPart) {
return new SimpleActivitySelectionEditPolicy();
}
return new NonResizableEditPolicy();
}

@Override
protected Command getAddCommand(Request req) {
ChangeBoundsRequest request = (ChangeBoundsRequest) req;
List editParts = request.getEditParts();
CompoundCommand command = new CompoundCommand();
for (int i = 0; i < editParts.size(); i++) {
EditPart child = (EditPart) editParts.get(i);
command.add(createAddCommand(child));
}
request.getEditParts().forEach(child -> command.add(createAddCommand(child)));
return command.unwrap();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*******************************************************************************/
package org.eclipse.gef.examples.logicdesigner.edit;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
Expand All @@ -35,11 +33,10 @@ protected Command getCreateCommand(CreateRequest request) {

@Override
public Command getOrphanChildrenCommand(GroupRequest request) {
List parts = request.getEditParts();
CompoundCommand result = new CompoundCommand(LogicMessages.LogicContainerEditPolicy_OrphanCommandLabelText);
for (int i = 0; i < parts.size(); i++) {
for (EditPart child : request.getEditParts()) {
OrphanChildCommand orphan = new OrphanChildCommand();
orphan.setChild((LogicSubpart) ((EditPart) parts.get(i)).getModel());
orphan.setChild((LogicSubpart) child.getModel());
orphan.setParent((LogicDiagram) getHost().getModel());
orphan.setLabel(LogicMessages.LogicElementEditPolicy_OrphanCommandLabelText);
result.add(orphan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*******************************************************************************/
package org.eclipse.gef.examples.logicdesigner.edit;

import java.util.Iterator;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
Expand Down Expand Up @@ -48,12 +46,10 @@ protected Command getCloneCommand(ChangeBoundsRequest request) {
EditPart after = getInsertionReference(request);
int index = getHost().getChildren().indexOf(after);

Iterator i = request.getEditParts().iterator();
GraphicalEditPart currPart = null;

while (i.hasNext()) {
currPart = (GraphicalEditPart) i.next();
clone.addPart((LogicSubpart) currPart.getModel(), index++);
for (EditPart ep : request.getEditParts()) {
GraphicalEditPart currPart = (GraphicalEditPart) ep;
clone.addPart((LogicSubpart) currPart.getModel(), index);
index++;
}

return clone;
Expand Down Expand Up @@ -85,8 +81,9 @@ protected Command createMoveChildCommand(EditPart child, EditPart after) {
LogicDiagram parentModel = (LogicDiagram) getHost().getModel();
int oldIndex = getHost().getChildren().indexOf(child);
int newIndex = getHost().getChildren().indexOf(after);
if (newIndex > oldIndex)
if (newIndex > oldIndex) {
newIndex--;
}
return new ReorderPartCommand(childModel, parentModel, newIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,22 @@ protected Command createCreateCommand(LogicSubpart child, Rectangle r, int index
cmd.setParent((LogicDiagram) getHost().getModel());
cmd.setChild(child);
cmd.setLabel(label);
if (index >= 0)
if (index >= 0) {
cmd.setIndex(index);
}
return cmd;
}

@Override
protected Command getAddCommand(ChangeBoundsRequest request) {
CompoundCommand command = new CompoundCommand();
command.setDebugLabel("Add in LogicTreeContainerEditPolicy");//$NON-NLS-1$
List editparts = request.getEditParts();
int index = findIndexOfTreeItemAt(request.getLocation());

for (int i = 0; i < editparts.size(); i++) {
EditPart child = (EditPart) editparts.get(i);
if (isAncestor(child, getHost()))
for (EditPart child : request.getEditParts()) {
if (isAncestor(child, getHost())) {
command.add(UnexecutableCommand.INSTANCE);
else {
} else {
LogicSubpart childModel = (LogicSubpart) child.getModel();
command.add(createCreateCommand(childModel,
new Rectangle(new org.eclipse.draw2d.geometry.Point(), childModel.getSize()), index,
Expand All @@ -81,18 +80,17 @@ protected Command getCreateCommand(CreateRequest request) {
@Override
protected Command getMoveChildrenCommand(ChangeBoundsRequest request) {
CompoundCommand command = new CompoundCommand();
List editparts = request.getEditParts();
List<? extends EditPart> children = getHost().getChildren();
int newIndex = findIndexOfTreeItemAt(request.getLocation());

for (int i = 0; i < editparts.size(); i++) {
EditPart child = (EditPart) editparts.get(i);
for (EditPart child : request.getEditParts()) {
int tempIndex = newIndex;
int oldIndex = children.indexOf(child);
if (oldIndex == tempIndex || oldIndex + 1 == tempIndex) {
command.add(UnexecutableCommand.INSTANCE);
return command;
} else if (oldIndex <= tempIndex) {
}
if (oldIndex <= tempIndex) {
tempIndex--;
}
command.add(new ReorderPartCommand((LogicSubpart) child.getModel(), (LogicDiagram) getHost().getModel(),
Expand All @@ -102,10 +100,12 @@ protected Command getMoveChildrenCommand(ChangeBoundsRequest request) {
}

protected boolean isAncestor(EditPart source, EditPart target) {
if (source == target)
if (source == target) {
return true;
if (target.getParent() != null)
}
if (target.getParent() != null) {
return isAncestor(source, target.getParent());
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*******************************************************************************/
package org.eclipse.gef.examples.logicdesigner.edit;

import java.util.Iterator;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
Expand Down Expand Up @@ -201,9 +199,8 @@ private static int getResizeDirections(Class modelClass) {
}
if (LogicLabel.class.equals(modelClass)) {
return PositionConstants.EAST | PositionConstants.WEST;
} else {
return PositionConstants.NSEW;
}
return PositionConstants.NSEW;
}

/*
Expand Down Expand Up @@ -255,11 +252,8 @@ protected Command getCloneCommand(ChangeBoundsRequest request) {

clone.setParent((LogicDiagram) getHost().getModel());

Iterator i = request.getEditParts().iterator();
GraphicalEditPart currPart = null;

while (i.hasNext()) {
currPart = (GraphicalEditPart) i.next();
for (EditPart ep : request.getEditParts()) {
GraphicalEditPart currPart = (GraphicalEditPart) ep;
clone.addPart((LogicSubpart) currPart.getModel(), (Rectangle) getConstraintFor(request, currPart));
}

Expand Down
39 changes: 21 additions & 18 deletions org.eclipse.gef/src/org/eclipse/gef/SnapToGeometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ protected static class Entry {
* @param location the location
*/
protected Entry(int type, int location) {
if (type < -1 || type > 1)
if (type < -1 || type > 1) {
throw new IllegalArgumentException("Unrecognized snap type"); //$NON-NLS-1$
}
this.type = type;
this.location = location;
}
Expand Down Expand Up @@ -201,7 +202,7 @@ protected void setThreshold(double newThreshold) {
* @param exclusions the children to exclude
* @return a list of parts which should be snapped to
*/
protected List generateSnapPartsList(List exclusions) {
protected List<? extends GraphicalEditPart> generateSnapPartsList(List<? extends EditPart> exclusions) {
// Don't snap to any figure that is being dragged
List<? extends GraphicalEditPart> children = new ArrayList<>(container.getChildren());
children.removeAll(exclusions);
Expand Down Expand Up @@ -229,13 +230,16 @@ protected double getCorrectionFor(Entry[] entries, Map extendedData, boolean ver
// far) there is no middle pixel so favor the left-most/top-most pixel
// (which is what
// populateRowsAndCols() does by using int precision).
if ((int) (near - far) % 2 != 0)
if ((int) (near - far) % 2 != 0) {
total -= 1.0;
}
double result = getCorrectionFor(entries, extendedData, vert, total / 2, 0);
if (result == getThreshold())
if (result == getThreshold()) {
result = getCorrectionFor(entries, extendedData, vert, near, -1);
if (result == getThreshold())
}
if (result == getThreshold()) {
result = getCorrectionFor(entries, extendedData, vert, far, 1);
}
return result;
}

Expand All @@ -255,13 +259,13 @@ protected double getCorrectionFor(Entry[] entries, Map extendedData, boolean ver
double result = getThreshold();

String property;
if (side == -1)
if (side == -1) {
property = vert ? KEY_WEST_ANCHOR : KEY_NORTH_ANCHOR;
else
} else {
property = vert ? KEY_EAST_ANCHOR : KEY_SOUTH_ANCHOR;
}

for (int i = 0; i < entries.length; i++) {
Entry entry = entries[i];
for (Entry entry : entries) {
double magnitude;

if (entry.type == -1 && side != 0) {
Expand Down Expand Up @@ -309,11 +313,11 @@ protected Rectangle getFigureBounds(GraphicalEditPart part) {
* @since 3.0
* @param parts a List of EditParts
*/
protected void populateRowsAndCols(List parts) {
protected void populateRowsAndCols(List<? extends GraphicalEditPart> parts) {
rows = new Entry[parts.size() * 3];
cols = new Entry[parts.size() * 3];
for (int i = 0; i < parts.size(); i++) {
GraphicalEditPart child = (GraphicalEditPart) parts.get(i);
GraphicalEditPart child = parts.get(i);
Rectangle bounds = getFigureBounds(child);
cols[i * 3] = new Entry(-1, bounds.x);
rows[i * 3] = new Entry(-1, bounds.y);
Expand Down Expand Up @@ -341,15 +345,15 @@ public int snapRectangle(Request request, int snapOrientation, PrecisionRectangl
boolean isClone = request.getType().equals(RequestConstants.REQ_CLONE);
if (rows == null || cols == null || isClone != cachedCloneBool) {
cachedCloneBool = isClone;
List exclusionSet = Collections.EMPTY_LIST;
if (!isClone && request instanceof GroupRequest)
exclusionSet = ((GroupRequest) request).getEditParts();
List<? extends EditPart> exclusionSet = Collections.emptyList();
if (!isClone && request instanceof GroupRequest groupRequest) {
exclusionSet = groupRequest.getEditParts();
}
populateRowsAndCols(generateSnapPartsList(exclusionSet));
}

if ((snapOrientation & HORIZONTAL) != 0) {
double xcorrect = getThreshold();
xcorrect = getCorrectionFor(cols, request.getExtendedData(), true, baseRect.preciseX(),
double xcorrect = getCorrectionFor(cols, request.getExtendedData(), true, baseRect.preciseX(),
baseRect.preciseRight());
if (xcorrect != getThreshold()) {
snapOrientation &= ~HORIZONTAL;
Expand All @@ -358,8 +362,7 @@ public int snapRectangle(Request request, int snapOrientation, PrecisionRectangl
}

if ((snapOrientation & VERTICAL) != 0) {
double ycorrect = getThreshold();
ycorrect = getCorrectionFor(rows, request.getExtendedData(), false, baseRect.preciseY(),
double ycorrect = getCorrectionFor(rows, request.getExtendedData(), false, baseRect.preciseY(),
baseRect.preciseBottom());
if (ycorrect != getThreshold()) {
snapOrientation &= ~VERTICAL;
Expand Down
Loading

0 comments on commit 3485608

Please sign in to comment.