Class SWTUtilities

java.lang.Object
org.knime.core.ui.util.SWTUtilities

public class SWTUtilities extends Object
I did as best a search as i could to find a pre-existing class of this ilk (something that provides SWT related helper methods and could find none.) Also, i named this SWTUtilities instead of SWTUtil, the latter which better obey the majority of the utility class names in this package, because there is already an internal Eclipse class of that name. It seems plausible that there is this sort of collection of methods elsewhere; if there isn't, consider moving the graphics-related static utility methods in org.knime.workbench.editor2.editparts.AnnotationEditPart into this class as well.
Since:
3.7
Author:
loki der quaeler
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.eclipse.swt.widgets.Shell
    As discovered in AP-10122, when KNIME receives an action due to mouse event passthrough (a situation possible on macOS where mouse & scroll events can pass to a desktop-visible window even though the owning application is not the application in the foreground), Display.getCurrent().getActiveShell() returns null.
    static org.eclipse.swt.widgets.Shell
    getActiveShell(org.eclipse.swt.widgets.Display display)
    As discovered in AP-10122, when KNIME receives an action due to mouse event passthrough (a situation possible on macOS where mouse & scroll events can pass to a desktop-visible window even though the owning application is not the application in the foreground), Display.getCurrent().getActiveShell() returns null.
    static org.eclipse.swt.widgets.Shell
    This method can be called after application startup has completed in order to get the Shell that is the parent of the main KAP workbench.
    static Optional<org.eclipse.swt.graphics.Font>
    loadFontFromInputStream(InputStream is, int size, int style)
    This loads a font via our main workbench Display.
    static Optional<org.eclipse.swt.widgets.Shell>
    This method is invoked from KNIMEApplicationWorkbenchAdvisor#postStartup().
    static void
    removeAllChildren(org.eclipse.swt.widgets.Composite parent)
    Oh com'on SWT - seriously: why isn't this a method in Composite?
    static void
    spaceReclaimingSetVisible(org.eclipse.swt.widgets.Control widget, boolean visible)
    I'm not sure why the SWT authors thought implementing 'setVisible(false)' should mean we space should continue to be laid out but just not painted, like the worlds most obvious invisibility cloak...

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SWTUtilities

      public SWTUtilities()
  • Method Details

    • markKNIMEShell

      public static Optional<org.eclipse.swt.widgets.Shell> markKNIMEShell()
      This method is invoked from KNIMEApplicationWorkbenchAdvisor#postStartup(). There's a fair philosophical argument of "if in some edge cases, the shell at this point is null and then we need rely on matching its name to the product, why not always just do that search, as opposed to doing the data-marking here and searching on the data-marking later." My response would be that even if we're forced to walk the shells at this point (thank you very much, SWT) as we've just launched, the chances that some other random shell starting with the product name is also present is much lower (like zero) now than at a later time during the application's lifespan.
      Returns:
      The shell identified as the KNIME shell (window)
    • getKNIMEWorkbenchShell

      public static org.eclipse.swt.widgets.Shell getKNIMEWorkbenchShell()
      This method can be called after application startup has completed in order to get the Shell that is the parent of the main KAP workbench.
      Returns:
      the Shell containing the main KAP workbench, or null if we failed to mark one at start-up.
    • getActiveShell

      public static org.eclipse.swt.widgets.Shell getActiveShell(org.eclipse.swt.widgets.Display display)
      As discovered in AP-10122, when KNIME receives an action due to mouse event passthrough (a situation possible on macOS where mouse & scroll events can pass to a desktop-visible window even though the owning application is not the application in the foreground), Display.getCurrent().getActiveShell() returns null. This method will attempt to return the applicable Shell instance, by hook or by crook. If you're considering calling this, perhaps you really want to call getKNIMEWorkbenchShell().
      Parameters:
      display - an instance of Display which contains the active shell; if this is null, this method will return null
      Returns:
      a Shell instance which is the active shell for the application
    • getActiveShell

      public static org.eclipse.swt.widgets.Shell getActiveShell()
      As discovered in AP-10122, when KNIME receives an action due to mouse event passthrough (a situation possible on macOS where mouse & scroll events can pass to a desktop-visible window even though the owning application is not the application in the foreground), Display.getCurrent().getActiveShell() returns null. This method will attempt to return the applicable Shell instance, by hook or by crook.
      Returns:
      a Shell instance which is the active shell for the application
    • removeAllChildren

      public static void removeAllChildren(org.eclipse.swt.widgets.Composite parent)
      Oh com'on SWT - seriously: why isn't this a method in Composite?
      Parameters:
      parent - the Composite instance which will have all of its children removed.
      Since:
      4.0
    • spaceReclaimingSetVisible

      public static void spaceReclaimingSetVisible(org.eclipse.swt.widgets.Control widget, boolean visible)
      I'm not sure why the SWT authors thought implementing 'setVisible(false)' should mean we space should continue to be laid out but just not painted, like the worlds most obvious invisibility cloak... but they did - bless'em.
      Parameters:
      widget - the widget to make visible or hidden; the widget's layout data must be GridData or else this method will do nothing more than invoke Control.setVisible(boolean)
      visible - true to make visible, false to make hidden
      Since:
      4.0
    • loadFontFromInputStream

      public static Optional<org.eclipse.swt.graphics.Font> loadFontFromInputStream(InputStream is, int size, int style)
      This loads a font via our main workbench Display. The SWT method only takes a file path so we need write this font to a temporary file first (and we take an InputStream as we will be getting this asset usually out of bundle.) The SWT authors swear that once loaded into a given Display, it can be fetched from that Display instance, but this is not what testing bears out; we can still create an instance of Font referencing the correct name. We should cache this loading in the future.
      Parameters:
      is - a stream for a TTF; this will be closed by this method
      size - the desired point size of the font
      style - e.g SWT.BOLD
      Returns:
      an instance of Optional, populated with an instance of Font if the load is successful; remember to dispose() when appropriate
      Since:
      4.0