1 16 package org.apache.log4j.lf5.viewer.configure; 17 18 import java.io.*; 19 import java.net.URL ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 23 24 31 32 34 public class MRUFileManager { 35 private static final String CONFIG_FILE_NAME = "mru_file_manager"; 39 private static final int DEFAULT_MAX_SIZE = 3; 40 41 45 private int _maxSize = 0; 49 private LinkedList _mruFileList; 50 51 public MRUFileManager() { 55 load(); 56 setMaxSize(DEFAULT_MAX_SIZE); 57 } 58 59 public MRUFileManager(int maxSize) { 60 load(); 61 setMaxSize(maxSize); 62 } 63 67 70 public void save() { 71 File file = new File(getFilename()); 72 73 try { 74 ObjectOutputStream oos = new ObjectOutputStream(new 75 FileOutputStream(file)); 76 oos.writeObject(_mruFileList); 77 oos.flush(); 78 oos.close(); 79 } catch (Exception e) { 80 e.printStackTrace(); 82 } 83 } 84 85 88 public int size() { 89 return _mruFileList.size(); 90 } 91 92 96 public Object getFile(int index) { 97 if (index < size()) { 98 return _mruFileList.get(index); 99 } 100 101 return null; 102 } 103 104 107 public InputStream getInputStream(int index) throws IOException, 108 FileNotFoundException { 109 if (index < size()) { 110 Object o = getFile(index); 111 if (o instanceof File) { 112 return getInputStream((File) o); 113 } else { 114 return getInputStream((URL ) o); 115 } 116 } 117 return null; 118 } 119 120 123 public void set(File file) { 124 setMRU(file); 125 } 126 127 130 public void set(URL url) { 131 setMRU(url); 132 } 133 134 137 public String [] getMRUFileList() { 138 if (size() == 0) { 139 return null; 140 } 141 142 String [] ss = new String [size()]; 143 144 for (int i = 0; i < size(); i++) { 145 Object o = getFile(i); 146 if (o instanceof File) { 147 ss[i] = ((File) o).getAbsolutePath(); 148 } else { 150 ss[i] = o.toString(); 151 } 152 153 } 154 155 return ss; 156 } 157 158 163 public void moveToTop(int index) { 164 _mruFileList.add(0, _mruFileList.remove(index)); 165 } 166 167 173 public static void createConfigurationDirectory() { 174 String home = System.getProperty("user.home"); 175 String sep = System.getProperty("file.separator"); 176 File f = new File(home + sep + "lf5"); 177 if (!f.exists()) { 178 try { 179 f.mkdir(); 180 } catch (SecurityException e) { 181 e.printStackTrace(); 182 } 183 } 184 185 } 186 195 protected InputStream getInputStream(File file) throws IOException, 196 FileNotFoundException { 197 BufferedInputStream reader = 198 new BufferedInputStream(new FileInputStream(file)); 199 200 return reader; 201 } 202 203 209 protected InputStream getInputStream(URL url) throws IOException { 210 return url.openStream(); 211 } 212 213 216 protected void setMRU(Object o) { 217 int index = _mruFileList.indexOf(o); 218 219 if (index == -1) { 220 _mruFileList.add(0, o); 221 setMaxSize(_maxSize); 222 } else { 223 moveToTop(index); 224 } 225 } 226 227 231 protected void load() { 232 createConfigurationDirectory(); 233 File file = new File(getFilename()); 234 if (file.exists()) { 235 try { 236 ObjectInputStream ois = new ObjectInputStream( 237 new FileInputStream(file)); 238 _mruFileList = (LinkedList ) ois.readObject(); 239 ois.close(); 240 241 Iterator it = _mruFileList.iterator(); 243 while (it.hasNext()) { 244 Object o = it.next(); 245 if (!(o instanceof File) && !(o instanceof URL )) { 246 it.remove(); 247 } 248 } 249 } catch (Exception e) { 250 _mruFileList = new LinkedList (); 251 } 252 } else { 253 _mruFileList = new LinkedList (); 254 } 255 256 } 257 258 protected String getFilename() { 259 String home = System.getProperty("user.home"); 260 String sep = System.getProperty("file.separator"); 261 262 return home + sep + "lf5" + sep + CONFIG_FILE_NAME; 263 } 264 265 268 protected void setMaxSize(int maxSize) { 269 if (maxSize < _mruFileList.size()) { 270 for (int i = 0; i < _mruFileList.size() - maxSize; i++) { 271 _mruFileList.removeLast(); 272 } 273 } 274 275 _maxSize = maxSize; 276 } 277 281 } | Popular Tags |