diff --git a/org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/uml/UMLClassFigure.java b/org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/uml/UMLClassFigure.java index b2b7645a4..ea8442bb6 100644 --- a/org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/uml/UMLClassFigure.java +++ b/org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/uml/UMLClassFigure.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2023 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 @@ -18,15 +18,15 @@ import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Image; +import org.eclipse.draw2d.Border; import org.eclipse.draw2d.Figure; -import org.eclipse.draw2d.Graphics; -import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.Label; import org.eclipse.draw2d.LineBorder; import org.eclipse.draw2d.MarginBorder; +import org.eclipse.draw2d.PositionConstants; +import org.eclipse.draw2d.SeparatorBorder; import org.eclipse.draw2d.ToolbarLayout; import org.eclipse.draw2d.geometry.Insets; -import org.eclipse.draw2d.geometry.Rectangle; public class UMLClassFigure extends Figure { @@ -37,18 +37,6 @@ public class UMLClassFigure extends Figure { static Font BOLD = new Font(null, "", 10, SWT.BOLD); //$NON-NLS-1$ public UMLClassFigure() { - class SeparatorBorder extends MarginBorder { - SeparatorBorder() { - super(3, 5, 3, 5); - } - - @Override - public void paint(IFigure figure, Graphics graphics, Insets insets) { - Rectangle where = getPaintRectangle(figure, insets); - graphics.drawLine(where.getTopLeft(), where.getTopRight()); - } - } - Label header = new Label("ClassName", classImage); //$NON-NLS-1$ header.setFont(BOLD); header.setBorder(new MarginBorder(3, 5, 3, 5)); @@ -59,7 +47,8 @@ public void paint(IFigure figure, Graphics graphics, Insets insets) { layout.setStretchMinorAxis(false); attributes.add(new Label("name : String")); //$NON-NLS-1$ attributes.add(new Label("ID : String")); //$NON-NLS-1$ - attributes.setBorder(new SeparatorBorder()); + Border attributeBorder = new SeparatorBorder(new Insets(3, 5, 3, 5), PositionConstants.TOP); + attributes.setBorder(attributeBorder); Figure methods = new Figure(); layout = new ToolbarLayout(); @@ -67,7 +56,8 @@ public void paint(IFigure figure, Graphics graphics, Insets insets) { layout.setStretchMinorAxis(false); methods.add(new Label("foo() : int")); //$NON-NLS-1$ methods.add(new Label("bar() : char")); //$NON-NLS-1$ - methods.setBorder(new SeparatorBorder()); + Border methodBorder = new SeparatorBorder(new Insets(3, 5, 3, 5), PositionConstants.TOP); + methods.setBorder(methodBorder); setBorder(new LineBorder()); setLayoutManager(new ToolbarLayout()); diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/SeparatorBorder.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/SeparatorBorder.java new file mode 100644 index 000000000..81a348ca3 --- /dev/null +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/SeparatorBorder.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2000, 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 + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.draw2d; + +import org.eclipse.swt.graphics.Color; + +import org.eclipse.draw2d.geometry.Insets; +import org.eclipse.draw2d.geometry.Rectangle; + +/** + * This border behaves similar to a {@link LineBorder} with width {@code 1}, + * except that individual sides are drawn. The sides are specified via + * {@link PositionConstants#LEFT}, {@link PositionConstants#RIGHT}, + * {@link PositionConstants#TOP} and {@link PositionConstants#BOTTOM}. Multiple + * sides can be specified by logically or-ing these constants. + * + * @since 3.16 + */ +public final class SeparatorBorder extends MarginBorder { + private final int position; + private Color color; + + /** + * The sides are specified via {@link PositionConstants#LEFT}, + * {@link PositionConstants#RIGHT}, {@link PositionConstants#TOP}, + * {@link PositionConstants#BOTTOM}. Multiple sides can be specified by + * logically or-ing these constants. + * + * @param insets The Insets for the border + * @param sides The integer-encoded sides that should be drawn. + * @see PositionConstants#LEFT + * @see PositionConstants#RIGHT + * @see PositionConstants#TOP + * @see PositionConstants#BOTTOM + */ + public SeparatorBorder(Insets insets, int sides) { + super(insets); + this.position = sides; + } + + /** + * If {@code null} is given as a parameter, the current foreground color of the + * graphics object is used while painting. + * + * @param color The color used for drawing each side. + */ + public void setColor(Color color) { + this.color = color; + } + + @Override + public void paint(IFigure f, Graphics g, Insets i) { + Rectangle r = getPaintRectangle(f, i); + if (color != null) { + g.setForegroundColor(color); + } + g.setLineWidth(1); + g.setLineStyle(Graphics.LINE_SOLID); + if ((position & PositionConstants.LEFT) > 0) { + g.drawLine(r.getTopLeft(), r.getBottomLeft()); + } + if ((position & PositionConstants.RIGHT) > 0) { + r.width--; + g.drawLine(r.getTopRight(), r.getBottomRight()); + r.width++; + } + if ((position & PositionConstants.TOP) > 0) { + g.drawLine(r.getTopLeft(), r.getTopRight()); + } + if ((position & PositionConstants.BOTTOM) > 0) { + r.height--; + g.drawLine(r.getBottomLeft(), r.getBottomRight()); + r.height++; + } + } +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SeparatorBorder.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SeparatorBorder.java deleted file mode 100644 index 3028cd403..000000000 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SeparatorBorder.java +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2010 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 - * http://www.eclipse.org/legal/epl-2.0. - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.gef.internal.ui.palette.editparts; - -import org.eclipse.draw2d.ColorConstants; -import org.eclipse.draw2d.Graphics; -import org.eclipse.draw2d.IFigure; -import org.eclipse.draw2d.MarginBorder; -import org.eclipse.draw2d.geometry.Insets; -import org.eclipse.draw2d.geometry.Rectangle; - -final class SeparatorBorder extends MarginBorder { - - SeparatorBorder(int t, int l, int b, int r) { - super(t, l, b, r); - } - - @Override - public void paint(IFigure f, Graphics g, Insets i) { - Rectangle r = getPaintRectangle(f, i); - r.height--; - g.setForegroundColor(ColorConstants.buttonDarker); - g.drawLine(r.x, r.bottom(), r.right(), r.bottom()); - } - -}