Class FxUtilities


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

      Constructors 
      Constructor Description
      FxUtilities()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addStyleSheet​(java.lang.String url)
      Adds a stylesheet for the whole application
      void addStyleSheets()
      Configures the application-wide CSS.
      protected boolean applyBindingOption​(FxTextComponent comp, org.tentackle.bind.BindingMember member, java.lang.String option)
      Applies a single binding option to a text component.
      protected boolean applyBindingOption​(TableColumnConfiguration columnConfiguration, org.tentackle.bind.BindingMember member, java.lang.String option)
      Processes an option for a table binding.
      void applyBindingOptions​(FxTextComponent comp, org.tentackle.bind.BindingMember member, java.lang.String options)
      Applies the bindable options to a text component.
      void applyBindingOptions​(TableColumnConfiguration columnConfiguration, org.tentackle.bind.BindingMember member, java.lang.String options)
      Applies the bindable options to a table column.
      void closeStageHierarchy​(javafx.scene.Node node, javafx.stage.Stage stopStage)
      Closes the hierarchy of stages from the given node stopping at given stage.
      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.
      javafx.geometry.Point2D determineCenteredLocation​(javafx.stage.Window window)
      Calculates the location of a window so that it will be centered on the screen.
      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.
      java.lang.String dumpComponentHierarchy​(javafx.scene.Node node)
      Dumps the component hierarchy.
      void filterKeys​(javafx.scene.control.Control control)
      Filters certain keys for special features.
      void focusGained​(FxComponent component)
      Performs all necessary operations on an FxComponent when it gained the focus.
      void focusLost​(FxComponent component)
      Performs all necessary operations on an FxComponent when it lost the focus.
      javafx.collections.ObservableList<javafx.stage.Stage> getAllShowingStages()
      Delivers a lis of all showing stages.
      java.lang.String getBindingOption​(java.lang.String options, java.lang.String key)
      Gets a binding option from an options string.
      int getDefaultTextAreaColumns​(FxTextArea textArea)
      Gets the default column width for text areas if not set by the binding or the model's COLS= option.
      static FxUtilities getInstance()
      The singleton.
      long getPrefixSelectionTimeout()
      Gets the timeout for the prefix selection in dropdown lists such as in comboboxes.
      javafx.stage.Stage getStage​(javafx.scene.Node node)
      Gets the stage for a node.
      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...).
      boolean isModal​(javafx.stage.Stage stage)
      Returns whether the stage is modal.
      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.
      void registerWindowEventFilters​(javafx.stage.Window window)
      Registers event filters for windows.
      void remapKeys​(javafx.scene.control.Control control)
      Remaps certain keys.
      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.
      void runAndWait​(java.lang.Runnable runnable)
      Runs a command on the FX application thread and waits for its execution to finish.
      protected void setAlertMessage​(javafx.scene.control.Alert alert, java.lang.String message)
      Workaround for the bug that messages are not completely displayed if text is wrapped.
      void setupFocusHandling​(javafx.scene.control.Control control)
      Adds focus handling to sync with model.
      javafx.scene.control.Alert showErrorDialog​(java.lang.String message, java.lang.Throwable t, java.lang.String title)
      Shows an error dialog.
      javafx.stage.PopupWindow showErrorPopup​(ErrorPopupSupported component)
      Shows an error popup for a component.
      void showHelp​(FxControl control)
      Opens the online help for a given component.
      javafx.scene.control.Alert showInfoDialog​(java.lang.String message, java.lang.String title)
      Shows an info dialog.
      javafx.stage.PopupWindow showInfoPopup​(InfoPopupSupported component)
      Shows an info popup for a component.
      javafx.scene.control.Alert showQuestionDialog​(java.lang.String message, boolean defaultYes, java.lang.String title, java.util.function.Consumer<java.lang.Boolean> answer)
      Shows a question dialog.
      To avoid showAndWait, the method returns void and the result is provided via a Consumer.
      javafx.scene.control.Alert showWarningDialog​(java.lang.String message, java.lang.String title)
      Shows a warning dialog.
      • Methods inherited from class java.lang.Object

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

      • FxUtilities

        public FxUtilities()
    • Method Detail

      • getInstance

        public static FxUtilities getInstance()
        The singleton.
        Returns:
        the singleton
      • 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 comboboxes.
        Returns:
        the timeout in milliseconds (default is 500)
      • showHelp

        public void showHelp​(FxControl control)
        Opens the online help for a given component.
        Parameters:
        control - the Fx control
      • 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.
      • 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 java.lang.String dumpComponentHierarchy​(javafx.scene.Node node)
        Dumps the component hierarchy.
        Parameters:
        node - the node
        Returns:
        the formatted string
      • runAndWait

        public void runAndWait​(java.lang.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)
      • setAlertMessage

        protected void setAlertMessage​(javafx.scene.control.Alert alert,
                                       java.lang.String message)
        Workaround for the bug that messages are not completely displayed if text is wrapped.
        Parameters:
        alert - the alert dialog
        message - the text message
      • showInfoDialog

        public javafx.scene.control.Alert showInfoDialog​(java.lang.String message,
                                                         java.lang.String title)
        Shows an info dialog.
        Parameters:
        message - the message
        title - optional title
        Returns:
        the alert dialog
      • showWarningDialog

        public javafx.scene.control.Alert showWarningDialog​(java.lang.String message,
                                                            java.lang.String title)
        Shows a warning dialog.
        Parameters:
        message - the message
        title - optional title
        Returns:
        the alert dialog
      • showQuestionDialog

        public javafx.scene.control.Alert showQuestionDialog​(java.lang.String message,
                                                             boolean defaultYes,
                                                             java.lang.String title,
                                                             java.util.function.Consumer<java.lang.Boolean> answer)
        Shows a question dialog.
        To avoid showAndWait, the method returns void and the result is provided via a Consumer.
        Parameters:
        message - the message
        defaultYes - true if yes is the default button
        title - optional title
        answer - the user's answer (invoked with Boolean.TRUE or Boolean.FALSE, never null)
        Returns:
        the alert dialog
        See Also:
        Consumer, Holder
      • showErrorDialog

        public javafx.scene.control.Alert showErrorDialog​(java.lang.String message,
                                                          java.lang.Throwable t,
                                                          java.lang.String title)
        Shows an error dialog.
        Parameters:
        message - the message
        t - optional throwable
        title - optional title
        Returns:
        the alert dialog
      • 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 infomessage in component
      • 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
      • 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
      • addStyleSheet

        public void addStyleSheet​(java.lang.String url)
        Adds a stylesheet for the whole application
        Parameters:
        url - The file URL, either relative or absolute, as a String
      • addStyleSheets

        public void addStyleSheets()
        Configures the application-wide CSS.
      • getBindingOption

        public java.lang.String getBindingOption​(java.lang.String options,
                                                 java.lang.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,
                                        org.tentackle.bind.BindingMember member,
                                        java.lang.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,
                                        org.tentackle.bind.BindingMember member,
                                        java.lang.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,
                                             org.tentackle.bind.BindingMember member,
                                             java.lang.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,
                                             org.tentackle.bind.BindingMember member,
                                             java.lang.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
      • 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