Package com.diffplug.common.swt
Class SwtMisc
- java.lang.Object
-
- com.diffplug.common.swt.SwtMisc
-
public class SwtMisc extends java.lang.ObjectMiscellaneous SWT functions.
-
-
Constructor Summary
Constructors Constructor Description SwtMisc()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ListenerasListener(java.lang.Runnable runnable)Converts aRunnableinto aListener.static voidassertClean(Composite cmp)Throws anIllegalArgumentExceptioniff the given `Composite` has any children or a non-null layout.static voidassertNotUI()Asserts that the user didn't call this from the UI thread.static DisplayassertUI()Returns the Display instance, asserting that the method was called from the UI thread.static voidasyncLayout(Composite cmp)Performs an asynchronous layout on the given composite.static voidasyncLayoutOnResize(Composite cmp)Performs an asynchronous layout on the given composite anytime that it is resized.static voidblockForError(java.lang.String title, java.lang.String msg)Blocks to notify about an error.static voidblockForError(java.lang.String title, java.lang.String msg, Shell parent)Blocks to notify about an error.static intblockForMessageBox(java.lang.String title, java.lang.String message, int style)Opens a message box with the given title, message, and style and returns its result.static intblockForMessageBox(java.lang.String title, java.lang.String message, Shell parent, int style)Opens a message box with the given title, message, and style and returns its result.static booleanblockForOkCancel(java.lang.String title, java.lang.String message)Blocks to ask an Ok/Cancel question.static booleanblockForOkCancel(java.lang.String title, java.lang.String message, Shell parent)Blocks to ask an Ok/Cancel question.static booleanblockForQuestion(java.lang.String title, java.lang.String message)Blocks to ask a yes/no question.static booleanblockForQuestion(java.lang.String title, java.lang.String message, Shell parent)Blocks to ask a yes/no question.static voidblockForSuccess(java.lang.String title, java.lang.String msg)Blocks to notify about a success.static voidblockForSuccess(java.lang.String title, java.lang.String msg, Shell parent)Blocks to notify about a success.static voidclean(Composite cmp)Disposes all children of the given composite, and sets the layout to null.static EventcopyEvent(Event event)Returns a deep copy of the given SWT event.static intdefaultButtonWidth()Returns the default width of a button, scaled for the system font.static intdefaultDialogWidth()Returns the default width of a dialog.static booleanflagIsSet(int flag, int style)Returns true if `flag` is set in `style`.static booleanflagIsSet(int flag, Widget widget)Returns true if `flag` is set in the style value of `widget`.static voidforEachDeep(Composite root, java.util.function.Consumer<Control> ctlSetter)Calls the given consumer on the given composite and all of its children, recursively.static ColorgetSystemColor(int code)Returns the given system color.static CursorgetSystemCursor(int cursor)Returns the given system color.static ImagegetSystemIcon(int icon)Returns the given system icon.static RectangleglobalBounds(ControlWrapper control)Returns the global bounds of the given ControlWrapper.static RectangleglobalBounds(Control control)Returns the bounds of the given control in global coordinates.static voidloopUntil(java.util.function.Predicate<Display> until)Runs the display loop until the given `Predicate` returns false. static voidloopUntilDisposed(Widget widget)Runs the display loop until the `widget` has been disposed.static <T> TloopUntilGet(java.util.concurrent.CompletionStage<T> future)Runs the display loop until the given future has returned.static java.util.Optional<Monitor>monitorFor(Point p)Returns the monitor (if any) which contains the given point.static voidrequestLayout(Control control)Deprecated.As of SWT 4.6 (Neon), this functionality isControl.requestLayout().static ShellrootShell(Control ctl)Returns the root shell of the given control.static PointscaleByFont(int cols, int rows)Returns a point that represents the size of a (cols x rows) grid of characters printed in the standard system font.static PointscaleByFont(java.lang.String str)Returns a dimension which is guaranteed to be comfortable for the given string.static intscaleByFontHeight(int rows)Returns a dimension which is scaled by the system font's height.static PointscaleByFontHeight(int cols, int rows)Returns a size which is scaled by the system font's height.static voidsetBackgroundDeep(Composite root, Color background)Sets the background color of the given composite and all of its children, recursively.static voidsetEnabledDeep(Composite root, boolean enabled)Sets the enabled status of every child, grandchild, etc.static intsetFlag(int flag, boolean isSet, int style)Sets whether the flag is set for the given style.static voidsetForegroundDeep(Composite root, Color foreground)Sets the foreground color of the given composite and all of its children, recursively.static intsystemFontHeight()Returns the height of the system font.static intsystemFontSnug()Returns a distance which is a snug fit for a line of text in the system font.static intsystemFontWidth()Returns the width of the system font.static RectangletoDisplay(Control control, Rectangle rect)Converts a rectangle to global coordinates using the given control as a reference frame.static TreeDef.Parented<Composite>treeDefComposite()static TreeDef.Parented<Control>treeDefControl()static TreeDef.Parented<Shell>treeDefShell()static <T> TwithGcCompute(java.util.function.Function<GC,T> function)Computes some function using a temporary GC.static voidwithGcRun(java.util.function.Consumer<GC> consumer)Runs some function using a temporary GC.
-
-
-
Method Detail
-
flagIsSet
public static boolean flagIsSet(int flag, int style)Returns true if `flag` is set in `style`.
-
flagIsSet
public static boolean flagIsSet(int flag, Widget widget)Returns true if `flag` is set in the style value of `widget`.
-
setFlag
public static int setFlag(int flag, boolean isSet, int style)Sets whether the flag is set for the given style.
-
asListener
public static Listener asListener(java.lang.Runnable runnable)
Converts aRunnableinto aListener.
-
assertUI
public static Display assertUI()
Returns the Display instance, asserting that the method was called from the UI thread.
-
assertNotUI
public static void assertNotUI()
Asserts that the user didn't call this from the UI thread.
-
getSystemColor
public static Color getSystemColor(int code)
Returns the given system color. SWT.COLOR_*
-
getSystemCursor
public static Cursor getSystemCursor(int cursor)
Returns the given system color. SWT.CURSOR_*
-
getSystemIcon
public static Image getSystemIcon(int icon)
Returns the given system icon. SWT.ICON_*
-
requestLayout
@Deprecated public static void requestLayout(Control control)
Deprecated.As of SWT 4.6 (Neon), this functionality isControl.requestLayout().
-
asyncLayout
public static void asyncLayout(Composite cmp)
Performs an asynchronous layout on the given composite.Oftentimes, a layout will not be successful unless it is performed in a
Display.asyncExec(Runnable)call, because the current list of events must be processed before the layout can take place.
-
asyncLayoutOnResize
public static void asyncLayoutOnResize(Composite cmp)
Performs an asynchronous layout on the given composite anytime that it is resized.This can often fix graphical glitches with resize-to-fit layouts, such as a `TableColumnLayout`.
-
clean
public static void clean(Composite cmp)
Disposes all children of the given composite, and sets the layout to null.
-
assertClean
public static void assertClean(Composite cmp)
Throws anIllegalArgumentExceptioniff the given `Composite` has any children or a non-null layout.
-
withGcRun
public static void withGcRun(java.util.function.Consumer<GC> consumer)
Runs some function using a temporary GC.
-
withGcCompute
public static <T> T withGcCompute(java.util.function.Function<GC,T> function)
Computes some function using a temporary GC.
-
loopUntil
public static void loopUntil(java.util.function.Predicate<Display> until)
Runs the display loop until the given `Predicate` returns false.
-
loopUntilDisposed
public static void loopUntilDisposed(Widget widget)
Runs the display loop until the `widget` has been disposed.
-
loopUntilGet
public static <T> T loopUntilGet(java.util.concurrent.CompletionStage<T> future)
Runs the display loop until the given future has returned.
-
blockForSuccess
public static void blockForSuccess(java.lang.String title, java.lang.String msg, @Nullable Shell parent)Blocks to notify about a success. Can be called from any thread.
-
blockForSuccess
public static void blockForSuccess(java.lang.String title, java.lang.String msg)Blocks to notify about a success. Can be called from any thread.
-
blockForError
public static void blockForError(java.lang.String title, java.lang.String msg, @Nullable Shell parent)Blocks to notify about an error. Can be called from any thread.
-
blockForError
public static void blockForError(java.lang.String title, java.lang.String msg)Blocks to notify about an error. Can be called from any thread.
-
blockForQuestion
public static boolean blockForQuestion(java.lang.String title, java.lang.String message, @Nullable Shell parent)Blocks to ask a yes/no question. Can be called from any thread.
-
blockForQuestion
public static boolean blockForQuestion(java.lang.String title, java.lang.String message)Blocks to ask a yes/no question. Can be called from any thread.
-
blockForOkCancel
public static boolean blockForOkCancel(java.lang.String title, java.lang.String message, @Nullable Shell parent)Blocks to ask an Ok/Cancel question. Can be called from any thread.
-
blockForOkCancel
public static boolean blockForOkCancel(java.lang.String title, java.lang.String message)Blocks to ask an Ok/Cancel question. Can be called from any thread.
-
blockForMessageBox
public static int blockForMessageBox(java.lang.String title, java.lang.String message, @Nullable Shell parent, int style)Opens a message box with the given title, message, and style and returns its result. Can be called from any thread.- Parameters:
title- The title of the box.message- The message in the box.parent- The parent shell. Null means it will be on the current active shell.style- An OR'ed combination of SWT.YES/NO, SWT.OK/CANCEL, and SWT.ICON_*- Returns:
- The button that was pressed (e.g. SWT.YES, SWT.OK, etc)
-
blockForMessageBox
public static int blockForMessageBox(java.lang.String title, java.lang.String message, int style)Opens a message box with the given title, message, and style and returns its result. Can be called from any thread.- Parameters:
title- The title of the box.message- The message in the box.style- An OR'ed combination of SWT.YES/NO, SWT.OK/CANCEL, and SWT.ICON_*- Returns:
- The button that was pressed (e.g. SWT.YES, SWT.OK, etc)
-
systemFontHeight
public static int systemFontHeight()
Returns the height of the system font.
-
systemFontWidth
public static int systemFontWidth()
Returns the width of the system font.
-
systemFontSnug
public static int systemFontSnug()
Returns a distance which is a snug fit for a line of text in the system font.
-
defaultButtonWidth
public static int defaultButtonWidth()
Returns the default width of a button, scaled for the system font.
-
defaultDialogWidth
public static int defaultDialogWidth()
Returns the default width of a dialog.
-
scaleByFontHeight
public static Point scaleByFontHeight(int cols, int rows)
Returns a size which is scaled by the system font's height.
-
scaleByFontHeight
public static int scaleByFontHeight(int rows)
Returns a dimension which is scaled by the system font's height.
-
scaleByFont
public static Point scaleByFont(int cols, int rows)
Returns a point that represents the size of a (cols x rows) grid of characters printed in the standard system font.
-
scaleByFont
public static Point scaleByFont(java.lang.String str)
Returns a dimension which is guaranteed to be comfortable for the given string.
-
globalBounds
public static Rectangle globalBounds(Control control)
Returns the bounds of the given control in global coordinates.
-
globalBounds
public static Rectangle globalBounds(ControlWrapper control)
Returns the global bounds of the given ControlWrapper.
-
toDisplay
public static Rectangle toDisplay(Control control, Rectangle rect)
Converts a rectangle to global coordinates using the given control as a reference frame.
-
monitorFor
public static java.util.Optional<Monitor> monitorFor(Point p)
Returns the monitor (if any) which contains the given point.
-
setEnabledDeep
public static void setEnabledDeep(Composite root, boolean enabled)
Sets the enabled status of every child, grandchild, etc. of the given composite. Skips plain-jane Composites.
-
forEachDeep
public static void forEachDeep(Composite root, java.util.function.Consumer<Control> ctlSetter)
Calls the given consumer on the given composite and all of its children, recursively.
-
setForegroundDeep
public static void setForegroundDeep(Composite root, Color foreground)
Sets the foreground color of the given composite and all of its children, recursively.
-
setBackgroundDeep
public static void setBackgroundDeep(Composite root, Color background)
Sets the background color of the given composite and all of its children, recursively.
-
treeDefComposite
public static TreeDef.Parented<Composite> treeDefComposite()
-
treeDefControl
public static TreeDef.Parented<Control> treeDefControl()
-
treeDefShell
public static TreeDef.Parented<Shell> treeDefShell()
-
-