Skip to content

Commit

Permalink
Minor code clean-ups in all Borders
Browse files Browse the repository at this point in the history
Fixes issues reported by sonarlint in all Border classes.
  • Loading branch information
azoitl committed Nov 4, 2023
1 parent f5ae4fa commit b4387db
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

package org.eclipse.draw2d.examples.uml;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
Expand All @@ -22,10 +27,6 @@
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

public class UMLClassFigure extends Figure {

Expand Down Expand Up @@ -53,15 +54,16 @@ public void paint(IFigure figure, Graphics graphics, Insets insets) {
header.setBorder(new MarginBorder(3, 5, 3, 5));

Figure attributes = new Figure();
ToolbarLayout layout;
attributes.setLayoutManager(layout = new ToolbarLayout());
ToolbarLayout layout = new ToolbarLayout();
attributes.setLayoutManager(layout);
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());

Figure methods = new Figure();
methods.setLayoutManager(layout = new ToolbarLayout());
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$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.OrderedLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;

Expand All @@ -27,7 +28,7 @@ public class CompartmentFigure extends Figure {

public CompartmentFigure() {
ToolbarLayout layout = new ToolbarLayout();
layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
layout.setMinorAlignment(OrderedLayout.ALIGN_TOPLEFT);
layout.setStretchMinorAxis(false);
layout.setSpacing(2);
setLayoutManager(layout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
*/
public class AbstractTextTest extends BaseTestCase {

private static final String FRAGMENT_LOCATION_ERROR = "Fragment location error"; //$NON-NLS-1$

static class TestBorder extends AbstractFlowBorder {
private Insets insets;
private final Insets insets;

TestBorder(Insets insets) {
this.insets = insets;
Expand All @@ -40,10 +42,10 @@ public Insets getInsets(IFigure figure) {
public Rectangle expected = new Rectangle();

protected void assertFragmentLocation(TextFragmentBox box) {
assertEquals("Fragment location error", box.getX(), expected.x);
assertEquals("Fragment location error", box.getBaseline() - box.getAscent(), expected.y);
assertEquals("Fragment location error", box.getBaseline() + box.getDescent(), expected.bottom());
assertEquals("Fragment location error", box.getWidth() + box.getX(), expected.right());
assertEquals(FRAGMENT_LOCATION_ERROR, box.getX(), expected.x);
assertEquals(FRAGMENT_LOCATION_ERROR, box.getBaseline() - box.getAscent(), expected.y);
assertEquals(FRAGMENT_LOCATION_ERROR, box.getBaseline() + box.getDescent(), expected.bottom());
assertEquals(FRAGMENT_LOCATION_ERROR, box.getWidth() + box.getX(), expected.right());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Insets getInsets(IFigure figure) {
*/
@Override
public void paint(IFigure figure, Graphics graphics, Insets insets) {
// empty default implementation
}

/**
Expand All @@ -55,6 +56,7 @@ public void paint(IFigure figure, Graphics graphics, Insets insets) {
* @since 3.2
*/
public void paintBackground(IFigure figure, Graphics graphics, Insets insets) {
// empty default implementation
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class AbstractBorder implements Border {
*/
protected static final Rectangle getPaintRectangle(IFigure figure, Insets insets) {
tempRect.setBounds(figure.getBounds());
return tempRect.crop(insets);
return tempRect.shrink(insets);
}

/**
Expand Down
32 changes: 21 additions & 11 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/ButtonBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ButtonBorder extends SchemeBorder {
*/
public static final Border BUTTON = new ButtonBorder(SCHEMES.BUTTON);
/**
* Inverted hightlight colors from BUTTON.
* Inverted highlight colors from BUTTON.
*
* @see SCHEMES#BUTTON_CONTRAST
*/
Expand All @@ -59,7 +59,8 @@ public class ButtonBorder extends SchemeBorder {
* an extra set of borders for the pressed states.
*/
public static class ButtonScheme extends Scheme {
private Color highlightPressed[] = null, shadowPressed[] = null;
private Color[] highlightPressed = null;
private Color[] shadowPressed = null;

/**
* Constructs a new button scheme where the input colors are the colors for the
Expand Down Expand Up @@ -132,20 +133,27 @@ protected Insets calculateInsets() {
*/
@Override
protected boolean calculateOpaque() {
if (!super.calculateOpaque())
if (!super.calculateOpaque()) {
return false;
if (getHighlight().length != getShadowPressed().length)
}
if (getHighlight().length != getShadowPressed().length) {
return false;
if (getShadow().length != getHighlightPressed().length)
}
if (getShadow().length != getHighlightPressed().length) {
return false;
}
Color[] colors = getHighlightPressed();
for (int i = 0; i < colors.length; i++)
if (colors[i] == null)
for (Color color : colors) {
if (color == null) {
return false;
}
}
colors = getShadowPressed();
for (int i = 0; i < colors.length; i++)
if (colors[i] == null)
for (Color color : colors) {
if (color == null) {
return false;
}
}
return true;
}

Expand Down Expand Up @@ -228,10 +236,12 @@ public void paint(IFigure figure, Graphics graphics, Insets insets) {
ButtonModel model = clickable.getModel();
ButtonScheme colorScheme = (ButtonScheme) getScheme();

if (clickable.isRolloverEnabled() && !model.isMouseOver() && !model.isSelected())
if (clickable.isRolloverEnabled() && !model.isMouseOver() && !model.isSelected()) {
return;
}

Color tl[], br[];
Color[] tl;
Color[] br;
if (model.isSelected() || model.isArmed()) {
tl = colorScheme.getShadowPressed();
br = colorScheme.getHighlightPressed();
Expand Down
10 changes: 6 additions & 4 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/CompoundBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public Border getInnerBorder() {
@Override
public Insets getInsets(IFigure figure) {
Insets insets = null;
if (inner != null)
if (inner != null) {
insets = inner.getInsets(figure);
else
} else {
insets = new Insets();
}
if (outer != null) {
Insets moreInsets = outer.getInsets(figure);
insets = insets.getAdded(moreInsets);
Expand Down Expand Up @@ -111,7 +112,7 @@ public Border getOuterBorder() {
*/
@Override
public boolean isOpaque() {
return ((inner != null) ? inner.isOpaque() : false) && ((outer != null) ? outer.isOpaque() : false);
return (inner != null && inner.isOpaque()) && (outer != null && outer.isOpaque());
}

/**
Expand All @@ -126,8 +127,9 @@ public void paint(IFigure figure, Graphics g, Insets insets) {

insets = insets.getAdded(outer.getInsets(figure));
}
if (inner != null)
if (inner != null) {
inner.paint(figure, g, insets);
}
}

}
6 changes: 0 additions & 6 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/FocusBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
*/
public class FocusBorder extends AbstractBorder {

/**
* Constructs a new FocusBorder.
*/
public FocusBorder() {
}

/**
* @see org.eclipse.draw2d.Border#getInsets(IFigure)
*/
Expand Down
6 changes: 2 additions & 4 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/FrameBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ public class FrameBorder extends CompoundBorder implements LabeledBorder {
new Color[] { ColorConstants.button, ColorConstants.buttonLightest, ColorConstants.button },
new Color[] { ColorConstants.buttonDarkest, ColorConstants.buttonDarker, ColorConstants.button });

{
createBorders();
}

/**
* Constructs a FrameBorder with its label set to the name of the
* {@link TitleBarBorder} class.
*
* @since 2.0
*/
public FrameBorder() {
createBorders();
}

/**
Expand All @@ -49,6 +46,7 @@ public FrameBorder() {
* @since 2.0
*/
public FrameBorder(String label) {
this();
setLabel(label);
}

Expand Down
5 changes: 3 additions & 2 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/GroupBoxBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ public Dimension getPreferredSize(IFigure fig) {
public void paint(IFigure figure, Graphics g, Insets insets) {
tempRect.setBounds(getPaintRectangle(figure, insets));
Rectangle r = tempRect;
if (r.isEmpty())
if (r.isEmpty()) {
return;
}

Rectangle textLoc = new Rectangle(r.getTopLeft(), getTextExtents(figure));
r.crop(new Insets(getTextExtents(figure).height / 2));
r.shrink(new Insets(getTextExtents(figure).height / 2));
FigureUtilities.paintEtchedBorder(g, r);

textLoc.x += getInsets(figure).left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public Insets getInsets(IFigure figure) {
*/
@Override
public void paint(IFigure figure, Graphics graphics, Insets insets) {
// empty default implementation
}

}
39 changes: 22 additions & 17 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/SchemeBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class SchemeBorder extends AbstractBorder implements ColorConstants {
protected Scheme scheme = null;

/** Arrays of Colors, used for shadow or highlight effects **/
protected static final Color DARKEST_DARKER[] = new Color[] { buttonDarkest, buttonDarker },
LIGHTER_DARKER[] = new Color[] { buttonLightest, buttonDarker },
DARKER_LIGHTER[] = new Color[] { buttonDarker, buttonLightest };
protected static final Color[] DARKEST_DARKER = { buttonDarkest, buttonDarker };
protected static final Color[] LIGHTER_DARKER = { buttonLightest, buttonDarker };
protected static final Color[] DARKER_LIGHTER = { buttonDarker, buttonLightest };

/**
* Holds a set of information about a border, which can be changed to create a
Expand All @@ -42,8 +42,9 @@ public static class Scheme {
private Insets insets;
private boolean isOpaque = false;

/** Arrays of Colors, used for highlight and shadow effecsts */
protected Color highlight[], shadow[];
/** Arrays of Colors, used for highlight and shadow effects */
protected Color[] highlight;
protected Color[] shadow;

/**
* Constructs a default border Scheme with no border sides.
Expand Down Expand Up @@ -108,14 +109,18 @@ protected Insets calculateInsets() {
* @since 2.0
*/
protected boolean calculateOpaque() {
Color colors[] = getHighlight();
for (int i = 0; i < colors.length; i++)
if (colors[i] == null)
Color[] colors = getHighlight();
for (Color color : colors) {
if (color == null) {
return false;
}
}
colors = getShadow();
for (int i = 0; i < colors.length; i++)
if (colors[i] == null)
for (Color color : colors) {
if (color == null) {
return false;
}
}
return true;
}

Expand Down Expand Up @@ -178,13 +183,13 @@ protected boolean isOpaque() {
public static interface SCHEMES {

/** Schemes used for shadow and highlight effects **/
Scheme BUTTON_CONTRAST = new Scheme(new Color[] { button, buttonLightest }, DARKEST_DARKER),
BUTTON_RAISED = new Scheme(new Color[] { buttonLightest }, DARKEST_DARKER),
BUTTON_PRESSED = new Scheme(DARKEST_DARKER, new Color[] { buttonLightest }),
RAISED = new Scheme(new Color[] { buttonLightest }, new Color[] { buttonDarkest }),
LOWERED = new Scheme(new Color[] { buttonDarkest }, new Color[] { buttonLightest }),
RIDGED = new Scheme(LIGHTER_DARKER, DARKER_LIGHTER),
ETCHED = new Scheme(DARKER_LIGHTER, LIGHTER_DARKER);
Scheme BUTTON_CONTRAST = new Scheme(new Color[] { button, buttonLightest }, DARKEST_DARKER);
Scheme BUTTON_RAISED = new Scheme(new Color[] { buttonLightest }, DARKEST_DARKER);
Scheme BUTTON_PRESSED = new Scheme(DARKEST_DARKER, new Color[] { buttonLightest });
Scheme RAISED = new Scheme(new Color[] { buttonLightest }, new Color[] { buttonDarkest });
Scheme LOWERED = new Scheme(new Color[] { buttonDarkest }, new Color[] { buttonLightest });
Scheme RIDGED = new Scheme(LIGHTER_DARKER, DARKER_LIGHTER);
Scheme ETCHED = new Scheme(DARKER_LIGHTER, LIGHTER_DARKER);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,27 @@ public class CircuitBorder extends AbstractBorder {
}

private static void drawConnectors(Graphics g, Rectangle rec) {
int y1 = rec.y, width = rec.width, x1, bottom = y1 + rec.height;
g.setBackgroundColor(LogicColorConstants.connectorGreen);
for (int i = 0; i < 4; i++) {
x1 = rec.x + (2 * i + 1) * width / 8;
int x1 = rec.x + (2 * i + 1) * rec.width / 8;

// Draw the "gap" for the connector
g.setForegroundColor(ColorConstants.listBackground);
g.drawLine(x1 - 2, y1 + 2, x1 + 3, y1 + 2);
g.drawLine(x1 - 2, rec.y + 2, x1 + 3, rec.y + 2);

// Draw the connectors
g.setForegroundColor(LogicColorConstants.connectorGreen);
connector.translate(x1, y1);
connector.translate(x1, rec.y);
g.fillPolygon(connector);
g.drawPolygon(connector);
connector.translate(-x1, -y1);
connector.translate(-x1, -rec.y);
g.setForegroundColor(ColorConstants.listBackground);
g.drawLine(x1 - 2, bottom - 3, x1 + 3, bottom - 3);
g.drawLine(x1 - 2, rec.bottom() - 3, x1 + 3, rec.bottom() - 3);
g.setForegroundColor(LogicColorConstants.connectorGreen);
bottomConnector.translate(x1, bottom);
bottomConnector.translate(x1, rec.bottom());
g.fillPolygon(bottomConnector);
g.drawPolygon(bottomConnector);
bottomConnector.translate(-x1, -bottom);
bottomConnector.translate(-x1, -rec.bottom());
}
}

Expand Down
Loading

0 comments on commit b4387db

Please sign in to comment.