From a90e09a3b987c42a183d7b8f87806fe9fcae6fdb Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Fri, 12 Apr 2024 17:30:42 +0200 Subject: [PATCH] Changed RulerLayout to directly inherit from AbstractLayout #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 https://github.com/eclipse/gef-classic/issues/418 --- .../src/org/eclipse/draw2d/XYLayout.java | 6 ++- .../gef/internal/ui/rulers/RulerLayout.java | 46 +++++++++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/XYLayout.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/XYLayout.java index 0f335c644..891c2a73a 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/XYLayout.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/XYLayout.java @@ -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); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerLayout.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerLayout.java index 6c3a56ae3..aa8865c1a 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerLayout.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerLayout.java @@ -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 @@ -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; @@ -25,7 +28,9 @@ * @author Pratik Shah * @since 3.0 */ -public class RulerLayout extends XYLayout { +public class RulerLayout extends AbstractLayout { + + private final Map constraints = new HashMap<>(); /** * @see org.eclipse.draw2d.AbstractLayout#calculatePreferredSize(org.eclipse.draw2d.IFigure, @@ -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); } @@ -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); @@ -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); + } + }