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
16
17
18
19
20
21
22 public class ApplicationStore
23 {
24
25
26 private static final int MAX = 10;
27
28
29 private final Properties store = new Properties();
30
31
32 private final String applicationName;
33
34
35 private final String file;
36
37
38
39
40
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
64
65
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
75
76
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
98
99
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
116
117
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
138
139
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
150
151
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
164
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 }