View Javadoc

1   package com.explosion.expf.preferences;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.io.File;
24  import java.util.Hashtable;
25  import java.util.Properties;
26  import java.util.Vector;
27  import java.util.prefs.Preferences;
28  import javax.swing.UIManager;
29  import com.explosion.expf.Application;
30  import com.explosion.expf.ExpConstants;
31  import com.explosion.utilities.GeneralConstants;
32  import com.explosion.utilities.GeneralUtils;
33  import com.explosion.utilities.preferences.Preference;
34  
35  /***
36   * Author: Stephen Cowx Date: Nov 7, 2002 Time: 11:39:28 PM
37   */
38  
39  public class SystemPreferences
40  {
41  
42      private static Vector lafValues = new Vector();
43      private static Vector preferences = new Vector();;
44      private static Hashtable preferenceHash = new Hashtable();
45  
46      static
47      {
48          lafValues.addElement(UIManager.getSystemLookAndFeelClassName());
49          lafValues.addElement(ExpConstants.METAL);
50          lafValues.addElement(ExpConstants.MOTIF);
51          lafValues.addElement(ExpConstants.JGOOD_EXTWIN);
52          lafValues.addElement(ExpConstants.JGOOD_PLASTIC);
53          lafValues.addElement(ExpConstants.JGOOD_PLASTIC3D); 
54          lafValues.addElement(ExpConstants.JGOOD_PLASTICXP);
55      }
56  
57      public static Vector getPreferences()
58      {
59          return preferences;
60      }
61  
62      public Hashtable getPreferenceHash()
63      {
64          return preferenceHash;
65      }
66  
67      /***
68       * Builds a vector of Preference objects applicable to all applications
69       */
70      public static void initialisePreferences(Preferences prefs, Properties properties)
71      {
72          try
73          {
74              Preference appName = new Preference(ExpConstants.EXPF_APP_NAME, Preference.PROPERTY_TYPE_TEXT, "Unknown application name", prefs);
75              appName.setLongName("Application name");
76              appName.setDescription("This is the name of the application.");
77              appName.setEditable(false);
78              appName.setValue(properties.getProperty(ExpConstants.EXPF_APP_NAME));
79              appName.save();
80              appName.setVisible(false);
81              preferences.addElement(appName);
82              preferenceHash.put(ExpConstants.EXPF_APP_NAME, appName);
83  
84              Preference appVersion = new Preference(ExpConstants.EXPF_APP_VERSION, Preference.PROPERTY_TYPE_TEXT, "Unknown application version", prefs);
85              appVersion.setLongName("Application version");
86              appVersion.setDescription("This is the version of this application.");
87              appVersion.setEditable(false);
88              appVersion.setValue(properties.getProperty(ExpConstants.EXPF_APP_VERSION));
89              appVersion.save();
90              appVersion.setVisible(false);
91              preferences.addElement(appVersion);
92              preferenceHash.put(ExpConstants.EXPF_APP_VERSION, appVersion);
93  
94              Preference appPrefix = new Preference(ExpConstants.EXPF_APP_PREFIX, Preference.PROPERTY_TYPE_TEXT, "UnknownPrefix", prefs);
95              appPrefix.setLongName("Application prefix");
96              appPrefix.setDescription("This is a unique prefix used for this application for standard paths etc.");
97              appPrefix.setEditable(false);
98              appPrefix.setValue(properties.getProperty(ExpConstants.EXPF_APP_PREFIX));
99              appPrefix.save();
100             appPrefix.setVisible(false);
101             preferences.addElement(appPrefix);
102             preferenceHash.put(ExpConstants.EXPF_APP_PREFIX, appPrefix);
103 
104             Preference vendor = new Preference(ExpConstants.EXPF_APP_VENDOR, Preference.PROPERTY_TYPE_TEXT, "Unknown vendor", prefs);
105             vendor.setLongName("Application vendor");
106             vendor.setDescription("This is the company that created the application.");
107             vendor.setEditable(false);
108             vendor.setVisible(false);
109             vendor.setValue(properties.getProperty(ExpConstants.EXPF_APP_VENDOR));
110             vendor.save();
111             preferences.addElement(vendor);
112             preferenceHash.put(ExpConstants.EXPF_APP_VENDOR, vendor);
113 
114             Preference vendorURL = new Preference(ExpConstants.EXPF_APP_VENDOR_URL, Preference.PROPERTY_TYPE_TEXT, "http://www.explosion-it.net", prefs);
115             vendorURL.setLongName("Application vendor URL");
116             vendorURL.setDescription("This is the URL of the company that created the application.");
117             vendorURL.setEditable(false);
118             vendorURL.setVisible(false);
119             vendorURL.setValue(properties.getProperty(ExpConstants.EXPF_APP_VENDOR_URL));
120             vendorURL.save();
121             preferences.addElement(vendorURL);
122             preferenceHash.put(ExpConstants.EXPF_APP_VENDOR_URL, vendorURL);
123 
124             Preference author = new Preference(ExpConstants.EXPF_APP_AUTHOR, Preference.PROPERTY_TYPE_TEXT, "Unknown author", prefs);
125             author.setLongName("Author");
126             author.setDescription("This is the person who wrote the application.");
127             author.setEditable(false);
128             author.setVisible(false);
129             author.setValue(properties.getProperty(ExpConstants.EXPF_APP_AUTHOR));
130             author.save();
131             preferences.addElement(author);
132             preferenceHash.put(ExpConstants.EXPF_APP_AUTHOR, author);
133 
134             Preference year = new Preference(ExpConstants.EXPF_APP_COPYRIGHT_YEAR, Preference.PROPERTY_TYPE_TEXT, "2003", prefs);
135             year.setLongName("Copyright Year");
136             year.setDescription("This is the year from which the application has been copyrighted.");
137             year.setEditable(false);
138             year.setVisible(false);
139             year.setValue(properties.getProperty(ExpConstants.EXPF_APP_COPYRIGHT_YEAR));
140             year.save();
141             preferences.addElement(year);
142             preferenceHash.put(ExpConstants.EXPF_APP_COPYRIGHT_YEAR, year);
143 
144             String installDirString = prefs.get(ExpConstants.INSTALL_DIR, System.getProperty("user.home"));
145             Preference installDir = new Preference(ExpConstants.INSTALL_DIR, Preference.PROPERTY_TYPE_DIRECTORY, new File(GeneralConstants.DEFAULT_FILE), prefs);
146             installDir.setDescription("This is the directory into which the application has been installed.");
147             installDir.setLongName("Installation directory");
148             installDir.setEditable(false);
149             installDir.setVisible(false);
150             preferences.addElement(installDir);
151             preferenceHash.put(ExpConstants.INSTALL_DIR, installDir);
152 
153             Preference saveSettingsOnExit = new Preference(ExpConstants.SAVESETTINGSONEXIT, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(true), prefs);
154             saveSettingsOnExit.setDescription("This determines whether or not the application properties will be saved when the application shuts down.");
155             saveSettingsOnExit.setLongName("Save settings on exit");
156             preferences.addElement(saveSettingsOnExit);
157             preferenceHash.put(ExpConstants.SAVESETTINGSONEXIT, saveSettingsOnExit);
158 
159             Preference lastDir = new Preference(ExpConstants.LASTDIR, Preference.PROPERTY_TYPE_TEXT, System.getProperty("user.home"), prefs);
160             lastDir.setLongName("Last used directory");
161             lastDir.setDescription("This is the last directory that was used, most Open dialogs default to here");
162             lastDir.setEditable(false);
163             lastDir.setVisible(false);
164             preferences.addElement(lastDir);
165             preferenceHash.put(ExpConstants.LASTDIR, lastDir);
166 
167             Preference tempDir = new Preference(ExpConstants.TEMPDIR, Preference.PROPERTY_TYPE_DIRECTORY, new File(System.getProperty("java.io.tmpdir")), prefs);
168             tempDir.setLongName("Temporary directory");
169             tempDir.setDescription("This is the directory to where temporary files will be written.");
170             preferences.addElement(tempDir);
171             preferenceHash.put(ExpConstants.TEMPDIR, tempDir);
172 
173             Preference helpFile = new Preference(ExpConstants.HELP_FILE_NAME, Preference.PROPERTY_TYPE_TEXT, properties.getProperty(ExpConstants.EXPF_APP_HELP_FILE), prefs);
174             helpFile.setLongName("Help file name");
175             helpFile.setDescription("This is the name of the file which is used to initalise the help for this application.");
176             if (properties.getProperty(ExpConstants.EXPF_APP_HELP_FILE) != null)
177                 helpFile.setValue(properties.getProperty(ExpConstants.EXPF_APP_HELP_FILE));
178             preferences.addElement(helpFile);
179             helpFile.setEditable(false);
180             helpFile.setVisible(false);
181             preferenceHash.put(ExpConstants.HELP_FILE_NAME, helpFile);
182 
183             Preference helpStartUpID = new Preference(ExpConstants.HELP_STARTUP_ID, Preference.PROPERTY_TYPE_TEXT, "", prefs);
184             helpStartUpID.setLongName("Help Startup Page ID");
185             helpStartUpID.setDescription("This is the ID of the page you want to be displayed 1st in your help screen.  It corresponds to the value in your Helpset Map file.");
186             if (properties.getProperty(ExpConstants.EXPF_APP_HELP_STARTUPID) != null)
187                 helpStartUpID.setValue(properties.getProperty(ExpConstants.EXPF_APP_HELP_STARTUPID));
188             preferences.addElement(helpStartUpID);
189             helpStartUpID.setEditable(false);
190             helpStartUpID.setVisible(false);
191             preferenceHash.put(ExpConstants.HELP_STARTUP_ID, helpStartUpID);
192 
193             Preference helpEmbedded = new Preference(ExpConstants.HELP_EMBEDDED, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(true), prefs);
194             helpEmbedded.setDescription("This determines whether or not the application help should be displayed as an embedded part of the application or in a frame of it's own.");
195             helpEmbedded.setLongName("Help embedded");
196             preferences.addElement(helpEmbedded);
197             preferenceHash.put(ExpConstants.HELP_EMBEDDED, helpEmbedded);
198 
199             Preference helpHeight = new Preference(ExpConstants.HELP_HEIGHT, Preference.PROPERTY_TYPE_INT, new Integer(500), prefs);
200             helpHeight.setLongName("Help window height");
201             helpHeight.setDescription("This is the saved Help window height.");
202             helpHeight.setEditable(false);
203             helpHeight.setVisible(false);
204             preferences.addElement(helpHeight);
205             preferenceHash.put(ExpConstants.HELP_HEIGHT, helpHeight);
206 
207             Preference helpWidth = new Preference(ExpConstants.HELP_WIDTH, Preference.PROPERTY_TYPE_INT, new Integer(700), prefs);
208             helpWidth.setLongName("Help window width");
209             helpWidth.setDescription("This is the saved Help window width.");
210             helpWidth.setEditable(false);
211             helpWidth.setVisible(false);
212             preferences.addElement(helpWidth);
213             preferenceHash.put(ExpConstants.HELP_WIDTH, helpWidth);
214 
215             Preference lookAndFeel = new Preference(ExpConstants.LAF, Preference.PROPERTY_TYPE_STRING_CHOICE, UIManager.getSystemLookAndFeelClassName(), prefs);
216             lookAndFeel.setLongName("Look and Feel");
217             lookAndFeel.setDescription("This property sets the Look and Feel of the application.");
218             lookAndFeel.setChoiceValues(lafValues);
219             preferences.addElement(lookAndFeel);
220             preferenceHash.put(ExpConstants.LAF, lookAndFeel);
221 
222             Preference startMaximised = new Preference(ExpConstants.STARTMAXIMISED, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(true), prefs);
223             startMaximised.setDescription("This determines whether or not the application will start maximised.");
224             startMaximised.setLongName("Start maximised");
225             preferences.addElement(startMaximised);
226             preferenceHash.put(ExpConstants.STARTMAXIMISED, startMaximised);
227             
228             Preference height = new Preference(ExpConstants.HEIGHT, Preference.PROPERTY_TYPE_INT, new Integer(550), prefs);
229             height.setLongName("Application height");
230             height.setDescription("This is the saved application height.");
231             height.setEditable(false);
232             height.setVisible(false);
233             preferences.addElement(height);
234             preferenceHash.put(ExpConstants.HEIGHT, height);
235 
236             Preference width = new Preference(ExpConstants.WIDTH, Preference.PROPERTY_TYPE_INT, new Integer(750), prefs);
237             width.setLongName("Application Width");
238             width.setDescription("This is the saved application Width.");
239             width.setEditable(false);
240             width.setVisible(false);
241             preferences.addElement(width);
242             preferenceHash.put(ExpConstants.WIDTH, width);
243             
244             Preference startCentered = new Preference(ExpConstants.STARTCENTERED, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(false), prefs);
245             startCentered.setDescription("This determines whether or not the application will start in the middle of the screen or if it will go to the last known position.");
246             startCentered.setLongName("Start centered");
247             preferences.addElement(startCentered);
248             preferenceHash.put(ExpConstants.STARTCENTERED, startCentered);
249             
250             Preference xpos = new Preference(ExpConstants.XPOS, Preference.PROPERTY_TYPE_INT, new Integer(0), prefs);
251             xpos.setLongName("X position of application");
252             xpos.setDescription("This is the saved X position of the application.");
253             xpos.setEditable(false);
254             xpos.setVisible(false);
255             preferences.addElement(xpos);
256             preferenceHash.put(ExpConstants.XPOS, xpos);
257 
258             Preference ypos = new Preference(ExpConstants.YPOS, Preference.PROPERTY_TYPE_INT, new Integer(0), prefs);
259             ypos.setLongName("Y position of application");
260             ypos.setDescription("This is the saved Y position of the application");
261             ypos.setEditable(false);
262             ypos.setVisible(false);
263             preferences.addElement(ypos);
264             preferenceHash.put(ExpConstants.YPOS, ypos);
265 
266             Preference baseFont = new Preference(ExpConstants.BASE_FONT, Preference.PROPERTY_TYPE_FONT, GeneralConstants.DEFAULT_WINDOW_FONT, prefs);
267             baseFont.setLongName("Application base font");
268             baseFont.setDescription("This is the default application font which gets used for windows, menu's , headings, titles etc.");
269             preferences.addElement(baseFont);
270             preferenceHash.put(ExpConstants.BASE_FONT, baseFont);
271 
272             Preference windowFont = new Preference(ExpConstants.WINDOW_FONT, Preference.PROPERTY_TYPE_FONT, GeneralConstants.DEFAULT_TEXTAREA_FONT, prefs);
273             windowFont.setLongName("Text font");
274             windowFont.setDescription("This is the default text font which gets used for text areas etc.");
275             preferences.addElement(windowFont);
276             preferenceHash.put(ExpConstants.WINDOW_FONT, windowFont);
277 
278         }
279         catch (Exception e)
280         {
281             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while initialising properties.");
282         }
283     }
284 
285     /***
286      * Initialises the logging preferences
287      * 
288      * @param prefs
289      * @param appPrefix
290      * @throws Exception
291      */
292     public static void initilaiseloggingPreferences(Preferences prefs) throws Exception
293     {
294         File currentDirectory = GeneralUtils.getSafeUserDir();
295             
296         Preference logErrors = new Preference(ExpConstants.LOGERRORS, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(true), prefs);
297         logErrors.setLongName("Log errors");
298         logErrors.setDescription("Determines whether errors will be logged to file or not.");
299         preferences.addElement(logErrors);
300         preferenceHash.put(ExpConstants.LOGERRORS, logErrors);
301 
302         Preference errorFile = new Preference(ExpConstants.ERROR_FILE, Preference.PROPERTY_TYPE_FILE, new File(currentDirectory, "error.txt"), prefs);
303         errorFile.setLongName("Error file");
304         errorFile.setDescription("This is the file into which errors will be written.");
305         preferences.addElement(errorFile);
306         preferenceHash.put(ExpConstants.ERROR_FILE, errorFile);
307 
308         Vector logLevelValues = new Vector();
309         logLevelValues.add(ExpConstants.LOG_LEVEL_FATAL);
310         logLevelValues.add(ExpConstants.LOG_LEVEL_ERROR);
311         logLevelValues.add(ExpConstants.LOG_LEVEL_INFO);
312         logLevelValues.add(ExpConstants.LOG_LEVEL_WARN);
313         logLevelValues.add(ExpConstants.LOG_LEVEL_DEBUG);
314 
315         Preference rootLogLevel = new Preference(ExpConstants.ROOT_LOG_LEVEL, Preference.PROPERTY_TYPE_STRING_CHOICE, ExpConstants.LOG_LEVEL_ERROR, prefs);
316         rootLogLevel.setLongName("Root log level");
317         rootLogLevel.setDescription("This property sets the base logging level for all components.  It can be overridden for individual components using the Custom Logging facility.");
318         rootLogLevel.setChoiceValues(logLevelValues);
319         preferences.addElement(rootLogLevel);
320         preferenceHash.put(ExpConstants.ROOT_LOG_LEVEL, rootLogLevel);
321 
322         Preference logToFile = new Preference(ExpConstants.LOG_TO_FILE, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(true), prefs);
323         logToFile.setLongName("Log to file");
324         logToFile.setDescription("Determines whether logs will be written to file or not.");
325         preferences.addElement(logToFile);
326         preferenceHash.put(ExpConstants.LOG_TO_FILE, logToFile);
327 
328         Preference logFile = new Preference(ExpConstants.LOG_FILE, Preference.PROPERTY_TYPE_FILE, new File(currentDirectory, "log.txt"), prefs);
329         logFile.setLongName("Log file");
330         logFile.setDescription("This is the file into which logs will be written.");
331         preferences.addElement(logFile);
332         preferenceHash.put(ExpConstants.LOG_FILE, logFile);
333 
334         Preference logToConsole = new Preference(ExpConstants.LOG_TO_CONSOLE, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(true), prefs);
335         logToConsole.setLongName("Log to console");
336         logToConsole.setDescription("Determines whether logs will be written to the console or not.");
337         preferences.addElement(logToConsole);
338         preferenceHash.put(ExpConstants.LOG_TO_CONSOLE, logToConsole);
339 
340         Preference customLogLevel = new Preference(ExpConstants.CUSTOM_LOG_LEVEL, Preference.PROPERTY_TYPE_STRING_CHOICE, ExpConstants.LOG_LEVEL_DEBUG, prefs);
341         customLogLevel.setLongName("Custom log level");
342         customLogLevel.setDescription("This property sets the logging level for the custom logs.  Custom logs will only report on the components whose names are in the 'Custom Log Entries' list.");
343         customLogLevel.setChoiceValues(logLevelValues);
344         preferences.addElement(customLogLevel);
345         preferenceHash.put(ExpConstants.CUSTOM_LOG_LEVEL, customLogLevel);
346 
347         Preference customLogValues = new Preference(ExpConstants.CUSTOM_LOG_VALUES, Preference.PROPERTY_TYPE_COLLECTION, Preference.PROPERTY_TYPE_TEXT, "com.explosion.datastream", prefs);
348         customLogValues.setLongName("Custom log entries");
349         customLogValues.setDescription("This is a list of components which will be reported on at the level of debug specified by the preference 'Custom log level'.");
350         preferences.addElement(customLogValues);
351         preferenceHash.put(ExpConstants.CUSTOM_LOG_VALUES, customLogValues);
352 
353         Preference logPattern = new Preference(ExpConstants.LOG_PATTERN, Preference.PROPERTY_TYPE_TEXT, "%5p [%t] (%F:%L) - %m%n", prefs);
354         logPattern.setLongName("Log pattern");
355         logPattern.setDescription("This is the pattern which will be applied to each log message.");
356         preferences.addElement(logPattern);
357         preferenceHash.put(ExpConstants.LOG_PATTERN, logPattern);
358 
359     }
360 
361     public static Preference getPreference(String preferenceName)
362     {
363         return (Preference) preferenceHash.get(preferenceName);
364     }
365 
366     public static void applyPreferences()
367     {
368         Application.getInstance().updateLookAndFeel();
369     }
370 
371 }
372