Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed RulerLayout to directly inherit from AbstractLayout #418 #419

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion org.eclipse.draw2d/src/org/eclipse/draw2d/XYLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public void remove(IFigure figure) {
@Override
public void setConstraint(IFigure figure, Object newConstraint) {
super.setConstraint(figure, newConstraint);
if (newConstraint instanceof Rectangle rect) {
if (newConstraint != null) {
if (!(newConstraint instanceof Rectangle rect)) {
ptziegler marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
Expand Down
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,31 @@ 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 != null) {
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);
}
}

}
Loading