001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.gui.base.dir;
017:
018: import java.awt.Image;
019: import java.awt.event.ActionListener;
020: import java.text.DateFormat;
021: import java.util.Date;
022: import java.util.Hashtable;
023:
024: import javax.swing.ImageIcon;
025: import javax.swing.JLabel;
026:
027: import net.sf.jftp.config.Settings;
028: import net.sf.jftp.gui.framework.HImage;
029: import net.sf.jftp.net.FtpConnection;
030:
031: public class DirEntry {
032: // to check file permissions
033: public static final int R = FtpConnection.R;
034: public static final int W = FtpConnection.W;
035: public static final int DENIED = FtpConnection.DENIED;
036: static Hashtable extensionMap = new Hashtable();
037: final static Object[] extensions = {
038: new String[] { Settings.textFileImage, ".txt", ".doc",
039: ".rtf" },
040: new String[] { Settings.htmlFileImage, ".htm", ".html" },
041: new String[] { Settings.zipFileImage, ".arj", ".bz",
042: ".bz2", ".deb", ".jar", ".gz", ".rav", ".rpm",
043: ".tar", ".tgz", ".zip", ".z", ".iso" },
044: new String[] { Settings.imageFileImage, "bmp", ".gif",
045: ".jpg", ".png", ".xbm", ".xpm" },
046: new String[] { Settings.codeFileImage, ".c", ".cc", ".h",
047: ".java" },
048: new String[] { Settings.audioFileImage, ".au", ".mid",
049: ".midi", ".mp3", ".wav" },
050: new String[] { Settings.execFileImage, ".bat", ".csh",
051: ".cgi", ".com", ".class", ".cmd", ".csh", ".dtksh",
052: ".exe", ".ksh", ".pdksh", ".pl", ".sh", ".tcl",
053: ".tksh", ".zsh" },
054: new String[] { Settings.presentationFileImage, ".ppt" },
055: new String[] { Settings.spreadsheetFileImage, ".xls" },
056: new String[] { Settings.videoFileImage, ".asf", ".avi",
057: ".mpg", "mpeg", ".wmf" } };
058:
059: static {
060: for (int i = 0; i < extensions.length; i++) {
061: String[] temp = (String[]) extensions[i];
062:
063: for (int j = 1; j < temp.length; j++) {
064: extensionMap.put(temp[j], temp[0]);
065: }
066: }
067: }
068:
069: public String file = "";
070: private JLabel c = new JLabel();
071: public boolean selected = false;
072: public ActionListener who = null;
073: //private boolean entered = false;
074: private Image img;
075: public boolean isFile = true;
076: private boolean isDirectory = false;
077: private long size = 0;
078: private boolean isLink = false;
079: private int accessible = -1;
080: private boolean noRender = false;
081: public Date date = null;
082:
083: public DirEntry(String file, ActionListener who) {
084: this .file = file;
085: this .who = who;
086: setFile();
087: }
088:
089: public void setFile() {
090: String f = file;
091:
092: if ((f.indexOf("<") >= 0) && (f.indexOf(">") >= 0)) {
093: f = file.substring(file.indexOf("<") + 1);
094: f = f.substring(0, f.lastIndexOf(">"));
095: }
096:
097: int lastIndex = f.lastIndexOf(".");
098: String image = Settings.fileImage; // default
099:
100: if (lastIndex != -1) {
101: String ext = f.substring(lastIndex);
102: String tmp = (String) extensionMap.get(ext.toLowerCase());
103:
104: if (tmp != null) // we found an extension, let's use it's image
105: {
106: image = tmp;
107: }
108: }
109:
110: // else use the default
111: img = HImage.getImage(c, image);
112:
113: if (img == null) {
114: img = HImage.getImage(c, Settings.fileImage);
115: }
116:
117: isFile = true;
118: isDirectory = false;
119: }
120:
121: public void setDirectory() {
122: img = HImage.getImage(c, Settings.dirImage);
123: isFile = false;
124: isDirectory = true;
125: }
126:
127: public void setNoRender() {
128: noRender = true;
129: }
130:
131: public boolean getNoRender() {
132: return noRender;
133: }
134:
135: public void setPermission(int what) {
136: accessible = what;
137: }
138:
139: public int getPermission() {
140: return accessible;
141: }
142:
143: public void setSelected(boolean state) {
144: selected = state;
145: }
146:
147: public boolean isDirectory() {
148: return isDirectory;
149: }
150:
151: public boolean isFile() {
152: return isFile;
153: }
154:
155: public boolean isSelected() {
156: return selected;
157: }
158:
159: public String toString() {
160: return file;
161: }
162:
163: public Image getImage() {
164: return img;
165: }
166:
167: public ImageIcon getImageIcon() {
168: return new ImageIcon(img);
169: }
170:
171: public void setDate(Date d) {
172: date = d;
173: }
174:
175: public String getDate() {
176: if (date == null) {
177: return "";
178: }
179:
180: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
181:
182: return df.format(date);
183: }
184:
185: public void setFileSize(long s) {
186: size = s;
187: }
188:
189: public String getFileSize() {
190: if (isDirectory() || (size < 0)) {
191: return " ";
192: }
193:
194: long rsize = size;
195:
196: String type = "bs";
197:
198: if (rsize > 1024) {
199: rsize = rsize / 1024;
200: type = "kb";
201: }
202:
203: if (rsize > 1024) {
204: rsize = rsize / 1024;
205: type = "mb";
206: }
207:
208: if (rsize > 1024) {
209: rsize = rsize / 1024;
210: type = "gb";
211: }
212:
213: String x = Long.toString(rsize);
214:
215: while (x.length() < 4) {
216: x = " " + x;
217: }
218:
219: return x + " " + type + " > ";
220: }
221:
222: public long getRawSize() {
223: return size;
224: }
225:
226: public void setLink() {
227: img = HImage.getImage(c, Settings.linkImage);
228: file = file.substring(0, file.lastIndexOf("###"));
229: isLink = true;
230: }
231:
232: public boolean isLink() {
233: return isLink;
234: }
235:
236: }
|