Skip to content

Commit ded6745

Browse files
ptzieglerazoitl
authored andcommitted
Modernize Draw2D Zoom/Path Example
This removes the usage of the soon-to-be deprecated `ScaledGraphics` in both the `PathExample` and `ZoomExample`. For the `ZoomExample`, the old GIF icons have been converted to PNG/SVG. Contributes to #901
1 parent d1468e7 commit ded6745

17 files changed

+635
-93
lines changed

org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/path/PathExample.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2023 IBM Corporation and others.
2+
* Copyright (c) 2008, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -13,10 +13,7 @@
1313
package org.eclipse.draw2d.examples.path;
1414

1515
import org.eclipse.swt.SWT;
16-
import org.eclipse.swt.events.SelectionEvent;
17-
import org.eclipse.swt.events.SelectionListener;
1816
import org.eclipse.swt.layout.GridData;
19-
import org.eclipse.swt.widgets.Button;
2017
import org.eclipse.swt.widgets.Canvas;
2118
import org.eclipse.swt.widgets.Display;
2219
import org.eclipse.swt.widgets.Shell;
@@ -66,27 +63,6 @@ public static void main(String[] args) {
6663
zoomFigure.setLayoutManager(new BorderLayout());
6764
zoomFigure.setScale(1);
6865

69-
final Button zoomMethodButton = new Button(shell, SWT.CHECK);
70-
zoomMethodButton.setSelection(true);
71-
zoomMethodButton.setText("EMULATED_SCALING"); //$NON-NLS-1$
72-
73-
zoomMethodButton.addSelectionListener(new SelectionListener() {
74-
75-
@Override
76-
public void widgetSelected(SelectionEvent e) {
77-
if (zoomMethodButton.getSelection()) {
78-
zoomFigure.setScaleMethod(ZoomFigure.EMULATED_SCALING);
79-
} else {
80-
zoomFigure.setScaleMethod(ZoomFigure.NATIVE_SCALING);
81-
}
82-
zoomFigure.revalidate();
83-
}
84-
85-
@Override
86-
public void widgetDefaultSelected(SelectionEvent e) {
87-
}
88-
});
89-
9066
PathFigure polyline = new PathFigure();
9167
int w = 70;
9268
int k = 50;

org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/path/PathFigure.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2023 IBM Corporation and others.
2+
* Copyright (c) 2008, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -21,7 +21,6 @@
2121
import org.eclipse.draw2d.ColorConstants;
2222
import org.eclipse.draw2d.Graphics;
2323
import org.eclipse.draw2d.Polyline;
24-
import org.eclipse.draw2d.ScaledGraphics;
2524
import org.eclipse.draw2d.geometry.Point;
2625
import org.eclipse.draw2d.geometry.PointList;
2726
import org.eclipse.draw2d.geometry.PrecisionPoint;
@@ -68,7 +67,7 @@ private void drawShape(Graphics g, boolean fill) {
6867

6968
TextLayout textLayout = new TextLayout(Display.getDefault());
7069
textLayout.setFont(g.getFont());
71-
textLayout.setText("zoom" + (g instanceof ScaledGraphics ? "[e]" : "[n]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
70+
textLayout.setText("zoom"); //$NON-NLS-1$
7271
TextStyle textStyle = new TextStyle();
7372
textStyle.underline = true;
7473
textStyle.underlineColor = ColorConstants.blue;

org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/path/ZoomFigure.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2023 IBM Corporation and others.
2+
* Copyright (c) 2008, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -15,47 +15,34 @@
1515
import org.eclipse.draw2d.Figure;
1616
import org.eclipse.draw2d.Graphics;
1717
import org.eclipse.draw2d.ScalableFigure;
18-
import org.eclipse.draw2d.ScaledGraphics;
1918
import org.eclipse.draw2d.geometry.Dimension;
2019
import org.eclipse.draw2d.geometry.Rectangle;
2120
import org.eclipse.draw2d.geometry.Translatable;
2221

2322
public class ZoomFigure extends Figure implements ScalableFigure {
2423

25-
public static final int NATIVE_SCALING = 0;
26-
public static final int EMULATED_SCALING = 1;
27-
2824
private double scale = 1.0;
29-
private int scaleMethod = EMULATED_SCALING;
3025

3126
/*
3227
* @see org.eclipse.draw2d.Figure#paintClientArea(Graphics)
3328
*/
3429
@Override
35-
protected void paintClientArea(Graphics graphics) {
30+
protected void paintClientArea(Graphics g) {
3631
if (getChildren().isEmpty()) {
3732
return;
3833
}
39-
if ((scale == 1.0) && (scaleMethod != EMULATED_SCALING)) {
40-
super.paintClientArea(graphics);
34+
if ((scale == 1.0)) {
35+
super.paintClientArea(g);
4136
} else {
42-
Graphics g = graphics;
43-
if (EMULATED_SCALING == scaleMethod) {
44-
g = new ScaledGraphics(graphics);
45-
}
4637
if (!optimizeClip()) {
4738
g.clipRect(getBounds().getShrinked(getInsets()));
4839
}
4940
g.translate(getBounds().x + getInsets().left, getBounds().y + getInsets().top);
5041
g.scale(scale);
5142
g.pushState();
5243
paintChildren(g);
53-
if (EMULATED_SCALING == scaleMethod) {
54-
g.dispose();
55-
} else {
56-
g.popState();
57-
}
58-
graphics.restoreState();
44+
g.popState();
45+
g.restoreState();
5946
}
6047
}
6148

@@ -126,9 +113,4 @@ public void translateFromParent(Translatable t) {
126113
protected boolean useLocalCoordinates() {
127114
return true;
128115
}
129-
130-
public void setScaleMethod(int scaleMethod) {
131-
this.scaleMethod = scaleMethod;
132-
}
133-
134116
}

org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/zoom/ZoomContainer.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2005, 2023 IBM Corporation and others.
2+
* Copyright (c) 2005, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -14,7 +14,6 @@
1414

1515
import org.eclipse.draw2d.Figure;
1616
import org.eclipse.draw2d.Graphics;
17-
import org.eclipse.draw2d.ScaledGraphics;
1817
import org.eclipse.draw2d.StackLayout;
1918
import org.eclipse.draw2d.geometry.Dimension;
2019
import org.eclipse.draw2d.geometry.Rectangle;
@@ -51,13 +50,11 @@ public Dimension getPreferredSize(int wHint, int hHint) {
5150
* @see org.eclipse.draw2d.Figure#paintClientArea(Graphics)
5251
*/
5352
@Override
54-
protected void paintClientArea(Graphics graphics) {
53+
protected void paintClientArea(Graphics g) {
5554
if (getChildren().isEmpty()) {
5655
return;
5756
}
5857

59-
ScaledGraphics g = new ScaledGraphics(graphics);
60-
6158
if (!optimizeClip()) {
6259
g.clipRect(getBounds().getShrinked(getInsets()));
6360
}
@@ -66,8 +63,7 @@ protected void paintClientArea(Graphics graphics) {
6663
g.pushState();
6764
paintChildren(g);
6865
g.popState();
69-
g.dispose();
70-
graphics.restoreState();
66+
g.restoreState();
7167
}
7268

7369
public void setZoom(float zoom) {

org.eclipse.draw2d.examples/src/org/eclipse/draw2d/examples/zoom/ZoomExample.java

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2005, 2023 IBM Corporation and others.
2+
* Copyright (c) 2005, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -29,6 +29,7 @@
2929
import org.eclipse.draw2d.XYLayout;
3030
import org.eclipse.draw2d.geometry.Dimension;
3131
import org.eclipse.draw2d.geometry.Rectangle;
32+
import org.eclipse.draw2d.internal.FileImageDataProvider;
3233
import org.eclipse.draw2d.parts.Thumbnail;
3334

3435
/**
@@ -38,12 +39,17 @@
3839
*/
3940

4041
public class ZoomExample {
41-
42+
private static final Image IMG_CLASS = FileImageDataProvider.createImage(UMLClassFigure.class,
43+
"images/class_obj.svg"); //$NON-NLS-1$
44+
private static final Image IMG_FIELD_PRIVATE = FileImageDataProvider.createImage(UMLClassFigure.class,
45+
"images/field_private_obj.svg"); //$NON-NLS-1$
46+
private static final Image IMG_METHOD_PUBLIC = FileImageDataProvider.createImage(UMLClassFigure.class,
47+
"images/methpub_obj.svg"); //$NON-NLS-1$
4248
private static Figure contents;
4349

4450
public static void main(String[] args) {
45-
Display d = new Display();
46-
final Shell shell = new Shell(d);
51+
final Shell shell = new Shell();
52+
final Display d = shell.getDisplay();
4753
shell.setSize(800, 800);
4854
LightweightSystem lws = new LightweightSystem(shell);
4955

@@ -77,20 +83,10 @@ public static void main(String[] args) {
7783

7884
lws.setContents(fig);
7985

80-
// overviewShell = new Shell(shell, SWT.TITLE| SWT.RESIZE | SWT.NO_REDRAW_RESIZE
81-
// | SWT.NO_BACKGROUND);
82-
// overviewShell.setText("Overview Shell");
83-
// overviewShell.setLayout(new FillLayout());
84-
// LightweightSystem overviewLWS = new LightweightSystem(overviewShell);
85-
// overviewLWS.setContents(createThumbnail(getContents()));
86-
// overviewShell.setSize(200, 200);
87-
8886
shell.open();
89-
// overviewShell.open();
9087
while (!shell.isDisposed()) {
9188
while (!d.readAndDispatch()) {
9289
d.sleep();
93-
// overviewShell.dispose();
9490
}
9591
}
9692
}
@@ -115,39 +111,29 @@ private static Figure createContents() {
115111
contents.setLayoutManager(layout);
116112

117113
Font classFont = new Font(null, "Arial", 12, SWT.BOLD); //$NON-NLS-1$
118-
Label classLabel1 = new Label("Table", //$NON-NLS-1$
119-
new Image(null, ZoomExample.class.getResourceAsStream("images/class_obj.gif"))); //$NON-NLS-1$
114+
Label classLabel1 = new Label("Table", IMG_CLASS); //$NON-NLS-1$
120115
classLabel1.setFont(classFont);
121116

122-
Label classLabel2 = new Label("Column", //$NON-NLS-1$
123-
new Image(null, ZoomExample.class.getResourceAsStream("images/class_obj.gif"))); //$NON-NLS-1$
117+
Label classLabel2 = new Label("Column", IMG_CLASS);//$NON-NLS-1$
124118
classLabel2.setFont(classFont);
125119

126120
final UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
127121
final UMLClassFigure classFigure2 = new UMLClassFigure(classLabel2);
128122

129-
Label attribute1 = new Label("columns: Column[]", //$NON-NLS-1$
130-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/field_private_obj.gif"))); //$NON-NLS-1$
131-
Label attribute2 = new Label("rows: Row[]", //$NON-NLS-1$
132-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/field_private_obj.gif"))); //$NON-NLS-1$
133-
Label attribute3 = new Label("columnID: int", //$NON-NLS-1$
134-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/field_private_obj.gif"))); //$NON-NLS-1$
135-
Label attribute4 = new Label("items: List", //$NON-NLS-1$
136-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/field_private_obj.gif"))); //$NON-NLS-1$
123+
Label attribute1 = new Label("columns: Column[]", IMG_FIELD_PRIVATE); //$NON-NLS-1$
124+
Label attribute2 = new Label("rows: Row[]", IMG_FIELD_PRIVATE); //$NON-NLS-1$
125+
Label attribute3 = new Label("columnID: int", IMG_FIELD_PRIVATE);//$NON-NLS-1$
126+
Label attribute4 = new Label("items: List", IMG_FIELD_PRIVATE);//$NON-NLS-1$
137127

138128
classFigure.getAttributesCompartment().add(attribute1);
139129
classFigure.getAttributesCompartment().add(attribute2);
140130
classFigure2.getAttributesCompartment().add(attribute3);
141131
classFigure2.getAttributesCompartment().add(attribute4);
142132

143-
Label method1 = new Label("getColumns(): Column[]", //$NON-NLS-1$
144-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/methpub_obj.gif"))); //$NON-NLS-1$
145-
Label method2 = new Label("getRows(): Row[]", //$NON-NLS-1$
146-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/methpub_obj.gif"))); //$NON-NLS-1$
147-
Label method3 = new Label("getColumnID(): int", //$NON-NLS-1$
148-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/methpub_obj.gif"))); //$NON-NLS-1$
149-
Label method4 = new Label("getItems(): List", //$NON-NLS-1$
150-
new Image(null, UMLClassFigure.class.getResourceAsStream("images/methpub_obj.gif"))); //$NON-NLS-1$
133+
Label method1 = new Label("getColumns(): Column[]", IMG_METHOD_PUBLIC); //$NON-NLS-1$
134+
Label method2 = new Label("getRows(): Row[]", IMG_METHOD_PUBLIC); //$NON-NLS-1$
135+
Label method3 = new Label("getColumnID(): int", IMG_METHOD_PUBLIC); //$NON-NLS-1$
136+
Label method4 = new Label("getItems(): List", IMG_METHOD_PUBLIC); //$NON-NLS-1$
151137

152138
classFigure.getMethodsCompartment().add(method1);
153139
classFigure.getMethodsCompartment().add(method2);
Binary file not shown.
755 Bytes
Loading

0 commit comments

Comments
 (0)