View Javadoc

1   /*
2    * $Id: HistoryUtilities.java,v 1.12 2007/09/26 12:29:29 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui.history;
22  
23  import java.net.URI;
24  import java.util.List;
25  
26  import org.bounce.util.URIUtils;
27  import org.xmlhammer.PreferencesHandler;
28  import org.xmlhammer.model.preferences.HistoryList;
29  
30  public class HistoryUtilities {
31      private static HistoryUtilities utils = null;
32      private HistoryListModel searches = null;
33      private HistoryListModel sources = null;
34      private HistoryListModel results = null;
35      private HistoryListModel schemas = null;
36      private HistoryListModel patterns = null;
37      private HistoryListModel expressions = null;
38      private HistoryListModel directories = null;
39      private HistoryListModel extensions = null;
40      private HistoryListModel stylesheets = null;
41      private HistoryListModel doctypeSystems = null;
42      private HistoryListModel doctypePublics = null;
43      private HistoryListModel cdataSectionElements = null;
44      private HistoryListModel medias = null;
45      private HistoryListModel titles = null;
46      private HistoryListModel charsets = null;
47  //    private DefaultListModel LookIn = null;
48      
49      private HistoryUtilities() {
50      }
51      
52      public static HistoryUtilities getInstance() {
53          if ( utils == null) {
54              utils = new HistoryUtilities();
55          }
56          
57          return utils;
58      }
59      
60      // Add a recently opened project.
61      public void addOpenedProject(URI uri) {
62          if ( uri != null) {
63              add(PreferencesHandler.getInstance().getPreferences().getHistory().getProjects(), URIUtils.toString(uri));
64          }
65      }
66  
67      private void add(HistoryList hist, String value) {
68          if (value != null && value.length() > 0) {
69              List<String> list = hist.getValue();
70              
71              for (String item : list) {
72                  if (item.equals(value)) {
73                      list.remove(item);
74                      break;
75                  }
76              }
77              
78              list.add(0, value);
79  
80              if (hist.getSize() < list.size()) {
81                  // remove the last item!
82                  list.remove(list.size()-1);
83              }
84          }
85      }
86  
87  //    private void add( DefaultListModel model, HistoryList hist, String value) {
88  //        if ( value != null && value.length() > 0) {
89  //            List<String> list = hist.getValue();
90  //            
91  //            for ( String item : list) {
92  //                if ( item.equals( value)) {
93  //                    list.remove( item);
94  //                    model.removeElement( value);
95  //                    break;
96  //                }
97  //            }
98  //            
99  //            if ( hist.getSize() < list.size()) {
100 //                // remove the last item!
101 //                String last = list.get( list.size()-1);
102 //    
103 //                model.removeElement(last);
104 //                list.remove(last);
105 //            }
106 //            
107 //            list.add( 0, value);
108 //            model.add( 0, value);
109 //        }
110 //    }
111 
112 //    // Add a recently opened URI.
113 //    public void addInput( URI uri) {
114 //        if ( uri != null) {
115 //            add( (DefaultListModel)getInputsModel(), preferences.getHistory().getInputs(), uri.toString());
116 //        }
117 //    }
118 //
119 //    // Add a recently opened URI.
120 //    public void addSchema( URI uri) {
121 //        if ( uri != null) {
122 //            add( (DefaultListModel)getSchemasModel(), preferences.getHistory().getSchemas(), uri.toString());
123 //        }
124 //    }
125 //
126 //    public void addDirectory( String uri) {
127 //        add( (DefaultListModel)getDirectories(), preferences.getHistory().getDirectories(), uri);
128 //    }
129 //
130 //    // Add a pattern.
131 //    public void addPattern( String pattern) {
132 //        add( (DefaultListModel)getPatterns(), preferences.getHistory().getPatterns(), pattern);
133 //    }
134 //
135 //    // Add a expression.
136 //    public void addExpression( String expression) {
137 //        add( (DefaultListModel)getExpressions(), preferences.getHistory().getExpressions(), expression);
138 //    }
139         
140     public HistoryListModel getSearches() {
141         if (searches == null) {
142             searches = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getSearches());
143         }
144 
145         return searches;
146     }
147 
148     public HistoryListModel getSourcesModel() {
149         if ( sources == null) {
150             sources = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getSources());
151         }
152 
153         return sources;
154     }
155 
156     public HistoryListModel getResultsModel() {
157         if (results == null) {
158             results = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getResults());
159         }
160 
161         return results;
162     }
163 
164     public HistoryListModel getSchemasModel() {
165         if ( schemas == null) {
166             schemas = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getSchemas());
167         }
168 
169         return schemas;
170     }
171 
172     public HistoryListModel getExpressions() {
173         if ( expressions == null) {
174             expressions = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getExpressions());
175         }
176 
177         return expressions;
178     }
179 
180     public HistoryListModel getPatterns() {
181         if ( patterns == null) {
182             patterns = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getPatterns());
183         }
184         
185         return patterns;
186     }
187 
188     public HistoryListModel getDirectories() {
189         if ( directories == null) {
190             directories = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getDirectories());
191         }
192 
193         return directories;
194     }
195     
196     public HistoryListModel getExtensions() {
197         if ( extensions == null) {
198             extensions = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getExtensions());
199         }
200 
201         return extensions;
202     }
203 
204     public HistoryListModel getStylesheets() {
205         if (stylesheets == null) {
206             stylesheets = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getStylesheets());
207         }
208 
209         return stylesheets;
210     }
211 
212     public HistoryListModel getDoctypeSystems() {
213         if (doctypeSystems == null) {
214             doctypeSystems = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getDoctypeSystems());
215         }
216 
217         return doctypeSystems;
218     }
219 
220     public HistoryListModel getDoctypePublics() {
221         if (doctypePublics == null) {
222             doctypePublics = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getDoctypePublics());
223         }
224 
225         return doctypePublics;
226     }
227 
228     public HistoryListModel getCDataSectionElements() {
229         if (cdataSectionElements == null) {
230             cdataSectionElements = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getCdataSectionElements());
231         }
232 
233         return cdataSectionElements;
234     }
235 
236     public HistoryListModel getMedias() {
237         if (medias == null) {
238             medias = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getMedias());
239         }
240 
241         return medias;
242     }
243 
244     public HistoryListModel getTitles() {
245         if (titles == null) {
246             titles = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getTitles());
247         }
248 
249         return titles;
250     }
251 
252     public HistoryListModel getCharsets() {
253         if (charsets == null) {
254             charsets = createModel(PreferencesHandler.getInstance().getPreferences().getHistory().getCharsets());
255         }
256 
257         return charsets;
258     }
259 
260     private HistoryListModel createModel( HistoryList hist) {
261         return new HistoryListModel( hist);
262     }
263  }