Package org.freeplane.plugin.script

# Class GroovyStaticImports

java.lang.Object
org.freeplane.plugin.script.GroovyStaticImports

public class GroovyStaticImports extends Object
Provides static imports for Freeplane scripting utilities to enable easy access to common functionality when compiling Groovy source code outside of Freeplane's script environment.

This class is particularly useful when creating utility scripts or add-on classes that need to be compiled as JAR files. In regular Freeplane scripts, these utilities are available as global variables, but when compiling outside Freeplane, you need to import them explicitly.

Usage:

     import static org.freeplane.plugin.script.GroovyStaticImports.*

     // Now you can use:
     logger.info("Hello from my utility script")
     ui.informationMessage("This is a message")
     String plainText = htmlUtils.htmlToPlain("<b>Bold text</b>")

The following utilities are made available through static imports:

- `logger` - for logging messages (see `LogUtils`)
- `ui` - for GUI operations and dialogs (see `UITools`)
- `htmlUtils` - for HTML/XML processing (see `HtmlUtils`)
- `textUtils` - for text processing and translations (see `TextUtils`)
- `menuUtils` - for menu operations (see `MenuUtils`)
- `scriptUtils` - for script-specific utilities (see `ScriptUtils`)
- `config` - for accessing Freeplane configuration (see `FreeplaneScriptBaseClass.ConfigProperties`)

Additionally, this class provides utility methods for common operations like null checking, number rounding, text parsing, and formatting.

Since:
  1.12.x (created on 30 Jan 2024)
See Also:
  - `FreeplaneScriptBaseClass`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final FreeplaneScriptBaseClass.ConfigProperties`
  `config`
  Accessor for Freeplane's configuration properties.
  `static final HtmlUtils`
  `htmlUtils`
  Utilities for HTML/XML processing including conversion between HTML and plain text.
  `static final LogUtils`
  `logger`
  Utilities for logging messages to Freeplane's log file.
  `static final MenuUtils`
  `menuUtils`
  Utilities for menu operations and menu item execution.
  `static final ScriptUtils`
  `scriptUtils`
  Utilities for script-specific operations, particularly useful in utility scripts and add-ons.
  `static final TextUtils`
  `textUtils`
  Utilities for text processing, translations, formatting, and string operations.
  `static final UITools`
  `ui`
  Utilities for GUI operations including dialogs, message boxes, and UI components access.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GroovyStaticImports()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Object`
  `format(Object object)`
  Applies default date-time format for dates or default number format for numbers.
  `static Object`
  `format(Object object, String formatString)`
  uses formatString to return a FormattedObject.
  `static Object`
  `formatDate(Date date)`
  Applies default date format (instead of standard date-time) format on the given date.
  `static Object`
  `ifNull(Object value, Object valueIfNull)`
  returns valueIfNull if value is null and value otherwise.
  `static <T> T`
  `ignoreCycles(Supplier<T> closure)`
  Executes the given closure while ignoring any cyclic dependencies in formulas.
  `static Object`
  `parse(String text)`
  parses text to the proper data type, if possible, setting format to the standard.
  `static Long`
  `round(Double d)`
  rounds a number to integral type.
  `static Double`
  `round(Double d, int precision)`
  round to the given number of decimal places: `round(0.1234, 2) → 0.12`
  `static String`
  `toString(Object o)`
  formats according to the internal standard, that is the conversion will be reversible for types that are handled special by the scripting api namely Dates and Numbers.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### logger

    public static final LogUtils logger
    Utilities for logging messages to Freeplane's log file. Use for debugging and error reporting.
    See Also:
      - `LogUtils`

  - ### ui

    public static final UITools ui
    Utilities for GUI operations including dialogs, message boxes, and UI components access.
    See Also:
      - `UITools`

  - ### htmlUtils

    public static final HtmlUtils htmlUtils
    Utilities for HTML/XML processing including conversion between HTML and plain text.
    See Also:
      - `HtmlUtils`

  - ### textUtils

    public static final TextUtils textUtils
    Utilities for text processing, translations, formatting, and string operations.
    See Also:
      - `TextUtils`

  - ### menuUtils

    public static final MenuUtils menuUtils
    Utilities for menu operations and menu item execution.
    See Also:
      - `MenuUtils`

  - ### scriptUtils

    public static final ScriptUtils scriptUtils
    Utilities for script-specific operations, particularly useful in utility scripts and add-ons.
    See Also:
      - `ScriptUtils`

  - ### config

    public static final FreeplaneScriptBaseClass.ConfigProperties config
    Accessor for Freeplane's configuration properties. Provides access to all configuration settings.

    Note: In utility scripts and add-on classes, this static instance is the recommended way to access configuration, as the global `config` variable is not available when compiling outside Freeplane.

- ## Constructor Details

  - ### GroovyStaticImports

    public GroovyStaticImports()

- ## Method Details

  - ### ignoreCycles

    public static <T> T ignoreCycles(Supplier<T> closure)
    Executes the given closure while ignoring any cyclic dependencies in formulas. If there are cyclic dependencies, formulas are skipped without warnings or exceptions.
    Type Parameters:
      `T` - the return type of the closure
    Parameters:
      `closure` - the operation to execute
    Returns:
      the result of the closure execution

  - ### ifNull

    public static Object ifNull(Object value, Object valueIfNull)
    returns valueIfNull if value is null and value otherwise.

  - ### round

    public static Long round(Double d)
    rounds a number to integral type.

  - ### round

    public static Double round(Double d, int precision)
    round to the given number of decimal places: `round(0.1234, 2) → 0.12`

  - ### parse

    public static Object parse(String text)
    parses text to the proper data type, if possible, setting format to the standard. Parsing is configured via config file scanner.xml

         assert parse('2012-11-30') instanceof Date
         assert parse('1.22') instanceof Number
         // if parsing fails the original string is returned
         assert parse('2012XX11-30') == '2012XX11-30'

         def d = parse('2012-10-30')
         c.statusInfo = "${d} is ${new Date() - d} days ago"

  - ### format

    public static Object format(Object object, String formatString)
    uses formatString to return a FormattedObject.

    Note: If you want to format the node core better use the format node attribute instead:

         node.object = new Date()
         node.format = 'dd/MM/yy'

    Returns:
      invalid reference

          IFormattedObject

      if object is formattable and the unchanged object otherwise.

  - ### format

    public static Object format(Object object)
    Applies default date-time format for dates or default number format for numbers. All other objects are left unchanged.
    Returns:
      invalid reference

          IFormattedObject

      if object is formattable and the unchanged object otherwise.

  - ### formatDate

    public static Object formatDate(Date date)
    Applies default date format (instead of standard date-time) format on the given date.
    Returns:
      invalid reference

          IFormattedObject

      if object is formattable and the unchanged object otherwise.

  - ### toString

    public static String toString(Object o)
    formats according to the internal standard, that is the conversion will be reversible for types that are handled special by the scripting api namely Dates and Numbers.
    See Also:
      - `Convertible.toString(Object)`