1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.gui;
26
27
28 import java.util.*;
29
30 import mk.fsgrep.Fsgrep;
31 import mk.fsgrep.find.Finder;
32 import mk.fsgrep.find.ScanProfile;
33 import mk.fsgrep.util.MRUList;
34
35
36 /***
37 * A utility that keep a record of recently referenced scan profiles.
38 *
39 * @author Murali Krishnan
40 *
41 */
42 public class RecentProfiles {
43
44
45
46
47
48 protected static final String PERSISTENT_PREFIX = "recent_profile_";
49
50
51
52
53
54
55
56
57
58
59 private MRUList _list = new MRUList();
60 private Map<String,Finder> _map = new HashMap<String,Finder>();
61
62
63
64
65
66
67 protected RecentProfiles() {
68 }
69
70
71
72
73
74 protected MRUList getList() {return _list;}
75 protected Map<String,Finder> getMap() {return _map;}
76
77
78
79
80
81
82
83
84
85
86
87 /***
88 * Clear the map, then (re)populate it.
89 *
90 * @param model The underlying model containing the data.
91 */
92 protected void refreshMap(Fsgrep model) {
93 getMap().clear();
94 Iterator<String> iterator = iterator();
95 while (iterator.hasNext()) {
96 String spec = iterator.next();
97 getMap().put(spec, new Finder(model, new ScanProfile(spec)));
98 }
99 }
100
101
102
103
104
105 public String toString() {
106 String result = "RecentProfiles[" + getList() + "]";
107
108 return result;
109 }
110
111
112 /***
113 * Add an entry to the (front of) list. If it already exists, then just
114 * move it to the front. Also add the association to the map.
115 *
116 * @param entry The string spec.
117 * @param pFinder The associated Finder object.
118 */
119 public void add(String entry, Finder pFinder) {
120 getList().add(entry);
121 getMap().put(entry, pFinder);
122 }
123
124
125 /***
126 * Lookup the Finder object for a given spec.
127 *
128 * @return The Finder object associated with the given spec.
129 * @param key The string spec.
130 */
131 public Finder get(String key) {
132 Finder result = getMap().get(key);
133
134 return result;
135 }
136
137
138 /***
139 * A callback function for the GUI that gets called when the user selects
140 * a spec from the list. The list gets reordered with the selected entry
141 * moved to the front, and then it is persisted.
142 *
143 * @param key The string spec chosen by the user.
144 */
145 public void select(String key) {
146 Finder value = get(key);
147
148 if (value != null) {
149 add(key, value);
150 }
151
152 getList().saveData();
153 }
154
155
156 /***
157 * Give the "currently selected spec" by giving the first one on the list.
158 *
159 * @return The String spec.
160 */
161 public String getCurrent() {
162 String result = getList().getFirst();
163
164 return result;
165 }
166
167
168 /***
169 * Determine whether the given spec is on the list.
170 *
171 * @return boolean flag
172 * @param key The String spec.
173 */
174 public boolean contains(String key) {
175 boolean result = getList().contains(key);
176
177 return result;
178 }
179
180
181 /***
182 * Determine whether the list of specs is empty.
183 *
184 * @return boolean flag
185 */
186 public boolean isEmpty() {
187 boolean result = getList().isEmpty();
188
189 return result;
190 }
191
192
193 /***
194 * Remove all items except the first one (current).
195 *
196 */
197 public void keepFirst() {
198 if (!isEmpty()) {
199 String currentString = getCurrent();
200 Finder currentFinder = get(currentString);
201
202 getList().clear();
203 getMap().clear();
204 add(currentString, currentFinder);
205
206 getList().saveData();
207 }
208 }
209
210
211 /***
212 * Read the stored profiles from the persistent data. This will
213 * instantiate the Finder objects for each using the passed model.
214 *
215 * @param model The model object that the finder will reference.
216 */
217 public void readData(Fsgrep model) {
218 getList().readData(PERSISTENT_PREFIX);
219 refreshMap(model);
220 }
221
222
223 /***
224 *
225 * @return All the strings in order.
226 */
227 public Iterator<String> iterator() {
228 return getList().iterator();
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 }