001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.tools;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.FileNotFoundException;
022    import java.io.FileOutputStream;
023    import java.io.IOException;
024    import java.util.Properties;
025    
026    /**
027     * Class SearsProperties.
028     * <br><b>Summary:</b><br>
029     * This class contains the Sears Properties.
030     */
031    public class SearsProperties {
032        /**The properties*/
033        private Properties properties;
034        /**The instance of the class.*/
035        private static SearsProperties instance = new SearsProperties();
036        
037        /**The config file name.*/
038        private static final String CONFIG_FILE = "config.cfg";
039        
040        /**The config folder name.*/
041        private static final String CONFIG_FOLDER = ".sears";
042        
043        
044        /**The width suffix property.*/
045        public static final String SUFFIX_WIDTH = "_width";
046        /**The heigth suffix property.*/
047        public static final String SUFFIX_HEIGTH = "_heigth";
048        /**The posX suffix property.*/
049        public static final String SUFFIX_POSX = "_posX";
050        /**The posY suffix property.*/
051        public static final String SUFFIX_POSY = "_posY";
052        /**The last folder property*/
053        public static final String LAST_FOLDER = "File_lastFolder";
054        /**The language property*/
055        public static final String LANGUAGE = "Sears_lang";
056        /**The country property*/
057        public static final String COUNTRY = "Sears_count";  
058        /**The player property*/
059        public static final String PLAYER_FULL_PATH = "Player_fullPath";
060        /**The look and feel property.*/
061        public static final String LOOK_AND_FEEL = "Sears_lookAndFeel";
062        /**The DOS line separator property.*/
063        public static final String DOS_LINE_SEPARATOR = "Sears_useDOSLineSeparator";
064        /**The player selected.*/
065        public static final String PLAYER_SELECTED = "Sears_selectedPlayer";  
066        /**The update address.*/
067        public static String UPDATE_ADDRESS = "Sears_update";    
068        /**The key to the other player choice.*/
069        public final static int KEY_OTHER = 1;
070        /**The key to the VLC player choice.*/
071        public final static int KEY_VLC = 0;
072        /**The ICON_SET_FILE: The file that contains the icons.*/
073        public static final String ICON_SET = "Sears_iconSet";
074        /**The DEFAULT_ICON_SET: The default icon set.*/
075        public static final String DEFAULT_ICON_SET = "Flat.jar";
076        /** (<b>String</b>) RECENT_FILES: The RECENT_FILES */
077        public static final String RECENT_FILES = "Sears_recentFiles";
078        /** (<b>String</b>) SEARCH_WIDTH: the search area width */
079        public static final String TOOLBAR_DIVIDER_LOCATION = "toolbar_divider_location";
080        
081        
082        /**The VLC_RESTART: to indicate wether vlc should restart after a subtitle change.*/
083        public static String VLC_RESTART = "Sears_vlcRestart";
084        /**The VLC_PORT: the port to connect vlc.*/
085        public static String VLC_PORT = "Sears_vlcPort";
086       
087        /**
088         * Constructor SearsProperties.
089         * <br><b>Summary:</b><br>
090         * Constructor of the class.
091         */
092        public SearsProperties(){
093            properties = loadProperties();
094        }
095        
096        
097        /**
098         * Method loadProperties.
099         * <br><b>Summary:</b><br>
100         * Use this method to load the software properties.
101         * @return  <b>Properties</b>   The loaded properties.
102         */
103        private Properties loadProperties() {
104            //the result of the method.
105            Properties result = new Properties();
106            try {
107                    //Load the property file.
108                result.load(new FileInputStream(getConfigFile()));
109            } catch (FileNotFoundException e) {
110                e.printStackTrace();
111            } catch (IOException e) {
112                e.printStackTrace();
113            }
114            //return the result
115            return result;
116        }
117        
118        /**
119         * Method saveProperties.
120         * <br><b>Summary:</b><br>
121         * This method saves the properties of Sears in the property file.
122         */
123        public static void saveProperties() {
124            try {
125                //save the properties.
126                instance.properties.store(new FileOutputStream(getConfigFile()), "");
127            } catch (FileNotFoundException e) {
128                e.printStackTrace();
129            } catch (IOException e) {
130                e.printStackTrace();
131            }
132        }
133        
134        /**
135         * Method getProperty.
136         * <br><b>Summary:</b><br>
137         * Return the sears property that correspond to the key. or "" value if not found.
138         * @param key               The key to search.
139         * @return <b>String</b>    The sears property that correspond to the key. or "" value if not found.
140         */
141        public static String getProperty(String key){
142            return getProperty(key, "");
143        }
144        
145        /**
146         * Method getProperty.
147         * <br><b>Summary:</b><br>
148         * Return the sears property that correspond to the key. or default value if not found.
149         * @param key               The key to search.
150         * @param defaultValue      The default value to return if not found.
151         * @return <b>String</b>    The sears property that correspond to the key. or default value if not found.
152         */
153        public static String getProperty(String key, String defaultValue){
154            //The result of the method.
155            String result = "";
156            //try to get the result from the properties.
157            result = instance.properties.getProperty(key, defaultValue);
158            //return the result.
159            return result;
160        }
161        
162        /**
163         * Method setProperty.
164         * <br><b>Summary:</b><br>
165         * Set the Sears property.
166         * @param key       The key to set.
167         * @param value     The value to associate.
168         */
169        public static void setProperty(String key, String value){
170            instance.properties.setProperty(key, value);
171        }
172        
173        public static void resetProperty() throws IOException {
174            getConfigFile().delete();
175            instance = new SearsProperties();
176        }
177        
178        /**
179         * Method getConfigFile.
180         * <br><b>Summary:</b><br>
181         * This method return the config file to be used with Sears.
182         * It creates it if not found.
183         * It uses the user.home property of the system.
184         * @return  (<b>File</b>)   The config file to be used to load and save Sears propertiess.
185         * @throws IOException
186         */
187        private static File getConfigFile() throws IOException{
188            //The result of the method.
189            File result = new File(System.getProperty("user.home")+File.separator+CONFIG_FOLDER+File.separator+CONFIG_FILE);
190            //check parent folder existence
191            if(!result.getParentFile().exists()){
192                    result.getParentFile().mkdirs();
193            }
194            //Then create the property file if does not exist.
195            if(!result.exists()){
196                    result.createNewFile();
197            }
198            //return the result.
199            return result;
200        }
201    }