Package manager

Source Code of manager.Settings

package manager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Properties;

import javax.swing.JOptionPane;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class Settings {
  private Document document = null;
  private Node rootNode = null;
  private String settingFile = null;
  private OptionsPanel panel = null;
 
  //  ------------------------------------------------
  public boolean loadFromFile(String settingFile)
  {
    this.settingFile = settingFile;
    if(!readFromXml(settingFile)) //impossible to load settings.xml, incorrect file or the file doesn't exists
    {
      try
      {
        //First ask for the language the user wants
        Hashtable<String,String> languagesTable =  LanguageEnumerator.getSupportedLanguages();
        Collection<String> languagesList= languagesTable.keySet();
        String result = (String)JOptionPane.showInputDialog(
                            null,
                            "Please choose a language among this list",
                            "Languages",
                            JOptionPane.PLAIN_MESSAGE,
                            null,
                            languagesList.toArray(),
                            null);
        String language;
        if ((result != null) && (result.length() > 0))
          language =languagesTable.get(result);
        else
          language ="en";
        //we may now create a new settings file
        String defaultConfiguration="<?xml version=\"1.0\" encoding=\"UTF-8\"?><settings><lastPath/><language>";
        defaultConfiguration += language;
        defaultConfiguration += "</language><lookAndFeel/><undoLevel>10</undoLevel><askForSave>true</askForSave><automaticPartialApply>true</automaticPartialApply><lastOpens></lastOpens></settings>";
        FileWriter settingsFile = new FileWriter("settings.xml");
        settingsFile.write(defaultConfiguration);
        settingsFile.flush();
        settingsFile.close();
      }
      catch(Exception exception)
      {
        System.err.println(exception);
        return false;
      }
      if(!readFromXml(settingFile))
      {
        return false;
      }
    }
    rootNode = this.getXmlChildNode(document,"settings");
    if(rootNode == document)
      return false;
    return true;
  }
  public void save() {
      try{
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        Properties properties = new Properties();
        properties.put("method", "xml");
        properties.put("encoding", "UTF-8");
        properties.put("indent", "yes");
        transformer.setOutputProperties(properties);

        Source input = new DOMSource(document);
        File outputFile = new File(settingFile);
        FileOutputStream outputStream = new FileOutputStream(outputFile);

        if(outputStream != null) {
          Result output = new StreamResult(outputStream);
          transformer.transform(input, output);
        }

        outputStream.flush();
        outputStream.close();
      }
      catch (FileNotFoundException e) {
        e.printStackTrace();
      }
      catch (FactoryConfigurationError e) {
        //erreur de configuration de fabrique
        e.printStackTrace();
      }
      catch (TransformerException e) {
        //erreur lors de la transformation
        e.printStackTrace();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
 
  //adding a modal argument may be a good idea
  public void showPanel()
  {
    if(this.panel == null)
      panel = new OptionsPanel(this);
    panel.showPanel();
  }
  public boolean hidePanel()
  {
    if(this.panel != null)
      return panel.close();
    return true;
  }
 
  //  ------------------------------------------------
  //accessors
 
 
  public String getLastPath()
  {
    return this.getNodeContent("lastPath");
  }
 
  public void setLastPath(String value)
  {
    this.setNodeContent("lastPath",value);
  }
 
  public String getLanguage()
  {
    return this.getNodeContent("language");
  }
 
  public void setLanguage(String value)
  {
    this.setNodeContent("language",value);
  }
  public String getLookAndFeel()
  {
    return this.getNodeContent("lookAndFeel");
  }
  public void setLookAndFeel(String value)
  {
    this.setNodeContent("lookAndFeel",value);
  }
  public boolean getAutomaticPartialApply()
  {
    String value = this.getNodeContent("automaticPartialApply");
    if(value.compareToIgnoreCase("true") == 0)
      return true;
    return false;
  }
  public void setAutomaticPartialApply(boolean value)
  {
    if(value)
      this.setNodeContent("automaticPartialApply","true");
    else
      this.setNodeContent("automaticPartialApply","false");
  }
  public boolean getAskForSave()
  {
    String value = this.getNodeContent("askForSave");
    if(value.compareToIgnoreCase("true") == 0)
      return true;
    return false;
  }
  public void setAskForSave(boolean value)
  {
    if(value)
      this.setNodeContent("askForSave","true");
    else
      this.setNodeContent("askForSave","false");
  }
 
  public int getUndoLevel()
  {
    String value = this.getNodeContent("undoLevel");
    if(value == "")
      return 0;
    return Integer.parseInt(value);
  }
  public void setUndoLevel(String value)
  {
    this.setNodeContent("undoLevel",value);
  }
 
 
 
  public Hashtable<String,String> getExportLanguages() {
    return TemplateEnumerator.getExportLanguages();
  }
 
  //------------------------------------------------
  //services methods
 
  private Node getXmlChildNode(Node father,String name)
  {
    if(father == null)
      return null;
    Node child= father.getFirstChild();
    do
    {
      if(child.getLocalName() == name)
        return child;
    }while((child = child.getNextSibling()) != null);
    return father;
 
 
  private String getNodeContent(String nodeName)
  {
    if(rootNode == null)
      return "";
    Node node = this.getXmlChildNode(rootNode,nodeName);
    if(node == rootNode)
      return "";
    return node.getTextContent();
  }
 
  public void setNodeContent(String nodeName,String value)
  {
    if(rootNode == null)
      return;
    Node node = this.getXmlChildNode(rootNode,nodeName);
    if(node == rootNode)
      return;
    node.setTextContent(value);
  }
 
  private boolean readFromXml(String xmlPath)
  {
      try
      {
        DOMParser parser = new DOMParser();
          parser.parse(xmlPath);
          this.document = parser.getDocument();         
      }  
      catch (IOException ie) {
            System.err.println("impossible de charge le fichier de configuration :" + xmlPath);
            return false;
      }  
      catch (SAXException se) {
        System.err.println("impossible de charge le fichier de configuration :" + xmlPath);
            return false;
      }
      return true;
  }
}
TOP

Related Classes of manager.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.