View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.util.prefs;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.abstracthorizon.aequo.util.Preferences;
19  
20  /**
21   * Recent values in preferences file. Methods here will use supplied name to form a key adding .1, .2, etc...
22   *
23   * @author Daniel Sendula
24   */
25  public class Recent {
26  
27      /** Maximum recent values */
28      public static final int MAX_RECENT = 10;
29  
30      /**
31       * This method returns list of all recent values for given name from preferences
32       * @param prefs preferences
33       * @return list of all recent values from preferences
34       */
35      public static List<String> allRecent(Preferences prefs, String name) {
36          ArrayList<String> result = new ArrayList<String>();
37          
38          for (int i = 1; i <= MAX_RECENT; i++) {
39              String f = prefs.getProperty(name + "." + i);
40              if (f != null) {
41                  result.add(f);
42              }
43          }
44  
45          return result;
46      }
47  
48      /**
49       * This method writes new values to the preferences
50       * @param prefs preferences
51       * @param name name of the value entry
52       * @param value value to be written
53       * @return <code>true</code> if new value has been added
54       */
55      public static boolean writeNew(Preferences prefs, String name, String value) {
56          for (int i = 1; i <= MAX_RECENT; i++) {
57              String f = prefs.getProperty(name + "." + i);
58              if (value.equals(f)) {
59                  return false;
60              }
61          }
62          
63          for (int i = MAX_RECENT; i > 1; i--) {
64              String f = prefs.getProperty(name + "." + (i - 1));
65              if (f != null) {
66                  prefs.setProperty(name + "." + i, f);
67              }
68          }
69          prefs.setProperty(name + ".1", value);
70          return true;
71      }
72  }