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 org.abstracthorizon.aequo.util.Preferences;
16  
17  /**
18   * Recent file entry
19   *
20   * @author Daniel Sendula
21   */
22  public class RecentFileEntry {
23      
24      /** Maximum number of recent entries. Value is 10 */
25      public static final int MAX_RECENT = 10;
26      
27      /** Left file name */
28      public String left;
29  
30      /** Right file name */
31      public String right;
32  
33      /** Filters */
34      public String filters;
35      
36      /**
37       * Constructor
38       */
39      public RecentFileEntry() {
40      }
41  
42      /**
43       * Constructor
44       * @param left left filename
45       * @param right right filename
46       * @param filters filters
47       */
48      public RecentFileEntry(String left, String right, String filters) {
49          this.left = left;
50          this.right = right;
51          this.filters = filters;
52      }
53  
54      /**
55       * Reads a file entry from preferences at given index
56       * @param prefs preferences
57       * @param no index
58       * @return entry
59       */
60      public static RecentFileEntry read(Preferences prefs, int no) {
61          RecentFileEntry entry = new RecentFileEntry();
62          entry.left = prefs.getProperty("recent." + no + ".left");
63          entry.right = prefs.getProperty("recent." + no + ".right");
64          entry.filters = prefs.getProperty("recent." + no + ".filters");
65          return entry;
66      }
67      
68      /**
69       * Writes recent file entry to preferences at given index
70       * @param prefs preferences
71       * @param entry entry
72       * @param no index
73       */
74      public static void write(Preferences prefs, RecentFileEntry entry, int no) {
75          prefs.setProperty("recent." + no + ".left", entry.left);
76          prefs.setProperty("recent." + no + ".right", entry.right);
77          prefs.setProperty("recent." + no + ".filters", entry.filters);
78      }
79      
80      /**
81       * Checks if recent file entry exists in preferences and returns index of it
82       * @param prefs preferences
83       * @param entry entry
84       * @return index or given entry or <code>-1</code> if it doesn't exist
85       */
86      public static int find(Preferences prefs, RecentFileEntry entry) {
87          for (int i = 1; i <= MAX_RECENT; i++) {
88              String l = prefs.getProperty("recent." + (i - 1) + ".left");
89              String r = prefs.getProperty("recent." + (i - 1) + ".right");
90              if ((entry.left.equals(l) && entry.right.equals(r))) {
91                  return (i - 1);
92              }
93          }
94          return -1;
95      }
96      
97      /**
98       * Writes new entry to the begining of a list of recent file entries
99       * @param prefs preferences
100      * @param entry entry
101      * @return <code>true</code> if new has been added or <code>false</code> if entry already existed.
102      */
103     public static boolean writeNew(Preferences prefs, RecentFileEntry entry) {
104         for (int i = 1; i <= MAX_RECENT; i++) {
105             String l = prefs.getProperty("recent." + i + ".left");
106             String r = prefs.getProperty("recent." + i + ".right");
107             if ((entry.left.equals(l) && entry.right.equals(r))) {
108                 String f = entry.filters;
109                 String oldF = prefs.getProperty("recent." + i + ".filters");
110                 if (f == oldF) {
111                     return false;
112                 } else {
113                     if ((f != null) && !f.equals(oldF)) {
114                         prefs.setProperty("recent." + i + ".filters", f);
115                         return true;
116                     } else {
117                         prefs.setProperty("recent." + i + ".filters", "");
118                         return true;
119                     }
120                 }
121             }
122         }
123         
124         for (int i = MAX_RECENT; i > 1; i--) {
125             String l = prefs.getProperty("recent." + (i - 1) + ".left");
126             String r = prefs.getProperty("recent." + (i - 1) + ".right");
127             String f = prefs.getProperty("recent." + (i - 1) + ".filters");
128             if ((l != null) && (r != null)) {
129                 prefs.setProperty("recent." + i + ".left", l);
130                 prefs.setProperty("recent." + i + ".right", r);
131                 if (f != null) {
132                     prefs.setProperty("recent." + i + ".filters", f);
133                 } else {
134                     prefs.remove("recent." + i + ".filters");
135                 }
136             }
137         }
138         prefs.setProperty("recent.1.left", entry.left);
139         prefs.setProperty("recent.1.right", entry.right);
140         prefs.setProperty("recent.1.filters", entry.filters);
141         return true;
142     }
143     
144     public static int getNumberOfRecent(Preferences prefs) {
145         for (int i = MAX_RECENT; i > 0; i--) {
146             String l = prefs.getProperty("recent." + i + ".left");
147             String r = prefs.getProperty("recent." + i + ".right");
148             if ((l != null) && (r != null)) {
149                 return i;
150             }
151         }
152         return 0;
153     }
154 }