Skip to content

Commit c21bea1

Browse files
committed
Extract GC related operation in TextUtilities and FigureUtilities to be Drawable aware
This commit introduces two new utility classes to fully replace TextUtilities and partially replace FigureUtilities, when calculations on a GC are involded. Therefor DrawableTextUtilities or DrawableFigureUtilities can to be instantiated with a Drawable passed as context when a GC is instantiated.
1 parent a7c108e commit c21bea1

File tree

5 files changed

+276
-1
lines changed

5 files changed

+276
-1
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta 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+
* Yatta - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.draw2d;
14+
15+
import org.eclipse.swt.graphics.Drawable;
16+
import org.eclipse.swt.graphics.Font;
17+
import org.eclipse.swt.graphics.FontMetrics;
18+
import org.eclipse.swt.graphics.GC;
19+
20+
import org.eclipse.draw2d.geometry.Dimension;
21+
22+
/**
23+
* Provides miscellaneous Figure operations calculated with the zoom context of
24+
* the the provided {@code Drawable}.
25+
*
26+
* All GC related operations are mirrored from {@code FigureUtilities}
27+
*
28+
* @since 3.27
29+
*/
30+
public class DrawableFigureUtilities {
31+
private final GC gc;
32+
private Font appliedFont;
33+
private FontMetrics metrics;
34+
35+
public DrawableFigureUtilities(Drawable source) {
36+
gc = new GC(source);
37+
appliedFont = gc.getFont();
38+
}
39+
40+
/**
41+
* Returns the FontMetrics associated with the passed Font.
42+
*
43+
* @param f the font
44+
* @return the FontMetrics for the given font
45+
* @see GC#getFontMetrics()
46+
* @since 3.27
47+
*/
48+
public FontMetrics getFontMetrics(Font f) {
49+
setFont(f);
50+
if (metrics == null) {
51+
metrics = gc.getFontMetrics();
52+
}
53+
return metrics;
54+
}
55+
56+
/**
57+
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. Tab
58+
* expansion and carriage return processing are performed.
59+
*
60+
* @param s the string
61+
* @param f the font
62+
* @return the text's dimensions
63+
* @see GC#textExtent(String)
64+
* @since 3.27
65+
*/
66+
protected org.eclipse.swt.graphics.Point getTextDimension(String s, Font f) {
67+
setFont(f);
68+
return gc.textExtent(s);
69+
}
70+
71+
/**
72+
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. No tab
73+
* expansion or carriage return processing will be performed.
74+
*
75+
* @param s the string
76+
* @param f the font
77+
* @return the string's dimensions
78+
* @see GC#stringExtent(java.lang.String)
79+
* @since 3.27
80+
*/
81+
protected org.eclipse.swt.graphics.Point getStringDimension(String s, Font f) {
82+
setFont(f);
83+
return gc.stringExtent(s);
84+
}
85+
86+
/**
87+
* Returns the Dimensions of the given text, converting newlines and tabs
88+
* appropriately.
89+
*
90+
* @param text the text
91+
* @param f the font
92+
* @return the dimensions of the given text
93+
* @since 3.27
94+
*/
95+
public Dimension getTextExtents(String text, Font f) {
96+
return new Dimension(getTextDimension(text, f));
97+
}
98+
99+
/**
100+
* Returns the Dimensions of <i>s</i> in Font <i>f</i>.
101+
*
102+
* @param s the string
103+
* @param f the font
104+
* @return the dimensions of the given string
105+
* @since 3.27
106+
*/
107+
public Dimension getStringExtents(String s, Font f) {
108+
return new Dimension(getStringDimension(s, f));
109+
}
110+
111+
/**
112+
* Returns the Dimensions of the given text, converting newlines and tabs
113+
* appropriately.
114+
*
115+
* @param s the string
116+
* @param f the font
117+
* @param result the Dimension that will contain the result of this calculation
118+
* @since 3.27
119+
*/
120+
public void getTextExtents(String s, Font f, Dimension result) {
121+
org.eclipse.swt.graphics.Point pt = getTextDimension(s, f);
122+
result.width = pt.x;
123+
result.height = pt.y;
124+
}
125+
126+
/**
127+
* Returns the width of <i>s</i> in Font <i>f</i>.
128+
*
129+
* @param s the string
130+
* @param f the font
131+
* @return the width
132+
* @since 3.27
133+
*/
134+
public int getTextWidth(String s, Font f) {
135+
return getTextDimension(s, f).x;
136+
}
137+
138+
private void setFont(Font f) {
139+
if (appliedFont == f || (f != null && f.equals(appliedFont))) {
140+
return;
141+
}
142+
gc.setFont(f);
143+
appliedFont = f;
144+
metrics = null;
145+
}
146+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta 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+
* Yatta - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.draw2d;
15+
16+
import org.eclipse.swt.graphics.Drawable;
17+
import org.eclipse.swt.graphics.Font;
18+
import org.eclipse.swt.graphics.FontMetrics;
19+
20+
import org.eclipse.draw2d.geometry.Dimension;
21+
22+
/**
23+
* Provides miscellaneous text operations calculated with the zoom context of
24+
* the the provided {@code Drawable}.
25+
*
26+
* @since 3.27
27+
*/
28+
public class DrawableTextUtilities extends TextUtilities {
29+
30+
private final DrawableFigureUtilities figureUtilities;
31+
32+
public DrawableTextUtilities(Drawable source) {
33+
figureUtilities = new DrawableFigureUtilities(source);
34+
}
35+
36+
/**
37+
* Returns the Dimensions of <i>s</i> in Font <i>f</i>.
38+
*
39+
* @param s the string
40+
* @param f the font
41+
* @return the dimensions of the given string
42+
*/
43+
@Override
44+
public Dimension getStringExtents(String s, Font f) {
45+
return figureUtilities.getStringExtents(s, f);
46+
}
47+
48+
/**
49+
* Returns the Dimensions of the given text, converting newlines and tabs
50+
* appropriately.
51+
*
52+
* @param s the text
53+
* @param f the font
54+
* @return the dimensions of the given text
55+
*/
56+
@Override
57+
public Dimension getTextExtents(String s, Font f) {
58+
return figureUtilities.getTextExtents(s, f);
59+
}
60+
61+
/**
62+
* Gets the font's ascent.
63+
*
64+
* @param font
65+
* @return the font's ascent
66+
*/
67+
@Override
68+
public int getAscent(Font font) {
69+
FontMetrics fm = figureUtilities.getFontMetrics(font);
70+
return fm.getHeight() - fm.getDescent();
71+
}
72+
73+
/**
74+
* Gets the font's descent.
75+
*
76+
* @param font
77+
* @return the font's descent
78+
*/
79+
@Override
80+
public int getDescent(Font font) {
81+
return figureUtilities.getFontMetrics(font).getDescent();
82+
}
83+
}

org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.eclipse.draw2d.ButtonBorder;
7474
import org.eclipse.draw2d.ColorConstants;
7575
import org.eclipse.draw2d.Cursors;
76+
import org.eclipse.draw2d.DrawableTextUtilities;
7677
import org.eclipse.draw2d.FocusEvent;
7778
import org.eclipse.draw2d.FocusListener;
7879
import org.eclipse.draw2d.Graphics;
@@ -81,6 +82,7 @@
8182
import org.eclipse.draw2d.LightweightSystem;
8283
import org.eclipse.draw2d.MarginBorder;
8384
import org.eclipse.draw2d.PositionConstants;
85+
import org.eclipse.draw2d.TextUtilities;
8486
import org.eclipse.draw2d.Triangle;
8587
import org.eclipse.draw2d.geometry.Dimension;
8688

@@ -1114,6 +1116,7 @@ protected void updateState() {
11141116
private class TitleLabel extends Label {
11151117
protected static final Border BORDER = new MarginBorder(4, 3, 4, 3);
11161118
protected static final Border TOOL_TIP_BORDER = new MarginBorder(0, 2, 0, 2);
1119+
private TextUtilities textUtilities;
11171120

11181121
public TitleLabel(boolean isHorizontal) {
11191122
super(GEFMessages.Palette_Label, InternalImages.get(InternalImages.IMG_PALETTE));
@@ -1125,6 +1128,14 @@ public TitleLabel(boolean isHorizontal) {
11251128
setForegroundColor(ColorConstants.listForeground);
11261129
}
11271130

1131+
@Override
1132+
public TextUtilities getTextUtilities() {
1133+
if (textUtilities == null) {
1134+
textUtilities = new DrawableTextUtilities(paletteContainer);
1135+
}
1136+
return textUtilities;
1137+
}
1138+
11281139
@Override
11291140
public IFigure getToolTip() {
11301141
if (isTextTruncated()) {

org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.eclipse.swt.graphics.Drawable;
2425
import org.eclipse.swt.graphics.Image;
2526
import org.eclipse.swt.widgets.Display;
2627

2728
import org.eclipse.jface.resource.ImageDescriptor;
2829
import org.eclipse.ui.IMemento;
2930

3031
import org.eclipse.draw2d.Border;
32+
import org.eclipse.draw2d.DrawableTextUtilities;
3133
import org.eclipse.draw2d.IFigure;
3234
import org.eclipse.draw2d.MarginBorder;
35+
import org.eclipse.draw2d.TextUtilities;
3336
import org.eclipse.draw2d.geometry.Dimension;
3437
import org.eclipse.draw2d.text.FlowPage;
38+
import org.eclipse.draw2d.text.FlowUtilities;
3539
import org.eclipse.draw2d.text.TextFlow;
3640

3741
import org.eclipse.gef.AccessibleEditPart;
@@ -46,6 +50,7 @@
4650
import org.eclipse.gef.ui.palette.PaletteMessages;
4751
import org.eclipse.gef.ui.palette.PaletteViewer;
4852
import org.eclipse.gef.ui.palette.PaletteViewerPreferences;
53+
import org.eclipse.gef.util.EditPartUtilities;
4954

5055
/**
5156
* The abstract implementation of palette edit parts. All edit parts used in the
@@ -129,12 +134,36 @@ public Dimension getPreferredSize(int w, int h) {
129134
};
130135
fp.setOpaque(true);
131136
fp.setBorder(TOOLTIP_BORDER);
132-
TextFlow tf = new TextFlow();
137+
TextFlow tf = new PaletteTooltipTextFlow(EditPartUtilities.getMainControl(this).getShell());
133138
tf.setText(message);
134139
fp.add(tf);
135140
return fp;
136141
}
137142

143+
private class PaletteTooltipTextFlow extends TextFlow {
144+
145+
private final TextUtilities textUtilities;
146+
147+
private PaletteTooltipTextFlow(Drawable drawable) {
148+
textUtilities = new DrawableTextUtilities(drawable);
149+
}
150+
151+
@Override
152+
protected FlowUtilities getFlowUtilities() {
153+
return new FlowUtilities() {
154+
@Override
155+
protected TextUtilities getTextUtilities() {
156+
return textUtilities;
157+
}
158+
};
159+
}
160+
161+
@Override
162+
protected TextUtilities getTextUtilities() {
163+
return textUtilities;
164+
}
165+
}
166+
138167
/**
139168
* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
140169
*/

org.eclipse.gef/src/org/eclipse/gef/util/EditPartUtilities.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.HashSet;
1717
import java.util.LinkedHashSet;
1818

19+
import org.eclipse.swt.widgets.Control;
20+
1921
import org.eclipse.gef.ConnectionEditPart;
2022
import org.eclipse.gef.EditPart;
2123
import org.eclipse.gef.GraphicalEditPart;
@@ -86,4 +88,8 @@ public static HashSet<? extends ConnectionEditPart> getNestedConnectionEditParts
8688
});
8789
return edges;
8890
}
91+
92+
public static Control getMainControl(EditPart editPart) {
93+
return editPart.getRoot().getViewer().getControl().getShell();
94+
}
8995
}

0 commit comments

Comments
 (0)