home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / SettingsObject.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  6.4 KB  |  306 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. //
  5. //
  6. // SettingsObject
  7. //
  8. // Class to encapsulate the file (or whatever) that is going to 
  9. // store program settings.
  10. // We keep everything in memory, so make sure you don't store
  11. // TOO much stuff in here. :)
  12. // 7/30/97 TJS
  13.  
  14. import java.io.*;
  15. import java.util.*;
  16. import com.ms.ui.*;
  17. import com.ms.fx.FxToolkit;
  18.  
  19. public class SettingsObject 
  20. {
  21.     private Hashtable settingsTable;
  22.     private String sFileName;
  23.     
  24.     public SettingsObject(String filename)
  25.     {
  26.         init(filename);
  27.     }
  28.     
  29.     private void init(String filename)
  30.     {
  31.         sFileName = filename;
  32.         settingsTable = new Hashtable(5);
  33.     }
  34.     
  35.     protected void loadDefaults()
  36.     {
  37.     }
  38.     
  39.     // internal method to set an option
  40.     private void setValue(String section, String key, String value)
  41.     {
  42.         Hashtable sectionTable = (Hashtable)settingsTable.get(section);
  43.         if (sectionTable == null)
  44.         {
  45.             sectionTable = new Hashtable(11);
  46.             settingsTable.put(section, sectionTable);                    
  47.         }
  48.         
  49.         sectionTable.put(key, value);        
  50.     }
  51.     
  52.     // internal method to get an option. 
  53.     private String getValue(String section, String key)
  54.     {
  55.         Hashtable sectionTable = (Hashtable)settingsTable.get(section);
  56.         if (sectionTable == null)
  57.         {
  58.             return null;
  59.         }
  60.         
  61.         return (String)sectionTable.get(key);
  62.     }
  63.     
  64.     // public methods to set options. We overload the setOption
  65.     // method to let you pass it strings, ints, or booleans. 
  66.     // All are converted to strings and then saved using setValue()
  67.     public void put(String section, String key, String value)
  68.     {
  69.         setValue(section, key, value);
  70.     }
  71.     
  72.     public void put(String section, String key, int value)
  73.     {
  74.         setValue(section, key, String.valueOf(value));
  75.     }
  76.     
  77.     public void put(String section, String key, boolean value)
  78.     {
  79.         setValue(section, key, String.valueOf(value));
  80.     }
  81.     
  82.     // bunch of get() methods. Different methods allow you
  83.     // to have data returned in String, int, or boolean format.
  84.     // returns the last parameter passed if the key was not found.
  85.     
  86.     // get() is the default method to grab data from this table...
  87.     // it returns a String. It's the same as getString()
  88.     public String get(String section, String key, String def)
  89.     {
  90.         return getString(section, key, def);
  91.     }
  92.     
  93.     public String getString(String section, String key, String def)
  94.     {
  95.         String s = getValue(section, key);
  96.         
  97.         if (s != null)
  98.         {
  99.             return s;
  100.         }
  101.         else
  102.         {
  103.             return def;
  104.         }
  105.     }    
  106.     
  107.     public int getInt(String section, String key, int def)
  108.     {
  109.         int i = 0;
  110.         try
  111.         {
  112.             i = Integer.parseInt(getValue(section, key));
  113.         }
  114.         catch (NumberFormatException e)
  115.         {
  116.             i = def;
  117.         }
  118.         
  119.         return i;
  120.     }
  121.     
  122.     // getOptionBoolean() returns either false if the string
  123.     // is not equal to "true", so there's no need to pass
  124.     // a default value.
  125.     public boolean getBoolean(String section, String key)
  126.     {
  127.         return Boolean.valueOf(getValue(section, key)).booleanValue();
  128.     }    
  129.     
  130.     public Enumeration getKeys(String section)
  131.     {
  132.         Hashtable sectionTable = (Hashtable)settingsTable.get(section);
  133.         if (sectionTable == null)
  134.         {
  135.             return null;
  136.         }
  137.         return sectionTable.keys();
  138.     }
  139.     
  140.     public boolean load()
  141.     {
  142.         DataInputStream inputStream = null;
  143.         
  144.         try 
  145.         {
  146.             inputStream = new DataInputStream(new FileInputStream(sFileName));
  147.         }
  148.         catch (IOException e)
  149.         {
  150.             // error reading settings file, using default settings
  151.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  152.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  153.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOSETTINGS),
  154.                 UIMessageBox.STOP, UIButtonBar.OK);
  155.             _box_.doModal();
  156.             
  157.             return false;
  158.         }
  159.         
  160.         boolean ret = true;
  161.         
  162.         try
  163.         {
  164.             readData(inputStream);
  165.         }
  166.         catch (IOException e)
  167.         {
  168.             // error reading settings
  169.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  170.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  171.                 JNotePad.loadString(ResourceIDs.IDS_MSGSETTINGSERR),
  172.                 UIMessageBox.STOP, UIButtonBar.OK);
  173.             _box_.doModal();
  174.             
  175.             ret = false;
  176.         }
  177.         
  178.         try
  179.         {
  180.             inputStream.close();
  181.         }
  182.         catch (IOException e)
  183.         {
  184.         }
  185.         
  186.         return ret;
  187.     }
  188.     
  189.     public boolean save()
  190.     {
  191.         DataOutputStream outputStream = null;
  192.         
  193.         try 
  194.         {
  195.             outputStream = new DataOutputStream(new FileOutputStream(sFileName));
  196.         }
  197.         catch (IOException e)
  198.         {
  199.             // error saving settings
  200.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  201.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  202.                 JNotePad.loadString(ResourceIDs.IDS_MSGSETTINGSSAVEERR),
  203.                 UIMessageBox.STOP, UIButtonBar.OK);
  204.             _box_.doModal();                                                        
  205.             
  206.             return false;
  207.         }
  208.         
  209.         boolean ret = true;
  210.         
  211.         try
  212.         {
  213.             writeData(outputStream);
  214.         }
  215.         catch (IOException e)
  216.         {
  217.             ret = false;
  218.         }
  219.         
  220.         try
  221.         {
  222.             outputStream.close();
  223.         }
  224.         catch (IOException e)
  225.         {
  226.         }
  227.         
  228.         return ret;
  229.     }
  230.     
  231.     private void writeData(DataOutputStream outputStream) throws IOException
  232.     {
  233.         Enumeration e = settingsTable.keys();
  234.         while (e.hasMoreElements())
  235.         {    
  236.             String sectionName = (String)e.nextElement();
  237.             Hashtable sectionTable = (Hashtable)settingsTable.get(sectionName);
  238.             
  239.             outputStream.writeBytes("\r\n["+sectionName+"]\r\n");
  240.             
  241.             Enumeration e2 = sectionTable.keys();
  242.             while (e2.hasMoreElements())
  243.             {
  244.                 String key = (String)e2.nextElement();
  245.                 String value = (String)sectionTable.get(key);
  246.                 // !! This should be an unnecessary test
  247.                 if (value == null)
  248.                 {
  249.                     return;
  250.                 }
  251.                 
  252.                 outputStream.writeBytes(key+"="+value+"\r\n");
  253.             }                
  254.         }
  255.         
  256.         return;
  257.     }
  258.     
  259.     private void readData(DataInputStream inputStream) throws IOException
  260.     {
  261.         String s = null;
  262.         String sectionName = null;
  263.         String key = null;
  264.         String value = null;
  265.         
  266.         clear();
  267.         
  268.         while ((s = inputStream.readLine()) != null)
  269.         {        
  270.             if (s.length() > 0)
  271.             {
  272.                 if (s.charAt(0) == '[')
  273.                 {
  274.                     sectionName = s.substring(1, s.length()-1);
  275.                 }
  276.                 else
  277.                 {
  278.                     int iIndex = s.indexOf('=');
  279.                     if (iIndex < 0)
  280.                     {
  281.                         throw new IOException();
  282.                     }
  283.                     key = s.substring(0, iIndex);
  284.                     value = s.substring(iIndex+1);
  285.                     
  286.                     setValue(sectionName, key, value);
  287.                 }
  288.             }
  289.         }
  290.     }
  291.     
  292.     public void clear()
  293.     {
  294.         Enumeration e = settingsTable.elements();
  295.         
  296.         while (e.hasMoreElements())
  297.         {
  298.             ((Hashtable)e.nextElement()).clear();
  299.         }
  300.         
  301.         settingsTable.clear();
  302.     }        
  303.     
  304. }
  305.  
  306.