KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > base > dir > DirEntry


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.gui.base.dir;
17
18 import java.awt.Image JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Hashtable JavaDoc;
23
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26
27 import net.sf.jftp.config.Settings;
28 import net.sf.jftp.gui.framework.HImage;
29 import net.sf.jftp.net.FtpConnection;
30
31
32 public class DirEntry
33 {
34     // to check file permissions
35
public static final int R = FtpConnection.R;
36     public static final int W = FtpConnection.W;
37     public static final int DENIED = FtpConnection.DENIED;
38     static Hashtable JavaDoc extensionMap = new Hashtable JavaDoc();
39     final static Object JavaDoc[] extensions =
40                                        {
41                                            new String JavaDoc[]
42                                            {
43                                                Settings.textFileImage, ".txt",
44                                                ".doc", ".rtf"
45                                            },
46                                            new String JavaDoc[]
47                                            {
48                                                Settings.htmlFileImage, ".htm",
49                                                ".html"
50                                            },
51                                            new String JavaDoc[]
52                                            {
53                                                Settings.zipFileImage, ".arj",
54                                                ".bz", ".bz2", ".deb", ".jar",
55                                                ".gz", ".rav", ".rpm", ".tar",
56                                                ".tgz", ".zip", ".z", ".iso"
57                                            },
58                                            new String JavaDoc[]
59                                            {
60                                                Settings.imageFileImage, "bmp",
61                                                ".gif", ".jpg", ".png", ".xbm",
62                                                ".xpm"
63                                            },
64                                            new String JavaDoc[]
65                                            {
66                                                Settings.codeFileImage, ".c",
67                                                ".cc", ".h", ".java"
68                                            },
69                                            new String JavaDoc[]
70                                            {
71                                                Settings.audioFileImage, ".au",
72                                                ".mid", ".midi", ".mp3", ".wav"
73                                            },
74                                            new String JavaDoc[]
75                                            {
76                                                Settings.execFileImage, ".bat",
77                                                ".csh", ".cgi", ".com", ".class",
78                                                ".cmd", ".csh", ".dtksh", ".exe",
79                                                ".ksh", ".pdksh", ".pl", ".sh",
80                                                ".tcl", ".tksh", ".zsh"
81                                            },
82                                            new String JavaDoc[]
83                                            {
84                                                Settings.presentationFileImage,
85                                                ".ppt"
86                                            },
87                                            new String JavaDoc[]
88                                            {
89                                                Settings.spreadsheetFileImage,
90                                                ".xls"
91                                            },
92                                            new String JavaDoc[]
93                                            {
94                                                Settings.videoFileImage, ".asf",
95                                                ".avi", ".mpg", "mpeg", ".wmf"
96                                            }
97                                        };
98
99     static
100     {
101         for(int i = 0; i < extensions.length; i++)
102         {
103             String JavaDoc[] temp = (String JavaDoc[]) extensions[i];
104
105             for(int j = 1; j < temp.length; j++)
106             {
107                 extensionMap.put(temp[j], temp[0]);
108             }
109         }
110     }
111
112     public String JavaDoc file = "";
113     private JLabel JavaDoc c = new JLabel JavaDoc();
114     public boolean selected = false;
115     public ActionListener JavaDoc who = null;
116     //private boolean entered = false;
117
private Image JavaDoc img;
118     public boolean isFile = true;
119     private boolean isDirectory = false;
120     private long size = 0;
121     private boolean isLink = false;
122     private int accessible = -1;
123     private boolean noRender = false;
124     public Date JavaDoc date = null;
125
126     public DirEntry(String JavaDoc file, ActionListener JavaDoc who)
127     {
128         this.file = file;
129         this.who = who;
130         setFile();
131     }
132
133     public void setFile()
134     {
135         String JavaDoc f = file;
136
137         if((f.indexOf("<") >= 0) && (f.indexOf(">") >= 0))
138         {
139             f = file.substring(file.indexOf("<") + 1);
140             f = f.substring(0, f.lastIndexOf(">"));
141         }
142
143         int lastIndex = f.lastIndexOf(".");
144         String JavaDoc image = Settings.fileImage; // default
145

146         if(lastIndex != -1)
147         {
148             String JavaDoc ext = f.substring(lastIndex);
149             String JavaDoc tmp = (String JavaDoc) extensionMap.get(ext.toLowerCase());
150
151             if(tmp != null) // we found an extension, let's use it's image
152
{
153                 image = tmp;
154             }
155         }
156
157         // else use the default
158
img = HImage.getImage(c, image);
159
160         if(img == null)
161         {
162             img = HImage.getImage(c, Settings.fileImage);
163         }
164
165         isFile = true;
166         isDirectory = false;
167     }
168
169     public void setDirectory()
170     {
171         img = HImage.getImage(c, Settings.dirImage);
172         isFile = false;
173         isDirectory = true;
174     }
175
176     public void setNoRender()
177     {
178         noRender = true;
179     }
180
181     public boolean getNoRender()
182     {
183         return noRender;
184     }
185
186     public void setPermission(int what)
187     {
188         accessible = what;
189     }
190
191     public int getPermission()
192     {
193         return accessible;
194     }
195
196     public void setSelected(boolean state)
197     {
198         selected = state;
199     }
200
201     public boolean isDirectory()
202     {
203         return isDirectory;
204     }
205
206     public boolean isFile()
207     {
208         return isFile;
209     }
210
211     public boolean isSelected()
212     {
213         return selected;
214     }
215
216     public String JavaDoc toString()
217     {
218         return file;
219     }
220
221     public Image JavaDoc getImage()
222     {
223         return img;
224     }
225     
226     public ImageIcon JavaDoc getImageIcon()
227     {
228         return new ImageIcon JavaDoc(img);
229     }
230
231     public void setDate(Date JavaDoc d)
232     {
233         date = d;
234     }
235
236     public String JavaDoc getDate()
237     {
238         if(date == null)
239         {
240             return "";
241         }
242
243         DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT);
244
245         return df.format(date);
246     }
247
248     public void setFileSize(long s)
249     {
250         size = s;
251     }
252
253     public String JavaDoc getFileSize()
254     {
255         if(isDirectory() || (size < 0))
256         {
257             return " ";
258         }
259
260         long rsize = size;
261
262         String JavaDoc type = "bs";
263
264         if(rsize > 1024)
265         {
266             rsize = rsize / 1024;
267             type = "kb";
268         }
269
270         if(rsize > 1024)
271         {
272             rsize = rsize / 1024;
273             type = "mb";
274         }
275
276         if(rsize > 1024)
277         {
278             rsize = rsize / 1024;
279             type = "gb";
280         }
281
282         String JavaDoc x = Long.toString(rsize);
283
284         while(x.length() < 4)
285         {
286             x = " " + x;
287         }
288
289         return x + " " + type + " > ";
290     }
291
292     public long getRawSize()
293     {
294         return size;
295     }
296
297     public void setLink()
298     {
299         img = HImage.getImage(c, Settings.linkImage);
300         file = file.substring(0, file.lastIndexOf("###"));
301         isLink = true;
302     }
303
304     public boolean isLink()
305     {
306         return isLink;
307     }
308     
309
310 }
311
Popular Tags