Skip to content

Commit 5264240

Browse files
Replace usages of new Image(device, width, height)
Replacing it with imageGcDrawer
1 parent 586ef10 commit 5264240

File tree

8 files changed

+129
-128
lines changed

8 files changed

+129
-128
lines changed

bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/GradientBackgroundListener.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.eclipse.swt.graphics.GC;
3838
import org.eclipse.swt.graphics.Image;
3939
import org.eclipse.swt.graphics.ImageData;
40+
import org.eclipse.swt.graphics.ImageGcDrawer;
4041
import org.eclipse.swt.graphics.PaletteData;
4142
import org.eclipse.swt.graphics.Point;
4243
import org.eclipse.swt.graphics.RGB;
@@ -149,23 +150,23 @@ public void handleEvent(Event event) {
149150
boolean verticalGradient = grad.getVerticalGradient();
150151
int x = verticalGradient? 2 : size.x;
151152
int y = verticalGradient ? size.y : 2;
152-
gradientImage = new Image(control.getDisplay(), x, y);
153-
GC gc = new GC(gradientImage);
154-
List<Color> colors = new ArrayList<>();
155-
for (Object rgbObj : grad.getRGBs()) {
156-
if (rgbObj instanceof RGBA) {
157-
RGBA rgba = (RGBA) rgbObj;
158-
Color color = new Color(control.getDisplay(), rgba);
159-
colors.add(color);
160-
} else if (rgbObj instanceof RGB) {
161-
RGB rgb = (RGB) rgbObj;
162-
Color color = new Color(control.getDisplay(), rgb);
163-
colors.add(color);
153+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
154+
List<Color> colors = new ArrayList<>();
155+
for (Object rgbObj : grad.getRGBs()) {
156+
if (rgbObj instanceof RGBA) {
157+
RGBA rgba = (RGBA) rgbObj;
158+
Color color = new Color(control.getDisplay(), rgba);
159+
colors.add(color);
160+
} else if (rgbObj instanceof RGB) {
161+
RGB rgb = (RGB) rgbObj;
162+
Color color = new Color(control.getDisplay(), rgb);
163+
colors.add(color);
164+
}
164165
}
165-
}
166-
fillGradient(gc, new Rectangle(0, 0, x, y), colors,
167-
CSSSWTColorHelper.getPercents(grad), grad.getVerticalGradient());
168-
gc.dispose();
166+
fillGradient(gc, new Rectangle(0, 0, width, height), colors, CSSSWTColorHelper.getPercents(grad),
167+
grad.getVerticalGradient());
168+
};
169+
gradientImage = new Image(control.getDisplay(), imageGcDrawer, x, y);
169170
}
170171
if (gradientImage != null) {
171172
control.setBackgroundImage(gradientImage);

bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/ResourceUtility.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import org.eclipse.e4.ui.workbench.swt.util.ISWTResourceUtilities;
2020
import org.eclipse.emf.common.util.URI;
2121
import org.eclipse.jface.resource.ImageDescriptor;
22-
import org.eclipse.swt.graphics.GC;
2322
import org.eclipse.swt.graphics.Image;
23+
import org.eclipse.swt.graphics.ImageGcDrawer;
2424
import org.eclipse.swt.graphics.Rectangle;
2525

2626
public class ResourceUtility implements ISWTResourceUtilities {
@@ -48,12 +48,12 @@ public Image adornImage(Image toAdorn, Image adornment) {
4848
return toAdorn;
4949
Rectangle adornmentSize = adornment.getBounds();
5050

51-
Image adornedImage = new Image(toAdorn.getDevice(), 16, 16);
52-
GC gc = new GC(adornedImage);
53-
gc.drawImage(toAdorn, 0, 0);
54-
// For now assume top-right
55-
gc.drawImage(adornment, 16 - adornmentSize.width, 0);
56-
gc.dispose();
51+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
52+
gc.drawImage(toAdorn, 0, 0);
53+
// For now assume top-right
54+
gc.drawImage(adornment, 16 - adornmentSize.width, 0);
55+
};
56+
Image adornedImage = new Image(toAdorn.getDevice(), imageGcDrawer, 16, 16);
5757

5858
return adornedImage;
5959
}

bundles/org.eclipse.text.quicksearch/src/org/eclipse/text/quicksearch/internal/ui/QuickSearchDialog.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.eclipse.swt.events.SelectionEvent;
8181
import org.eclipse.swt.graphics.Color;
8282
import org.eclipse.swt.graphics.Image;
83+
import org.eclipse.swt.graphics.ImageGcDrawer;
8384
import org.eclipse.swt.graphics.Point;
8485
import org.eclipse.swt.graphics.Rectangle;
8586
import org.eclipse.swt.layout.GridData;
@@ -239,7 +240,8 @@ public void update(ViewerCell cell) {
239240

240241
private Image getBlankImage() {
241242
if (blankImage==null) {
242-
blankImage = new Image(Display.getDefault(), 1, 1);
243+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {};
244+
blankImage = new Image(Display.getDefault(), imageGcDrawer, 1, 1);
243245
// GC gc = new GC(blankImage);
244246
// gc.fillRectangle(0, 0, 16, 16);
245247
// gc.dispose();

bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/editors/text/TextEditorDefaultsPreferencePage.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.eclipse.swt.events.SelectionEvent;
2929
import org.eclipse.swt.events.SelectionListener;
3030
import org.eclipse.swt.graphics.Color;
31-
import org.eclipse.swt.graphics.GC;
3231
import org.eclipse.swt.graphics.Image;
32+
import org.eclipse.swt.graphics.ImageGcDrawer;
3333
import org.eclipse.swt.graphics.RGB;
3434
import org.eclipse.swt.layout.GridData;
3535
import org.eclipse.swt.layout.GridLayout;
@@ -1121,16 +1121,16 @@ public Image getImage(Object element) {
11211121
RGB rgb= colorEntry.isSystemDefault() ? colorEntry.systemColorRGB : colorEntry.getRGB();
11221122
Color color= new Color(tableComposite.getParent().getDisplay(), rgb.red, rgb.green, rgb.blue);
11231123
int dimensions= 10;
1124-
Image image= new Image(tableComposite.getParent().getDisplay(), dimensions, dimensions);
1125-
GC gc= new GC(image);
1126-
// Draw color preview
1127-
gc.setBackground(color);
1128-
gc.fillRectangle(0, 0, dimensions, dimensions);
1129-
// Draw outline around color preview
1130-
gc.setBackground(new Color(tableComposite.getParent().getDisplay(), 0, 0, 0));
1131-
gc.setLineWidth(2);
1132-
gc.drawRectangle(0, 0, dimensions, dimensions);
1133-
gc.dispose();
1124+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
1125+
// Draw color preview
1126+
gc.setBackground(color);
1127+
gc.fillRectangle(0, 0, width, height);
1128+
// Draw outline around color preview
1129+
gc.setBackground(new Color(tableComposite.getParent().getDisplay(), 0, 0, 0));
1130+
gc.setLineWidth(2);
1131+
gc.drawRectangle(0, 0, width, height);
1132+
};
1133+
Image image = new Image(tableComposite.getParent().getDisplay(), imageGcDrawer, dimensions, dimensions);
11341134
colorPreviewImages.add(image);
11351135
return image;
11361136
}

bundles/org.eclipse.ui.forms/src/org/eclipse/ui/forms/widgets/FormText.java

+20-24
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.eclipse.swt.graphics.FontMetrics;
4949
import org.eclipse.swt.graphics.GC;
5050
import org.eclipse.swt.graphics.Image;
51+
import org.eclipse.swt.graphics.ImageGcDrawer;
5152
import org.eclipse.swt.graphics.Point;
5253
import org.eclipse.swt.graphics.Rectangle;
5354
import org.eclipse.swt.widgets.Canvas;
@@ -1557,32 +1558,27 @@ private void paint(PaintEvent e) {
15571558
}
15581559

15591560
private void repaint(GC gc, int x, int y, int width, int height) {
1560-
Image textBuffer = new Image(getDisplay(), width, height);
1561-
Color bg = getBackground();
1562-
Color fg = getForeground();
1563-
if (!getEnabled()) {
1564-
bg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
1565-
fg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
1566-
}
1567-
GC textGC = new GC(textBuffer, gc.getStyle());
1568-
textGC.setForeground(fg);
1569-
textGC.setBackground(bg);
1570-
textGC.setFont(getFont());
1571-
textGC.fillRectangle(0, 0, width, height);
1572-
Rectangle repaintRegion = new Rectangle(x, y, width, height);
1561+
Color bg = getEnabled() ? getBackground() : getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
1562+
Color fg = getEnabled() ? getForeground() : getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
15731563

15741564
Paragraph[] paragraphs = model.getParagraphs();
1575-
IHyperlinkSegment selectedLink = getSelectedLink();
1576-
if (getDisplay().getFocusControl() != this)
1577-
selectedLink = null;
1578-
for (Paragraph p : paragraphs) {
1579-
p
1580-
.paint(textGC, repaintRegion, resourceTable, selectedLink,
1581-
selData);
1582-
}
1583-
if (hasFocus && !model.hasFocusSegments())
1584-
textGC.drawFocus(x, y, width, height);
1585-
textGC.dispose();
1565+
IHyperlinkSegment selectedLink = getDisplay().getFocusControl() != this ? null : getSelectedLink();
1566+
1567+
final ImageGcDrawer textDrawer = (textGC, iWidth, iHeight) -> {
1568+
textGC.setForeground(fg);
1569+
textGC.setBackground(bg);
1570+
textGC.setFont(getFont());
1571+
textGC.fillRectangle(0, 0, iWidth, iHeight);
1572+
Rectangle repaintRegion = new Rectangle(x, y, iWidth, iHeight);
1573+
for (Paragraph p : paragraphs) {
1574+
p.paint(textGC, repaintRegion, resourceTable, selectedLink, selData);
1575+
}
1576+
if (hasFocus && !model.hasFocusSegments()) {
1577+
textGC.drawFocus(x, y, iWidth, iHeight);
1578+
}
1579+
};
1580+
Image textBuffer = new Image(getDisplay(), textDrawer, width, height);
1581+
15861582
gc.drawImage(textBuffer, x, y);
15871583
textBuffer.dispose();
15881584
}

bundles/org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormHeading.java

+32-34
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.eclipse.swt.graphics.FontMetrics;
3939
import org.eclipse.swt.graphics.GC;
4040
import org.eclipse.swt.graphics.Image;
41+
import org.eclipse.swt.graphics.ImageGcDrawer;
4142
import org.eclipse.swt.graphics.Point;
4243
import org.eclipse.swt.graphics.Rectangle;
4344
import org.eclipse.swt.widgets.Canvas;
@@ -852,38 +853,35 @@ private void onPaint(GC gc) {
852853
if (carea.width == 0 || carea.height == 0) {
853854
return;
854855
}
855-
Image buffer = new Image(getDisplay(), carea.width, carea.height);
856-
buffer.setBackground(getBackground());
857-
GC igc = new GC(buffer);
858-
igc.setBackground(getBackground());
859-
igc.fillRectangle(0, 0, carea.width, carea.height);
860-
if (getBackgroundImage() != null) {
861-
if (gradientInfo != null)
862-
drawBackground(igc, carea.x, carea.y, carea.width, carea.height);
863-
else {
864-
Image bgImage = getBackgroundImage();
865-
Rectangle ibounds = bgImage.getBounds();
866-
drawBackground(igc, carea.x, carea.y, ibounds.width,
867-
ibounds.height);
856+
final ImageGcDrawer imageGcDrawer = (igc, width, height) -> {
857+
igc.setBackground(getBackground());
858+
igc.fillRectangle(0, 0, width, height);
859+
if (getBackgroundImage() != null) {
860+
if (gradientInfo != null)
861+
drawBackground(igc, carea.x, carea.y, width, height);
862+
else {
863+
Image bgImage = getBackgroundImage();
864+
Rectangle ibounds = bgImage.getBounds();
865+
drawBackground(igc, carea.x, carea.y, ibounds.width, ibounds.height);
866+
}
868867
}
869-
}
870868

871-
if (isSeparatorVisible()) {
872-
// bg separator
873-
if (hasColor(IFormColors.H_BOTTOM_KEYLINE1))
874-
igc.setForeground(getColor(IFormColors.H_BOTTOM_KEYLINE1));
875-
else
876-
igc.setForeground(getBackground());
877-
igc.drawLine(carea.x, carea.height - 2, carea.x + carea.width - 1,
878-
carea.height - 2);
879-
if (hasColor(IFormColors.H_BOTTOM_KEYLINE2))
880-
igc.setForeground(getColor(IFormColors.H_BOTTOM_KEYLINE2));
881-
else
882-
igc.setForeground(getForeground());
883-
igc.drawLine(carea.x, carea.height - 1, carea.x + carea.width - 1,
884-
carea.height - 1);
885-
}
886-
igc.dispose();
869+
if (isSeparatorVisible()) {
870+
// bg separator
871+
if (hasColor(IFormColors.H_BOTTOM_KEYLINE1))
872+
igc.setForeground(getColor(IFormColors.H_BOTTOM_KEYLINE1));
873+
else
874+
igc.setForeground(getBackground());
875+
igc.drawLine(carea.x, height - 2, carea.x + width - 1, height - 2);
876+
if (hasColor(IFormColors.H_BOTTOM_KEYLINE2))
877+
igc.setForeground(getColor(IFormColors.H_BOTTOM_KEYLINE2));
878+
else
879+
igc.setForeground(getForeground());
880+
igc.drawLine(carea.x, height - 1, carea.x + width - 1, height - 1);
881+
}
882+
};
883+
Image buffer = new Image(getDisplay(), imageGcDrawer, carea.width, carea.height);
884+
buffer.setBackground(getBackground());
887885
gc.drawImage(buffer, carea.x, carea.y);
888886
buffer.dispose();
889887
}
@@ -908,12 +906,12 @@ private void updateGradientImage() {
908906
gradientImage = FormImages.getInstance().getGradient(gradientInfo.gradientColors, gradientInfo.percents,
909907
gradientInfo.vertical ? rect.height : rect.width, gradientInfo.vertical, getColor(COLOR_BASE_BG), getDisplay());
910908
} else if (backgroundImage != null && !isBackgroundImageTiled()) {
911-
gradientImage = new Image(getDisplay(), Math.max(rect.width, 1),
909+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
910+
gc.drawImage(backgroundImage, 0, 0);
911+
};
912+
gradientImage = new Image(getDisplay(), imageGcDrawer, Math.max(rect.width, 1),
912913
Math.max(rect.height, 1));
913914
gradientImage.setBackground(getBackground());
914-
GC gc = new GC(gradientImage);
915-
gc.drawImage(backgroundImage, 0, 0);
916-
gc.dispose();
917915
}
918916
if (oldGradientImage != null) {
919917
FormImages.getInstance().markFinished(oldGradientImage, getDisplay());

bundles/org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormImages.java

+33-29
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.eclipse.swt.graphics.GC;
2929
import org.eclipse.swt.graphics.Image;
3030
import org.eclipse.swt.graphics.ImageData;
31+
import org.eclipse.swt.graphics.ImageGcDrawer;
3132
import org.eclipse.swt.graphics.RGB;
3233
import org.eclipse.swt.widgets.Display;
3334

@@ -113,16 +114,19 @@ public ImageData getImageData() {
113114

114115
@Override
115116
public Image createImage(boolean returnMissingImageOnError, Device device) {
116-
Image image = new Image(device, 1, fLength);
117117
Color color1 = new Color(device, fRGBs[0]);
118118
Color color2 = new Color(device, fRGBs[1]);
119+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
120+
gc.setBackground(color1);
121+
gc.fillRectangle(0, 0, width, height);
122+
gc.setForeground(color2);
123+
gc.setBackground(color1);
124+
gc.fillGradientRectangle(0, fMarginHeight + 2, 1, fTheight - 2, true);
125+
};
126+
Image image = new Image(device, imageGcDrawer, 1, fLength);
119127
image.setBackground(color1);
120128
GC gc = new GC(image);
121-
gc.setBackground(color1);
122-
gc.fillRectangle(0, 0, 1, fLength);
123-
gc.setForeground(color2);
124-
gc.setBackground(color1);
125-
gc.fillGradientRectangle(0, fMarginHeight + 2, 1, fTheight - 2, true);
129+
126130
gc.dispose();
127131
return image;
128132
}
@@ -181,15 +185,15 @@ public ImageData getImageData() {
181185
public Image createImage(boolean returnMissingImageOnError, Device device) {
182186
int width = fVertical ? 1 : fLength;
183187
int height = fVertical ? fLength : 1;
184-
Image gradient = new Image(device, Math.max(width, 1), Math
188+
final ImageGcDrawer imageGcDrawer = (gc, iWidth, iHeight) -> {
189+
Color[] colors = new Color[fRGBs.length];
190+
for (int i = 0; i < colors.length; i++)
191+
colors[i] = new Color(device, fRGBs[i]);
192+
Color bg = fBgRGB == null ? null : new Color(device, fBgRGB);
193+
drawTextGradient(gc, iWidth, iHeight, colors, fPercents, fVertical, bg);
194+
};
195+
Image gradient = new Image(device, imageGcDrawer, Math.max(width, 1), Math
185196
.max(height, 1));
186-
GC gc = new GC(gradient);
187-
Color[] colors = new Color[fRGBs.length];
188-
for (int i = 0; i < colors.length; i++)
189-
colors[i] = new Color(device, fRGBs[i]);
190-
Color bg = fBgRGB == null ? null : new Color(device, fBgRGB);
191-
drawTextGradient(gc, width, height, colors, fPercents, fVertical, bg);
192-
gc.dispose();
193197
return gradient;
194198
}
195199

@@ -275,16 +279,16 @@ public ImageData getImageData() {
275279

276280
@Override
277281
public Image createImage(boolean returnMissingImageOnError, Device device) {
278-
Image image = new Image(device, 1, fLength);
279282
Color originalBgColor = new Color(device, fRGBs[0]);
280283
Color color1 = new Color(device, fRGBs[1]);
284+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
285+
gc.setBackground(color1);
286+
gc.fillRectangle(0, fMarginHeight + 2, width, fTheight - fMarginHeight - 3);
287+
gc.setBackground(originalBgColor);
288+
gc.fillRectangle(0, fTheight - fMarginHeight - 4, 1, 4);
289+
};
290+
Image image = new Image(device, imageGcDrawer, 1, fLength);
281291
image.setBackground(originalBgColor);
282-
GC gc = new GC(image);
283-
gc.setBackground(color1);
284-
gc.fillRectangle(0, fMarginHeight + 2, 1, fTheight - fMarginHeight - 3);
285-
gc.setBackground(originalBgColor);
286-
gc.fillRectangle(0, fTheight - fMarginHeight - 4, 1, 4);
287-
gc.dispose();
288292
return image;
289293
}
290294
}
@@ -299,17 +303,17 @@ private class SimpleSectionGradientImageDescriptor extends SimpleSectionImageDes
299303

300304
@Override
301305
public Image createImage(boolean returnMissingImageOnError, Device device) {
302-
Image image = new Image(device, 1, fLength);
303306
Color color1 = new Color(device, fRGBs[0]);
304307
Color color2 = new Color(device, fRGBs[1]);
308+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
309+
gc.setBackground(color1);
310+
gc.fillRectangle(0, 0, width, width);
311+
gc.setForeground(color2);
312+
gc.setBackground(color1);
313+
gc.fillGradientRectangle(0, fMarginHeight + 2, 1, fTheight - 2, true);
314+
};
315+
Image image = new Image(device, imageGcDrawer, 1, fLength);
305316
image.setBackground(color1);
306-
GC gc = new GC(image);
307-
gc.setBackground(color1);
308-
gc.fillRectangle(0, 0, 1, fLength);
309-
gc.setForeground(color2);
310-
gc.setBackground(color1);
311-
gc.fillGradientRectangle(0, fMarginHeight + 2, 1, fTheight - 2, true);
312-
gc.dispose();
313317

314318
return image;
315319
}

0 commit comments

Comments
 (0)