Skip to content

Commit

Permalink
Changed RulerLayout to directly inherit from AbstractLayout eclipse-g…
Browse files Browse the repository at this point in the history
…ef#418

RulerLayout inherited from XYLayout. The later assumed that children
constraint are rectangles. As this is not valid for RulerLayouts the
RulerLayout was changed to inherit from AstractLayout and use its own
constraint map with Integers.

Fixes eclipse-gef#418
  • Loading branch information
azoitl committed Apr 12, 2024
1 parent 8fafd3c commit a90e09a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
6 changes: 4 additions & 2 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/XYLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ public void remove(IFigure figure) {
@Override
public void setConstraint(IFigure figure, Object newConstraint) {
super.setConstraint(figure, newConstraint);
if (newConstraint instanceof Rectangle rect) {
constraints.put(figure, rect);
if (!(newConstraint instanceof Rectangle rect)) {
throw new IllegalArgumentException("XYLayout was given " + newConstraint.getClass().getName() //$NON-NLS-1$
+ " as constraint for Figure. Rectangle expected!"); //$NON-NLS-1$
}
constraints.put(figure, rect);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2010 IBM Corporation and others.
* Copyright (c) 2003, 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,8 +12,11 @@
*******************************************************************************/
package org.eclipse.gef.internal.ui.rulers;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

Expand All @@ -25,7 +28,9 @@
* @author Pratik Shah
* @since 3.0
*/
public class RulerLayout extends XYLayout {
public class RulerLayout extends AbstractLayout {

private final Map<IFigure, Integer> constraints = new HashMap<>();

/**
* @see org.eclipse.draw2d.AbstractLayout#calculatePreferredSize(org.eclipse.draw2d.IFigure,
Expand All @@ -40,7 +45,7 @@ protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHi
* @see org.eclipse.draw2d.AbstractLayout#getConstraint(org.eclipse.draw2d.IFigure)
*/
@Override
public Object getConstraint(IFigure child) {
public Integer getConstraint(IFigure child) {
return constraints.get(child);
}

Expand All @@ -52,7 +57,13 @@ public void layout(IFigure container) {
Rectangle rulerSize = container.getClientArea();
for (IFigure child : container.getChildren()) {
Dimension childSize = child.getPreferredSize();
int position = ((Integer) getConstraint(child)).intValue();

Integer childPos = getConstraint(child);
if (childPos == null) {
continue;
}

int position = childPos.intValue();
if (((RulerFigure) container).isHorizontal()) {
childSize.height = rulerSize.height - 1;
Rectangle.SINGLETON.setLocation(position - (childSize.width / 2), rulerSize.y);
Expand All @@ -65,4 +76,29 @@ public void layout(IFigure container) {
}
}

/**
* @see org.eclipse.draw2d.LayoutManager#remove(IFigure)
*/
@Override
public void remove(IFigure figure) {
super.remove(figure);
constraints.remove(figure);
}

/**
* Sets the layout constraint of the given figure. The constraints can only be
* of type {@link Integer}.
*
* @see org.eclipse.draw2d.LayoutManager#setConstraint(IFigure, Object)
*/
@Override
public void setConstraint(IFigure figure, Object newConstraint) {
super.setConstraint(figure, newConstraint);
if (!(newConstraint instanceof Integer intConstraint)) {
throw new IllegalArgumentException("RulerLayout was given " + newConstraint.getClass().getName() //$NON-NLS-1$
+ " as constraint for Figure. Integer expected!"); //$NON-NLS-1$
}
constraints.put(figure, intConstraint);
}

}

0 comments on commit a90e09a

Please sign in to comment.