Author: jerome
Date: 2009-01-19 15:05:32 +0100 (Mon, 19 Jan 2009)
New Revision: 3484

Modified:
   
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/FileUtils.java
   
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/ccp_main.java
Log:
* Tooltip configuration is now stored on a simple file.
* Remove access to the settings.xml file.

Modified: 
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/FileUtils.java
===================================================================
--- 
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/FileUtils.java
   2009-01-19 13:12:14 UTC (rev 3483)
+++ 
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/FileUtils.java
   2009-01-19 14:05:32 UTC (rev 3484)
@@ -22,45 +22,32 @@
 
 
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 
 import javax.swing.text.html.HTMLDocument;
 import javax.swing.text.html.HTMLEditorKit;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.xml.sax.SAXException;
-
 public class FileUtils {
        
        private static HTMLEditorKit kit = new HTMLEditorKit();
     private static HTMLDocument  doc = new HTMLDocument();
+    private static File show = new File(System.getProperty("user.home") + 
File.separator + "MyTux" + File.separator 
+                       + "TuxDroidSettings" + File.separator + "tooltipState");
        
     /**
      * Parse a String with markup.
      * @param file
      * @return cleaned String as text.
      */
-       public static synchronized String Parse(File file){
+       public static synchronized String Parse(File file)
+    {
                //Getting file as string.
                String htmlAsString = FileUtils.getHtmlAsText(file);
                
@@ -82,7 +69,8 @@
         * @param file
         * @return a String containing html balises.
         */
-       private static String getHtmlAsText(File file){
+       private static String getHtmlAsText(File file)
+       {
                BufferedReader lecteurAvecBuffer = null;
            String ligne;
            String text = "";
@@ -106,176 +94,80 @@
        /**
         * Register display value to TuxDroid settings.
         */
-       public static void register(boolean show, File settings){
+       public static void register(boolean show, File settings)
+       {
                if(!settings.getAbsolutePath().endsWith(".xml")) return; //It's 
not a config file.
                if(!settings.isFile()) return; //settings doesn't exists yet.
                //else go go go !!!
-               FileUtils.write("ShowStartupHelp", settings, 
String.valueOf(show));
+               FileUtils.createEntry(FileUtils.show, show);
        }
+
        
        
        /**
-        * Detect encoding to make use if file exists.
-        * @throws IOException 
+        * Return true if user want to show window at startup.
+        * @param settings 
+        * @param string 
+        * @return
         */
-       private static String detectEncoding(File settings) throws IOException
-       {       
-               //Try to get xml file header.
-               String encoding = "";
-               BufferedReader br = null;
+       public static boolean getShowAtStartup()
+       {
+               if(!show.isFile())
+               {
+                       createEntry(show, true);
+                       return true;
+               }
                
+               //getting line value here.
+               String line = "";
                try
                {
-                       InputStream ips = new FileInputStream(settings); 
-                       InputStreamReader ipsr = new InputStreamReader(ips);
-                       br = new BufferedReader(ipsr);
-                       
-                       //Read the first line of xml file.
-                       encoding = br.readLine();
-                       encoding = 
encoding.substring(encoding.indexOf("encoding"));
-                       encoding = encoding.substring(encoding.indexOf("=") + 
1);
-                       encoding = encoding.substring(0, encoding.indexOf(" 
")).replace("\"", "");
-                       
-                       if(encoding.equalsIgnoreCase("UTF-8"))
+                       BufferedReader buff = new BufferedReader(new 
FileReader(show));
+
+                       try 
                        {
-                               encoding = "UTF-8";
-                       }
-                       
-                       else
+                               line = buff.readLine();
+                               
if(Boolean.valueOf(line.substring(line.indexOf("=") + 1 )))
+                               {
+                                       return true;
+                               }
+                               else
+                               {
+                                       return false;
+                               }
+                       } 
+                       finally 
                        {
-                               encoding = "ISO-8859-1";
+                               buff.close();
                        }
                        
-                       br.close();
-                       ipsr.close();
-                       ips.close();
-               }               
-               
-               catch (Exception e)
+               } 
+               catch (IOException ioe) 
                {
-                       br.close();
-               }
-               
-               return encoding;
-       }
-       
-       /**
-        * Register changes into xml file.
-        * @param document
-        */
-       private static synchronized void registerChanges(Document document, 
File file){
-               try {
-            // Create Dom source.
-            Source source = new DOMSource(document);
-            
-            //Create output.
-            Result resultat = new StreamResult(file);
-           
-            TransformerFactory fabrique = TransformerFactory.newInstance();
-            Transformer transformer = fabrique.newTransformer();
-            //Setting xml properties.
-            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-            transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
-            transformer.setOutputProperty(OutputKeys.ENCODING, 
detectEncoding(file));
-            
-            // Registration.
-            transformer.transform(source, resultat);
-            }
-               catch(Exception e){
-               e.printStackTrace();
-            }
-       }
-       
-       
-       /**
-        * This function return the current coplete parsed document.
-        * @return xmlReader.document as Document
-        */
-       private static Document getDocument(File file){
-               DocumentBuilderFactory fact = 
DocumentBuilderFactory.newInstance();
-        DocumentBuilder builder;
-               try {
-                       builder = fact.newDocumentBuilder();
-                       Document document;
-                       document = builder.parse(file);
-                       return document;
-               } catch (ParserConfigurationException e) {
-                       e.printStackTrace();
-               } catch (SAXException e) {
-                       e.printStackTrace();
-               } catch (IOException e) {
-                       e.printStackTrace();
-               }
-               return null;
-       }
-       
-       /**
-        * Write into specified markup.
-        * @param markup
-        */
-       public static void write(String markup, File file, String value){
-               Document document = FileUtils.getDocument(file);
-               try{
-                       Node langn = 
document.getElementsByTagName(markup).item(0);
-                       langn.getChildNodes().item(0).setNodeValue(value);
-                       FileUtils.registerChanges(document, file);
-               }catch(NullPointerException e){
-                       FileUtils.createKey(markup, file, value);
-               }
-       }
-       
-       
-       /**
-        * Return true if user want to show window at startup.
-        * @return
-        */
-       public static boolean getShowAtStartup(String markup, File file){
-               Document document = FileUtils.getDocument(file);
-               try{
-                       Node langn = 
document.getElementsByTagName(markup).item(0);
-                       return 
Boolean.valueOf(langn.getChildNodes().item(0).getTextContent());
-               }catch(NullPointerException e){
                        return true;
                }
        }
        
        
        /**
-        * Create key into xml file.
-        * @param key
+        * Registering value.
         * @param file
         * @param value
         */
-       public static void createKey(String key,File file, String value){
-               // Create simple xml format.
-               Document document = FileUtils.getDocument(file); //getting 
document.
+       public static void createEntry(File file, boolean value)
+       {
+               try 
+               {
+                       file.delete();
+                       file.createNewFile();
+                       BufferedWriter out = new BufferedWriter(new 
FileWriter(file));
+               out.write(String.format("start=%s", String.valueOf(value)));
+               out.close();
+               } 
+               catch (IOException e) 
+               {
+                       e.printStackTrace();
+               }
                
-        Element racine = 
(Element)document.getElementsByTagName("settings").item(0);
-        
-        Element showAtStartup = document.createElement("ShowStartupHelp");
-        showAtStartup.setTextContent(value);
-        
-        racine.appendChild(showAtStartup);
-        FileUtils.registerChanges(document, file);
        }
-       
-       
-       /**
-        * Return the control center language.
-        * @return
-        */
-       public static String getCcLanguage(File file){
-               Document doc = FileUtils.getDocument(file);
-               Node node = doc.getElementsByTagName("lang").item(0);
-               String lang = node.getChildNodes().item(0).getNodeValue();
-               return lang;
-       }
-       
-       
-       public static String getCcCountry(File file){
-               Document doc = FileUtils.getDocument(file);
-               Node node = doc.getElementsByTagName("country").item(0);
-               String country = node.getChildNodes().item(0).getNodeValue();
-               return country;
-       }
 }

Modified: 
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/ccp_main.java
===================================================================
--- 
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/ccp_main.java
    2009-01-19 13:12:14 UTC (rev 3483)
+++ 
software_suite_v2/software/tools/control_center_presentation/trunk/control_center_presentation/src/ccp_main.java
    2009-01-19 14:05:32 UTC (rev 3484)
@@ -48,8 +48,14 @@
        public TuxAPI tux;
        public static String THEMEPACK_PATH = 
InstallerPaths.tuxSkinlfThempack.getAbsolutePath();
        
-       public ccp_main(){
+       private String lang, country;
+       
+       public ccp_main(String lang, String country)
+       {
                
+               this.lang = lang;
+               this.country = country;
+               
                // On windows
                if (System.getProperty("os.name").contains("Windows"))
                {       
@@ -120,7 +126,7 @@
                tux.server.autoConnect(TuxAPIConst.CLIENT_LEVEL_FREE, 
"tooltip", "myTuxToolTip");
                tux.server.waitConnected(2.0);
                
-               Properties properties = new 
Properties(FileUtils.getCcLanguage(settings), FileUtils.getCcCountry(settings));
+               Properties properties = new Properties(this.lang, this.country);
                atStartup = new JCheckBox();
                atStartup.setUI(new javax.swing.plaf.metal.MetalButtonUI());
                atStartup.setText(properties.getStartupText());
@@ -162,7 +168,7 @@
                frame.add(p);           
                frame.setResizable(false);
                frame.setVisible(true);
-               
atStartup.setSelected(FileUtils.getShowAtStartup("ShowStartupHelp", settings));
+               atStartup.setSelected(FileUtils.getShowAtStartup());
                if(atStartup.isSelected()){
                        atStartup.setIcon(
                                        new 
ImageIcon(getClass().getResource("images/checkbox-active.png")));
@@ -189,7 +195,8 @@
         * Play languag attitune.
         * @param language
         */
-       public void playAttitune(String language){
+       public void playAttitune(String language)
+       {
                if(tux.tts.getVoices().size() <= 0)
                        return;
                
@@ -210,7 +217,8 @@
         * Main class.
         * @param args
         */
-       public static void main(String[] args){
-               new ccp_main();
+       public static void main(String[] args)
+       {
+               new ccp_main("en", "US");
        }
 }


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to