View Javadoc

1   /*
2    * @(#)$Id: Configuration.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Michele Croci, mcroci@gmail.com,
5    *        
6    * Purpose      :   Geco configuration.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Jan 24, 2007     crocimi    Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  
26  package org.ximtec.igesture.geco;
27  
28  import java.io.File;
29  import java.util.List;
30  import java.util.logging.Level;
31  import java.util.logging.Logger;
32  
33  import org.apache.commons.configuration.ConfigurationException;
34  import org.apache.commons.configuration.XMLConfiguration;
35  import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
36  import org.sigtec.util.Constant;
37  import org.sigtec.util.FileHandler;
38  
39  
40  /**
41   * Geco configuration.
42   * 
43   * @version 1.0, Jan 2008
44   * @author Michele Croci, mcroci@gmail.com
45   */
46  public class Configuration {
47  
48     private static final Logger LOGGER = Logger.getLogger(Configuration.class
49           .getName());
50  
51     private static final String ALL_INPUT_DEVICES = "inputdevices/device/@name";
52     private static final String SELECTED_INPUT_DEVICE = "inputdevices/device[@selected='true']/@name";
53     private static final String SELECTED_INPUT_DEVICE2 = "inputdevices/device/name";
54  
55     private static final String PROPERTY_DATABASE = "database";
56     private static final String PROPERTY_ALGORITHM = "algorithm/class";
57     private static final String PROPERTY_TAB = "tab/class";
58  
59     private static final String MINIMIZE = "minimize";
60     private static final String LAST_PROJECT = "lastproject";
61  
62     private static final String TRUE = "true";
63  
64     private XMLConfiguration configuration;
65  
66  
67     public Configuration(String file) {
68        try {
69           File confFile = FileHandler.getResource(file);
70           configuration = new XMLConfiguration();
71           configuration.setFileName(confFile.getPath());
72           configuration.setExpressionEngine(new XPathExpressionEngine());
73           configuration.setAutoSave(true);
74           configuration.load();
75           selectDevice();
76        }
77        catch (ConfigurationException e) {
78           LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
79        }
80  
81     }
82  
83  
84     /**
85      * Returns the filename of the database.
86      * 
87      * @return the filename of the database.
88      */
89     public String getDatabase() {
90        return configuration.getString(PROPERTY_DATABASE);
91     } // getDatabase
92  
93  
94     /**
95      * Returns the list of algorithms.
96      * 
97      * @return the list of algorithms.
98      */
99     @SuppressWarnings("unchecked")
100    public List<String> getAlgorithms() {
101       return configuration.getList(PROPERTY_ALGORITHM);
102    } // getAlgorithms
103 
104 
105    /**
106     * Returns the list of tabs.
107     * 
108     * @return the list of tabs.
109     */
110    @SuppressWarnings("unchecked")
111    public List<String> getTabs() {
112       return configuration.getList(PROPERTY_TAB);
113    } // getTabs
114 
115 
116    
117 
118    /**
119     * Returns the selected input device name.
120     * 
121     * @return the selected input device name.
122     * 
123     */
124    @SuppressWarnings("unchecked")
125    public String getInputDeviceName() {
126       List<String> list = configuration.getList(SELECTED_INPUT_DEVICE);
127       if (!list.isEmpty()) {
128          return list.get(0);
129       }
130       return null;
131    } // getInputDeviceName
132 
133 
134    /**
135     * Returns the last opened project.
136     * 
137     * @return path of the last opened project.
138     * 
139     */
140    @SuppressWarnings("unchecked")
141    public String getMostRecentProject() {
142       List<String> list = configuration.getList(LAST_PROJECT);
143       if (!list.isEmpty()) {
144          return list.get(0);
145       }
146       return null;
147    } // getLastProject
148 
149 
150    /**
151     * Returns true if the value of <minimize> is true, otherwise false;
152     * 
153     * @return the value of <minimize>
154     * 
155     */
156    @SuppressWarnings("unchecked")
157    public boolean getMinimize() {
158       List<String> list = configuration.getList(MINIMIZE);
159       if (!list.isEmpty()) {
160          if ((list.get(0)).equals(TRUE)) {
161             return true;
162          }
163          else {
164             return false;
165          }
166       }
167       return false;
168    } // getInputDeviceName
169 
170 
171    /**
172     * Returns all the available input devices.
173     * 
174     * @return the input devices.
175     * 
176     */
177    @SuppressWarnings("unchecked")
178    public List<String> getInputDevices() {
179       List<String> list = configuration.getList(ALL_INPUT_DEVICES);
180       return list;
181    } // getInputDevices
182 
183 
184    
185 
186 
187    @SuppressWarnings("unchecked")
188    private synchronized void selectDevice() {
189       int i = 1;
190 
191       for (String deviceName : (List<String>)configuration
192             .getList(SELECTED_INPUT_DEVICE2)) {
193          // configuration.setProperty("inputdevices/device["+i+"] @selected",
194          // "true");
195          i++;
196       }
197 
198    } // selectDevice
199 
200 }