Skip to content

Commit

Permalink
Make SeparatorBorder public API and move it from GEF to Draw2D
Browse files Browse the repository at this point in the history
Use Case: Border between figures directly adjacent to one another. See
the UMLClass example, for reference.
  • Loading branch information
ptziegler authored and azoitl committed Apr 28, 2024
1 parent 3cfbd97 commit b45b480
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 54 deletions.
@@ -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
Expand All @@ -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 {

Expand All @@ -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));
Expand All @@ -59,15 +47,17 @@ 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();
methods.setLayoutManager(layout);
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());
Expand Down
86 changes: 86 additions & 0 deletions 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++;
}
}
}

This file was deleted.

0 comments on commit b45b480

Please sign in to comment.