Package jrackattack.util

Source Code of jrackattack.util.UserConfiguration

/* For License see bottom */
package jrackattack.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;

import jonkoshare.util.VersionInformation;

import org.apache.log4j.Logger;

/**
* User config wrapper. This class loads the user configuration from the home
* file.
*
* @since 0.7
* @version 0.7
* @author Alexander Methke
*/
@VersionInformation(//
lastChanged = "$LastChangedDate: 2009-07-25 04:59:33 -0500 (Sat, 25 Jul 2009) $", //
authors = { "Alexander Methke" }, //
revision = "$LastChangedRevision: 11 $", //
lastEditor = "$LastChangedBy: onkobu $", //
id = "$Id"//
)
public class UserConfiguration {
  private static final Logger log = Logger.getLogger(UserConfiguration.class);

  public static String CONFIG_FILE_NAME;
  public static final String USER_HOME_KEY = "user.home";

  public static final String MIDI_IN_DEVICE = "jrackattack.midi.in_device";
  public static final String MIDI_OUT_DEVICE = "jrackattack.midi.out_device";
  public static final String KEYBOARD_IN_DEVICE = "jrackattack.midi.keyboard_device";
  public static final String WINDOW_BOUNDS = "jrackattack.bounds";
  public static final String LOOK_AND_FEEL = "jrackattack.look_and_feel";
  public static final String MIDI_IN_INDEX = "jrackattack.device.midi_in";
  public static final String MIDI_OUT_INDEX = "jrackattack.device.midi_out";
  public static final String KEYBOARD_IN_INDEX = "jrackattack.device.keyboard_in";

  /**
   * Creates a new (empty) instance. Do not forget to call {@link #init
   * init()}.
   */
  public UserConfiguration() {
    CONFIG_FILE_NAME = getProperty(USER_HOME_KEY, "./")
        + "/.jrackattack.conf";
  }

  /**
   * Returns a property. If this instance was not initiated yet, it returns
   * the key from the System-Properties.
   *
   * @see java.lang.System#getProperties
   */
  public String getProperty(String key, String defVal) {
    String ret = System.getProperties().getProperty(key);
    if (properties == null) {
      return ret == null ? defVal : ret;
    }

    return properties.getProperty(key, defVal);
  }

  /**
   * Initiates the instance and tries to load the user config from a config
   * file in a home directory. If the OS does not support a home directory,
   * the current directory will be used.
   */
  public void init() {
    try {
      properties = new Properties();
      File f = new File(CONFIG_FILE_NAME);
      if (!f.exists() || !f.canRead()) {
        return;
      }
      FileInputStream fIn = new FileInputStream(f);
      BufferedInputStream bIn = new BufferedInputStream(fIn);
      Properties p = new Properties();
      p.loadFromXML(bIn);
      properties = p;
      System.getProperties().putAll(properties);
      bIn.close();
      fIn.close();
    } catch (IOException ex) {
      log.error("error", ex);
    }
  }

  /**
   * Stores the instance and tries to write the user config to a config file
   * in a home directory. If the OS does not support a home directory, the
   * current directory will be used.
   */
  public void done() {
    try {
      FileOutputStream fOut = new FileOutputStream(CONFIG_FILE_NAME);
      BufferedOutputStream bOut = new BufferedOutputStream(fOut);
      properties.storeToXML(bOut, "properties from "
          + Calendar.getInstance().getTime().getTime());
      bOut.close();
      fOut.close();
    } catch (IOException ex) {
      log.error("error", ex);
    }
  }

  /**
   * Puts the value to the properties.
   */
  public void putProperty(String key, String value) {
    properties.put(key, value);
  }

  /**
   * Puts the boolean value to the properties. According to the boolean value
   * it will be saved as "true" or "false"
   */
  public void putProperty(String key, boolean b) {
    properties.put(key, b ? "true" : "false");
  }

  /**
   * Puts the int value to the properties.
   */
  public void putProperty(String key, int val) {
    properties.put(key, "" + val);
  }

  /**
   * Puts the font value to the properties. This does not save it's size nor
   * it's style.
   */
  public void putProperty(String key, Font f) {
    putProperty(key + ".name", f.getFontName());
    putProperty(key + ".size", f.getSize());
    switch (f.getStyle()) {
    case Font.PLAIN: {
      putProperty(key + ".style", "plain");
    }
      break;
    case Font.BOLD: {
      putProperty(key + ".style", "bold");
    }
      break;
    case Font.ITALIC: {
      putProperty(key + ".style", "italic");
    }
      break;
    default: {
      putProperty(key + ".style", "plain");
    }
    }
  }

  /**
   * Puts the rectangle into the properties. Therefore the property's key is
   * extended by the values ".x", ".y", ".width" and ".height" to save the
   * values as ints
   */
  public void putProperty(String key, Rectangle bounds) {
    putProperty(key + ".x", bounds.x);
    putProperty(key + ".y", bounds.y);
    putProperty(key + ".width", bounds.width);
    putProperty(key + ".height", bounds.height);
  }

  /**
   * Puts the given Color as property into the user configuration. Therefore
   * the key will be extended by the values ".r", ".g" and ".b" to save the
   * according values. Remember that the color's alpha-value will not be
   * saved.
   */
  public void putProperty(String key, Color c) {
    putProperty(key + ".r", c.getRed());
    putProperty(key + ".g", c.getGreen());
    putProperty(key + ".b", c.getBlue());
  }

  /**
   * Returns the boolean value of the given key. Uses
   * {@link java.lang.Boolean Boolean}'s String constructor to retrieve the
   * value.
   *
   * @see #java
   */
  public boolean getBooleanProperty(String key, boolean def) {
    String v = getProperty(key, null);
    if (v == null) {
      return def;
    }
    return new Boolean(v).booleanValue();
  }

  /**
   * Returns the int property. If the int value is not parseable it will
   * return <code>def</code>, too. No exceptions expected.
   */
  public int getIntProperty(String key, int def) {
    String v = getProperty(key, null);
    if (v == null) {
      return def;
    }
    try {
      return Integer.parseInt(v);
    } catch (NumberFormatException ex) {
    }

    return def;
  }

  /**
   * Returns the rectangular property of this key.
   *
   * @see #putProperty(String, Rectangle)
   */
  public Rectangle getRectProperty(String key, Rectangle def) {
    int x = getIntProperty(key + ".x", -1);
    if (x == -1) {
      return def;
    }
    int y = getIntProperty(key + ".y", -1);
    if (y == -1) {
      return def;
    }
    int w = getIntProperty(key + ".width", -1);
    if (w == -1) {
      return def;
    }
    int h = getIntProperty(key + ".height", -1);
    if (h == -1) {
      return def;
    }
    return new Rectangle(x, y, w, h);
  }

  /**
   * Retrieves a font property. Remember to derive a usable font from this
   * plain-styled and 0-sized instance.
   */
  public Font getFontProperty(String key, Font def) {
    String n = getProperty(key + ".name", null);
    if (n == null) {
      return def;
    }

    String style = getProperty(key + ".style", "plain");
    int size = getIntProperty(key + ".size", 12);
    if (style.equals("plain")) {
      return new Font(n, Font.PLAIN, size);
    } else if (style.equals("bold")) {
      return new Font(n, Font.BOLD, size);
    } else if (style.equals("italic")) {
      return new Font(n, Font.ITALIC, size);
    } else {
      return new Font(n, Font.PLAIN, getIntProperty(key + ".size", 12));
    }
  }

  /**
   * Returns the Color-property of the given key.
   *
   * @see #putProperty(String, Color)
   */
  public Color getColorProperty(String key, Color def) {
    int r = getIntProperty(key + ".r", -1);
    if (r == -1) {
      return def;
    }
    int g = getIntProperty(key + ".g", -1);
    if (g == -1) {
      return def;
    }
    int b = getIntProperty(key + ".b", -1);
    if (b == -1) {
      return def;
    }

    return new Color(r, g, b);
  }

  public static UserConfiguration getInstance() {
    if (instance == null) {
      instance = new UserConfiguration();
    }
    return instance;
  }

  private static UserConfiguration instance;

  private Properties properties;
}
/*
* Copyright (C) 2008 Alexander Methke
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program (gplv3.txt).
*/
TOP

Related Classes of jrackattack.util.UserConfiguration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.