# Class Util

java.lang.Object
com.lightdev.app.shtm.Util

public class Util extends Object
Utility methods for application SimplyHTML.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`ANCHOR_SEPARATOR`
`static final String`
`CLASS_EXT`
`static final String`
`CLASS_SEPARATOR`
`static final char`
`CLASS_SEPARATOR_CHAR`
`static final String`
`CSS_ATTRIBUTE_ALIGN_CENTER`
`static final String`
`CSS_ATTRIBUTE_ALIGN_LEFT`
`static final String`
`CSS_ATTRIBUTE_ALIGN_RIGHT`
`static final String`
`CSS_ATTRIBUTE_LINE_THROUGH`
`static final String`
`CSS_ATTRIBUTE_NONE`
`static final String`
`CSS_ATTRIBUTE_NORMAL`
`static final String`
`CSS_ATTRIBUTE_UNDERLINE`
`static final String`
`DIR_UP_INDICATOR`
`static final String`
`FILE_PREFIX`
`static final String`
`JAR_EXTENSION`
`static final String`
`JAR_PREFIX`
`static final String`
`JAR_SEPARATOR`
`static final String`
`pct`
`static final String`
`PROTOCOL_SEPARATOR`
`static final String`
`pt`
`static final String`
`px`
`static final String`
`RELATIVE_PREFIX`
`static final String`
`URL_SEPARATOR`
`static final char`
`URL_SEPARATOR_CHAR`

## Constructor Summary

Constructors
Constructor
Description
`Util()`

## Method Summary

Modifier and Type
Method
Description
`static void`
`addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a)`
convenience method for adding a component to a container layered out by a GridBagLayout
`static void`
`addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a, int gw, int gh)`
convenience method for adding a component to a container layered out by a GridBagLayout
`static void`
`addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a, int gw, int gh, int f)`
convenience method for adding a component to a container layered out by a GridBagLayout
`static void`
`addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a, int gw, int gh, int f, double wx, double wy)`
convenience method for adding a component to a container layered out by a GridBagLayout
`static void`
`center(Component parent, Component comp)`
center a `Component` relative to another `Component`.
`static void`
`copyFile(File srcFile, File destFile)`
copies a single file.
`static void`
`deleteDir(File dir)`
delete a directory with all its contents
`static void`
`errMsg(Component owner, String msg, Throwable e)`
show an error message and print a stack trace to the console if in development mode (DEV_MODE = true)
`static Element`
`findElementDown(String name, Element parent)`
find the first occurrence of an `Element` in the element tree below a given `Element`
`static Element`
`findElementUp(String name1, String name2, Element start)`
find the first occurrence of an `Element` in the element tree above a given `Element`
`static Element`
`findElementUp(String name, Element start)`
find the first occurrence of an `Element` in the element tree above a given `Element`
`static Object`
`findLinkUp(Element elem)`
find the next link attribute from a given element upwards through the element hierarchy
`static float`
`getAbsoluteAttrVal(Object attr)`
get the absolute value of an attribute
`static float`
`getAttrValue(Object attr)`
get the numerical value for an attribute object
`static String`
`getClassFilePath(Class cls)`
Get the path of the class file for a given class.
`static int`
`getElementIndex(Element elem)`
get the index of a given element in the list of its parents elements.
`static String`
`getLastAttrUnit()`
get the unit string from the last attribute object which was converted to a numerical value
`static String`
`getPreference(String key, String defaultValue)`
`static <T extends Enum<T>>
T`
`getPreference(String key, T defaultValue)`
`static float`
`getPtValue(String valStr)`
quick hack for getting the point value from an attribute value string (needs to be refined and consolidated with length value)
`static String`
`getRelativePath(File fromFile, File toFile)`
get the path to a given file relative to a given directory
`static String`
`getResourceString(UIResources resources, String nm)`
get a string from the resources file
`static String`
`getResourceString(String nm)`
`static int`
`getRowIndex(Element cell)`
get the row index for a given table cell
`static Vector`
`getStyleNames(StyleSheet styles)`
get the names of all styles found in a given StyleSheet
`static Vector`
`getStyleNamesForTag(AttributeSet styles, String tag)`
get names of all styles for a given tag
`static Vector`
`getStyleNamesForTag(StyleSheet styles, String tag)`
get names of all styles for a given tag
`static boolean`
`msg(int options, String title, String msg, String item, String sep)`
Show a message with options to choose from
`static int`
`msgChoice(int options, String title, String msg, String item, String sep)`
Show a message with options to choose from
`static void`
`msgStart(String startMsg)`
write a message with a time stamp to System.out and remember the time stamp in a LIFO Vector
`static String`
`nameInput(Frame parent, String initialName, String regex, String title, String text)`
get a name by asking from the user
`static String`
`removeChar(String src, char c)`
remove all occurrences of a given char from a given string
`static String`
`removeExtension(String fileName)`
remove the extension from a file name
`static File`
`renameFile(File from, String newExt)`
rename a file to have a given extension
`static AttributeSet`
`resolveAttributes(AttributeSet style)`
resolve sets of attributes that are recursively stored in each other
`static URL`
`resolveRelativePath(URL base, String relPath)`
resolve a relative URL string against an absolute URL string.
`static StyleSheet`
`styleSheet()`
get a StyleSheet object for using its utility methods
`static String[]`
`tokenize(String input, String delim)`
Get an array of strings from a given string having several entries delimited by blanks.

### Methods inherited from class Object

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

## Field Details

### JAR_PREFIX

public static final String JAR_PREFIX

See Also:
  - Constant Field Values

### JAR_EXTENSION

public static final String JAR_EXTENSION

See Also:
  - Constant Field Values

### FILE_PREFIX

public static final String FILE_PREFIX

See Also:
  - Constant Field Values

### CLASS_EXT

public static final String CLASS_EXT

See Also:
  - Constant Field Values

### JAR_SEPARATOR

public static final String JAR_SEPARATOR

See Also:
  - Constant Field Values

### URL_SEPARATOR

public static final String URL_SEPARATOR

See Also:
  - Constant Field Values

### URL_SEPARATOR_CHAR

public static final char URL_SEPARATOR_CHAR

See Also:
  - Constant Field Values

### CLASS_SEPARATOR

public static final String CLASS_SEPARATOR

See Also:
  - Constant Field Values

### CLASS_SEPARATOR_CHAR

public static final char CLASS_SEPARATOR_CHAR

See Also:
  - Constant Field Values

### DIR_UP_INDICATOR

public static final String DIR_UP_INDICATOR

See Also:
  - Constant Field Values

### RELATIVE_PREFIX

public static final String RELATIVE_PREFIX

See Also:
  - Constant Field Values

### PROTOCOL_SEPARATOR

public static final String PROTOCOL_SEPARATOR

See Also:
  - Constant Field Values

### ANCHOR_SEPARATOR

public static final String ANCHOR_SEPARATOR

See Also:
  - Constant Field Values

### pct

public static final String pct

See Also:
  - Constant Field Values

### pt

public static final String pt

See Also:
  - Constant Field Values

### px

public static final String px

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_NORMAL

public static final String CSS_ATTRIBUTE_NORMAL

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_UNDERLINE

public static final String CSS_ATTRIBUTE_UNDERLINE

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_LINE_THROUGH

public static final String CSS_ATTRIBUTE_LINE_THROUGH

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_NONE

public static final String CSS_ATTRIBUTE_NONE

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_ALIGN_LEFT

public static final String CSS_ATTRIBUTE_ALIGN_LEFT

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_ALIGN_CENTER

public static final String CSS_ATTRIBUTE_ALIGN_CENTER

See Also:
  - Constant Field Values

### CSS_ATTRIBUTE_ALIGN_RIGHT

public static final String CSS_ATTRIBUTE_ALIGN_RIGHT

See Also:
  - Constant Field Values

## Constructor Details

### Util

public Util()

## Method Details

### renameFile

public static File renameFile(File from, String newExt)
rename a file to have a given extension

Parameters:
  `from` - the file to rename
  `newExt` - the new extension the file shall have
Returns:
  the renamed file

### findLinkUp

public static Object findLinkUp(Element elem)
find the next link attribute from a given element upwards through the element hierarchy

Parameters:
  `elem` - the element to start looking at
Returns:
  the link attribute found, or null, if none was found

### removeExtension

public static String removeExtension(String fileName)
remove the extension from a file name

Parameters:
  `fileName` - the file name to remove the extension from
Returns:
  the file name without extension

### resolveAttributes

public static AttributeSet resolveAttributes(AttributeSet style)
resolve sets of attributes that are recursively stored in each other

Parameters:
  `style` - the set of attributes containing other sets of attributes

### nameInput

public static String nameInput(Frame parent, String initialName, String regex, String title, String text)
get a name by asking from the user

Wrapper for JOptionPane with I18N support

Parameters:
  `initialName` - the name initially shown in option pane
  `title` - the title to be shown in the option pane
  `text` - the text to be shown in the option pane
Returns:
  the entered name or null if action was cancelled

### msgChoice

public static int msgChoice(int options, String title, String msg, String item, String sep)
Show a message with options to choose from

Wrapper for JOptionPane with I18N support

Parameters:
  `options` - the options to be shown in the dialog
  `title` - the title to be shown in the dialog
  `msg` - the message to be shown in the dialog
  `item` - a variable part to be shown before msg
  `sep` - a separator for msg and item (return or blank etc.)
Returns:
  the choice

### msg

public static boolean msg(int options, String title, String msg, String item, String sep)
Show a message with options to choose from

Wrapper for JOptionPane with I18N support

Parameters:
  `options` - the options to be shown in the dialog
  `title` - the title to be shown in the dialog
  `msg` - the message to be shown in the dialog
  `item` - a variable part to be shown before msg
  `sep` - a separator for msg and item (return or blank etc.)
Returns:
  true, if YES was chosen, false if not

### getStyleNamesForTag

public static Vector getStyleNamesForTag(StyleSheet styles, String tag)
get names of all styles for a given tag

Parameters:
  `styles` - the style sheet to look for style names
  `tag` - the tag to find style names for
Returns:
  a Vector with all style names found

### getStyleNamesForTag

public static Vector getStyleNamesForTag(AttributeSet styles, String tag)
get names of all styles for a given tag

Parameters:
  `styles` - the style sheet to look for style names
  `tag` - the tag to find style names for
Returns:
  a Vector with all style names found

### getStyleNames

public static Vector getStyleNames(StyleSheet styles)
get the names of all styles found in a given StyleSheet

Parameters:
  `styles` - the StyleSheet to look for style names
Returns:
  a Vector with all names found

### deleteDir

public static void deleteDir(File dir)
delete a directory with all its contents

CAUTION: This method deletes all content of the given directory including all subdirectories and their content

Parameters:
  `dir` - the directory to delete

### copyFile

public static void copyFile(File srcFile, File destFile) throws FileNotFoundException, IOException
copies a single file.

If destFile already exists or if both files are the same the method does nothing. The complete destination path will be created before copying, if necessary.

Parameters:
  `srcFile` - the file to copy from
  `destFile` - the file to copy to
Throws:
  `FileNotFoundException`
  `IOException`

### getElementIndex

public static int getElementIndex(Element elem)
get the index of a given element in the list of its parents elements.

Parameters:
  `elem` - the element to get the index number for
Returns:
  the index of the given element

### getClassFilePath

public static String getClassFilePath(Class cls)
Get the path of the class file for a given class.

This is either a directory of a class file or a directory of a JAR file. Thus, this class must reside in the same place as the application in question, not in a separate library for instance.

Parameters:
  `cls` - the class to get the path for
Returns:
  the path of this class file or the path of the JAR file this class file resides in, whatever applies

### getPtValue

public static float getPtValue(String valStr)
quick hack for getting the point value from an attribute value string (needs to be refined and consolidated with length value)

Parameters:
  `valStr` - the attribute value string to get the point size for
Returns:
  the point size from the given attribute value

### getLastAttrUnit

public static String getLastAttrUnit()
get the unit string from the last attribute object which was converted to a numerical value

Returns:
  the unit string from the last attribute object

### getAttrValue

public static float getAttrValue(Object attr)
get the numerical value for an attribute object

Parameters:
  `attr` - the attribute to get the value from
Returns:
  the numerical value

### getAbsoluteAttrVal

public static float getAbsoluteAttrVal(Object attr)
get the absolute value of an attribute

Parameters:
  `attr` - the attribute to get the value from
Returns:
  the absolute numerical value

### getRowIndex

public static int getRowIndex(Element cell)
get the row index for a given table cell

Parameters:
  `cell` - the cell element to get the row index for
Returns:
  the row index of the given cell element

### tokenize

public static String[] tokenize(String input, String delim)
Get an array of strings from a given string having several entries delimited by blanks.

In the resource file of SimplyHTML for instance menu bar and menu definitions are contained as strings having a key for each item. The keys are delimited with blanks.

A string "file edit help" from the resource file for instance would be broken into an array of strings looking as follows

String[0]="file"
String[1]="edit"
String[2]="help"

Parameters:
  `input` - the string to transform into a string array
Returns:
  the resulting string array

### msgStart

public static void msgStart(String startMsg)
write a message with a time stamp to System.out and remember the time stamp in a LIFO Vector

### findElementUp

public static Element findElementUp(String name, Element start)
find the first occurrence of an `Element` in the element tree above a given `Element`

Parameters:
  `name` - the name of the `Element` to search for
  `start` - the `Element` to start looking
Returns:
  the found `Element` or null if none is found

### findElementUp

public static Element findElementUp(String name1, String name2, Element start)
find the first occurrence of an `Element` in the element tree above a given `Element`

Parameters:
  `name1` - the primary name of the `Element` to search for
  `name2` - an alternative name for the `Element` to search for
  `start` - the `Element` to start looking
Returns:
  the found `Element` or null if none is found

### findElementDown

public static Element findElementDown(String name, Element parent)
find the first occurrence of an `Element` in the element tree below a given `Element`

Parameters:
  `name` - the name of the `Element` to search for
  `parent` - the `Element` to start looking
Returns:
  the found `Element` or null if none is found

### addGridBagComponent

public static void addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a)
convenience method for adding a component to a container layered out by a GridBagLayout

Parameters:
  `container` - the container to add a component to
  `comp` - the component to add to container
  `g` - the GridBagLayout associated with container
  `c` - the GridBagConstraints to use
  `gx` - the value to use for GridBagConstraints.gridx
  `gy` - the value to use for GridBagConstraints.gridy
  `a` - the value to use for GridBagConstraints.anchor

### addGridBagComponent

public static void addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a, int gw, int gh)
convenience method for adding a component to a container layered out by a GridBagLayout

Parameters:
  `container` - the container to add a component to
  `comp` - the component to add to container
  `g` - the GridBagLayout associated with container
  `c` - the GridBagConstraints to use
  `gx` - the value to use for GridBagConstraints.gridx
  `gy` - the value to use for GridBagConstraints.gridy
  `a` - the value to use for GridBagConstraints.anchor
  `gw` - the value to use for GridBagConstraints.gridwidth
  `gh` - teh value to use for GridBagConstraints.gridheight

### addGridBagComponent

public static void addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a, int gw, int gh, int f)
convenience method for adding a component to a container layered out by a GridBagLayout

Parameters:
  `container` - the container to add a component to
  `comp` - the component to add to container
  `g` - the GridBagLayout associated with container
  `c` - the GridBagConstraints to use
  `gx` - the value to use for GridBagConstraints.gridx
  `gy` - the value to use for GridBagConstraints.gridy
  `a` - the value to use for GridBagConstraints.anchor
  `gw` - the value to use for GridBagConstraints.gridwidth
  `gh` - teh value to use for GridBagConstraints.gridheight
  `f` - the value to use for GridBagConstraints.fill

### addGridBagComponent

public static void addGridBagComponent(JComponent container, JComponent comp, GridBagLayout g, GridBagConstraints c, int gx, int gy, int a, int gw, int gh, int f, double wx, double wy)
convenience method for adding a component to a container layered out by a GridBagLayout

Parameters:
  `container` - the container to add a component to
  `comp` - the component to add to container
  `g` - the GridBagLayout associated with container
  `c` - the GridBagConstraints to use
  `gx` - the value to use for GridBagConstraints.gridx
  `gy` - the value to use for GridBagConstraints.gridy
  `a` - the value to use for GridBagConstraints.anchor
  `gw` - the value to use for GridBagConstraints.gridwidth
  `gh` - teh value to use for GridBagConstraints.gridheight
  `f` - the value to use for GridBagConstraints.fill
  `wx` - the value to use for GridBagConstraints.weightx
  `wy` - the value to use for GridBagConstraints.weighty

### resolveRelativePath

public static URL resolveRelativePath(URL base, String relPath)
resolve a relative URL string against an absolute URL string.

the absolute URL string is the start point for the relative path.

Example:

      absolute path:  file:/d:/eigene dateien/eigene bilder/
      relative path:  ../images/test.jpg
      result:         file:/d:/eigene dateien/images/test.jpg

Parameters:
  `base` - the absolute URL to start at
  `relPath` - the relative URL string to resolve
Returns:
  the absolute URL string resulting from resolving relPath against absPath

### getRelativePath

public static String getRelativePath(File fromFile, File toFile)
get the path to a given file relative to a given directory

Parameters:
  `fromFile` - the directory having the file from which the link refers
  `toFile` - the file to which a link refers
Returns:
  the relative path

### errMsg

public static void errMsg(Component owner, String msg, Throwable e)
show an error message and print a stack trace to the console if in development mode (DEV_MODE = true)

Parameters:
  `owner` - the owner of the message, or null
  `msg` - the message to display, or null
  `e` - the exception object describing the error, or null

### center

public static void center(Component parent, Component comp)
center a `Component` relative to another `Component`.

Parameters:
  `parent` - the `Component` to be used as the basis for centering
  `comp` - the `Component` to be centered within parent

### styleSheet

public static StyleSheet styleSheet()
get a StyleSheet object for using its utility methods

### removeChar

public static String removeChar(String src, char c)
remove all occurrences of a given char from a given string

Parameters:
  `src` - the string to remove from
  `c` - the char to remove
Returns:
  a string copy of src with all occurrences of c removed

### getResourceString

public static String getResourceString(UIResources resources, String nm)
get a string from the resources file

Parameters:
  `resources` - the TextResources to get the string from
  `nm` - the key of the string
Returns:
  the string for the given key or null if not found

### getResourceString

public static String getResourceString(String nm)

### getPreference

public static <T extends Enum<T>> T getPreference(String key, T defaultValue)

### getPreference

public static String getPreference(String key, String defaultValue)