de.stefan1200.util

## Class ArrangedPropertiesWriter

- java.lang.Object
- - de.stefan1200.util.ArrangedPropertiesWriter

- All Implemented Interfaces:
    Cloneable

      public class ArrangedPropertiesWriter
      extends Object
      implements Cloneable

  ArrangedPropertiesWriter allows you to write a formatted property file with help texts for every key. Since version 2.0 it is possible to use a MySQL database as data source.
  You are allowed to use this file for free, but it would be nice to be named in the credits of your project. The author of this class is not responsible for any damage or data loss! It is not allowed to sell this class for money, it has to be free to get!
  E-Mail:
  info@stefan1200.de
  Homepage:
  http://www.stefan1200.de
  Version:
    2.4 (07.10.2016)
  Author:
    Stefan Martens

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ArrangedPropertiesWriter(JTS3ServerMod_Interface mainClass)` Create a new instance of the ArrangedPropertiesWriter.
    `ArrangedPropertiesWriter(JTS3ServerMod_Interface mainClass, HashMap<String,String> hmHelp, HashMap<String,String> hmValue, HashMap<String,Boolean> hmSave, Vector<String> vKeys)` Create a new instance with an already loaded configuration.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `addKey(String key, String helpText)` Add a new key at the current position.
    `boolean` | `addKey(String key, String helpText, boolean saveToFile)` Add a new key at the current position.
    `boolean` | `addKey(String key, String helpText, String defaultValue)` Add a new key at the current position.
    `boolean` | `addKey(String key, String helpText, String defaultValue, boolean saveToFile)` Add a new key at the current position.
    `void` | `addSeparator()` Add a separator at the current position.
    `boolean` | `canSaveToFile(String key)` Returns if this value can save to file.
    `ArrangedPropertiesWriter` | `clone()` Get a new instance of the ArrangedPropertiesWriter with the same loaded configuration.
    `String` | `getHelpText(String key)` Get the help text of a key.
    `int` | `getKeyCount()` Returns number of stored keys.
    `Vector<String>` | `getKeys()` Get a list of all known keys.
    `String` | `getValue(String key)` Returns the value of the key.
    `String` | `getValue(String key, String defValue)` Returns the value of the key.
    `boolean` | `insertKey(String key, int pos, String helpText)` Inserts a new key at the given position.
    `void` | `insertSeparator(int pos)` Insert separator at a given position.
    `boolean` | `loadValues(File file)` Load values from property file.
    `boolean` | `loadValues(MySQLConnect mysqlConnect, int instanceID)` Load values from SQL database.
    `boolean` | `loadValues(String filename)` Load values from property file.
    `void` | `removeAllSeparators()` Removes all separators.
    `void` | `removeAllValues()` Clears all values.
    `boolean` | `removeKey(String key)` Remove a key.
    `boolean` | `save()` Save values to the last loading location!
    `boolean` | `save(MySQLConnect mysqlConnect, int instanceID)` Save values to SQL database.
    `boolean` | `save(String filename, String header)` Write arranged property file to disk.
    `void` | `setDefaultConfigFileHeader(String header)` Set a default config file header.
    `void` | `setNewMainClass(JTS3ServerMod_Interface mainClass)` Set a new main class.
    `boolean` | `setValue(String key, boolean value)` Sets a value for the given key.
    `boolean` | `setValue(String key, double value)` Sets a value for the given key.
    `boolean` | `setValue(String key, long value)` Sets a value for the given key.
    `boolean` | `setValue(String key, String value)` Sets a value for the given key.
    `void` | `setWriteProtection(boolean flag)` Set write protection to `true` to make sure, that no values will be changed, removed or added.

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

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

- - ### Constructor Detail

    - #### ArrangedPropertiesWriter

          public ArrangedPropertiesWriter(JTS3ServerMod_Interface mainClass)

      Create a new instance of the ArrangedPropertiesWriter.
      Parameters:
        `mainClass` - A new main class, will be needed to access the addLogEntry method to write log entries.

    - #### ArrangedPropertiesWriter

          public ArrangedPropertiesWriter(JTS3ServerMod_Interface mainClass,
                                          HashMap<String,String> hmHelp,
                                          HashMap<String,String> hmValue,
                                          HashMap<String,Boolean> hmSave,
                                          Vector<String> vKeys)

      Create a new instance with an already loaded configuration.
      Parameters:
        `mainClass` - A new main class, will be needed to access the addLogEntry method to write log entries.
        `hmHelp` - Instance of hmHelp
        `hmValue` - Instance of hmValue
        `hmSave` - Instance of hmSave
        `vKeys` - Instance of vKeys

  - ### Method Detail

    - #### addKey

          public boolean addKey(String key,
                                String helpText)

      Add a new key at the current position.
      Parameters:
        `key` - Name of the key
        `helpText` - Help text of the key or `null` if not wanted.
      Returns:
        `true` if the key was added, `false` if the key is already known.

    - #### addKey

          public boolean addKey(String key,
                                String helpText,
                                boolean saveToFile)

      Add a new key at the current position.
      Parameters:
        `key` - Name of the key
        `helpText` - Help text of the key or `null` if not wanted.
        `saveToFile` - False if this config key should be ignored by the save method, if saving to file.
      Returns:
        `true` if the key was added, `false` if the key is already known.

    - #### addKey

          public boolean addKey(String key,
                                String helpText,
                                String defaultValue)

      Add a new key at the current position.
      Parameters:
        `key` - Name of the key
        `helpText` - Help text of the key or `null` if not wanted.
        `defaultValue` - Preset the key with a default value. Set `null` if no default value should be set!
      Returns:
        `true` if the key was added, `false` if the key is already known.

    - #### addKey

          public boolean addKey(String key,
                                String helpText,
                                String defaultValue,
                                boolean saveToFile)

      Add a new key at the current position.
      Parameters:
        `key` - Name of the key
        `helpText` - Help text of the key or `null` if not wanted.
        `defaultValue` - Preset the key with a default value. Set `null` if no default value should be set!
        `saveToFile` - False if this config key should be ignored by the save method, if saving to file.
      Returns:
        `true` if the key was added, `false` if the key is already known.

    - #### addSeparator

          public void addSeparator()

      Add a separator at the current position.

    - #### canSaveToFile

          public boolean canSaveToFile(String key)

      Returns if this value can save to file.
      Parameters:
        `key` - Name of the key
      Returns:
        `true` if the key can be saved to file or `false` if not.

    - #### clone

          public ArrangedPropertiesWriter clone()

      Get a new instance of the ArrangedPropertiesWriter with the same loaded configuration. The main class will be set to `null`.
      Important:
      This is not a deep copy, if the loaded configuration will be changed at the original ArrangedPropertiesWriter instance, this copy will change too.
      Overrides:
        `clone` in class `Object`
      Returns:
        Returns a shallow copy of this ArrangedPropertiesWriter.

    - #### getHelpText

          public String getHelpText(String key)

      Get the help text of a key.
      Parameters:
        `key` - Name of the key
      Returns:
        The help text as String or `null` if the key has no help text.

    - #### getKeyCount

          public int getKeyCount()

      Returns number of stored keys.
      Returns:
        Key count

    - #### getKeys

          public Vector<String> getKeys()

      Get a list of all known keys.
      Returns:
        List of keys

    - #### getValue

          public String getValue(String key)

      Returns the value of the key.
      Parameters:
        `key` - Name of the key
      Returns:
        The value of the key or `null` if there is no value.

    - #### getValue

          public String getValue(String key,
                                 String defValue)

      Returns the value of the key. If there is no value, the defValue will be returned.
      Parameters:
        `key` - Name of the key
        `defValue` - Default value
      Returns:
        The value of the key or the default value if there is no value.

    - #### insertKey

          public boolean insertKey(String key,
                                   int pos,
                                   String helpText)

      Inserts a new key at the given position.
      Parameters:
        `key` - Name of the key
        `pos` - Insert position
        `helpText` - Help text of the key or `null` if not wanted.
      Returns:
        `true` if the key was inserted, `false` if the key is already known.

    - #### insertSeparator

          public void insertSeparator(int pos)

      Insert separator at a given position.
      Parameters:
        `pos` - Position to insert separator.

    - #### loadValues

          public boolean loadValues(File file)

      Load values from property file. Only the values of known keys will be read from file.
      Parameters:
        `file` - File object of the property file.
      Returns:
        `true` if file was successfully read, `false` if not.

    - #### loadValues

          public boolean loadValues(MySQLConnect mysqlConnect,
                                    int instanceID)

      Load values from SQL database. Only the values of known keys will be read from database.
      Parameters:
        `mysqlConnect` - `MySQLConnect` class.
        `instanceID` - The instance ID in the database.
      Returns:
        `true` if database was successfully read, `false` if not.

    - #### loadValues

          public boolean loadValues(String filename)

      Load values from property file. Only the values of known keys will be read from file.
      Parameters:
        `filename` - Path to the property file.
      Returns:
        `true` if file was successfully read, `false` if not.

    - #### removeAllSeparators

          public void removeAllSeparators()

      Removes all separators.

    - #### removeAllValues

          public void removeAllValues()

      Clears all values.

    - #### removeKey

          public boolean removeKey(String key)

      Remove a key. The help text and value of the key will be also removed.
      Parameters:
        `key` - Name of the key
      Returns:
        `true` if the key was removed, `false` if key not found.

    - #### save

          public boolean save()

      Save values to the last loading location!
      Returns:
        `true` if values was successfully written, `false` if not.
      Since:
        2.4

    - #### save

          public boolean save(MySQLConnect mysqlConnect,
                              int instanceID)

      Save values to SQL database.
      Parameters:
        `mysqlConnect` - `MySQLConnect` class.
        `instanceID` - The instance ID in the database.
      Returns:
        `true` if values was successfully written to database, `false` if not.

    - #### save

          public boolean save(String filename,
                              String header)

      Write arranged property file to disk.
      Parameters:
        `filename` - Path where the file should be saved.
        `header` - The header of the file or `null` if not wanted.
      Returns:
        `true` if file was successfully written, `false` if not.

    - #### setDefaultConfigFileHeader

          public void setDefaultConfigFileHeader(String header)

      Set a default config file header. Useless for MySQL.
      Parameters:
        `header` - A simple config file header as String.
      Since:
        2.4

    - #### setNewMainClass

          public void setNewMainClass(JTS3ServerMod_Interface mainClass)

      Set a new main class. Will be needed to access the addLogEntry method to write log entries.
      Parameters:
        `mainClass` - A new main class, will be needed to access the addLogEntry method to write log entries.

    - #### setValue

          public boolean setValue(String key,
                                  boolean value)

      Sets a value for the given key.
      Parameters:
        `key` - Name of the key
        `value` - Value for the key, will be converted to String.
      Returns:
        `true` if the value was set, `false` if key not found.

    - #### setValue

          public boolean setValue(String key,
                                  double value)

      Sets a value for the given key.
      Parameters:
        `key` - Name of the key
        `value` - Value for the key, will be converted to String.
      Returns:
        `true` if the value was set, `false` if key not found.

    - #### setValue

          public boolean setValue(String key,
                                  long value)

      Sets a value for the given key.
      Parameters:
        `key` - Name of the key
        `value` - Value for the key, will be converted to String.
      Returns:
        `true` if the value was set, `false` if key not found.

    - #### setValue

          public boolean setValue(String key,
                                  String value)

      Sets a value for the given key.
      Parameters:
        `key` - Name of the key
        `value` - Value for the key
      Returns:
        `true` if the value was set, `false` if key not found.

    - #### setWriteProtection

          public void setWriteProtection(boolean flag)

      Set write protection to `true` to make sure, that no values will be changed, removed or added.
      Parameters:
        `flag` - Set `true` to prevent any changes to the configuration.
      Since:
        2.2