Skip to content

Commit

Permalink
[Modernize Code] Type the return value of GraphicalEditPolicy getHost (
Browse files Browse the repository at this point in the history
…#284)

Java now allows overriding return values. Using this for
GraphicalEditPolicy.getHost() reduces cast requirements and makes code
clearer.
  • Loading branch information
azoitl authored Nov 22, 2023
1 parent 75456f6 commit da30f06
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
*******************************************************************************/
package org.eclipse.gef.examples.flow.policies;

import org.eclipse.swt.graphics.Color;

import org.eclipse.draw2d.IFigure;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.swt.graphics.Color;

/**
* @author Daniel Lee
Expand All @@ -44,7 +45,7 @@ private Color getContainerBackground() {
}

private IFigure getContainerFigure() {
return ((GraphicalEditPart) getHost()).getFigure();
return getHost().getFigure();
}

/**
Expand Down Expand Up @@ -74,8 +75,10 @@ protected void showHighlight() {
*/
@Override
public void showTargetFeedback(Request request) {
if (request.getType().equals(RequestConstants.REQ_CREATE) || request.getType().equals(RequestConstants.REQ_ADD))
if (request.getType().equals(RequestConstants.REQ_CREATE)
|| request.getType().equals(RequestConstants.REQ_ADD)) {
showHighlight();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.eclipse.draw2d.IFigure;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;

Expand All @@ -42,7 +41,7 @@ private Color getContainerBackground() {
}

private IFigure getContainerFigure() {
return ((GraphicalEditPart) getHost()).getFigure();
return getHost().getFigure();
}

@Override
Expand All @@ -67,8 +66,9 @@ public void showTargetFeedback(Request request) {
|| request.getType().equals(RequestConstants.REQ_CLONE)
|| request.getType().equals(RequestConstants.REQ_CONNECTION_START)
|| request.getType().equals(RequestConstants.REQ_CONNECTION_END)
|| request.getType().equals(RequestConstants.REQ_CREATE))
|| request.getType().equals(RequestConstants.REQ_CREATE)) {
showHighlight();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class LabelDirectEditPolicy extends DirectEditPolicy {
protected Command getDirectEditCommand(DirectEditRequest edit) {
String labelText = (String) edit.getCellEditor().getValue();
LogicLabelEditPart label = (LogicLabelEditPart) getHost();
LogicLabelCommand command = new LogicLabelCommand((LogicLabel) label.getModel(), labelText);
return command;
return new LogicLabelCommand((LogicLabel) label.getModel(), labelText);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;

import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
Expand All @@ -43,8 +42,9 @@ protected Command getConnectionCompleteCommand(CreateConnectionRequest request)
ConnectionCommand command = (ConnectionCommand) request.getStartCommand();
command.setTarget(getLogicSubpart());
ConnectionAnchor ctor = getLogicEditPart().getTargetConnectionAnchor(request);
if (ctor == null)
if (ctor == null) {
return null;
}
command.setTargetTerminal(getLogicEditPart().mapConnectionAnchorToTerminal(ctor));
return command;
}
Expand Down Expand Up @@ -83,8 +83,9 @@ protected LogicSubpart getLogicSubpart() {

@Override
protected Command getReconnectTargetCommand(ReconnectRequest request) {
if (getLogicSubpart() instanceof LiveOutput || getLogicSubpart() instanceof GroundOutput)
if (getLogicSubpart() instanceof LiveOutput || getLogicSubpart() instanceof GroundOutput) {
return null;
}

ConnectionCommand cmd = new ConnectionCommand();
cmd.setWire((Wire) request.getConnectionEditPart().getModel());
Expand All @@ -107,7 +108,7 @@ protected Command getReconnectSourceCommand(ReconnectRequest request) {
}

protected NodeFigure getNodeFigure() {
return (NodeFigure) ((GraphicalEditPart) getHost()).getFigure();
return (NodeFigure) getHost().getFigure();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class LogicResizableEditPolicy extends ResizableEditPolicy {
*/
@Override
protected IFigure createDragSourceFeedbackFigure() {
IFigure figure = createFigure((GraphicalEditPart) getHost(), null);
IFigure figure = createFigure(getHost(), null);

figure.setBounds(getInitialFeedbackBounds());
figure.validate();
Expand All @@ -66,8 +66,9 @@ protected IFigure createDragSourceFeedbackFigure() {
protected IFigure createFigure(GraphicalEditPart part, IFigure parent) {
IFigure child = getCustomFeedbackFigure(part.getModel());

if (parent != null)
if (parent != null) {
parent.add(child);
}

Rectangle childBounds = part.getFigure().getBounds().getCopy();

Expand All @@ -86,25 +87,25 @@ protected IFigure createFigure(GraphicalEditPart part, IFigure parent) {
protected IFigure getCustomFeedbackFigure(Object modelPart) {
IFigure figure;

if (modelPart instanceof Circuit)
if (modelPart instanceof Circuit) {
figure = new CircuitFeedbackFigure();
else if (modelPart instanceof LogicFlowContainer)
} else if (modelPart instanceof LogicFlowContainer) {
figure = new LogicFlowFeedbackFigure();
else if (modelPart instanceof LogicLabel)
} else if (modelPart instanceof LogicLabel) {
figure = new LabelFeedbackFigure();
else if (modelPart instanceof LED)
} else if (modelPart instanceof LED) {
figure = new LEDFeedbackFigure();
else if (modelPart instanceof OrGate)
} else if (modelPart instanceof OrGate) {
figure = new OrGateFeedbackFigure();
else if (modelPart instanceof XORGate)
} else if (modelPart instanceof XORGate) {
figure = new XOrGateFeedbackFigure();
else if (modelPart instanceof GroundOutput)
} else if (modelPart instanceof GroundOutput) {
figure = new GroundFeedbackFigure();
else if (modelPart instanceof LiveOutput)
} else if (modelPart instanceof LiveOutput) {
figure = new LiveOutputFeedbackFigure();
else if (modelPart instanceof AndGate)
} else if (modelPart instanceof AndGate) {
figure = new AndGateFeedbackFigure();
else {
} else {
figure = new RectangleFigure();
((RectangleFigure) figure).setXOR(true);
((RectangleFigure) figure).setFill(true);
Expand Down Expand Up @@ -138,6 +139,6 @@ protected Rectangle getInitialFeedbackBounds() {
*/
@Override
protected ResizeTracker getResizeTracker(int direction) {
return new LogicResizeTracker((GraphicalEditPart) getHost(), direction);
return new LogicResizeTracker(getHost(), direction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import org.eclipse.draw2d.PolylineConnection;

import org.eclipse.gef.GraphicalEditPart;

public class WireEndpointEditPolicy extends org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy {

@Override
Expand All @@ -25,7 +23,7 @@ protected void addSelectionHandles() {
}

protected PolylineConnection getConnectionFigure() {
return (PolylineConnection) ((GraphicalEditPart) getHost()).getFigure();
return (PolylineConnection) getHost().getFigure();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.handles.BendpointCreationHandle;
import org.eclipse.gef.handles.BendpointHandle;
import org.eclipse.gef.handles.BendpointMoveHandle;
import org.eclipse.gef.requests.BendpointRequest;

Expand Down Expand Up @@ -66,24 +67,26 @@ private List createHandlesForAutomaticBendpoints() {
List list = new ArrayList();
ConnectionEditPart connEP = (ConnectionEditPart) getHost();
PointList points = getConnection().getPoints();
for (int i = 0; i < points.size() - 1; i++)
for (int i = 0; i < points.size() - 1; i++) {
list.add(new BendpointCreationHandle(connEP, 0, i));
}

return list;
}

private List createHandlesForUserBendpoints() {
List list = new ArrayList();
List<BendpointHandle> list = new ArrayList<>();
ConnectionEditPart connEP = (ConnectionEditPart) getHost();
PointList points = getConnection().getPoints();
List bendPoints = (List) getConnection().getRoutingConstraint();
int bendPointIndex = 0;
Point currBendPoint = null;

if (bendPoints == null)
if (bendPoints == null) {
bendPoints = NULL_CONSTRAINT;
else if (!bendPoints.isEmpty())
} else if (!bendPoints.isEmpty()) {
currBendPoint = ((Bendpoint) bendPoints.get(0)).getLocation();
}

for (int i = 0; i < points.size() - 1; i++) {
// Put a create handle on the middle of every segment
Expand All @@ -97,8 +100,9 @@ else if (!bendPoints.isEmpty())

// Go to the next user bendpoint
bendPointIndex++;
if (bendPointIndex < bendPoints.size())
if (bendPointIndex < bendPoints.size()) {
currBendPoint = ((Bendpoint) bendPoints.get(bendPointIndex)).getLocation();
}
}
}

Expand All @@ -117,12 +121,10 @@ else if (!bendPoints.isEmpty())
*/
@Override
protected List createSelectionHandles() {
List list = new ArrayList();
if (isAutomaticallyBending())
list = createHandlesForAutomaticBendpoints();
else
list = createHandlesForUserBendpoints();
return list;
if (isAutomaticallyBending()) {
return createHandlesForAutomaticBendpoints();
}
return createHandlesForUserBendpoints();
}

/**
Expand Down Expand Up @@ -154,8 +156,9 @@ protected void eraseConnectionFeedback(BendpointRequest request) {
*/
@Override
public void eraseSourceFeedback(Request request) {
if (REQ_MOVE_BENDPOINT.equals(request.getType()) || REQ_CREATE_BENDPOINT.equals(request.getType()))
if (REQ_MOVE_BENDPOINT.equals(request.getType()) || REQ_CREATE_BENDPOINT.equals(request.getType())) {
eraseConnectionFeedback((BendpointRequest) request);
}
}

/**
Expand All @@ -166,12 +169,14 @@ public void eraseSourceFeedback(Request request) {
@Override
public Command getCommand(Request request) {
if (REQ_MOVE_BENDPOINT.equals(request.getType())) {
if (isDeleting)
if (isDeleting) {
return getDeleteBendpointCommand((BendpointRequest) request);
}
return getMoveBendpointCommand((BendpointRequest) request);
}
if (REQ_CREATE_BENDPOINT.equals(request.getType()))
if (REQ_CREATE_BENDPOINT.equals(request.getType())) {
return getCreateBendpointCommand((BendpointRequest) request);
}
return null;
}

Expand Down Expand Up @@ -221,8 +226,9 @@ private boolean lineContainsPoint(Point p1, Point p2, Point p) {
rect.setLocation(p1.x, p1.y);
rect.union(p2.x, p2.y);
rect.expand(tolerance, tolerance);
if (!rect.contains(p.x, p.y))
if (!rect.contains(p.x, p.y)) {
return false;
}

int v1x, v1y, v2x, v2y;
int numerator, denominator;
Expand Down Expand Up @@ -254,8 +260,9 @@ private boolean lineContainsPoint(Point p1, Point p2, Point p) {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// $TODO optimize so that handles aren't added constantly.
if (getHost().getSelected() != EditPart.SELECTED_NONE)
if (getHost().getSelected() != EditPart.SELECTED_NONE) {
addSelectionHandles();
}
}

/**
Expand All @@ -264,10 +271,11 @@ public void propertyChange(PropertyChangeEvent evt) {
*/
protected void restoreOriginalConstraint() {
if (originalConstraint != null) {
if (originalConstraint == NULL_CONSTRAINT)
if (originalConstraint == NULL_CONSTRAINT) {
getConnection().setRoutingConstraint(null);
else
} else {
getConnection().setRoutingConstraint(originalConstraint);
}
}
}

Expand All @@ -277,8 +285,9 @@ protected void restoreOriginalConstraint() {
*/
protected void saveOriginalConstraint() {
originalConstraint = (List) getConnection().getRoutingConstraint();
if (originalConstraint == null)
if (originalConstraint == null) {
originalConstraint = NULL_CONSTRAINT;
}
getConnection().setRoutingConstraint(new ArrayList(originalConstraint));
}

Expand All @@ -296,8 +305,9 @@ private void setReferencePoints(BendpointRequest request) {
if (smallestDistance == -1 || points.getPoint(i).getDistance2(bp) < smallestDistance) {
bpIndex = i;
smallestDistance = points.getPoint(i).getDistance2(bp);
if (smallestDistance == 0)
if (smallestDistance == 0) {
break;
}
}
}

Expand Down Expand Up @@ -358,8 +368,9 @@ protected void showDeleteBendpointFeedback(BendpointRequest request) {
*/
protected void showMoveBendpointFeedback(BendpointRequest request) {
Point p = new Point(request.getLocation());
if (!isDeleting)
if (!isDeleting) {
setReferencePoints(request);
}

if (lineContainsPoint(ref1, ref2, p)) {
if (!isDeleting) {
Expand All @@ -373,8 +384,9 @@ protected void showMoveBendpointFeedback(BendpointRequest request) {
isDeleting = false;
eraseSourceFeedback(request);
}
if (originalConstraint == null)
if (originalConstraint == null) {
saveOriginalConstraint();
}
List constraint = (List) getConnection().getRoutingConstraint();
getConnection().translateToRelative(p);
Bendpoint bp = new AbsoluteBendpoint(p);
Expand All @@ -392,10 +404,11 @@ protected void showMoveBendpointFeedback(BendpointRequest request) {
*/
@Override
public void showSourceFeedback(Request request) {
if (REQ_MOVE_BENDPOINT.equals(request.getType()))
if (REQ_MOVE_BENDPOINT.equals(request.getType())) {
showMoveBendpointFeedback((BendpointRequest) request);
else if (REQ_CREATE_BENDPOINT.equals(request.getType()))
} else if (REQ_CREATE_BENDPOINT.equals(request.getType())) {
showCreateBendpointFeedback((BendpointRequest) request);
}
}

}
Loading

0 comments on commit da30f06

Please sign in to comment.