KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > config > ViewsConfigElement


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.config.ConfigElement;
25 import org.alfresco.config.ConfigException;
26 import org.alfresco.config.element.ConfigElementAdapter;
27
28 /**
29  * Custom config element that represents config values for views in the client
30  *
31  * @author Gavin Cornwell
32  */

33 public class ViewsConfigElement extends ConfigElementAdapter
34 {
35    public static final String JavaDoc CONFIG_ELEMENT_ID = "views";
36
37    public static final String JavaDoc VIEW_DETAILS = "details";
38    public static final String JavaDoc VIEW_ICONS = "icons";
39    public static final String JavaDoc VIEW_LIST = "list";
40    public static final String JavaDoc VIEW_BUBBLE = "bubble";
41    public static final String JavaDoc SORT_ASCENDING = "ascending";
42    public static final String JavaDoc SORT_DESCENDING = "descending";
43    
44    private static final String JavaDoc SEPARATOR = ":";
45    
46    // defaults
47
private int defaultPageSize = 10;
48    private String JavaDoc defaultView = "details";
49    private String JavaDoc defaultSortColumn = "name";
50    
51    // list to store all the configured views
52
private List JavaDoc<String JavaDoc> views = new ArrayList JavaDoc<String JavaDoc>(4);
53
54    // map to store all the default views
55
private Map JavaDoc<String JavaDoc, String JavaDoc> defaultViews = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(4);
56    
57    // map to store all default page sizes for configured client views
58
private Map JavaDoc<String JavaDoc, Integer JavaDoc> pageSizes = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>(10);
59    
60    // map to store default sort columns for configured views
61
private Map JavaDoc<String JavaDoc, String JavaDoc> sortColumns = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(4);
62    
63    // list of pages that have been configured to have ascending sorts
64
private Map JavaDoc<String JavaDoc, String JavaDoc> sortDirections = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(1);
65    
66    /**
67     * Default Constructor
68     */

69    public ViewsConfigElement()
70    {
71       super(CONFIG_ELEMENT_ID);
72       
73       // add the default page sizes to the map
74
this.pageSizes.put(VIEW_DETAILS, defaultPageSize);
75       this.pageSizes.put(VIEW_LIST, defaultPageSize);
76       this.pageSizes.put(VIEW_ICONS, 9);
77       this.pageSizes.put(VIEW_BUBBLE, 5);
78    }
79    
80    /**
81     * Constructor
82     *
83     * @param name Name of the element this config element represents
84     */

85    public ViewsConfigElement(String JavaDoc name)
86    {
87       super(name);
88    }
89
90    /**
91     * @see org.alfresco.config.element.ConfigElementAdapter#getChildren()
92     */

93    @Override JavaDoc
94    public List JavaDoc<ConfigElement> getChildren()
95    {
96       throw new ConfigException("Reading the views config via the generic interfaces is not supported");
97    }
98    
99    /**
100     * @see org.alfresco.config.element.ConfigElementAdapter#combine(org.alfresco.config.ConfigElement)
101     */

102    public ConfigElement combine(ConfigElement configElement)
103    {
104       ViewsConfigElement existingElement = (ViewsConfigElement)configElement;
105       ViewsConfigElement newElement = new ViewsConfigElement();
106       
107       // copy all the config from this element into the new one
108
for (String JavaDoc viewImpl : this.views)
109       {
110          newElement.addView(viewImpl);
111       }
112       
113       for (String JavaDoc page : this.defaultViews.keySet())
114       {
115          newElement.addDefaultView(page, this.defaultViews.get(page));
116       }
117       
118       for (String JavaDoc pageView : this.pageSizes.keySet())
119       {
120          if (pageView.indexOf(SEPARATOR) != -1)
121          {
122             String JavaDoc page = pageView.substring(0, pageView.indexOf(SEPARATOR));
123             String JavaDoc view = pageView.substring(pageView.indexOf(SEPARATOR)+1);
124             newElement.addDefaultPageSize(page, view, this.pageSizes.get(pageView).intValue());
125          }
126       }
127       
128       for (String JavaDoc page : this.sortColumns.keySet())
129       {
130          newElement.addDefaultSortColumn(page, this.sortColumns.get(page));
131       }
132       
133       for (String JavaDoc page : this.sortDirections.keySet())
134       {
135          newElement.addSortDirection(page, this.sortDirections.get(page));
136       }
137       
138       // copy all the config from the element to be combined into the new one
139
for (String JavaDoc viewImpl : existingElement.getViews())
140       {
141          newElement.addView(viewImpl);
142       }
143       
144       Map JavaDoc<String JavaDoc, String JavaDoc> existingDefaultViews = existingElement.getDefaultViews();
145       for (String JavaDoc page : existingDefaultViews.keySet())
146       {
147          newElement.addDefaultView(page, existingDefaultViews.get(page));
148       }
149       
150       Map JavaDoc<String JavaDoc, Integer JavaDoc> existingPageSizes = existingElement.getDefaultPageSizes();
151       for (String JavaDoc pageView : existingPageSizes.keySet())
152       {
153          if (pageView.indexOf(SEPARATOR) != -1)
154          {
155             String JavaDoc page = pageView.substring(0, pageView.indexOf(SEPARATOR));
156             String JavaDoc view = pageView.substring(pageView.indexOf(SEPARATOR)+1);
157             newElement.addDefaultPageSize(page, view, existingPageSizes.get(pageView).intValue());
158          }
159       }
160       
161       Map JavaDoc<String JavaDoc, String JavaDoc> existingSortColumns = existingElement.getDefaultSortColumns();
162       for (String JavaDoc page : existingSortColumns.keySet())
163       {
164          newElement.addDefaultSortColumn(page, existingSortColumns.get(page));
165       }
166       
167       Map JavaDoc<String JavaDoc, String JavaDoc> existingSortDirs = existingElement.getSortDirections();
168       for (String JavaDoc page : existingSortDirs.keySet())
169       {
170          newElement.addSortDirection(page, existingSortDirs.get(page));
171       }
172       
173       return newElement;
174    }
175
176    /**
177     * Adds a configured view
178     *
179     * @param renderer The implementation class of the view (the renderer)
180     */

181    /*package*/ void addView(String JavaDoc renderer)
182    {
183       this.views.add(renderer);
184    }
185    
186    /**
187     * Returns a map of configured views for the client
188     *
189     * @return List of the implementation classes for the configured views
190     */

191    public List JavaDoc<String JavaDoc> getViews()
192    {
193       return this.views;
194    }
195    
196    /**
197     * Adds a default view setting
198     *
199     * @param page The page to set the default view for
200     * @param view The view name that will be the default
201     */

202    /*package*/ void addDefaultView(String JavaDoc page, String JavaDoc view)
203    {
204       this.defaultViews.put(page, view);
205    }
206    
207    /**
208     * Returns the default view for the given page
209     *
210     * @param page The page to get the default view for
211     * @return The defualt view, if there isn't a configured default for the
212     * given page 'details' will be returned
213     */

214    public String JavaDoc getDefaultView(String JavaDoc page)
215    {
216       String JavaDoc view = this.defaultViews.get(page);
217       
218       if (view == null)
219       {
220          view = this.defaultView;
221       }
222       
223       return view;
224    }
225
226    /**
227     * Returns a map of default views for each page
228     *
229     * @return Map of default views
230     */

231    /*package*/ Map JavaDoc<String JavaDoc, String JavaDoc> getDefaultViews()
232    {
233       return this.defaultViews;
234    }
235    
236    /**
237     * Adds a configured page size to the internal store
238     *
239     * @param page The name of the page i.e. browse, forums etc.
240     * @param view The name of the view the size is for i.e. details, icons etc.
241     * @param size The size of the page
242     */

243    /*package*/ void addDefaultPageSize(String JavaDoc page, String JavaDoc view, int size)
244    {
245       this.pageSizes.put(page + SEPARATOR + view, new Integer JavaDoc(size));
246    }
247    
248    /**
249     * Returns the page size for the given page and view combination
250     *
251     * @param page The name of the page i.e. browse, forums etc.
252     * @param view The name of the view the size is for i.e. details, icons etc.
253     * @return The size of the requested page, if the combination doesn't exist
254     * the default for the view will be used, if the view doesn't exist either
255     * 10 will be returned.
256     */

257    public int getDefaultPageSize(String JavaDoc page, String JavaDoc view)
258    {
259       Integer JavaDoc pageSize = this.pageSizes.get(page + SEPARATOR + view);
260       
261       // try just the view if the combination isn't present
262
if (pageSize == null)
263       {
264          pageSize = this.pageSizes.get(view);
265          
266          // if the view is not present either default to 10
267
if (pageSize == null)
268          {
269             pageSize = new Integer JavaDoc(10);
270          }
271       }
272       
273       return pageSize.intValue();
274    }
275    
276    /**
277     * Returns a map of page sizes
278     *
279     * @return Map of page sizes
280     */

281    /*package*/ Map JavaDoc<String JavaDoc, Integer JavaDoc> getDefaultPageSizes()
282    {
283       return this.pageSizes;
284    }
285
286    /**
287     * Adds a default sorting column for the given page
288     *
289     * @param page The name of the page i.e. browse, forums etc.
290     * @param column The name of the column to initially sort by
291     */

292    /*package*/ void addDefaultSortColumn(String JavaDoc page, String JavaDoc column)
293    {
294       this.sortColumns.put(page, column);
295    }
296    
297    /**
298     * Returns the default sort column for the given page
299     *
300     * @param page The name of the page i.e. browse, forums etc.
301     * @return The name of the column to sort by, name is returned if
302     * the page is not found
303     */

304    public String JavaDoc getDefaultSortColumn(String JavaDoc page)
305    {
306       String JavaDoc column = this.sortColumns.get(page);
307       
308       if (column == null)
309       {
310          column = this.defaultSortColumn;
311       }
312       
313       return column;
314    }
315    
316    /**
317     * Returns a map of the sorted columns for each page
318     *
319     * @return Map of sort columns
320     */

321    /*package*/ Map JavaDoc<String JavaDoc, String JavaDoc> getDefaultSortColumns()
322    {
323       return this.sortColumns;
324    }
325    
326    /**
327     * Sets the given page as using the given sort direction
328     *
329     * @param page The name of the page i.e. browse, forums etc.
330     * @param dir The sort direction
331     */

332    /*package*/ void addSortDirection(String JavaDoc page, String JavaDoc dir)
333    {
334       this.sortDirections.put(page, dir);
335    }
336    
337    /**
338     * Determines whether the given page has been
339     * configured to use descending sorting by default
340     *
341     * @param page The name of the page i.e. browse, forums etc.
342     * @return true if the page should use descending sorts
343     */

344    public boolean hasDescendingSort(String JavaDoc page)
345    {
346       boolean usesDescendingSort = false;
347       
348       String JavaDoc sortDir = this.sortDirections.get(page);
349       if (sortDir != null && sortDir.equalsIgnoreCase(SORT_DESCENDING))
350       {
351          usesDescendingSort = true;
352       }
353       
354       return usesDescendingSort;
355    }
356    
357    /**
358     * Returns a map of the sort directions
359     *
360     * @return Map of sort directions
361     */

362    /*package*/ Map JavaDoc<String JavaDoc, String JavaDoc> getSortDirections()
363    {
364       return this.sortDirections;
365    }
366 }
367
Popular Tags