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 Oct 25, 2023
1 parent 45b2929 commit acf2f9e
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Copyright (c) 2003, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* 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.
*
Expand All @@ -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
@@ -1,7 +1,7 @@
/*******************************************************************************
* Copyright (c) 2005, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* 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.
*
Expand All @@ -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
@@ -1,7 +1,7 @@
/*******************************************************************************
* Copyright (c) 2004, 2010 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* 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.
*
Expand All @@ -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
@@ -1,7 +1,7 @@
/*******************************************************************************
* Copyright (c) 2007, 2010 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* 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.
*
Expand All @@ -22,7 +22,7 @@
* <P>
* WARNING: Experimental for 3.3. Clients should help validate the use cases of
* this new function.
*
*
* @since 3.3
*/
public class AbstractBackground extends AbstractBorder {
Expand All @@ -41,20 +41,22 @@ public Insets getInsets(IFigure figure) {
*/
@Override
public void paint(IFigure figure, Graphics graphics, Insets insets) {
// empty default implementation
}

/**
* Called when this Background should paint. If the background is being painted
* inside another border or background, the insets indicate how far inside the
* target figure the background should be painted. In most cases, the insets
* will be all zero.
*
*
* @param figure The figure on which the background is being painted
* @param graphics The graphics
* @param insets Amount to inset from the figure's bounds
* @since 3.2
*/
public void paintBackground(IFigure figure, Graphics graphics, Insets insets) {
// empty default implementation
}

}
8 changes: 4 additions & 4 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/AbstractBorder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* 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.
*
Expand All @@ -18,7 +18,7 @@

/**
* Provides generic support for borders.
*
*
* @author hudsonr
*/
public abstract class AbstractBorder implements Border {
Expand All @@ -33,15 +33,15 @@ public abstract class AbstractBorder implements Border {
* specified insets. This method exists for convenience and performance; the
* method does not new any Objects and returns a rectangle which the caller can
* manipulate.
*
*
* @since 2.0
* @param figure Figure for which the paintable rectangle is needed
* @param insets The insets
* @return The paintable region on the Figure f
*/
protected static final Rectangle getPaintRectangle(IFigure figure, Insets insets) {
tempRect.setBounds(figure.getBounds());
return tempRect.crop(insets);
return tempRect.shrink(insets);
}

/**
Expand Down
62 changes: 36 additions & 26 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/ButtonBorder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* 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.
*
Expand All @@ -22,33 +22,33 @@
* states the model of the figure could be. This border uses an extended
* {@link SchemeBorder.Scheme Scheme} called {@link ButtonScheme} which provides
* more information required by border to handle the the states of the model.
*
*
* @see SchemeBorder.Scheme
* @see ButtonScheme
*/
public class ButtonBorder extends SchemeBorder {

/**
* Default button border.
*
*
* @see SCHEMES#BUTTON
*/
public static final Border BUTTON = new ButtonBorder(SCHEMES.BUTTON);
/**
* Inverted hightlight colors from BUTTON.
*
* Inverted highlight colors from BUTTON.
*
* @see SCHEMES#BUTTON_CONTRAST
*/
public static final Border BUTTON_CONTRAST = new ButtonBorder(SCHEMES.BUTTON_CONTRAST);
/**
* Used for scrollbar buttons.
*
*
* @see SCHEMES#BUTTON_SCROLLBAR
*/
public static final Border BUTTON_SCROLLBAR = new ButtonBorder(SCHEMES.BUTTON_SCROLLBAR);
/**
* Used for toolbar buttons.
*
*
* @see SCHEMES#TOOLBAR
*/
public static final Border TOOLBAR = new ButtonBorder(SCHEMES.TOOLBAR);
Expand All @@ -59,14 +59,15 @@ 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
* top-left and bottom-right sides of the border. These colors serve as the
* colors when the border is in a pressed state too. The width of each side is
* determined by the number of colors passed in as input.
*
*
* @param highlight Colors for the top-left sides of the border
* @param shadow Colors for the bottom-right sides of the border
* @since 2.0
Expand All @@ -82,7 +83,7 @@ public ButtonScheme(Color[] highlight, Color[] shadow) {
* top-left and bottom-right sides of the border, for the normal and pressed
* states. The width of each side is determined by the number of colors passed
* in as input.
*
*
* @param hl Colors for the top-left sides of the border
* @param sh Colors for the bottom-right sides of the border
* @param hlp Colors for the top-left sides of the border when figure is pressed
Expand All @@ -101,7 +102,7 @@ public ButtonScheme(Color[] hl, Color[] sh, Color[] hlp, Color[] shp) {
/**
* Calculates and returns the Insets for this border. The calculations are based
* on the number of normal and pressed, highlight and shadow colors.
*
*
* @return The insets for this border
* @since 2.0
*/
Expand All @@ -126,32 +127,39 @@ protected Insets calculateInsets() {
* <code>null</code>
* </ul>
* This is done so that the entire region under the figure is properly covered.
*
*
* @return The opaque state of this border
* @since 2.0
*/
@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;
}

/**
* Returns the pressed highlight colors of this border.
*
*
* @return Colors as an array of Colors
* @since 2.0
*/
Expand All @@ -161,7 +169,7 @@ protected Color[] getHighlightPressed() {

/**
* Returns the pressed shadow colors of this border.
*
*
* @return Colors as an array of Colors
* @since 2.0
*/
Expand Down Expand Up @@ -196,7 +204,7 @@ public static interface SCHEMES {

/**
* Constructs a ButtonBorder with a predefined button scheme set as its default.
*
*
* @since 2.0
*/
public ButtonBorder() {
Expand All @@ -205,7 +213,7 @@ public ButtonBorder() {

/**
* Constructs a ButtonBorder with the input ButtonScheme set as its Scheme.
*
*
* @param scheme ButtonScheme for this ButtonBorder.
* @since 2.0
*/
Expand All @@ -217,7 +225,7 @@ public ButtonBorder(ButtonScheme scheme) {
* Paints this border with the help of the set scheme, the model of the
* clickable figure, and other inputs. The scheme is used in conjunction with
* the state of the model to get the appropriate colors for the border.
*
*
* @param figure The Clickable that this border belongs to
* @param graphics The graphics used for painting
* @param insets The insets
Expand All @@ -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
Loading

0 comments on commit acf2f9e

Please sign in to comment.