Class FxUtilities

java.lang.Object
org.tentackle.fx.FxUtilities

@Service(FxUtilities.class) public class FxUtilities extends Object
Utility methods for Fx.
Author:
harald
  • Constructor Details

    • FxUtilities

      public FxUtilities()
      Creates the FX utilities.
  • Method Details

    • getInstance

      public static FxUtilities getInstance()
      The singleton.
      Returns:
      the singleton
    • isDarkMode

      public boolean isDarkMode()
      Returns whether to enable dark mode.
      Returns:
      true to enable dark mode
    • setDarkMode

      public void setDarkMode(boolean darkMode)
      Enables dark mode.
      Parameters:
      darkMode - true to enable dark theme, false for light theme (default)
    • applyStylesheets

      public void applyStylesheets(javafx.scene.Scene scene)
      Applies the default stylesheets to a scene.
      Invoked for all newly created scenes.
      Parameters:
      scene - the scene
    • getDefaultCursor

      public javafx.scene.Cursor getDefaultCursor(javafx.scene.Node node)
      Gets the default cursor for the given node.
      Parameters:
      node - the node
      Returns:
      the default cursor, never null
    • getWaitCursor

      public javafx.scene.Cursor getWaitCursor(javafx.scene.Node node)
      Gets the wait cursor for the given node.
      Parameters:
      node - the node
      Returns:
      the wait cursor, never null
    • isLenientMoneyInput

      public boolean isLenientMoneyInput(FxTextComponent component)
      Returns whether decimal separator should be inserted automatically if missing in money input.
      This is usually a global setting for the whole application, but it can be filtered according to the component (parent component, etc...).
      Parameters:
      component - the input component
      Returns:
      true if insert separator according to scale if missing
    • getDefaultTextAreaColumns

      public int getDefaultTextAreaColumns(FxTextArea textArea)
      Gets the default column width for text areas if not set by the binding or the model's COLS= option.
      Parameters:
      textArea - the text area component
      Returns:
      the column width (default is 30)
    • getPrefixSelectionTimeout

      public long getPrefixSelectionTimeout()
      Gets the timeout for the prefix selection in dropdown lists such as in combo boxes.
      Returns:
      the timeout in milliseconds (default is 750)
    • setHelpURL

      public void setHelpURL(String helpURL)
      Sets the help URL prefix.
      Parameters:
      helpURL - the URL prefix
    • getHelpURL

      public String getHelpURL()
      Gets the help URL prefix.
      Returns:
      the URL prefix
    • showHelp

      public void showHelp(Object owner)
      Opens the online help for a given component.
      Parameters:
      owner - the owner window or node
      See Also:
    • terminate

      public void terminate()
      Terminates the FX client.
      The default implementation invokes Platform.exit().
    • browse

      public void browse(URI uri)
      Launches the default browser to display a URI.
      Throws FxRuntimeException if failed.
      Parameters:
      uri - the URI to be displayed in the user default browser
    • edit

      public void edit(Object owner, File file)
      Edits/opens a given file via the Desktop utilities.
      The corresponding application will be chosen according to the mime/file type.
      Parameters:
      owner - the owner window or node
      file - the file
    • getStage

      public javafx.stage.Stage getStage(javafx.scene.Node node)
      Gets the stage for a node.
      Parameters:
      node - the node
      Returns:
      the stage, null if node does not belong to a scene or scene does not belong to a stage.
    • getWindow

      public javafx.stage.Window getWindow(Object owner)
      Gets the window of an owner object.
      The owner can be a window, a scene or a node.
      Throws FxRuntimeException if the window cannot be determined.
      Parameters:
      owner - the owner object
      Returns:
      the window
    • isModal

      public boolean isModal(javafx.stage.Stage stage)
      Returns whether the stage is modal.
      Parameters:
      stage - the stage
      Returns:
      true if effectively modal
    • closeStageHierarchy

      public void closeStageHierarchy(javafx.scene.Node node, javafx.stage.Stage stopStage)
      Closes the hierarchy of stages from the given node stopping at given stage.
      Parameters:
      node - the node
      stopStage - the stop stage, null to close the whole application
    • dumpComponentHierarchy

      public String dumpComponentHierarchy(javafx.scene.Node node)
      Dumps the component hierarchy.
      Parameters:
      node - the node
      Returns:
      the formatted string
    • runAndWait

      public void runAndWait(Runnable runnable)
      Runs a command on the FX application thread and waits for its execution to finish.
      Parameters:
      runnable - the Runnable whose run method will be executed on the FX application thread
      Throws:
      FxRuntimeException - if the calling thread is the FX application thread
      See Also:
      • Platform.runLater(java.lang.Runnable)
    • run

      public void run(Object owner, Runnable runnable)
      Runs a runnable in a separate thread.
      Avoids blocking the FX-application thread.
      If the execution fails, an error dialog is shown.
      Parameters:
      owner - the owner window or node
      runnable - the runnable to execute in background
    • showNotification

      public void showNotification(Object owner, javafx.stage.Popup popup, javafx.scene.Parent notification, Runnable onClose)
      Shows a notification popup.
      The notification is centered above the owner window.
      Parameters:
      owner - the owner window or node
      popup - the popup
      notification - the notification node
      onClose - the optional runner to execute when notification is closed/hidden
    • showInfoDialog

      public void showInfoDialog(Object owner, String message, Runnable onClose)
      Shows an info dialog.
      Parameters:
      owner - the owner window or node
      message - the message
      onClose - optional runnable invoked when dialog is closed
    • showWarningDialog

      public void showWarningDialog(Object owner, String message, Runnable onClose)
      Shows a warning dialog.
      Parameters:
      owner - the owner window or node
      message - the message
      onClose - optional runnable invoked when dialog is closed
    • showQuestionDialog

      public void showQuestionDialog(Object owner, String message, boolean defaultYes, Consumer<Boolean> answer)
      Shows a question dialog.
      To avoid blocking the event queue (see Dialog.showAndWait()), the implementation is not based on Alert and the method returns immediately. The result is then provided via a Consumer.
      As a convenience, typing the key "0" is interpreted as "no" and "1" as "yes".

      Note: if you need to wait for a decision of the user (i.e. no async use), you can do that from another than the FX-thread by using a BlockingHolder like so:

            BlockingHolder<Boolean> answer = new BlockingHolder<>();
            Platform.runLater(() -> Fx.question(..., answer);
            if (answer.get()) {
              ...
            }
            else {
              ...
            }
       
      Parameters:
      owner - the owner window or node
      message - the message
      defaultYes - true if yes is the default button
      answer - the user's answer with Boolean.TRUE for YES or Boolean.FALSE for NO, never null
    • showErrorDialog

      public void showErrorDialog(Object owner, String message, Throwable t, Runnable onClose)
      Shows an error dialog.
      Parameters:
      owner - the owner window or node
      message - the message
      t - optional throwable
      onClose - optional runnable invoked when dialog is closed
    • showErrorPopup

      public javafx.stage.PopupWindow showErrorPopup(ErrorPopupSupported component)
      Shows an error popup for a component.
      Parameters:
      component - the component
      Returns:
      the popup, null if no errormessage in component
    • showInfoPopup

      public javafx.stage.PopupWindow showInfoPopup(InfoPopupSupported component)
      Shows an info popup for a component.
      Parameters:
      component - the component
      Returns:
      the popup, null if no info-message in component
    • createInteractiveError

      public InteractiveError createInteractiveError(ValidationResult validationResult, NavigableSet<ValidationMapper> validationMappers, Binder binder)
      Creates an interactive error from a validation result.
      Parameters:
      validationResult - the validation result
      validationMappers - the validation mappers
      binder - the binder
      Returns:
      the interactive error
    • createInteractiveErrors

      public List<InteractiveError> createInteractiveErrors(List<ValidationResult> validationResults, NavigableSet<ValidationMapper> validationMappers, Binder binder)
      Creates interactive errors from validation results.
      Parameters:
      validationResults - the validation results
      validationMappers - the validation mappers
      binder - the binder
      Returns:
      the interactive errors
    • showValidationResults

      public boolean showValidationResults(Object owner, ValidationFailedException ex, NavigableSet<ValidationMapper> validationMappers, Binder binder)
      Shows the validation warnings and errors in a dialog and marks the FxControls related to those errors.
      Errors and warnings are shown in error or info dialogs.
      Parameters:
      owner - the owner window or node
      ex - the validation exception
      validationMappers - the validation mappers
      binder - the binder
      Returns:
      true if there were errors, false if warnings only
    • setupFocusHandling

      public void setupFocusHandling(javafx.scene.control.Control control)
      Adds focus handling to sync with model.
      Parameters:
      control - the fx control
    • focusGained

      public void focusGained(FxComponent component)
      Performs all necessary operations on an FxComponent when it gained the focus.
      Parameters:
      component - the fx component
    • focusLost

      public void focusLost(FxComponent component)
      Performs all necessary operations on an FxComponent when it lost the focus.
      Parameters:
      component - the fx component
    • remapKeys

      public void remapKeys(javafx.scene.control.Control control)
      Remaps certain keys.
      Parameters:
      control - the fx control
    • filterKeys

      public void filterKeys(javafx.scene.control.Control control)
      Filters certain keys for special features.
      Parameters:
      control - the fx control
    • registerWindowEventFilters

      public void registerWindowEventFilters(javafx.stage.Window window)
      Registers event filters for windows.
      Parameters:
      window - the window
    • expandAll

      public <T> void expandAll(javafx.scene.control.TreeItem<T> treeItem)
      Recursively expands all tree items while checking for recursion loops.
      The default implementation in TreeViewBehavior and TreeTableViewBehavior is not only in a private API, but cannot be accessed from the applications at all. The default implementation is dangerous since it does not check for recursion loops. We replace it by mapping the event to this utility method.
      Type Parameters:
      T - the item's value type
      Parameters:
      treeItem - the item to start expansion
    • isValueInParentPath

      public <T> boolean isValueInParentPath(javafx.scene.control.TreeItem<T> treeItem)
      Checks whether one of the parents already contains the item's value.
      Type Parameters:
      T - the value's type
      Parameters:
      treeItem - the tree item to check
      Returns:
      true if value is in path
    • collapseAll

      public <T> void collapseAll(javafx.scene.control.TreeItem<T> treeItem)
      Recursively collapses all tree items.
      Type Parameters:
      T - the item's value type
      Parameters:
      treeItem - the item to start
    • collapseAll

      public <T> void collapseAll(Collection<javafx.scene.control.TreeItem<T>> treeItems)
      Recursively collapses all tree items.
      Type Parameters:
      T - the item's value type
      Parameters:
      treeItems - the list of items to collapse
      See Also:
    • print

      public void print(javafx.scene.Node node)
      Prints a node.
      The user selects the printer, and the node is scaled down if too large for the paper.
      Parameters:
      node - the node to print
    • getBindingOption

      public String getBindingOption(String options, String key)
      Gets a binding option from an options string.
      Parameters:
      options - the binding options string
      key - the option key
      Returns:
      the option, null if no such option
    • applyBindingOptions

      public void applyBindingOptions(FxTextComponent comp, BindingMember member, String options)
      Applies the bindable options to a text component.
      Parameters:
      comp - the component
      member - the binding member
      options - the options from the @Bindable annotation
    • applyBindingOptions

      public void applyBindingOptions(TableColumnConfiguration columnConfiguration, BindingMember member, String options)
      Applies the bindable options to a table column.
      Parameters:
      columnConfiguration - the column configuration
      member - the binding member
      options - the options from the @Bindable annotation
    • applyBindingOption

      protected boolean applyBindingOption(FxTextComponent comp, BindingMember member, String option)
      Applies a single binding option to a text component.
      Parameters:
      comp - the component
      member - the binding member
      option - the option
      Returns:
      true if option known and applied, false if unknown option
    • applyBindingOption

      protected boolean applyBindingOption(TableColumnConfiguration columnConfiguration, BindingMember member, String option)
      Processes an option for a table binding.
      Parameters:
      columnConfiguration - the column config
      member - the binding member
      option - the option
      Returns:
      true if option known and processed, false if unknown option
    • resizeColumnsToFitContent

      public void resizeColumnsToFitContent(javafx.scene.control.TableView<?> table)
      Resizes the width of all columns of a table to fit the displayed content.
      Unlike the "double-click on column separator"-feature this method computes the column widths according to the content currently displayed. For tables with a large number of rows this is much more efficient than scanning the whole table.
      Parameters:
      table - the table view
    • resizeColumnsToFitContent

      public void resizeColumnsToFitContent(javafx.scene.control.TreeTableView<?> table)
      Resizes the width of all columns of a tree table to fit the displayed content.
      Unlike the "double-click on column separator"-feature this method computes the column widths according to the content currently displayed. For tables with a large number of rows this is much more efficient than scanning the whole table.
      Parameters:
      table - the tree table view
    • getAllShowingStages

      public javafx.collections.ObservableList<javafx.stage.Stage> getAllShowingStages()
      Delivers a lis of all showing stages.
      Returns:
      the stages
    • determineCenteredLocation

      public javafx.geometry.Point2D determineCenteredLocation(javafx.stage.Window window)
      Calculates the location of a window so that it will be centered on the screen.
      Parameters:
      window - the window
      Returns:
      the location (top left corner)
    • determinePreferredStageLocation

      public javafx.geometry.Point2D determinePreferredStageLocation(javafx.stage.Stage stage)
      Calculates the position of a stage on the screen so that it is being display in an optimal manner.
      Parameters:
      stage - the stage to be positioned on the screen
      Returns:
      the location
    • determineAlignedStageLocation

      public javafx.geometry.Point2D determineAlignedStageLocation(javafx.stage.Stage stage, javafx.geometry.Point2D location)
      Calculates the location of a stage so that it is completely visible on the screen, using a "free" spot.
      Parameters:
      stage - the current stage
      location - the desired (not necessarily current!) location
      Returns:
      the aligned location
    • getVirtualFlow

      public javafx.scene.control.skin.VirtualFlow getVirtualFlow(javafx.scene.control.Skinnable control)
      Gets the virtual flow for a given control.
      Parameters:
      control - the control
      Returns:
      the virtual flow, null if no virtual flow associated to the control
    • computeScrollToCentered

      public int computeScrollToCentered(javafx.scene.control.Skinnable control, int row, int numRow)
      Computes the row number to scrollTo in order to position the given row in the middle of the viewport.
      Parameters:
      control - the control, usually a table- or tree-view
      row - the row to position in the middle of the viewport, if possible
      numRow - the total number of rows
      Returns:
      the row number to pass to the scrollTo method
    • getCsvSeparator

      public String getCsvSeparator()
      Gets the separator string for CSV exports.
      Returns:
      the separator, default is ";"