KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > utils > Prefs


1 package sellwin.utils;
2
3 import java.util.*;
4 import java.io.*;
5 import java.text.*;
6 import java.awt.*;
7
8 // SellWin http://sourceforge.net/projects/sellwincrm
9
//Contact support@open-app.com for commercial help with SellWin
10
//This software is provided "AS IS", without a warranty of any kind.
11

12 /**
13  * This class offers some routines that manage the user
14  * preferences that are stored in the '.sellwinrc' file
15  * in the user's home directory. Also, other basic user
16  * or system constants are stored here as well just as
17  * a common place to store such things.
18  */

19 public class Prefs {
20
21     public final static boolean DEBUG=true;
22
23     //database types that are supported
24
public final static int ORACLE = 1;
25     public final static int MYSQL = 2;
26
27     //AWT inset constants for grid bag field padding
28
public final static int I_TOP = 7;
29     public final static int I_LF = 5;
30     public final static int I_BOTT = 7;
31     public final static int I_RT = 5;
32
33     public final static int FIELD_HEIGHT = 20;
34
35     private static Properties systemProps=null;
36     public final static String JavaDoc CONNECTED="Connected";
37     public final static String JavaDoc TWO_TIER="2Tier";
38     public final static String JavaDoc LOCAL="Local";
39
40     public final static String JavaDoc[] LANGS = { "English", "French", "Spanish", "German" };
41     public final static String JavaDoc DEF_LANG = "defaultLanguage";
42     public final static String JavaDoc CONN_MODE = "connectionMode";
43     public final static String JavaDoc LOCAL_DB= "sellwin";
44     public final static String JavaDoc LOCAL_DB_ID= "localDBid";
45     public final static String JavaDoc LOCAL_DB_PSW= "localDBpsw";
46     public final static String JavaDoc SERVER_NAME= "serverName";
47     public final static String JavaDoc LAST_ID= "default";
48     public final static String JavaDoc PREF_BASE= ".sellwinrc";
49     public final static String JavaDoc SELLWIN_HOME= "sellwin";
50     public final static String JavaDoc DATA_BASE= "data";
51
52     public final static NumberFormat wholeMoney = new DecimalFormat("#,###,###,##0");
53     public final static NumberFormat money = new DecimalFormat("#,###,###,##0.00");
54     public final static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
55     public final static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yy hh:mm aa");
56
57
58     /**
59      * get the user's default language (e.g. English, Spanish)
60      * @return a string such as 'English' or 'Spanish'
61      */

62     public static String JavaDoc getDefaultLang() {
63         Properties props = getApplProps();
64         String JavaDoc lang = (String JavaDoc)(props.getProperty(DEF_LANG));
65         return lang;
66     }
67
68     /**
69      * get the user's default data directory where
70      * downloaded data will be stored
71      * @return the pathname of the data directory
72      */

73     public static String JavaDoc getDataDir() {
74         Properties props = getApplProps();
75         String JavaDoc dir = (String JavaDoc)(props.getProperty(LOCAL_DB));
76         return dir;
77     }
78
79     /**
80      * get the user's last login ID
81      * @return the last login name
82      */

83     public static String JavaDoc getLastID() {
84         Properties props = getApplProps();
85         String JavaDoc lastID = (String JavaDoc)(props.getProperty(LAST_ID));
86         if (lastID == null)
87             return "default";
88         else
89             return lastID;
90     }
91     /**
92      * get the user's default server host name
93      * for connections to a SellWin server
94      * @return the server name
95      */

96     public static String JavaDoc getServerName() {
97         Properties props = getApplProps();
98         String JavaDoc serverName = (String JavaDoc)(props.getProperty(SERVER_NAME));
99         if (serverName == null)
100             return "localhost";
101         else
102             return serverName;
103     }
104
105     /**
106      * get the user's connection type, this is either
107      * 'connected' or 'disconnected'
108      * @return the connection type
109      */

110     public static String JavaDoc getConnectionMode() {
111         Properties props = getApplProps();
112         String JavaDoc mode = (String JavaDoc)(props.getProperty(CONN_MODE));
113         return mode;
114     }
115
116     /**
117      * store the user properties in the .sellwinrc file
118      * in the user's home directory
119      * @param props the Properties we are storing to disk
120      */

121     public static void saveApplProps(Properties props) {
122         FileOutputStream outStream=null;
123         try {
124             System.out.println("saving props to "+ Prefs.getApplPropsPath());
125             outStream = new FileOutputStream(Prefs.getApplPropsPath());
126             props.store(outStream, "SellWin application properties");
127         } catch (IOException e) {
128             e.printStackTrace();
129         } finally {
130             try {
131                 if (outStream != null)
132                     outStream.close();
133             } catch (Exception JavaDoc f) {
134                 f.printStackTrace();
135             }
136         }
137     }
138
139     /**
140      * get the user's default Properties from disk
141      * @return the user's Properties
142      */

143     public static Properties getApplProps() {
144         String JavaDoc appPropsPath = getApplPropsPath();
145         Properties props = new Properties();
146
147         FileInputStream inStream=null;
148         try {
149             inStream = new FileInputStream(appPropsPath);
150             props.load(inStream);
151         } catch (FileNotFoundException e) {
152             System.out.println(appPropsPath + " does not exist...creating with default values");
153             props.setProperty(DEF_LANG, LANGS[0]);
154             props.setProperty(CONN_MODE, LOCAL);
155             props.setProperty(LAST_ID, "trial");
156             props.setProperty(LOCAL_DB, "sellwin");
157             props.setProperty(LOCAL_DB_ID, "localid");
158             props.setProperty(LOCAL_DB_PSW, "localpsw");
159             props.setProperty(SERVER_NAME, "localhost");
160             saveApplProps(props);
161         } catch (IOException e) {
162             e.printStackTrace();
163         } finally {
164             try {
165                 if (inStream != null)
166                     inStream.close();
167             } catch (Exception JavaDoc f) {
168                 f.printStackTrace();
169             }
170         }
171         return props;
172     }
173
174     /**
175      * get the database server host (RMI Server only)
176      * @return the database server IP or hostname
177      */

178     public static String JavaDoc getDatabaseHost() {
179         Properties props = System.getProperties();
180         return props.getProperty("DATABASE_HOST");
181     }
182
183     /**
184      * get the database server port (RMI Server only)
185      * @return the database server port
186      */

187     public static String JavaDoc getDatabasePort() {
188         Properties props = System.getProperties();
189         return props.getProperty("DATABASE_PORT");
190     }
191
192     /**
193      * get the database server type (RMI Server only)
194      * @return the database server type
195      */

196     public static String JavaDoc getDatabaseType() {
197         Properties props = System.getProperties();
198         return props.getProperty("DATABASE_TYPE");
199     }
200
201     /**
202      * get the database name
203      * @return the database name
204      */

205     public static String JavaDoc getDatabaseName() {
206         Properties props = System.getProperties();
207         return props.getProperty("DATABASE_NAME");
208     }
209
210     /**
211      * get the database user id
212      * @return the database user id
213      */

214     public static String JavaDoc getDatabaseID() {
215         Properties props = System.getProperties();
216         return props.getProperty("DATABASE_ID");
217     }
218
219     /**
220      * get the database server user password
221      * @return the database user password
222      */

223     public static String JavaDoc getDatabasePassword() {
224         Properties props = System.getProperties();
225         return props.getProperty("DATABASE_PASSWORD");
226     }
227
228     /**
229      * get the rmi server host IP or hostname (RMI server only)
230      * @return the RMI server host or IP
231      */

232     public static String JavaDoc getServerHost() {
233         Properties props = System.getProperties();
234         return props.getProperty("SERVER_HOST");
235     }
236     /**
237      * get the local database name
238      * @return the local database name
239      */

240     public static String JavaDoc getLocalDB() {
241         Properties props = System.getProperties();
242         return props.getProperty("LOCAL_DB");
243     }
244     /**
245      * get the local database id
246      * @return the local database id
247      */

248     public static String JavaDoc getLocalDBID() {
249         Properties props = System.getProperties();
250         return props.getProperty("LOCAL_DB_ID");
251     }
252     /**
253      * get the local database password
254      * @return the local database password
255      */

256     public static String JavaDoc getLocalDBPSW() {
257         Properties props = System.getProperties();
258         return props.getProperty("LOCAL_DB_PSW");
259     }
260
261     /**
262      * get the user's home directory
263      * @return the user's home directory path
264      */

265     public static String JavaDoc getHomeDir() {
266         if (systemProps == null)
267             systemProps = System.getProperties();
268         return systemProps.getProperty("user.home");
269     }
270
271     /**
272      * get the system file separator character
273      * @return the file separator character
274      */

275     public static String JavaDoc getFileSep() {
276         if (systemProps == null)
277             systemProps = System.getProperties();
278
279         return systemProps.getProperty("file.separator");
280     }
281
282     /**
283      * get the full path name to the user's default
284      * property file
285      * @return the path name of the property file
286      */

287     public static String JavaDoc getApplPropsPath() {
288         String JavaDoc homeDir = getHomeDir();
289         return homeDir + getFileSep() + PREF_BASE;
290     }
291 }
292
293
Popular Tags