src/java/isac/util/users/UserSettings.java
branchjava_rename_format
changeset 3922 fcd5648113ca
equal deleted inserted replaced
963:b4896ce4d1b0 3922:fcd5648113ca
       
     1 /*
       
     2  * Created on Feb 22, 2005
       
     3  *
       
     4  * @author Alan Krempler
       
     5  */
       
     6 
       
     7 package isac.util.users;
       
     8 
       
     9 import java.io.FileInputStream;
       
    10 import java.io.FileNotFoundException;
       
    11 import java.io.FileOutputStream;
       
    12 import java.io.IOException;
       
    13 import java.util.Enumeration;
       
    14 import java.util.HashMap;
       
    15 import java.util.Map;
       
    16 import java.util.Properties;
       
    17 
       
    18 /**
       
    19  * Class for storing/retrieving user settings, i.e. settings which can be set
       
    20  * manually by the user as opposed to the
       
    21  * {@link isac.util.usersettings.UserModel#}, which is an abstraction of the
       
    22  * system's expeience with a user and canot be altered manually. As there is no
       
    23  * fixed set of user settings, you can regard this class as a simple storage
       
    24  * backend for key/value pairs.
       
    25  * 
       
    26  * @author Alan Krempler
       
    27  *  
       
    28  */
       
    29 public class UserSettings extends Properties implements IUserSettings {
       
    30 
       
    31     private HashMap storage_;
       
    32 
       
    33     String userpath_;
       
    34 
       
    35     String username_;
       
    36 
       
    37     public static Map valstr_valint_;
       
    38 
       
    39     /**
       
    40      * connect the String-representation of a UserSettings-value with the
       
    41      * respective Integer-representation
       
    42      * 
       
    43      * @see isac.uti.users.IUserSettings
       
    44      */
       
    45     private HashMap createValstrValint() {
       
    46         HashMap m = new HashMap();
       
    47         m.put("DUMMY", new Integer(IUserSettings.DUMMY));
       
    48         m.put("VAL1_SKIP_SPECIFY_TO_START_SOLVE", new Integer(
       
    49                 IUserSettings.VAL1_SKIP_SPECIFY_TO_START_SOLVE));
       
    50         m.put("VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS", new Integer(
       
    51                 IUserSettings.VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS));
       
    52         m.put("VAL2_FORMULAE_ONLY", new Integer(
       
    53                 IUserSettings.VAL2_FORMULAE_ONLY));
       
    54         m.put("VAL2_FORMULA_TACTIC_FORMULA_", new Integer(
       
    55                 IUserSettings.VAL2_FORMULA_TACTIC_FORMULA_));
       
    56         //m.put("",
       
    57         //		new Integer(IUserSettings.));
       
    58         return m;
       
    59     }
       
    60 
       
    61     public UserSettings(String userpath, String username) {
       
    62         userpath_ = userpath;
       
    63         username_ = username;
       
    64 
       
    65         //load Properties (keystr, valstr) temporarily
       
    66         Properties keystr_valstr = new Properties();
       
    67         String filename = userpath_ + username + "_settings.txt";
       
    68         try {
       
    69             FileInputStream propInFile = new FileInputStream(filename);
       
    70             keystr_valstr.load(propInFile);
       
    71             keystr_valstr.list(System.out);
       
    72         } catch (FileNotFoundException e) {
       
    73             System.err.println("Can?t find " + filename);
       
    74         } catch (IOException e) {
       
    75             System.err.println("I/O failed.");
       
    76         }
       
    77 
       
    78         //create HashMap (valstr, valint) temporarily
       
    79         HashMap valstr_valint = new HashMap();
       
    80         valstr_valint = this.createValstrValint();
       
    81 
       
    82         //create HashMap (keystr, \\valstr\\ valint) and store it
       
    83         storage_ = new HashMap();
       
    84         Enumeration keystrs = keystr_valstr.propertyNames();//WN050311 how
       
    85         // better ?
       
    86         String keystr = null;
       
    87         String valstr = null;
       
    88         Object valint = null;
       
    89         while (keystrs.hasMoreElements()) {
       
    90             keystr = (String) keystrs.nextElement();
       
    91             valstr = keystr_valstr.getProperty(keystr);
       
    92             valint = valstr_valint.get(valstr);
       
    93             storage_.put(keystr, valint);
       
    94         }
       
    95     }
       
    96 
       
    97     /**
       
    98      * @param key
       
    99      *            A string key
       
   100      * @return The string value associated with the key parameter; null, if
       
   101      *         nothing is stored under this key
       
   102      */
       
   103     public int getValue(String key) {
       
   104         return ((Integer) storage_.get(key)).intValue();
       
   105     }
       
   106 
       
   107     /**
       
   108      * @param key
       
   109      *            The key under which "value" is to be stored. Existing data
       
   110      *            unde the same key will be replaced.
       
   111      * @param value
       
   112      *            The value to be stored under "key"
       
   113      * @return true in case of success, false in case of error, for example if
       
   114      *         trying to replace stored values without proper permissions
       
   115      */
       
   116     public boolean setValue(String key, int value) {
       
   117         //TODO.WN050311 storage_.put(key, value);
       
   118         return true;
       
   119     }
       
   120 
       
   121     //WN050311 just for creation of the first file
       
   122     //beware of the fix path !
       
   123     public static void main(String[] args) {
       
   124         UserSettings s = new UserSettings("isac/xmldata/users/", "x");
       
   125         s.setProperty("start_worksheet", "SKIP_SPECIFY_START_SOLVE");
       
   126         s.setProperty("enter_specify", "SKIP_SPECIFY_START_SOLVE");
       
   127         s.setProperty("next_button", "FORMULAE_ONLY");
       
   128 
       
   129         String filename = "isac/xmldata/users/x_settings.txt";
       
   130         try {
       
   131             FileOutputStream propOutFile = new FileOutputStream(filename);
       
   132             s.store(propOutFile, "static dialog data of user 'x'");
       
   133 
       
   134         } catch (IOException e) {
       
   135             System.err.println("I/O failed.");
       
   136         }
       
   137     }
       
   138 
       
   139 }
       
   140