Class GuiUtils

java.lang.Object
sc.fiji.snt.gui.GuiUtils

public class GuiUtils extends Object
Misc. utilities for SNT's GUI.
  • Constructor Details

    • GuiUtils

      public GuiUtils(Component parent)
    • GuiUtils

      public GuiUtils()
      Create a new GuiUtils instance using active (focused) window as parent.
  • Method Details

    • setParentToActiveWindow

      public void setParentToActiveWindow()
    • setParent

      public void setParent(Component parent)
    • error

      public void error(String msg)
    • error

      public void error(String msg, String title)
    • warning

      public void warning(String msg)
    • notifyIfNewVersion

      public static void notifyIfNewVersion(int msDelayBeforeShow)
    • notifyIfOldVersion

      public static void notifyIfOldVersion(int msDelayBeforeCheck)
      Checks for available SNT updates in the background by querying the Neuroanatomy update site. If the site's content is newer than the local installation, a notification is displayed suggesting the user run the Fiji updater. Respects the Fiji updater's own preferences (e.g., "never remind me", "remind me later") and fails silently on any error (network, missing updater, etc.).
      Parameters:
      msDelayBeforeCheck - delay in ms before the background check starts
    • applyRoundCorners

      public static void applyRoundCorners(JComponent component)
    • floatingMsg

      public JDialog floatingMsg(String msg, boolean autodismiss)
    • tempMsg

      public void tempMsg(String msg)
    • tempMsg

      public void tempMsg(String msg, int location)
    • isLegacy3DViewerAvailable

      public static boolean isLegacy3DViewerAvailable()
    • notify

      public static void notify(String msg)
      Displays a floating notification in the upper corner of the active screen.
      Parameters:
      msg - the message to be displayed. May include a minimal subset of Markdown for emphasis: **bold**/__bold__ and *italic*/_italic_ see GuiUtils.Text.markdownToHtml(String). Anything else, including literal '<'/'>'/'&', is treated as plain text.
      See Also:
    • notify

      public static void notify(String msg, int msDelayBeforeShow)
      Displays a floating notification in the upper corner of the active screen.
      Parameters:
      msg - the message to be displayed. May include a minimal subset of Markdown for emphasis: **bold**/__bold__ and *italic*/_italic_ see GuiUtils.Text.markdownToHtml(String). Anything else, including literal '<'/'>'/'&', is treated as plain text.
      msDelayBeforeShow - the amount (in ms) of time before notification should be displayed
    • getTimeStamp

      public static String getTimeStamp()
    • yesNoDialog

      public int yesNoDialog(String msg, String title, String yesButtonLabel, String noButtonLabel)
    • yesNoDialog

      public int yesNoDialog(String msg, String title)
    • yesNoPrompt

      public org.scijava.ui.DialogPrompt.Result yesNoPrompt(String message, String title)
    • getConfirmation

      public boolean getConfirmation(String msg, String title)
    • error

      public void error(String msg, String title, String helpURI)
    • getConfirmation

      public boolean getConfirmation(String msg, String title, String yesLabel, String noLabel)
    • getConfirmation2

      public Boolean getConfirmation2(String msg, String title, String yesLabel, String noLabel)
    • getChoice

      public String getChoice(String message, String title, String[] choices, String defaultChoice)
    • getChoiceRaceable

      public Optional<String> getChoiceRaceable(String message, String title, String[] choices, String defaultChoice, Future<?> task) throws InterruptedException
      Like getChoice(String, String, String[], String), but the choices are shown as buttons and the prompt races a background task: if it completes before the user picks a button, the dialog is dismissed automatically/ Useful for "this is taking a while, keep waiting / open as-is / abort?" prompts wrapped around slow network fetches/
      Parameters:
      message - the prompt message
      title - the dialog title
      choices - the button labels, shown in order
      defaultChoice - the button focused by default, or null for choices[0]
      task - the background task racing the prompt
      Returns:
      the label of the button the user clicked, or Optional.empty() if task completed on its own (or the dialog was closed without a button click) before the user answered
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting
    • getTwoChoices

      public String[] getTwoChoices(String title, String choice1Label, String[] choices1, String defaultChoice1, String choice2Label, String[] choices2, String defaultChoice2)
    • getChoiceWithInput

      public String[] getChoiceWithInput(String message, String title, String[] choices, String defaultChoice, String defaultInput)
    • getChoiceAndDouble

      public Object[] getChoiceAndDouble(String message, String title, String[] choices, String defaultChoice, Double defaultValue)
    • getChoice

      public String getChoice(String message, String title, String[] choices, String[] descriptions, String defaultChoice)
    • getMultipleChoices

      public List<String> getMultipleChoices(String title, String[] choices, String defaultChoice)
    • getMultipleChoicesAndChoice

      public Object[] getMultipleChoicesAndChoice(String title, String[] choices1, String defaultChoice1, boolean multipleSelectionAllowed1, String[] choices2, String defaultChoice2)
    • getPersistentConfirmation

      public boolean[] getPersistentConfirmation(String msg, String title)
    • getConfirmationAndOption

      public boolean[] getConfirmationAndOption(String msg, String title, String checkboxLabel, boolean checkboxDefault, String[] yesNoButtonLabels)
    • getConfirmationAndOption

      public boolean[] getConfirmationAndOption(String msg, String title, String checkboxLabel, boolean checkboxDefault)
    • getChoiceWithOptionAndInfo

      public Object[] getChoiceWithOptionAndInfo(String title, String message, String[] choices, String defaultChoice, String infoText, String checkboxLabel, boolean checkboxDefault)
      Displays a dialog with radio button choices, optional info text, and an optional checkbox. Useful for operations that require a selection with additional context and options.
      Parameters:
      title - the dialog title
      message - the message/prompt shown at the top (can be HTML)
      choices - array of choices to display as radio buttons
      defaultChoice - the initially selected choice (must be in choices array)
      infoText - optional informational text shown below the choices (null to hide)
      checkboxLabel - optional checkbox label (null to hide checkbox)
      checkboxDefault - default state of checkbox (ignored if checkboxLabel is null)
      Returns:
      Object array: {selectedChoice (String), checkboxSelected (Boolean)}, or null if canceled. checkboxSelected is false if checkbox was hidden.
    • getPersistentWarning

      public Boolean getPersistentWarning(String msg, String title)
    • getParent

      public Component getParent()
    • getString

      public String getString(String promptMsg, String promptTitle, String defaultValue)
    • getStrings

      public String[] getStrings(String promptTitle, String[] labels, String[] defaultValues, String optionalMsg)
    • getStrings

      public String[] getStrings(String promptTitle, Map<String,List<String>> choicesMap, String... defaultChoices)
    • getStringSet

      public Set<String> getStringSet(String promptMsg, String promptTitle, Collection<String> defaultValues)
    • getColorRGB

      public org.scijava.util.ColorRGB getColorRGB(String title, org.scijava.util.ColorRGB defaultValue, String... panes)
      Simplified color chooser for ColorRGB.
      See Also:
    • getColor

      public Color getColor(String title, Color defaultValue, String... panes)
      Simplified color chooser.
      Parameters:
      title - the title of the chooser dialog
      defaultValue - the initial color set in the chooser
      panes - the panes a list of strings specifying which tabs should be displayed. In most platforms this includes: "Swatches", "HSB" and "RGB". Note that e.g., the GTK L&F may only include the default GtkColorChooser pane. Set to null to include all available panes.
      Returns:
      the color
    • getDouble

      public Double getDouble(String promptMsg, String promptTitle, Number defaultValue)
    • getPercentage

      public Integer getPercentage(String promptMsg, String promptTitle, int defaultValue)
    • getDouble

      public Double getDouble(String promptMsg, String promptTitle, double defaultValue, double min, double max, String unit)
    • getInt

      public Integer getInt(String promptMsg, String promptTitle, int defaultValue, int min, int max)
    • getTwoNumbers

      public Number[] getTwoNumbers(String promptMsg, String promptTitle, Number[] defaultValues, String[] labels, int decimalPlaces)
    • getThreeNumbers

      public Number[] getThreeNumbers(String promptMsg, String promptTitle, Number[] defaultValues, String[] labels, int decimalPlaces)
    • getCoordinates

      public SNTPoint getCoordinates(String promptMsg, String promptTitle, SNTPoint defaultValue, int decimalPlaces, SNTPoint resetValue)
    • getCoordinatesInternal

      public SNTPoint getCoordinatesInternal(String promptMsg, String promptTitle, SNTPoint defaultValue, String[] labels, int decimalPlaces, SNTPoint resetValue)
    • getRange

      public float[] getRange(String promptMsg, String promptTitle, float[] defaultRange)
    • adjustComponentThroughPrompt

      public void adjustComponentThroughPrompt(Component container)
    • getOpenFile

      public File getOpenFile(String title, File file, String... allowedExtensions)
    • getOpenFileOrDirectory

      public File getOpenFileOrDirectory(String title, File file)
      As getOpenFile(String, File, String...), but the dialog also allows selecting a directory directly. Needed for pointing directly at an N5/OME-Zarr container; a metadata file *inside* such a container (e.g. zarr.json, .zattrs) can still be selected too. Both resolve to the same container root,

      NB: unlike most getXxxFile(s) methods above, this always uses the classic chooser and ignores SNTPrefs.getUseNativeFileChooser() -- FlatLaf's SystemFileChooser (the native alternative) does not support a mixed files-and-directories selection mode at all.

    • getSaveFile

      public File getSaveFile(String title, File file, String... allowedExtensions)
      Shows a "Save" dialog, honoring SNTPrefs.getUseNativeFileChooser(): either SNT's classic FileChooser or FlatLaf's SystemFileChooser (native OS dialog).
      Parameters:
      title - the dialog title
      file - a proposed file (name and/or directory) to pre-select, or null
      allowedExtensions - the choosable extension(s) offered in the dialog's filter; if a single extension is given, it is appended to the chosen file if the user typed a name without it
      Returns:
      the chosen File, or null if the dialog was cancelled
    • getReconstructionFiles

      public File[] getReconstructionFiles(File selectedFile)
    • getFile

      public File getFile(File file, String extensionWithoutPeriod)
    • getImageFile

      public File getImageFile(File file)
    • getReconstructionFile

      public File getReconstructionFile(File file, String extension)
    • fileChooser

      public static JFileChooser fileChooser(String title, File file, int type, int selectionMode)
    • centeredMsg

      public void centeredMsg(String msg, String title)
    • centeredMsg

      public void centeredMsg(String msg, String title, String buttonLabel)
    • dialog

      public JDialog dialog(String msg, JComponent component, String title)
    • centerOnParent

      public static void centerOnParent(Component parent, Window dialog)
    • centerOnParent

      public static void centerOnParent(Rectangle parentBounds, Window dialog)
    • setLocationRelativeTo

      public static void setLocationRelativeTo(Window target, Window reference)
      Positions a target window relative to a reference window while preventing it from being hidden behind system docks, taskbars, or menu bars across macOS, Windows, and Linux.

      Precondition: The target window must have its size initialized

      Parameters:
      target - the window to be positioned (e.g., JFrame, JDialog); cannot be null
      reference - the reference window to position relative to; if null or not visible, the target window centers on the primary screen launcher bounds
    • displayBanner

      public static void displayBanner(String msg, Color background, Component parent)
    • addTooltip

      public static void addTooltip(JComponent c, String text)
    • getWrappedText

      public static String getWrappedText(JComponent c, String text, int maxWidth)
      Wraps text in an HTML <div> capped at maxWidth, so a long line wraps onto further lines instead of being clipped or silently growing its container. No-op if text already looks like HTML.
      Parameters:
      c - the component the text will be displayed in (used only for font metrics)
      text - the text to wrap
      maxWidth - the maximum width, in pixels, of the wrapped text
      Returns:
      the (possibly) HTML-wrapped text
    • blinkingError

      public void blinkingError(JComponent blinkingComponent, String msg)
    • shortSmallMsg

      public static JLabel shortSmallMsg(String msg, boolean enabled)
    • shortSmallMsg

      public static JLabel shortSmallMsg(String msg)
    • longSmallMsg

      public static JTextArea longSmallMsg(String msg, String svgIconPath, boolean enabled, Component parent)
    • longSmallMsg

      public static JTextArea longSmallMsg(String msg, Component parent)
    • showAboutDialog

      public static JDialog showAboutDialog()
    • showDirectory

      public void showDirectory(File file)
    • toString

      public static <T> String toString(Iterable<T> iterable)
    • uiFontSize

      public static float uiFontSize()
    • uiScale

      public static double uiScale()
      Returns the effective UI scale factor for the current display environment, accounting for HiDPI displays and user-configured scaling.

      The detection strategy is:

      1. FlatLaf's UIScale.getUserScaleFactor()the most reliable source when FlatLaf is active, as it combines OS-level DPI with any user overrides
      2. AWT's GraphicsConfiguration.getDefaultTransform().getScaleX(), the platform-level fallback
      3. ij.Prefs.getGuiScale(): last resort
      Returns:
      the scale factor (1.0 = no scaling, 2.0 = 200%, etc.)
    • osScale

      public static float osScale()
      Returns the OS-level scale factor that is NOT already accounted for by Java's HiDPI awareness.

      On macOS and Windows, Java intercepts HiDPI natively: so this returns 1.0. On Linux with GDK_SCALE, the OS seems to scale everything before Java sees it: AWT/Swing stay at 1.0 scaling while uiScale() absorbs the GDK factor, so font sizes, etc. are in device pixels and must be divided by the returned value to reach FlatLaF logical units.

      Uses the default screen device; prefer osScale(Component) when a component is available so multi-monitor setups are handled correctly.
    • osScale

      public static float osScale(Component c)
      Component-aware variant of osScale(): uses the component's own GraphicsConfiguration so the correct scale is returned on multi-monitor setups where screens may have different DPI.
    • initSplashScreen

      public static void initSplashScreen()
      Registers one more caller that wants the loading splash screen open, showing it if it is not already showing. Calls nest: the splash is only actually closed once every initSplashScreen() has been matched by a closeSplashScreen(). handled here
    • setSplashMessage

      public static void setSplashMessage(String message)
      Updates the loading splash screen's status text. Does nothing if the splash screen is not currently showing
      Parameters:
      message - the new status text
    • setSplashStreamMode

      public static void setSplashStreamMode(boolean streamMode)
      Switches the loading splash screen to (or back from) its wider "SNT Stream" layout, for callers that will post longer, dynamic status messages via setSplashMessage(String). Does nothing if the splash screen is not currently showing
      Parameters:
      streamMode - true for the wide "SNT Stream" layout, false for the default one
    • closeSplashScreen

      public static void closeSplashScreen()
      Releases one registration made by initSplashScreen(); the splash only actually closes once every such registration has been released - see SplashScreen#close()
    • addSeparator

      public static void addSeparator(JComponent component, String heading, boolean vgap, GridBagConstraints c)
    • addSeparator

      public static void addSeparator(JComponent component, JLabel label, boolean vgap, GridBagConstraints c)
    • addSeparator

      public static void addSeparator(JPopupMenu menu, String header)
    • addSeparator

      public static void addSeparator(JMenu menu, String header)
    • leftAlignedLabel

      public static JLabel leftAlignedLabel(String text, boolean enabled)
    • leftAlignedLabel

      public static JLabel leftAlignedLabel(String text, String uri, boolean enabled)
    • leftAlignedLabel

      public static JLabel leftAlignedLabel(String text, String uri, Runnable action, boolean enabled)
      As leftAlignedLabel(String, String, boolean), but also (or instead of a uri) running an in-app action on click -- e.g., for a label that should trigger something within the application, such as starting a wizard/tour, rather than (or in addition to) opening an external link.
      Parameters:
      text - the label's (HTML-formatted, if applicable) text
      uri - an optional URL to be opened on click, or null
      action - an optional action to be run on click (after uri, if any, is opened), or null
      enabled - whether the label starts off enabled (a disabled label ignores clicks)
    • searchForum

      public void searchForum(String query)
    • openURL

      public static void openURL(String uri)
    • showKeyboardShortcuts

      public void showKeyboardShortcuts(InputMap[] inputMaps, ActionMap... actionMaps)
      Displays a sorted HTML table of all registered keyboard shortcuts in the specified inputMaps. Action names are title-cased for readability.
      Parameters:
      inputMaps - The InputMap(s) to be listed
      actionMaps - If null, all key binding are listed, including those with no corresponding action (e.g. disabled or inherited Swing defaults) will be listed
    • ctrlKey

      public static String ctrlKey()
    • getConsole

      public static Window getConsole()
    • showConsole

      public static void showConsole()
      Brings the (scijava) console pane to front
    • setRenderingHints

      public static void setRenderingHints(Graphics2D g2)
    • defaultGbc

      public static GridBagConstraints defaultGbc()
    • removeIcon

      public static void removeIcon(Object rootPaneContainerOrWindow)
    • enableComponents

      public static void enableComponents(Container container, boolean enable)
    • ensureSameWidth

      public static void ensureSameWidth(JComponent... components)
      Resizes components so that they all share the same preferred/minimum width (the largest among them), while each component keeps its own height. Useful to align icon-driven controls (e.g., dropdown/options buttons) that live in unrelated panels but whose glyph-dependent widths would otherwise differ slightly
      Parameters:
      components - the components to resize; needs at least two to have an effect
    • micrometer

      public static String micrometer()
    • scaledMicrometer

      public static String scaledMicrometer(double umLength, int digits)
      Returns a more human-readable representation of a length in micrometers.

      E.g., scaledMicrometer(0.01,1) returns "1.0nm"

      Parameters:
      umLength - the length in micrometers
      digits - the number of output decimals
      Returns:
      the scaled unit
    • errorPrompt

      public static void errorPrompt(String msg)
    • errorPrompt

      public static void errorPrompt(String msg, boolean centeredOnScreen)
    • friendlyErrorMessage

      public static String friendlyErrorMessage(Throwable t)
      Rewrites a common network-connectivity exception (unknown host, refused/timed-out connection) into a short, user-facing message, instead of the exception's own message, which for e.g. UnknownHostException is just the bare, unexplained hostname. Falls back to t.getMessage() for anything else.
      Parameters:
      t - the exception to inspect (its cause chain is also walked)
      Returns:
      a friendly message describing t
    • getDnDFileChooser

      public static JFileChooser getDnDFileChooser()
    • setAutoDismiss

      public static void setAutoDismiss(JDialog dialog)
    • tile

      public static void tile(List<? extends Window> windowList, boolean resize)
    • showHTMLDialog

      public static void showHTMLDialog(String msg, String title)
    • showHTMLDialog

      public JDialog showHTMLDialog(String msg, String title, boolean modal)
    • yesNoHTMLDialog

      public boolean yesNoHTMLDialog(String htmlMsg, String title, String yesLabel, String noLabel)
    • combineSNTChartPrompt

      public void combineSNTChartPrompt()
    • drawDragAndDropPlaceHolder

      public static void drawDragAndDropPlaceHolder(Component component, Graphics2D g)
    • getTabbedPane

      public static JTabbedPane getTabbedPane()
    • centerWindow

      public static void centerWindow(Window dialogToCenter, Window... windows)
    • addCloseShortcut

      public static void addCloseShortcut(JFrame frame)
    • closeAllPlots

      public static void closeAllPlots()