View Javadoc

1   package org.wcb.common;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   *  Filename:  $Id: UIDefaultsLoader.java,v 1.10 2004/06/09 18:53:30 wbogaardt Exp $
20   *
21   *  Abstract:  User Interface default loader for UIManager
22   *
23   */
24  import java.util.Properties;
25  import java.util.Enumeration;
26  import java.util.StringTokenizer;
27  import java.io.BufferedInputStream;
28  import java.io.FileInputStream;
29  import java.io.FileWriter;
30  import java.io.BufferedWriter;
31  import java.io.PrintWriter;
32  import java.io.IOException;
33  import java.io.FileOutputStream;
34  import java.awt.Color;
35  import java.awt.Font;
36  import javax.swing.*;
37  
38  import org.wcb.autohome.interfaces.X10DeviceConstants;
39  import org.wcb.autohome.AutoHomeAdminSession;
40  
41  public class UIDefaultsLoader implements X10DeviceConstants {
42  
43      public static final String DEFAULTFILENAME = "jhome.prop";
44      public static final String FILEHEADER =
45              "#This is the property file for bubblesoft UI views.\n"+
46              "# To set values in this file uncomment the line you wish to set a value\n"+
47              "# for (by deleting the # at the beginning) and then fill in a value after the =\n"+
48              "# For color values, start the value with 'Color.' and follow with either the name\n"+
49              "# of a color or at rgb value\n"+
50              "# example: Label.background=Color.black \n" +
51              "#          Label.background=Color.#ffffff \n" +
52              "# both of these are the same color.  \n" +
53              "# The valid color names are: white, black, red, blue, yellow, green, pink, \n"+
54              "# gray, orange, magenta, cyan"+
55              "# For font customization values, start the value with 'Font.' and follow the name,\n"+
56              "# style, size\n"+
57              "# example: Font. Dialog,2,10\n"+
58              "# The valid fonts are Dialog, DialogInput, SansSerif, Monospaced.\n"+
59              "# Valid styles are 0 = plain, 1 = Bold, 2 = Italic, 3 = Bold+Italic\n"+
60              "# Valid sizes are 9, 10, 12, 14, 18, 24";
61      private String userHome;
62      protected Properties swingProperties = null;
63      protected static String saveFileName;
64  
65      public UIDefaultsLoader()
66      {
67          this(DEFAULTFILENAME);
68      }
69  
70      public UIDefaultsLoader(String fileName)
71      {
72          this(fileName, null);
73      }
74  
75      public UIDefaultsLoader(String fileName, String directory)
76      {
77          String sFilename = DEFAULTFILENAME;
78          if (null == directory)
79          {
80              userHome = USER_HOME;
81          }
82          else
83              userHome = directory;
84          if(null == fileName)
85          {
86              sFilename = DEFAULTFILENAME;
87          }
88          else
89          {
90              sFilename = fileName;
91          }
92          swingProperties = load(userHome, sFilename);
93      }
94  
95      /***
96       * Load the specified properties file.  If the file does not exist, a
97       * default file will be created.
98       */
99      protected Properties load(String directory, String file)
100     {
101         Properties returnValue = null;
102         try
103         {
104             returnValue = loadProperties(directory, file);
105         }
106         catch (IOException e)
107         {
108             System.err.println("No jhome.prop file found. Now building default...");
109             try
110             {
111                 buildDefaultFile(directory,file);
112                 returnValue = loadProperties(directory, file);
113             }
114             catch(IOException ex)
115             {
116             }
117         }
118         return returnValue;
119     }
120 
121     /***
122      * Load the properties file.
123      */
124     protected Properties loadProperties(String directory, String file)
125             throws IOException
126     {
127         saveFileName = directory+SEPARATOR+file;
128         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(saveFileName));
129         Properties returnValue = new Properties();
130         returnValue.load(bis);
131         return returnValue;
132     }
133 
134     /***
135      * This sets the UI properties such as colors and font format
136      * from what was saved previously in jhome.prop file.
137      */
138     private void setUIProps()
139     {
140         if (null == swingProperties) return;
141         Enumeration enumr = swingProperties.keys();
142         Object nextElement;
143         Object value;
144         while (enumr.hasMoreElements())
145         {
146             nextElement = enumr.nextElement();
147             value = swingProperties.get(nextElement);
148             if (null != value)
149             {
150                 if (isColor(value.toString()))
151                 {
152                     Color cValue = convertToColor(value.toString());
153                     if (null != cValue)
154                         UIManager.put(nextElement, cValue);
155                 }
156                 if (isFont(value.toString()))
157                 {
158                     Font fValue = convertToFont(value.toString());
159                     if(null !=fValue)
160                         UIManager.put(nextElement, fValue);
161                 }
162             }
163         }
164     }
165 
166     /***
167      * sets the UIManger to the look and feel indicated by the
168      * cockpit properties file
169      */
170     public void initiateLF()
171     {
172         String value = AutoHomeAdminSession.getInstance().getLookAndFeel();
173         try
174         {
175             UIManager.setLookAndFeel(value);
176             if(value.equalsIgnoreCase(METAL) || value.equalsIgnoreCase(MAC) || value.equalsIgnoreCase(WINDOWS) || value.equalsIgnoreCase(MOTIF))
177             {
178                 this.setUIProps();
179             }
180         }
181         catch (UnsupportedLookAndFeelException e)
182         {
183         }
184         catch(InstantiationException ie)
185         {
186         }
187         catch(IllegalAccessException iae)
188         {
189         }
190         catch(ClassNotFoundException cnfe)
191         {
192         }
193         catch(Exception ex)
194         {
195         }
196     }
197 
198     public void setProperty(String sKey, String sValue)
199     {
200         swingProperties.setProperty(sKey, sValue);
201     }
202 
203     public Properties getProperties(){
204         if(swingProperties == null)
205         {
206             swingProperties = load(userHome,DEFAULTFILENAME);
207         }
208         return swingProperties;
209     }
210 
211     public String getProperty(String val)
212     {
213         return swingProperties.getProperty(val);
214     }
215 
216     private boolean isColor(String value)
217     {
218         boolean returnValue = false;
219         if (value.startsWith("Color"))
220         {
221             returnValue = true;
222         }
223         return returnValue;
224     }
225 
226     private Color convertToColor(String value)
227     {
228         Color returnValue = null;
229         value = value.substring(value.indexOf(".")+1);
230         returnValue = GuiLib.getColor(value);
231         if (null == returnValue)
232         {
233             try
234             {
235                 returnValue = Color.decode(value);
236             }
237             catch (Exception e)
238             {
239                 e.printStackTrace();
240             }
241         }
242         return returnValue;
243     }
244 
245     private boolean isFont(String value)
246     {
247         boolean returnValue = false;
248         if(value.startsWith("Font"))
249         {
250             returnValue = true;
251         }
252         return returnValue;
253     }
254 
255     private Font convertToFont(String value)
256     {
257         Font returnValue = null;
258         String family, sStyle, sSize;
259         int style, size;
260         StringTokenizer fontName=new StringTokenizer(value.substring(value.indexOf(".")+1),",");
261         family = fontName.nextToken();
262         sStyle = fontName.nextToken();
263         sSize = fontName.nextToken();
264         try {
265             style = Integer.parseInt(sStyle);
266             size = Integer.parseInt(sSize);
267             returnValue=new Font(family, style, size);
268         }
269         catch(NumberFormatException nfe)
270         {
271             returnValue = new Font("Dialog", 1, 12);
272         }
273         return returnValue;
274     }
275 
276     public void outputToScreen()
277     {
278         if (swingProperties != null)
279             try
280             {
281                 swingProperties.store(System.out, "Saving properties");
282             }
283             catch(IOException ioe)
284             {
285                 System.err.println("Error tying to save file properties "+ioe);
286             }
287     }
288 
289     /***
290      * build a generic properties file with no values loaded
291      * and have that files properties set to null and commented out
292      */
293     protected void buildDefaultFile(String directory, String file) throws IOException
294     {
295         UIDefaults uidef = UIManager.getDefaults();
296         Enumeration keys = uidef.keys();
297         saveFileName = directory+SEPARATOR+file;
298         PrintWriter out
299                 = new PrintWriter(new BufferedWriter(new FileWriter(saveFileName)));
300         out.println(FILEHEADER);
301         while (keys.hasMoreElements())
302         {
303             out.println("#"+keys.nextElement()+"=");
304         }
305         out.println(BROWSER + "=");
306         out.println(LAST_FILE + "=");
307         out.println(RMI_HOST + "=" + LOCALHOST);
308         out.flush();
309         out.close();
310     }
311 
312     public void saveProperties()
313     {
314         try
315         {
316             FileOutputStream output = new FileOutputStream(saveFileName);
317             swingProperties.store(output, FILEHEADER);
318             output.close();
319         }
320         catch(IOException ioe)
321         {
322             System.err.println("Error :"+ioe);
323         }
324     }
325 
326     /***
327      * Returns true if the specified property is empty.
328      */
329     public boolean isEmpty(String propName) {
330         String propValue = getProperty(propName);
331         return (propValue == null || propValue.trim().length() < 1);
332     }
333 
334     public void setProperties(Properties newProp) {
335            swingProperties = newProp;
336        }
337 
338     public static void main(String[] args)
339     {
340         UIDefaultsLoader uiDef = new UIDefaultsLoader();
341         uiDef.outputToScreen();
342         uiDef.setUIProps();
343     }
344 }
345 
346 
347 
348 
349 
350 
351 
352 
353 
354