Skip to content

Commit

Permalink
Issue #466 Move bounds-related method to RenderingFacade
Browse files Browse the repository at this point in the history
With this commit, the SelectionModel class is no longer responsible for
computing bounds.
  • Loading branch information
prmr committed Jun 7, 2022
1 parent 06875c1 commit d79f928
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
6 changes: 3 additions & 3 deletions src/org/jetuml/gui/DiagramCanvasController.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ else if( aDragMode == DragMode.DRAG_LASSO )
private void alignMoveToGrid()
{
Iterator<Node> selectedNodes = aSelectionModel.getSelectedNodes().iterator();
Rectangle entireBounds = aSelectionModel.getEntireSelectionBounds();
Rectangle entireBounds = RenderingFacade.getBoundsIncludingParents(aSelectionModel);

if( selectedNodes.hasNext() )
{
Expand Down Expand Up @@ -549,7 +549,7 @@ else if(aDragMode == DragMode.DRAG_RUBBERBAND)
// finds the point to reveal based on the entire selection
private Point computePointToReveal(Point pMousePoint)
{
Rectangle bounds = aSelectionModel.getEntireSelectionBounds();
Rectangle bounds = RenderingFacade.getBoundsIncludingParents(aSelectionModel);
int x = bounds.getMaxX();
int y = bounds.getMaxY();

Expand Down Expand Up @@ -577,7 +577,7 @@ private void moveSelection(Point pMousePoint)
aSelectionModel.getSelectedNodes().forEach(selected -> selected.translate(dx, dy));

// If this translation results in exceeding the canvas bounds, roll back.
Rectangle bounds = aSelectionModel.getEntireSelectionBounds();
Rectangle bounds =RenderingFacade.getBoundsIncludingParents(aSelectionModel);
int dxCorrection = Math.max(-bounds.getX(), 0)
+ Math.min((int)aCanvas.getWidth() - bounds.getMaxX(), 0);
int dyCorrection = Math.max(-bounds.getY(), 0)
Expand Down
56 changes: 7 additions & 49 deletions src/org/jetuml/gui/SelectionModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*******************************************************************************/
package org.jetuml.gui;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -32,7 +34,6 @@
import org.jetuml.diagram.Node;
import org.jetuml.geom.Line;
import org.jetuml.geom.Rectangle;
import org.jetuml.viewers.DiagramViewer;
import org.jetuml.viewers.edges.EdgeViewerRegistry;
import org.jetuml.viewers.nodes.NodeViewerRegistry;

Expand Down Expand Up @@ -77,59 +78,16 @@ public void selectAll(DiagramData pDiagramData)
pDiagramData.edges().forEach(this::internalAddToSelection);
aObserver.selectionModelChanged();
}

/*
* Returns a rectangle that represents the bounding box of the last selected element.
*/
private Rectangle getLastSelectedBounds()
{
Optional<DiagramElement> lastSelected = getLastSelected();
assert lastSelected.isPresent();
return DiagramViewer.getBounds(lastSelected.get());
}

/**
* @return A rectangle that represents the bounding box of the
* entire selection including the bounds of their parent nodes.
*/
public Rectangle getEntireSelectionBounds()
{
Rectangle bounds = getLastSelectedBounds();
for(DiagramElement selected : aSelected )
{
bounds = addBounds(bounds, selected);
}
return bounds;
}

// Recursively enlarge the current rectangle to include the selected DiagramElements
private Rectangle addBounds(Rectangle pBounds, DiagramElement pSelected)
{
if( pSelected instanceof Node && ((Node) pSelected).hasParent())
{
return addBounds(pBounds, ((Node) pSelected).getParent());
}
else
{
return pBounds.add(DiagramViewer.getBounds(pSelected));
}
}

/**
* @return An iterable of all selected nodes. This
* corresponds to the entire selection, except the edge.
* @return A list of all the selected nodes.
*/
public List<Node> getSelectedNodes()
{
List<Node> result = new ArrayList<>();
for( DiagramElement element : aSelected )
{
if( element instanceof Node )
{
result.add((Node) element);
}
}
return result;
return aSelected.stream()
.filter(e -> Node.class.isAssignableFrom(e.getClass()))
.map(Node.class::cast)
.collect(toList());
}

/**
Expand Down
43 changes: 42 additions & 1 deletion src/org/jetuml/viewers/RenderingFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public static Canvas createIcon(DiagramElement pElement)
}
}

/**
* @param pElements The elements whose bounds we are interested in.
* @return The rectangle that bounds all elements in pElements, excluding their parent node.
* @pre pElements != null
* @pre pElements has at least one element.
*/
public static Rectangle getBounds(Iterable<DiagramElement> pElements)
{
assert pElements != null;
Expand All @@ -70,5 +76,40 @@ public static Rectangle getBounds(Iterable<DiagramElement> pElements)
bounds = bounds.add(DiagramViewer.getBounds(elements.next()));
}
return bounds;
}
}

/**
* @param pElements The elements whose bounds we are interested in.
* @return A rectangle that represents the bounding box of the
* entire selection including the bounds of their parent nodes.
* @pre pElements != null
* @pre pElements has at least one element.
*/
public static Rectangle getBoundsIncludingParents(Iterable<DiagramElement> pElements)
{
assert pElements != null;
assert pElements.iterator().hasNext();
Iterator<DiagramElement> elements = pElements.iterator();
DiagramElement next = elements.next();
Rectangle bounds = DiagramViewer.getBounds(next);
bounds = addBounds(bounds, next);
while( elements.hasNext() )
{
bounds = addBounds(bounds, elements.next());
}
return bounds;
}

// Recursively enlarge the current rectangle to include the selected DiagramElements
private static Rectangle addBounds(Rectangle pBounds, DiagramElement pElement)
{
if( pElement instanceof Node && ((Node) pElement).hasParent())
{
return addBounds(pBounds, ((Node) pElement).getParent());
}
else
{
return pBounds.add(DiagramViewer.getBounds(pElement));
}
}
}
2 changes: 1 addition & 1 deletion test/org/jetuml/gui/TestSelectionModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void assertContent(DiagramElement... pElements)

private void assertEntireSelectionBounds(int pX, int pY, int pWidth, int pHeight)
{
assertEquals(new Rectangle(pX, pY, pWidth, pHeight), aModel.getEntireSelectionBounds());
assertEquals(new Rectangle(pX, pY, pWidth, pHeight), RenderingFacade.getBoundsIncludingParents(aModel));
}

private void assertSelectionBounds(int pX, int pY, int pWidth, int pHeight)
Expand Down

0 comments on commit d79f928

Please sign in to comment.