Skip to content

Commit

Permalink
Stop using deprecated FontMetrics.getAverageCharWidth()
Browse files Browse the repository at this point in the history
  • Loading branch information
akurtakov committed Apr 5, 2024
1 parent cccf0a0 commit ce64a2d
Showing 1 changed file with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -30,10 +30,10 @@
import org.eclipse.swt.widgets.Widget;

/**
* Utility class to simplify access to some SWT resources.
* Utility class to simplify access to some SWT resources.
*/
public class SWTUtil {

/*
* Returns the standard display to be used. The method first checks, if
* the thread calling this method has an associated disaply. If so, this
Expand All @@ -44,13 +44,13 @@ public static Display getStandardDisplay() {
display= Display.getCurrent();
if (display == null)
display= Display.getDefault();
return display;
return display;
}

/*
* Returns the shell for the given widget. If the widget doesn't represent
* a SWT object that manage a shell, <code>null</code> is returned.
*
*
* @return the shell for the given widget
*/
public static Shell getShell(Widget widget) {
Expand All @@ -65,10 +65,10 @@ public static Shell getShell(Widget widget) {
if (widget instanceof Menu)
return ((Menu)widget).getParent().getShell();
if (widget instanceof ScrollBar)
return ((ScrollBar)widget).getParent().getShell();
return null;
return ((ScrollBar)widget).getParent().getShell();
return null;
}

private static double getVerticalDialogUnitSize(Control control) {
GC gc= new GC(control);
try {
Expand All @@ -78,17 +78,17 @@ private static double getVerticalDialogUnitSize(Control control) {
gc.dispose();
}
}

private static double getHorizontalDialogUnitSize(Control control) {
GC gc= new GC(control);
try {
int averageWidth= gc.getFontMetrics().getAverageCharWidth();
double averageWidth = gc.getFontMetrics().getAverageCharacterWidth();
return averageWidth * 0.25;
} finally {
gc.dispose();
}
}
}

public static int convertHeightInCharsToPixels(int chars, Control control) {
return convertVerticalDLUsToPixels(chars * 8, control);
}
Expand All @@ -100,11 +100,11 @@ public static int convertHorizontalDLUsToPixels(int dlus, Control control) {
public static int convertVerticalDLUsToPixels(int dlus, Control control) {
return (int)Math.round(dlus * getVerticalDialogUnitSize(control));
}

public static int convertWidthInCharsToPixels(int chars, Control control) {
return convertHorizontalDLUsToPixels(chars * 4, control);
}

/*
* Returns a width hint for a button control.
*/
Expand All @@ -115,24 +115,24 @@ public static int getButtonWidthHint(Button button) {

/*
* Returns a height hint for a button control.
*/
*/
// public static int getButtonHeigthHint(Button button) {
// return convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT, button);
// }
// }

/*
* Sets width and height hint for the button control.
* <b>Note:</b> This is a NOP if the button's layout data is not
* an instance of <code>GridData</code>.
*
*
* @param the button for which to set the dimension hint
*/
*/
public static void setButtonDimensionHint(Button button) {
Assert.isNotNull(button);
Object gd= button.getLayoutData();
if (gd instanceof GridData) {
//((GridData)gd).heightHint= getButtonHeigthHint(button);
((GridData)gd).widthHint= getButtonWidthHint(button);
((GridData)gd).widthHint= getButtonWidthHint(button);
}
}
}

0 comments on commit ce64a2d

Please sign in to comment.