Skip to content

Commit 138e721

Browse files
committed
[Draw2D] Paint property values using Graphics instead of GC object
In preparation of converting the property table into a proper GEF viewer, the dependent classes need to be adjusted to use the Draw2D API. This change only wraps the GC object in a Graphics object, which is is then passed as an argument to the property editors. Internally, this Graphics object then calls the corresponding GC methods. In the future, this Graphics object is supplied by the viewer when painting the Draw2D figures and no longer needs to be constructed by us.
1 parent 5eb50e1 commit 138e721

File tree

11 files changed

+152
-74
lines changed

11 files changed

+152
-74
lines changed

org.eclipse.wb.core/src/org/eclipse/wb/internal/core/model/property/editor/BooleanObjectPropertyEditor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,7 +15,7 @@
1515
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
1616
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
1717

18-
import org.eclipse.swt.graphics.GC;
18+
import org.eclipse.draw2d.Graphics;
1919
import org.eclipse.swt.graphics.Image;
2020
import org.eclipse.swt.graphics.Point;
2121

@@ -45,33 +45,33 @@ private BooleanObjectPropertyEditor() {
4545
//
4646
////////////////////////////////////////////////////////////////////////////
4747
@Override
48-
public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception {
48+
public void paint(Property property, Graphics graphics, int x, int y, int width, int height) throws Exception {
4949
Object value = property.getValue();
5050
if (value instanceof Boolean) {
5151
boolean booleanValue = ((Boolean) value).booleanValue();
5252
Image image = booleanValue ? m_trueImage : m_falseImage;
5353
String text = Boolean.toString(booleanValue);
54-
paint(gc, x, y, width, height, text, image);
54+
paint(graphics, x, y, width, height, text, image);
5555
}
5656
if (value == null) {
5757
Image image = m_nullImage;
5858
String text = "null";
59-
paint(gc, x, y, width, height, text, image);
59+
paint(graphics, x, y, width, height, text, image);
6060
}
6161
}
6262

63-
private void paint(GC gc, int x, int y, int width, int height, String text, Image image) {
63+
private void paint(Graphics graphics, int x, int y, int width, int height, String text, Image image) {
6464
// draw image
6565
{
66-
DrawUtils.drawImageCV(gc, image, x, y, height);
66+
DrawUtils.drawImageCV(graphics, image, x, y, height);
6767
// prepare new position/width
6868
int imageWidth = image.getBounds().width + 2;
6969
x += imageWidth;
7070
width -= imageWidth;
7171
}
7272
// draw text
7373
{
74-
DrawUtils.drawStringCV(gc, text, x, y, width, height);
74+
DrawUtils.drawStringCV(graphics, text, x, y, width, height);
7575
}
7676
}
7777

org.eclipse.wb.core/src/org/eclipse/wb/internal/core/model/property/editor/BooleanPropertyEditor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,7 +15,7 @@
1515
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
1616
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
1717

18-
import org.eclipse.swt.graphics.GC;
18+
import org.eclipse.draw2d.Graphics;
1919
import org.eclipse.swt.graphics.Image;
2020
import org.eclipse.swt.graphics.Point;
2121

@@ -46,32 +46,32 @@ private BooleanPropertyEditor() {
4646
//
4747
////////////////////////////////////////////////////////////////////////////
4848
@Override
49-
public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception {
49+
public void paint(Property property, Graphics graphics, int x, int y, int width, int height) throws Exception {
5050
Object value = property.getValue();
5151
if (value instanceof Boolean) {
5252
boolean booleanValue = ((Boolean) value).booleanValue();
5353
Image image = booleanValue ? m_trueImage : m_falseImage;
5454
String text = Boolean.toString(booleanValue);
55-
paint(gc, x, y, width, height, image, text);
55+
paint(graphics, x, y, width, height, image, text);
5656
} else {
57-
paint(gc, x, y, width, height, m_unknownImage, "unknown");
57+
paint(graphics, x, y, width, height, m_unknownImage, "unknown");
5858
}
5959
}
6060

6161
/**
6262
* Paints {@link Image} and text.
6363
*/
64-
private void paint(GC gc, int x, int y, int width, int height, Image image, String text) {
64+
private void paint(Graphics graphics, int x, int y, int width, int height, Image image, String text) {
6565
// draw image
6666
{
67-
DrawUtils.drawImageCV(gc, image, x, y, height);
67+
DrawUtils.drawImageCV(graphics, image, x, y, height);
6868
// prepare new position/width
6969
int imageWidth = image.getBounds().width + 2;
7070
x += imageWidth;
7171
width -= imageWidth;
7272
}
7373
// draw text
74-
DrawUtils.drawStringCV(gc, text, x, y, width, height);
74+
DrawUtils.drawStringCV(graphics, text, x, y, width, height);
7575
}
7676

7777
////////////////////////////////////////////////////////////////////////////

org.eclipse.wb.core/src/org/eclipse/wb/internal/core/model/property/editor/PropertyEditor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,6 +15,7 @@
1515
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
1616
import org.eclipse.wb.internal.core.utils.IAdaptable;
1717

18+
import org.eclipse.draw2d.Graphics;
1819
import org.eclipse.swt.SWT;
1920
import org.eclipse.swt.events.KeyEvent;
2021
import org.eclipse.swt.graphics.GC;
@@ -45,7 +46,7 @@ public PropertyEditorPresentation getPresentation() {
4546
/**
4647
* Paints given {@link Property} given rectangle <code>(x, y, width, height)</code> of {@link GC}.
4748
*/
48-
public abstract void paint(Property property, GC gc, int x, int y, int width, int height)
49+
public abstract void paint(Property property, Graphics graphics, int x, int y, int width, int height)
4950
throws Exception;
5051

5152
////////////////////////////////////////////////////////////////////////////

org.eclipse.wb.core/src/org/eclipse/wb/internal/core/model/property/editor/TextDisplayPropertyEditor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,7 +15,7 @@
1515
import org.eclipse.wb.internal.core.model.property.table.PropertyTooltipProvider;
1616
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
1717

18-
import org.eclipse.swt.graphics.GC;
18+
import org.eclipse.draw2d.Graphics;
1919

2020
/**
2121
* Abstract {@link PropertyEditor} for displaying text as {@link Property} value in
@@ -31,10 +31,10 @@ public abstract class TextDisplayPropertyEditor extends PropertyEditor {
3131
//
3232
////////////////////////////////////////////////////////////////////////////
3333
@Override
34-
public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception {
34+
public void paint(Property property, Graphics graphics, int x, int y, int width, int height) throws Exception {
3535
String text = getText(property);
3636
if (text != null) {
37-
DrawUtils.drawStringCV(gc, text, x, y, width, height);
37+
DrawUtils.drawStringCV(graphics, text, x, y, width, height);
3838
}
3939
}
4040

org.eclipse.wb.core/src/org/eclipse/wb/internal/core/model/property/table/PropertyTable.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import org.eclipse.wb.internal.core.model.property.editor.complex.IComplexPropertyEditor;
2121
import org.eclipse.wb.internal.core.model.property.editor.presentation.PropertyEditorPresentation;
2222
import org.eclipse.wb.internal.core.utils.check.Assert;
23+
import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils;
2324
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
2425

2526
import org.eclipse.draw2d.ColorConstants;
2627
import org.eclipse.draw2d.Cursors;
28+
import org.eclipse.draw2d.Graphics;
29+
import org.eclipse.draw2d.SWTGraphics;
2730
import org.eclipse.jface.viewers.ISelection;
2831
import org.eclipse.jface.viewers.ISelectionChangedListener;
2932
import org.eclipse.jface.viewers.ISelectionProvider;
@@ -1214,7 +1217,14 @@ private void drawProperty(GC gc, PropertyInfo propertyInfo, int y, int height, i
12141217
int x = m_splitter + 4;
12151218
int w = width - x - MARGIN_RIGHT;
12161219
// paint value
1217-
property.getEditor().paint(property, gc, x, y, w, height);
1220+
Graphics graphics = new SWTGraphics(gc);
1221+
try {
1222+
property.getEditor().paint(property, graphics, x, y, w, height);
1223+
} finally {
1224+
// update clipping
1225+
ReflectionUtils.invokeMethod(graphics, "checkGC()");
1226+
graphics.dispose();
1227+
}
12181228
}
12191229
} catch (Throwable e) {
12201230
DesignerPlugin.log(e);

org.eclipse.wb.core/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,6 +11,9 @@
1111
package org.eclipse.wb.internal.core.utils.ui;
1212

1313
import org.eclipse.draw2d.ColorConstants;
14+
import org.eclipse.draw2d.FigureUtilities;
15+
import org.eclipse.draw2d.Graphics;
16+
import org.eclipse.draw2d.geometry.Rectangle;
1417
import org.eclipse.swt.SWT;
1518
import org.eclipse.swt.graphics.Color;
1619
import org.eclipse.swt.graphics.Font;
@@ -19,7 +22,6 @@
1922
import org.eclipse.swt.graphics.Image;
2023
import org.eclipse.swt.graphics.ImageData;
2124
import org.eclipse.swt.graphics.Point;
22-
import org.eclipse.swt.graphics.Rectangle;
2325
import org.eclipse.swt.widgets.Display;
2426

2527
import org.apache.commons.io.IOUtils;
@@ -43,11 +45,15 @@ public class DrawUtils {
4345
////////////////////////////////////////////////////////////////////////////
4446
/**
4547
* Draws given text clipped horizontally and centered vertically.
48+
*
49+
* @deprecated Use {@link #drawStringCV(Graphics, String, int, int, int, int)}
50+
* instead.
4651
*/
52+
@Deprecated
4753
public static final void drawStringCV(GC gc, String text, int x, int y, int width, int height) {
48-
Rectangle oldClipping = gc.getClipping();
54+
org.eclipse.swt.graphics.Rectangle oldClipping = gc.getClipping();
4955
try {
50-
gc.setClipping(new Rectangle(x, y, width, height));
56+
gc.setClipping(new org.eclipse.swt.graphics.Rectangle(x, y, width, height));
5157
//
5258
int textStartY = y + (height - gc.getFontMetrics().getHeight()) / 2;
5359
gc.drawString(clipString(gc, text, width), x, textStartY, true);
@@ -56,6 +62,21 @@ public static final void drawStringCV(GC gc, String text, int x, int y, int widt
5662
}
5763
}
5864

65+
/**
66+
* Draws given text clipped horizontally and centered vertically.
67+
*/
68+
public static final void drawStringCV(Graphics graphics, String text, int x, int y, int width, int height) {
69+
try {
70+
graphics.pushState();
71+
graphics.setClip(new Rectangle(x, y, width, height));
72+
//
73+
int textStartY = y + (height - graphics.getFontMetrics().getHeight()) / 2;
74+
graphics.drawString(clipString(graphics, text, width), x, textStartY);
75+
} finally {
76+
graphics.popState();
77+
}
78+
}
79+
5980
/**
6081
* Draws given text clipped or centered horizontally and centered vertically.
6182
*/
@@ -72,20 +93,33 @@ public static final void drawStringCHCV(GC gc, String text, int x, int y, int wi
7293

7394
/**
7495
* Draws image at given <code>x</code> and centered vertically.
96+
*
97+
* @deprecated Use {@link #drawImageCV(Graphics, Image, int, int, int)} instead.
7598
*/
99+
@Deprecated
76100
public static final void drawImageCV(GC gc, Image image, int x, int y, int height) {
77101
if (image != null) {
78-
Rectangle imageBounds = image.getBounds();
102+
org.eclipse.swt.graphics.Rectangle imageBounds = image.getBounds();
79103
gc.drawImage(image, x, y + (height - imageBounds.height) / 2);
80104
}
81105
}
82106

107+
/**
108+
* Draws image at given <code>x</code> and centered vertically.
109+
*/
110+
public static final void drawImageCV(Graphics graphics, Image image, int x, int y, int height) {
111+
if (image != null) {
112+
org.eclipse.swt.graphics.Rectangle imageBounds = image.getBounds();
113+
graphics.drawImage(image, x, y + (height - imageBounds.height) / 2);
114+
}
115+
}
116+
83117
/**
84118
* Draws image at given <code>x</code> and centered vertically.
85119
*/
86120
public static final void drawImageCHCV(GC gc, Image image, int x, int y, int width, int height) {
87121
if (image != null) {
88-
Rectangle imageBounds = image.getBounds();
122+
org.eclipse.swt.graphics.Rectangle imageBounds = image.getBounds();
89123
int centerX = (width - imageBounds.width) / 2;
90124
int centerY = y + (height - imageBounds.height) / 2;
91125
gc.drawImage(image, x + centerX, centerY);
@@ -97,7 +131,7 @@ public static final void drawImageCHCV(GC gc, Image image, int x, int y, int wid
97131
* bigger that {@link Rectangle}, {@link Image} will be scaled down as needed with keeping
98132
* proportions.
99133
*/
100-
public static void drawScaledImage(GC gc, Image image, Rectangle targetRectangle) {
134+
public static void drawScaledImage(GC gc, Image image, org.eclipse.swt.graphics.Rectangle targetRectangle) {
101135
int imageWidth = image.getBounds().width;
102136
int imageHeight = image.getBounds().height;
103137
// prepare scaled image size
@@ -126,7 +160,10 @@ public static void drawScaledImage(GC gc, Image image, Rectangle targetRectangle
126160

127161
/**
128162
* @return the string clipped to have width less than given. Clipping is done as trailing "...".
163+
*
164+
* @deprecated Use {@link #clipString(Graphics, String, int)} instead.
129165
*/
166+
@Deprecated
130167
public static String clipString(GC gc, String text, int width) {
131168
if (width <= 0) {
132169
return "";
@@ -150,6 +187,36 @@ public static String clipString(GC gc, String text, int width) {
150187
return text.substring(0, count) + DOTS;
151188
}
152189

190+
/**
191+
* @return the string clipped to have width less than given. Clipping is done as trailing "...".
192+
*/
193+
public static String clipString(Graphics graphics, String text, int width) {
194+
if (width <= 0) {
195+
return "";
196+
}
197+
// check if text already fits in given width
198+
if (getStringWidth(graphics, text) <= width) {
199+
return text;
200+
}
201+
// use average count of characters as base
202+
int count = Math.min(width / graphics.getFontMetrics().getAverageCharWidth(), text.length());
203+
if (getStringWidth(graphics, text.substring(0, count) + DOTS) > width) {
204+
while (count > 0 && getStringWidth(graphics, text.substring(0, count) + DOTS) > width) {
205+
count--;
206+
}
207+
} else {
208+
while (count < text.length() - 1
209+
&& getStringWidth(graphics, text.substring(0, count + 1) + DOTS) < width) {
210+
count++;
211+
}
212+
}
213+
return text.substring(0, count) + DOTS;
214+
}
215+
216+
private static int getStringWidth(Graphics graphics, String text) {
217+
return FigureUtilities.getTextWidth(text, graphics.getFont());
218+
}
219+
153220
/**
154221
* Draws {@link String} in rectangle, wraps at any character (not by words).
155222
*/
@@ -227,7 +294,7 @@ public static Image getThubmnail(Image image,
227294
int minHeight,
228295
int maxWidth,
229296
int maxHeight) {
230-
Rectangle imageBounds = image.getBounds();
297+
org.eclipse.swt.graphics.Rectangle imageBounds = image.getBounds();
231298
int imageWidth = imageBounds.width;
232299
int imageHeight = imageBounds.height;
233300
if (imageWidth < minWidth && imageHeight < minHeight) {
@@ -269,7 +336,7 @@ public static Image getThubmnail(Image image,
269336
* Returns a new Image part of original Image location to given bounds and with given alpha.
270337
*/
271338
public static Image createTransparentPart(Image original,
272-
org.eclipse.draw2d.geometry.Rectangle bounds,
339+
Rectangle bounds,
273340
int alpha) {
274341
// prepare image
275342
Image image = new Image(null, bounds.width, bounds.height);

org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/model/property/editor/beans/ComboPropertyEditor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -17,7 +17,7 @@
1717
import org.eclipse.wb.internal.core.model.property.editor.PropertyEditor;
1818
import org.eclipse.wb.internal.core.model.property.editor.presentation.PropertyEditorPresentation;
1919

20-
import org.eclipse.swt.graphics.GC;
20+
import org.eclipse.draw2d.Graphics;
2121

2222
/**
2323
* The {@link PropertyEditor} wrapper for tag's based AWT {@link java.beans.PropertyEditor}.
@@ -88,8 +88,8 @@ protected String getText(Property property) throws Exception {
8888
}
8989

9090
@Override
91-
public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception {
92-
m_editorWrapper.paint(property, gc, x, y, width, height);
91+
public void paint(Property property, Graphics graphics, int x, int y, int width, int height) throws Exception {
92+
m_editorWrapper.paint(property, graphics, x, y, width, height);
9393
}
9494

9595
////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)