View Javadoc

1   package com.explosion.expf.supportmodules;
2   
3   import java.awt.Color;
4   import java.awt.Font;
5   import java.io.File;
6   import java.io.FileWriter;
7   import java.util.Properties;
8   import java.util.Vector;
9   
10  import javax.swing.JPanel;
11  
12  import com.explosion.expf.Application;
13  import com.explosion.expf.ExpActionListener;
14  import com.explosion.expf.ExpModuleManager;
15  import com.explosion.expf.preferences.SystemPreferences;
16  import com.explosion.utilities.GeneralConstants;
17  import com.explosion.utilities.preferences.Preference;
18  
19  /*
20   * =============================================================================
21   * 
22   * Copyright 2004 Stephen Cowx
23   * 
24   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
25   * use this file except in compliance with the License. You may obtain a copy of
26   * the License at
27   * 
28   * http://www.apache.org/licenses/LICENSE-2.0
29   * 
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
32   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
33   * License for the specific language governing permissions and limitations under
34   * the License.
35   * 
36   * =============================================================================
37   */
38  
39  /***
40   * @author Stephen Created on Apr 10, 2004
41   */
42  public class ApplicationMetadataSupportModule implements ExpModuleManager
43  {
44  
45      private static final String NL = GeneralConstants.LS;
46  
47      /***
48       * @see com.explosion.expf.ExpModuleManager#initialiseGui()
49       * @throws Exception
50       */
51      public void initialiseGui() throws Exception
52      {}
53  
54      /***
55       * @see com.explosion.expf.ExpModuleManager#getVersion()
56       * @return
57       */
58      public String getVersion()
59      {
60          return "1.0";
61      }
62  
63      /***
64       * @see com.explosion.expf.ExpModuleManager#getName()
65       * @return
66       */
67      public String getName()
68      {
69          return "Application Metadata Support";
70      }
71  
72      /***
73       * @see com.explosion.expf.ExpModuleManager#getDescription()
74       * @return
75       */
76      public String getDescription()
77      {
78          return "This module assits with developing other modules by providing formatted meta data information.";
79      }
80  
81      /***
82       * @param moduleAttributes
83       * @see com.explosion.expf.ExpModuleManager#initialiseCore(Properties)
84       */
85      public void initialiseCore(Properties properties)
86      {
87          dumpPreferences();
88      }
89  
90      /***
91       * @see com.explosion.expf.ExpModuleManager#getPreferences()
92       * @return
93       */
94      public Vector getPreferences()
95      {
96          return null;
97      }
98  
99      /***
100      * @see com.explosion.expf.ExpModuleManager#getPreference(java.lang.String)
101      * @param preferenceName
102      * @return
103      */
104     public Preference getPreference(String preferenceName)
105     {
106         return null;
107     }
108 
109     /***
110      * @see com.explosion.expf.ExpModuleManager#getPreferencesEditor()
111      * @return
112      */
113     public JPanel getPreferencesEditor()
114     {
115         return null;
116     }
117 
118     /***
119      * @see com.explosion.expf.ExpModuleManager#getGlobalListener()
120      * @return
121      */
122     public ExpActionListener getGlobalListener()
123     {
124         return null;
125     }
126 
127     /***
128      * Writes the application properties out to a html format file, Yea, this is
129      * dirty. So what, it's useful for now.
130      */
131     public void dumpPreferences()
132     {
133         try
134         {
135 
136             /* Dump system preferences */
137             dumpPreferences("General system", SystemPreferences.getPreferences());
138             Vector modules = Application.getModules();
139 
140             /* Dump module preferences */
141             for (int i = 0; i < modules.size(); i++)
142             {
143                 ExpModuleManager manager = (ExpModuleManager) modules.elementAt(i);
144                 dumpPreferences(manager.getName(), manager.getPreferences());
145             }
146 
147         } catch (Exception e)
148         {
149             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while saving properties.  It is likely that they have not been properly saved.");
150         }
151     }
152 
153     /***
154      * Set up table headers etc before piling into the details
155      * 
156      * @param displayName
157      * @param preferences
158      * @param buffer
159      * @throws Exception
160      */
161     private void dumpPreferences(String displayName, Vector preferences) throws Exception
162     {
163         File file = new File("Preference_"+displayName.replaceAll(" ","_")+".htm");
164         //@TODO Make this path relative to the Application home.
165         StringBuffer buffer = new StringBuffer();
166         buffer.append("<html>" + NL);
167         buffer.append("<head>" + NL);
168         buffer.append("<title>Datastream Professional</title>" + NL);
169         buffer.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">" + NL);
170         buffer.append("<link rel=\"stylesheet\" href=\"../styles.css\" type=\"text/css\">" + NL);
171         buffer.append("</head>" + NL);
172         buffer.append("<body>" + NL);
173         buffer.append("<table width=\"100%\" border=\"0\">" + NL);
174 
175         boolean oneTableForEntireSet = true;
176         if (preferences == null || preferences.size() == 0)
177             return;
178 
179         buffer.append("<tr> " + NL);
180         buffer.append("  <td colspan=\"3\">&nbsp;</td>" + NL);
181         buffer.append("</tr>" + NL);
182 
183         dumpSet(displayName, preferences, buffer);
184 
185         buffer.append("</table>" + NL);
186         buffer.append(" </td>" + NL);
187         buffer.append("</tr>" + NL);
188 
189         buffer.append("</body>" + NL);
190         buffer.append("</html>" + NL);
191 
192         FileWriter stream = new FileWriter(file, false);
193         try
194         {
195             stream.write(buffer.toString());
196         } finally
197         {
198             if (stream != null)
199                 stream.close();
200         }
201     }
202 
203     /***
204      * Code for dumping a single set of properties
205      * 
206      * @param displayName
207      * @param preferences
208      * @param buffer
209      * @throws Exception
210      */
211     private void dumpSet(String displayName, Vector preferences, StringBuffer buffer) throws Exception
212     {
213         buffer.append("<tr> " + NL);
214         buffer.append("  <th colspan=\"3\"><a name=\"" + displayName + "\">" + displayName + " Preferences </th>" + NL);
215         buffer.append("</tr>" + NL);
216         buffer.append("<tr> " + NL);
217         buffer.append("  <th>Preference name</th>" + NL);
218         buffer.append("  <th>Default</th>" + NL);
219         buffer.append("   <th>Useage</th>" + NL);
220         buffer.append(" </tr>" + NL);
221 
222         if (preferences != null)
223         {
224             for (int i = 0; i < preferences.size(); i++)
225             {
226                 Preference preference = ((Preference) preferences.elementAt(i));
227                 buffer.append("<tr> " + NL);
228                 buffer.append("  <td>" + preference.getLongName() + "</td>" + NL);
229 
230                 if (preference.getType() == Preference.PROPERTY_TYPE_COLOR)
231                 {
232                     Color color = (Color) preference.getDefaultValue();
233                     String colorString = "#" + Integer.toHexString(color.getGreen()) + Integer.toHexString(color.getRed()) + Integer.toHexString(color.getBlue());
234                     buffer.append("  <td><table><tr><td>" + "RGB: " + colorString + "</td><td bgcolor=\"" + colorString + "\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table></td>" + NL);
235                 } else if (preference.getType() == Preference.PROPERTY_TYPE_FONT)
236                 {
237                     Font font = (Font) preference.getDefaultValue();
238                     String styleString = "style=\"font-family: " + font.getFamily() + ";font-size: " + font.getSize() + "px;\"";
239                     buffer.append("  <td " + styleString + ">" + font.getFamily() + " Size: " + font.getSize() + "</td>" + NL);
240                 } else
241                 {
242                     buffer.append("  <td>" + preference.getDefaultValue() + "</td>" + NL);
243                 }
244 
245                 buffer.append("   <td>" + preference.getDescription() + "</td>" + NL);
246                 buffer.append("</tr>" + NL);
247             }
248         }
249     }
250 
251 }