KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > file > FileSystemAdapter


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.adapter.file;
22
23 import java.io.File JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.List JavaDoc;
29
30 import net.mlw.vlh.DefaultListBackedValueList;
31 import net.mlw.vlh.ValueList;
32 import net.mlw.vlh.ValueListInfo;
33 import net.mlw.vlh.adapter.AbstractValueListAdapter;
34
35 /**
36  * This ValueListAdapter returns a ValueList of FileInfo(s).
37  *
38  * net.mlw.vlh.adapter.file.FileSystemAdapter
39  *
40  * @author Matthew L. Wilson
41  * @version $Revision: 1.6 $ $Date: 2005/08/19 16:04:30 $
42  */

43 public class FileSystemAdapter extends AbstractValueListAdapter
44 {
45    /** The name of the filter defining the current directory. */
46    public static final String JavaDoc FOLDER = "folder";
47
48    /** The default starting directory, if FOLDER was not in the filters. */
49    private String JavaDoc defaultFolder = ".";
50
51    private String JavaDoc dateFormat = "MM/dd/yyyy";
52
53    /**
54     * Default constructor.
55     */

56    public FileSystemAdapter()
57    {
58       setAdapterType(DO_SORT);
59    }
60
61    /**
62     * @see net.mlw.vlh.ValueListAdapter#getValueList(java.lang.String, net.mlw.vlh.ValueListInfo)
63     */

64    public ValueList getValueList(String JavaDoc name, ValueListInfo info)
65    {
66       SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(dateFormat);
67
68       Date JavaDoc startDate = null;
69       Date JavaDoc endDate = null;
70
71       try
72       {
73          String JavaDoc stringStartDate = (String JavaDoc) info.getFilters().get("startDate");
74          if (stringStartDate != null && stringStartDate.length() > 0)
75          {
76             startDate = df.parse(stringStartDate);
77          }
78       }
79       catch (ParseException JavaDoc e)
80       {
81          e.printStackTrace();
82       }
83       try
84       {
85          String JavaDoc stringEndDate = (String JavaDoc) info.getFilters().get("endDate");
86          if (stringEndDate != null && stringEndDate.length() > 0)
87          {
88             endDate = df.parse(stringEndDate);
89          }
90       }
91       catch (ParseException JavaDoc e)
92       {
93          e.printStackTrace();
94       }
95
96       if (info.getSortingColumn() == null)
97       {
98          info.setPrimarySortColumn(getDefaultSortColumn());
99          info.setPrimarySortDirection(getDefaultSortDirectionInteger());
100       }
101
102       if (info.getPagingNumberPer() == Integer.MAX_VALUE)
103       {
104          info.setPagingNumberPer(getDefaultNumberPerPage());
105       }
106
107       String JavaDoc folder = ((info.getFilters().get(FOLDER) != null) ? (String JavaDoc) info.getFilters().get(FOLDER) : defaultFolder);
108
109       File JavaDoc dir = new File JavaDoc(folder);
110       File JavaDoc[] files = dir.listFiles();
111       List JavaDoc list = new ArrayList JavaDoc(files.length);
112       for (int i = 0; i < files.length; i++)
113       {
114          FileInfo file = new FileInfo(files[i]);
115          if (startDate == null || startDate.before(file.getLastModified()))
116          {
117             if (endDate == null || endDate.after(file.getLastModified()))
118             {
119                list.add(file);
120             }
121          }
122
123       }
124       info.setTotalNumberOfEntries(list.size());
125       return new DefaultListBackedValueList(list, info);
126    }
127
128    /**
129     * @param defaultFolder
130     * The defaultFolder to set.
131     */

132    public void setDefaultFolder(String JavaDoc defaultFolder)
133    {
134       this.defaultFolder = defaultFolder;
135    }
136
137 }
Popular Tags