View Javadoc
1   package org.opentrafficsim.editor;
2   
3   import java.io.File;
4   import java.io.FileReader;
5   import java.io.FileWriter;
6   import java.io.IOException;
7   import java.util.ArrayList;
8   import java.util.Arrays;
9   import java.util.List;
10  import java.util.Properties;
11  
12  import org.djutils.exceptions.Throw;
13  
14  /**
15   * Stores preferences, recently opened files, etc.
16   * <p>
17   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public class ApplicationStore
23  {
24  
25      /** Maximum number of recent files. */
26      private static final int MAX = 10;
27      
28      /** Store of loaded and set properties. */
29      private final Properties store = new Properties();
30  
31      /** Application name. */
32      private final String applicationName;
33      
34      /** File to store settings. */
35      private final String file;
36  
37      /**
38       * Constructor. Properties are stored under "{user.home}/{enterprise}/{application}.ini".
39       * @param enterpriseName String; name of the enterprise.
40       * @param applicationName String; name of the application.
41       */
42      public ApplicationStore(final String enterpriseName, final String applicationName)
43      {
44          Throw.whenNull(enterpriseName, "Enterprise may not bee null.");
45          this.applicationName = applicationName;
46          this.file =
47                  System.getProperty("user.home") + File.separator + enterpriseName + File.separator + applicationName + ".ini";
48          if (new File(this.file).exists())
49          {
50              try
51              {
52                  FileReader reader = new FileReader(this.file);
53                  this.store.load(reader);
54              }
55              catch (IOException ex)
56              {
57                  //
58              }
59          }
60      }
61  
62      /**
63       * Returns the property value.
64       * @param key String; key.
65       * @return String; property value, or {@code null} if no value is given.
66       */
67      public synchronized String getProperty(final String key)
68      {
69          Throw.whenNull(key, "Key may not be null.");
70          return (String) this.store.get(key);
71      }
72  
73      /**
74       * Sets a property value.
75       * @param key String; key.
76       * @param value String; value.
77       */
78      public synchronized void setProperty(final String key, final String value)
79      {
80          Throw.whenNull(key, "Key may not be null.");
81          Throw.whenNull(value, "Value may not be null.");
82          this.store.put(key, value);
83          try
84          {
85              File f = new File(this.file);
86              f.getParentFile().mkdirs();
87              FileWriter writer = new FileWriter(f);
88              this.store.store(writer, this.applicationName);
89          }
90          catch (IOException exception)
91          {
92              //
93          }
94      }
95  
96      /**
97       * Returns recent files.
98       * @param key String; key under which files are stored.
99       * @return List&lt;String&gt;; files (recent to old).
100      */
101     public List<String> getRecentFiles(final String key)
102     {
103         Throw.whenNull(key, "Key may not be null.");
104         List<String> out = new ArrayList<>();
105         if (this.store.containsKey(key))
106         {
107             String filesAll = (String) this.store.get(key);
108             String[] files = filesAll.split("\\|");
109             Arrays.stream(files).forEach(out::add);
110         }
111         return out;
112     }
113 
114     /**
115      * Add recent file. If the file is already in the list, it is moved to the front.
116      * @param key String; key under which files are stored.
117      * @param file String; latest file.
118      */
119     public void addRecentFile(final String key, final String file)
120     {
121         Throw.whenNull(key, "Key may not be null.");
122         Throw.whenNull(file, "File may not be null.");
123         List<String> files = getRecentFiles(key);
124         if (files.contains(file))
125         {
126             if (files.get(0).equals(file))
127             {
128                 return;
129             }
130             files.remove(file);
131         }
132         files.add(0, file);
133         setFiles(key, files);
134     }
135     
136     /**
137      * Clears a recent file.
138      * @param key String; key.
139      * @param file String; file.
140      */
141     public void removeRecentFile(final String key, final String file)
142     {
143         List<String> files = getRecentFiles(key);
144         files.remove(file);
145         setFiles(key, files);
146     }
147     
148     /**
149      * Sets the files.
150      * @param key String; key.
151      * @param files List&lt;String&gt;; files.
152      */
153     private void setFiles(final String key, final List<String> files)
154     {
155         StringBuilder str = new StringBuilder();
156         int n = Math.min(files.size(), MAX);
157         files.stream().limit(n - 1).forEach((f) -> str.append(f).append("|"));
158         str.append(files.get(n - 1));
159         setProperty(key, str.toString());
160     }
161     
162     /**
163      * Removes key from the store.
164      * @param key String; key.
165      */
166     public void clearProperty(final String key)
167     {
168         Throw.whenNull(key, "Key may not be null.");
169         this.store.remove(key);
170     }
171 
172 }