Skip to content

Commit 002c96c

Browse files
committed
Move drop shadow classes to own package
Extracted abstract base class for capturing common drop shadow code.
1 parent 1260635 commit 002c96c

File tree

3 files changed

+128
-104
lines changed

3 files changed

+128
-104
lines changed

org.eclipse.draw2d/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Export-Package: org.eclipse.draw2d,
1616
org.eclipse.draw2d.tests,
1717
org.eclipse.gef.examples.flow",
1818
org.eclipse.draw2d.parts,
19+
org.eclipse.draw2d.shadows,
1920
org.eclipse.draw2d.text,
2021
org.eclipse.draw2d.widgets,
2122
org.eclipse.draw2d.zoom

org.eclipse.draw2d/src/org/eclipse/draw2d/RectDropShadowBorder.java renamed to org.eclipse.draw2d/src/org/eclipse/draw2d/shadows/AbstractDropShadowBorder.java

Lines changed: 15 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,45 @@
1111
* Alois Zoitl - initial API and implementation
1212
*******************************************************************************/
1313

14-
package org.eclipse.draw2d;
14+
package org.eclipse.draw2d.shadows;
1515

1616
import org.eclipse.swt.graphics.Color;
1717

18+
import org.eclipse.draw2d.AbstractBackground;
19+
import org.eclipse.draw2d.ColorProvider;
20+
import org.eclipse.draw2d.Graphics;
21+
import org.eclipse.draw2d.IFigure;
1822
import org.eclipse.draw2d.geometry.Insets;
1923
import org.eclipse.draw2d.geometry.Rectangle;
2024

21-
/**
22-
* A versatile border that provides a CSS-style drop shadow effect for both
23-
* rectangular and rounded-rectangular figures.
24-
*
25-
* This border simulates visual depth by layering semi-transparent shapes using
26-
* multi-pass exponential decay. It is designed to work "out of the box" for
27-
* standard diagramming nodes while remaining highly tunable.
28-
*
29-
* @since 3.2
30-
*/
31-
public class RectDropShadowBorder extends AbstractBackground {
32-
33-
/**
34-
* The default value for the corner radius is suited for rectangles
35-
*
36-
*/
37-
private static final int DEFAULT_CORNER_START_RADIUS = 4;
25+
public abstract class AbstractDropShadowBorder extends AbstractBackground {
3826

3927
/**
4028
* default value for the drop shadow size as suited for a figure in an diagram
4129
*
4230
*/
43-
private static final int DEFAULT_DROP_SHADOW_SIZE = 6;
44-
31+
protected static final int DEFAULT_DROP_SHADOW_SIZE = 6;
4532
/**
4633
* default value for the halo size as suited for a figure in an diagram
4734
*
4835
*/
49-
private static final int DEFAULT_HALO_SIZE = 3;
50-
51-
private static final int DEFAULT_SHADOW_ALPHA = 25;
52-
53-
private static final double DEFAULT_SOFTNESS = 4.0;
54-
36+
protected static final int DEFAULT_HALO_SIZE = 3;
37+
protected static final int DEFAULT_SHADOW_ALPHA = 25;
38+
protected static final double DEFAULT_SOFTNESS = 4.0;
5539
/**
5640
* Per default the shadow insets are empty so that the shadow will be outside of
5741
* the figure
5842
*/
5943
private static final Insets DEFAULT_SHADOW_INSETS = new Insets();
60-
6144
private static final Rectangle CLIP_RECT_CACHE = new Rectangle();
62-
63-
private int cornerStartRadius;
64-
6545
private int dropShadowSize;
66-
6746
private int haloSize;
68-
6947
private Insets insets;
70-
7148
private int shadowAlpha;
72-
7349
private Color shadowColor;
74-
7550
private double softness;
7651

77-
/**
78-
* Default constructor that set everything for a rectangular shaped figure.
79-
*
80-
* @since 3.2
81-
*/
82-
public RectDropShadowBorder() {
83-
this(DEFAULT_CORNER_START_RADIUS);
84-
85-
}
86-
87-
/**
88-
* Create a shadow border for a rounded rectangle with the given corner radius.
89-
*
90-
* @param cornerRadius The corner radius of the rounded rectangle
91-
* @since 3.2
92-
*/
93-
public RectDropShadowBorder(int cornerRadius) {
94-
cornerStartRadius = cornerRadius;
52+
protected AbstractDropShadowBorder() {
9553
dropShadowSize = DEFAULT_DROP_SHADOW_SIZE;
9654
haloSize = DEFAULT_HALO_SIZE;
9755
insets = DEFAULT_SHADOW_INSETS;
@@ -100,7 +58,7 @@ public RectDropShadowBorder(int cornerRadius) {
10058
softness = DEFAULT_SOFTNESS;
10159
}
10260

103-
private int calcAlphaValue(final double progress) {
61+
protected int calcAlphaValue(final double progress) {
10462
return Math.max(1, (int) (shadowAlpha * Math.exp(-softness * progress)));
10563
}
10664

@@ -139,56 +97,9 @@ public void paintBackground(IFigure figure, Graphics graphics, Insets insets) {
13997
graphics.clipRect(CLIP_RECT_CACHE);
14098
}
14199

142-
private void paintDropShadow(final Graphics graphics, final Rectangle shadowRect, final int size) {
143-
int bottomXStart = shadowRect.x + 1;
144-
int bottomY = shadowRect.y + 1 + shadowRect.height;
145-
final int bottomXEnd = shadowRect.x + shadowRect.width + 1 - cornerStartRadius;
146-
147-
int rightX = shadowRect.x + shadowRect.width + 1;
148-
int rightYStart = shadowRect.y + 1;
149-
final int rightYEnd = shadowRect.y + shadowRect.height + 1 - cornerStartRadius;
150-
int cornerDiameter = 2 * cornerStartRadius;
151-
152-
for (int i = 0; i <= size; i++) {
153-
final double progress = (double) i / size;
154-
graphics.setAlpha(calcAlphaValue(progress));
155-
156-
graphics.drawLine(bottomXStart, bottomY, bottomXEnd, bottomY);
157-
graphics.drawLine(rightX, rightYStart, rightX, rightYEnd);
158-
graphics.drawArc(rightX - cornerDiameter, bottomY - cornerDiameter, cornerDiameter, cornerDiameter, 270,
159-
90);
160-
161-
bottomXStart++;
162-
bottomY++;
163-
rightX++;
164-
rightYStart++;
165-
cornerDiameter += 2;
166-
}
167-
}
168-
169-
private void paintHalo(final Graphics graphics, final Rectangle shadowRect, final int size) {
170-
final Rectangle r = shadowRect.getCopy();
171-
int cornerRadius = cornerStartRadius;
172-
for (int i = 0; i < size; i++) {
173-
final double progress = (double) i / size;
174-
graphics.setAlpha(calcAlphaValue(progress));
175-
graphics.drawRoundRectangle(r, cornerRadius, cornerRadius);
176-
cornerRadius += 2;
177-
r.expand(1, 1);
178-
}
179-
}
100+
protected abstract void paintDropShadow(final Graphics graphics, final Rectangle shadowRect, final int size);
180101

181-
/**
182-
* Set the corner radius for the drop shadow. The default value is suited for
183-
* rectangles for rounded rectangles it should be set to the corner radius of
184-
* the rounded rectangle this shadow is bounding.
185-
*
186-
* @param cornerRadius
187-
* @since 3.2
188-
*/
189-
public void setCornerRadius(int cornerRadius) {
190-
this.cornerStartRadius = cornerRadius;
191-
}
102+
protected abstract void paintHalo(final Graphics graphics, final Rectangle shadowRect, final int size);
192103

193104
/**
194105
* Sets the size of the directional shadow that simulates a light source.
@@ -276,4 +187,4 @@ private void updateClip(Graphics graphics, Rectangle shadowRect) {
276187
shadowRect.shrink(shadowSize, shadowSize);
277188
}
278189

279-
}
190+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Johannes Kepler University Linz and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Alois Zoitl - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.draw2d.shadows;
15+
16+
import org.eclipse.draw2d.Graphics;
17+
import org.eclipse.draw2d.geometry.Rectangle;
18+
19+
/**
20+
* A versatile border that provides a CSS-style drop shadow effect for both
21+
* rectangular and rounded-rectangular figures.
22+
*
23+
* This border simulates visual depth by layering semi-transparent shapes using
24+
* multi-pass exponential decay. It is designed to work "out of the box" for
25+
* standard diagramming nodes while remaining highly tunable.
26+
*
27+
* @since 3.2
28+
*/
29+
public class RectDropShadowBorder extends AbstractDropShadowBorder {
30+
31+
/**
32+
* The default value for the corner radius is suited for rectangles
33+
*
34+
*/
35+
private static final int DEFAULT_CORNER_START_RADIUS = 4;
36+
37+
int cornerStartRadius;
38+
39+
/**
40+
* Default constructor that set everything for a rectangular shaped figure.
41+
*
42+
* @since 3.2
43+
*/
44+
public RectDropShadowBorder() {
45+
this(DEFAULT_CORNER_START_RADIUS);
46+
47+
}
48+
49+
/**
50+
* Create a shadow border for a rounded rectangle with the given corner radius.
51+
*
52+
* @param cornerRadius The corner radius of the rounded rectangle
53+
* @since 3.2
54+
*/
55+
public RectDropShadowBorder(int cornerRadius) {
56+
cornerStartRadius = cornerRadius;
57+
}
58+
59+
@Override
60+
protected void paintDropShadow(final Graphics graphics, final Rectangle shadowRect, final int size) {
61+
int bottomXStart = shadowRect.x + 1;
62+
int bottomY = shadowRect.y + 1 + shadowRect.height;
63+
final int bottomXEnd = shadowRect.x + shadowRect.width + 1 - cornerStartRadius;
64+
65+
int rightX = shadowRect.x + shadowRect.width + 1;
66+
int rightYStart = shadowRect.y + 1;
67+
final int rightYEnd = shadowRect.y + shadowRect.height + 1 - cornerStartRadius;
68+
int cornerDiameter = 2 * cornerStartRadius;
69+
70+
for (int i = 0; i <= size; i++) {
71+
final double progress = (double) i / size;
72+
graphics.setAlpha(calcAlphaValue(progress));
73+
74+
graphics.drawLine(bottomXStart, bottomY, bottomXEnd, bottomY);
75+
graphics.drawLine(rightX, rightYStart, rightX, rightYEnd);
76+
graphics.drawArc(rightX - cornerDiameter, bottomY - cornerDiameter, cornerDiameter, cornerDiameter, 270,
77+
90);
78+
79+
bottomXStart++;
80+
bottomY++;
81+
rightX++;
82+
rightYStart++;
83+
cornerDiameter += 2;
84+
}
85+
}
86+
87+
@Override
88+
protected void paintHalo(final Graphics graphics, final Rectangle shadowRect, final int size) {
89+
final Rectangle r = shadowRect.getCopy();
90+
int cornerRadius = cornerStartRadius;
91+
for (int i = 0; i < size; i++) {
92+
final double progress = (double) i / size;
93+
graphics.setAlpha(calcAlphaValue(progress));
94+
graphics.drawRoundRectangle(r, cornerRadius, cornerRadius);
95+
cornerRadius += 2;
96+
r.expand(1, 1);
97+
}
98+
}
99+
100+
/**
101+
* Set the corner radius for the drop shadow. The default value is suited for
102+
* rectangles for rounded rectangles it should be set to the corner radius of
103+
* the rounded rectangle this shadow is bounding.
104+
*
105+
* @param cornerRadius
106+
* @since 3.2
107+
*/
108+
public void setCornerRadius(int cornerRadius) {
109+
cornerStartRadius = cornerRadius;
110+
}
111+
112+
}

0 commit comments

Comments
 (0)