View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.util;
14  
15  import java.io.File;
16  import java.io.FileInputStream;
17  import java.io.FileOutputStream;
18  import java.io.IOException;
19  import java.util.Properties;
20  
21  /**
22   * Class that loads and saves preferences
23   *
24   * @author Daniel Sendula
25   */
26  public class Preferences extends Properties {
27  
28      /** Config file name. Default is ".hhd.config" */
29      protected String configFileName = ".config";
30  
31      /**
32       * Constructor
33       */
34      public Preferences(String configFileName) {
35          this.configFileName = configFileName;
36      }
37  
38      /**
39       * Saves prefereces
40       * 
41       * @throws IOException
42       */
43      public void save() throws IOException {
44          FileOutputStream out = new FileOutputStream(getConfigFile());
45          try {
46              super.store(out, "Configuration");
47          } finally {
48              out.close();
49          }
50      }
51  
52      /**
53       * Loads preferences
54       * 
55       * @throws IOException
56       */
57      public void load() throws IOException {
58          File configFile = getConfigFile();
59          if (configFile.exists()) {
60              FileInputStream in = new FileInputStream(configFile);
61              try {
62                  load(in);
63              } finally {
64                  in.close();
65              }
66          }
67      }
68  
69      /**
70       * Returns prefernces (config) file
71       * 
72       * @return prefernces file
73       */
74      public File getConfigFile() {
75          File f = new File(System.getProperty("user.home"));
76          return new File(f, getConfigFileName());
77      }
78  
79      /**
80       * Sets the name of preferences (config) file
81       * 
82       * @param fileName name of preferences file
83       */
84      public void setConfigFileName(String fileName) {
85          this.configFileName = fileName;
86      }
87  
88      /**
89       * Returns the name of preferences (config) file
90       * 
91       * @return fileName name of preferences file
92       */
93      public String getConfigFileName() {
94          return configFileName;
95      }
96  
97  }