Class SwtMisc

java.lang.Object
com.diffplug.common.swt.SwtMisc

public class SwtMisc extends Object
Miscellaneous SWT functions.
  • Constructor Details

    • SwtMisc

      public SwtMisc()
  • Method Details

    • 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(Runnable runnable)
      Converts a Runnable into a Listener.
    • 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 is Control.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 an IllegalArgumentException iff the given `Composite` has any children or a non-null layout.
    • copyEvent

      public static Event copyEvent(Event event)
      Returns a deep copy of the given SWT event.
    • withGcRun

      public static void withGcRun(Consumer<GC> consumer)
      Runs some function using a temporary GC.
    • withGcCompute

      public static <T> T withGcCompute(Function<GC,T> function)
      Computes some function using a temporary GC.
    • loopUntil

      public static void loopUntil(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(CompletionStage<T> future)
      Runs the display loop until the given future has returned.
    • blockForSuccess

      public static void blockForSuccess(String title, String msg, @Nullable Shell parent)
      Blocks to notify about a success. Can be called from any thread.
    • blockForSuccess

      public static void blockForSuccess(String title, String msg)
      Blocks to notify about a success. Can be called from any thread.
    • blockForError

      public static void blockForError(String title, String msg, @Nullable Shell parent)
      Blocks to notify about an error. Can be called from any thread.
    • blockForError

      public static void blockForError(String title, String msg)
      Blocks to notify about an error. Can be called from any thread.
    • blockForQuestion

      public static boolean blockForQuestion(String title, String message, @Nullable Shell parent)
      Blocks to ask a yes/no question. Can be called from any thread.
    • blockForQuestion

      public static boolean blockForQuestion(String title, String message)
      Blocks to ask a yes/no question. Can be called from any thread.
    • blockForOkCancel

      public static boolean blockForOkCancel(String title, String message, @Nullable Shell parent)
      Blocks to ask an Ok/Cancel question. Can be called from any thread.
    • blockForOkCancel

      public static boolean blockForOkCancel(String title, String message)
      Blocks to ask an Ok/Cancel question. Can be called from any thread.
    • blockForMessageBox

      public static int blockForMessageBox(String title, 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(String title, 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 double systemFontWidth()
      Returns the width of the system font.
    • systemFontWidthTimes

      public static int systemFontWidthTimes(int numChars)
    • systemFontWidthTimes

      public static int systemFontWidthTimes(String str)
    • 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(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(com.diffplug.common.swt.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 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, 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.
    • rootShell

      public static Shell rootShell(Control ctl)
      Returns the root shell of the given control.
    • treeDefComposite

      public static TreeDef.Parented<Composite> treeDefComposite()
    • treeDefControl

      public static TreeDef.Parented<Control> treeDefControl()
    • treeDefShell

      public static TreeDef.Parented<Shell> treeDefShell()