KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > model > FileSystemItem


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.networkplaces.model;
21
22 import java.text.NumberFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Calendar JavaDoc;
25
26 import org.apache.commons.vfs.FileType;
27
28 import com.sslexplorer.policyframework.LaunchSession;
29 import com.sslexplorer.table.TableItem;
30 import com.sslexplorer.vfs.webdav.DAVUtilities;
31
32 /**
33  * <p>
34  * FileSystemItems are the entries in the file system view. Implements <@link
35  * com.sslexplorer.table.TableItem> and <@link java.lang.Comparable>.
36  *
37  * @author James D Robinson
38  *
39  * @mail <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
40  */

41 public abstract class FileSystemItem implements TableItem, Comparable JavaDoc {
42
43     private String JavaDoc fileName;
44     private Calendar JavaDoc dateModified;
45     private String JavaDoc fileType;
46     private boolean checked;
47     private String JavaDoc bytes;
48     private long size;
49     private boolean sortFoldersFirst;
50     private boolean sortCaseSensitive;
51     private int idx;
52     private LaunchSession launchSession;
53
54     /**
55      * Constructor sets up class attributes.
56      *
57      * @param launchSession the launch session that launched the view that contains this item
58      * @param fileName The name of the file.
59      * @param dateModified The date the file was last modified.
60      * @param fileType The type of file.
61      * @param checked weather the item is to be selected.
62      * @param bytes The number of bytes in the file.
63      * @param idx index of item
64      */

65     public FileSystemItem(LaunchSession launchSession, String JavaDoc fileName, Calendar JavaDoc dateModified, String JavaDoc fileType, boolean checked, long bytes, int idx) {
66         this.fileName = fileName;
67         this.dateModified = dateModified;
68         this.fileType = fileType;
69         this.checked = checked;
70         this.bytes = formatSize(bytes);
71         this.size = bytes;
72         this.launchSession = launchSession;
73         this.sortFoldersFirst = true;
74         this.sortCaseSensitive = false;
75         this.idx = idx;
76     }
77     
78     /**
79      * Get the launched session that launched the view that contains
80      * this item.
81      *
82      * @return resource session
83      */

84     public LaunchSession getLaunchSession() {
85         return launchSession;
86     }
87
88     /**
89      * @return Get the date the file was last modified.
90      */

91     public String JavaDoc getDateModified() {
92         return (new SimpleDateFormat JavaDoc()).format(dateModified.getTime());
93     }
94
95     /**
96      * @param dateModified Set the date the file was last modified.
97      */

98     public void setDateModified(Calendar JavaDoc dateModified) {
99         this.dateModified = dateModified;
100     }
101
102     /**
103      * @return Get the file name.
104      */

105     public String JavaDoc getFileName() {
106         return fileName;
107     }
108
109     /**
110      * @return Get the encoded file name.
111      */

112     public String JavaDoc getEncodedFileName() {
113         return DAVUtilities.encodePath((getFileName()));
114     }
115
116     /**
117      * @param fileName Set the file name.
118      */

119     public void setFileName(String JavaDoc fileName) {
120         this.fileName = fileName;
121     }
122
123     /**
124      * @return Get the file type.
125      */

126     public String JavaDoc getFileType() {
127         return fileType;
128     }
129
130     /**
131      * @param fileType Set the file type.
132      */

133     public void setFileType(String JavaDoc fileType) {
134         this.fileType = fileType;
135     }
136
137     /**
138      * @return Get the selected state.
139      */

140     public boolean getChecked() {
141         return checked;
142     }
143
144     /**
145      * @param checked Set the selected state.
146      */

147     public void setChecked(boolean checked) {
148         this.checked = checked;
149     }
150
151     /* (non-Javadoc)
152      * @see com.sslexplorer.table.TableItem#getColumnValue(int)
153      */

154     public Object JavaDoc getColumnValue(int col) {
155         switch (col) {
156             case 0:
157                 return this;
158             case 1:
159                 return dateModified;
160             case 2:
161                 return new Long JavaDoc(size);
162         }
163         return null;
164     }
165
166     /* (non-Javadoc)
167      * @see java.lang.Comparable#compareTo(Object)
168      */

169     public int compareTo(Object JavaDoc arg0) {
170         FileSystemItem fsi = (FileSystemItem) arg0;
171         int lessThan = -1;
172         int moreThan = 1;
173         if (!sortFoldersFirst) {
174             lessThan = 1;
175             moreThan = -1;
176         }
177         if (fsi.getFileType().equals(FileType.FOLDER.getName()) && this.getFileType().equals(FileType.FILE.getName())) {
178             return moreThan;
179         } else if (fsi.getFileType().equals(FileType.FILE.getName()) && this.getFileType().equals(FileType.FOLDER.getName())) {
180             return lessThan;
181         } else {
182             if (!sortCaseSensitive)
183                 return fileName.compareToIgnoreCase(fsi.getFileName());
184             else
185                 return fileName.compareTo(fsi.getFileName());
186         }
187     }
188
189     /**
190      * @param path The current path to this location.
191      * @return Return the String defining the path when clicked.
192      */

193     public abstract String JavaDoc onClick(String JavaDoc path);
194
195     /**
196      * @return The String to open a folder in web folder view.
197      */

198     public abstract String JavaDoc getWebFolderPath();
199
200     /**
201      * @return The Size of the file in bytes.
202      */

203     public String JavaDoc getBytes() {
204         return bytes;
205     }
206
207     /**
208      * @param bytes The Size of the file in bytes.
209      */

210     public void setBytes(long bytes) {
211         this.bytes = formatSize(bytes);
212     }
213
214     /**
215      * @param val Format the size to a specified number of decimal places.
216      * @return The new formatted String.
217      */

218     private String JavaDoc formatSizeDPS(String JavaDoc val) {
219         int position = 2 + 1;
220         if (val.indexOf(".") + position < val.length()) {
221             return val.substring(0, val.indexOf(".") + position);
222         } else {
223             return val;
224         }
225     }
226
227     private String JavaDoc formatSize(long bytes) {
228
229         NumberFormat JavaDoc formatMb = NumberFormat.getNumberInstance();
230         NumberFormat JavaDoc formatGb = NumberFormat.getNumberInstance();
231         NumberFormat JavaDoc formatKb = NumberFormat.getNumberInstance();
232
233         if ((bytes / 1099511627776L) > 0) {
234             // Were in the gigabytes
235
return formatSizeDPS(formatGb.format((double) bytes / 1099511627776L)) + " GB";
236         } else if ((bytes / 1048576) > 0) {
237             // Were in the megabytes
238
return formatSizeDPS(formatMb.format((double) bytes / 1048576)) + " MB";
239         } else {
240             // Were still in Kilobytes
241
return formatSizeDPS(formatKb.format((double) bytes / 1024)) + " KB";
242         }
243     }
244
245     public boolean isSortFoldersFirst() {
246         return sortFoldersFirst;
247     }
248
249     public void setSortFoldersFirst(boolean sortFoldersFirst) {
250         this.sortFoldersFirst = sortFoldersFirst;
251     }
252
253     public boolean isSortCaseSensitive() {
254         return sortCaseSensitive;
255     }
256
257     public void setSortCaseSensitive(boolean sortCaseSensitive) {
258         this.sortCaseSensitive = sortCaseSensitive;
259     }
260     
261     /**
262      * Return a string representation of this file system item. This is
263      * used by the generic filter.
264      *
265      * @return string representation of file system item
266      */

267     public String JavaDoc toString() {
268         return getFileName();
269     }
270     
271     public int getIdx() {
272         return idx;
273     }
274
275 }
276
Popular Tags