Package main.settings

Source Code of main.settings.Settings

package main.settings;


import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.JPanel;
import javax.swing.tree.TreeNode;

import main.settings.category.AbstractCategory;
import main.settings.category.ApplicationCategory;
import main.settings.category.CategoryEnumeration;
import main.settings.category.ConnectionCategory;
import main.settings.category.DebugCategory;
import main.settings.category.SharingCategory;
import main.settings.category.UpdateCategory;
import main.settings.panels.MainSettingPanel;
import main.settings.reflect.AbstractSettingsContainerReflector;
import main.settings.reflect.SettingsRootReflector;


public class Settings extends AbstractCategory
{
  public static final ApplicationCategory application = new ApplicationCategory();
  public static final UpdateCategory update = new UpdateCategory();
  public static final SharingCategory share = new SharingCategory();
  public static final ConnectionCategory connection = new ConnectionCategory();
  public static final DebugCategory debug = new DebugCategory();

  public static final String title = "Sillian";

  public static boolean readFromFile()
  {
    Properties settingsProperties = new Properties();

    FileInputStream fis;
    try
    {
      fis = new FileInputStream(application.files.settings);
      settingsProperties.load(fis);
    }
    catch (IOException e)
    {
      return false;
    }

    SettingsRootReflector mainReflector = new SettingsRootReflector();
    mainReflector.fetchProperties(settingsProperties);

    closeIOStream(fis);
    return true;
  }

  public static boolean writeToFile()
  {
    Properties settingsProperties = new Properties();

    AbstractSettingsContainerReflector mainContainer = new SettingsRootReflector();
    mainContainer.translateContainer(settingsProperties);

    FileOutputStream fos;
    try
    {
      fos = new FileOutputStream(application.files.settings);
      settingsProperties.store(fos, title);
    }
    catch (IOException e)
    {
      return false;
    }

    closeIOStream(fos);
    return true;
  }

  public static void initialize()
  {
    if (!Settings.readFromFile())
    {
      // FIXME: show configuration wizard (the following settings are
      // temporary)

      Settings.connection.bandwidth.download = 524288; // valore per
                                // Alice,
                                // corrisponde a
                                // 4 megabit/s
      Settings.connection.bandwidth.upload = 32768; // valore per Alice,
                              // corrisponde a 256
                              // kbit/s

      Settings.writeToFile();
    }
  }

  private static void closeIOStream(Closeable stream)
  {
    try
    {
      stream.close();
    }
    catch (IOException ignored)
    {
    }
  }

  public boolean isLeaf()
  {
    return false;
  }

  public TreeNode getParent()
  {
    return null;
  }

  public Enumeration<AbstractCategory> children()
  {
    return new CategoryEnumeration();
  }

  public boolean getAllowsChildren()
  {
    return true;
  }

  public TreeNode getChildAt(int childIndex)
  {
    return new CategoryEnumeration().get(childIndex);
  }

  public int getChildCount()
  {
    return new CategoryEnumeration().size();
  }

  public JPanel getPanel()
  {
    return MainSettingPanel.getStaticPanel();
  }

}
TOP

Related Classes of main.settings.Settings

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.