View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
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      //- Class Variables
46  
47  
48      protected static final String PERSISTENT_PREFIX = "recent_profile_";
49  
50  
51      //------------------------------------------------------------
52      //- Class Functions
53  
54  
55  
56      //------------------------------------------------------------
57      //- Instance Variables
58  
59      private MRUList _list = new MRUList();
60      private Map<String,Finder> _map = new HashMap<String,Finder>();
61  
62  
63  
64      //------------------------------------------------------------
65      //- Constructors
66  
67      protected RecentProfiles() {
68      }
69  
70  
71      //------------------------------------------------------------
72      //- Accessors
73  
74      protected MRUList getList() {return _list;}
75      protected Map<String,Finder> getMap() {return _map;}
76  
77  
78  
79      //------------------------------------------------------------
80      //- Settors
81  
82  
83  
84      //------------------------------------------------------------
85      //- Private/Protected Utility Functions
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     //- Public Interface Functions
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     //- Class Interface Functions
234 
235 
236 
237     //------------------------------------------------------------
238     //- Inner Classes
239 
240 
241 
242     //------------------------------------------------------------
243     //- Main
244 
245 
246 
247 }