KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sf.jftp.config.Settings;
19 import net.sf.jftp.system.logging.Log;
20 import net.sf.jftp.gui.base.LocalDir;
21 import net.sf.jftp.gui.base.RemoteDir;
22 import net.sf.jftp.gui.framework.GUIDefaults;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import java.text.*;
28
29 import java.util.*;
30
31 import javax.swing.*;
32
33
34 // Display an icon and a string for each object in the list.
35
// class DirCellRenderer extends JLabel implements ListCellRenderer {
36
public class DirCellRenderer extends DefaultListCellRenderer
37 {
38     final static ImageIcon longIcon = new ImageIcon(Settings.dirImage);
39     final static ImageIcon shortIcon = new ImageIcon(Settings.fileImage);
40     private Object JavaDoc value;
41
42     public DirCellRenderer()
43     {
44     }
45
46     // This is the only method defined by ListCellRenderer.
47
// We just reconfigure the JLabel each time we're called.
48
public Component getListCellRendererComponent(JList list, Object JavaDoc value, // value to display
49
int index, // cell index
50
boolean isSelected, // is the cell selected
51
boolean cellHasFocus) // the list and the cell have the focus
52
{
53         /* The DefaultListCellRenderer class will take care of
54          * the JLabels text property, it's foreground and background
55          * colors, and so on.
56          */

57         super.getListCellRendererComponent(list, value, index, isSelected,
58                                            cellHasFocus);
59
60         ImageIcon i = new ImageIcon(((DirEntry) value).getImage());
61
62         if(i == null)
63         {
64             System.out.println("Img null: " + ((DirEntry) value).toString() +
65                                "/" + ((DirEntry) value).getImage());
66         }
67         else
68         {
69             setIcon((Icon) i);
70         }
71
72         this.value = value;
73         Log.devnull(this.value); // prevents eclipse warning
74

75         if(!((DirEntry) list.getModel().getElementAt(index)).getNoRender())
76         {
77             String JavaDoc size = "";
78
79             if(Settings.showFileSize)
80             {
81                 size = ((DirEntry) list.getModel().getElementAt(index)).getFileSize();
82             }
83
84             setFont(GUIDefaults.monospaced);
85
86             String JavaDoc s = value.toString();
87
88             String JavaDoc date = "";
89
90             if(Settings.showDateNoSize &&
91                    (((DirEntry) list.getModel().getElementAt(index)).who instanceof RemoteDir))
92             {
93                 size = ((DirEntry) list.getModel().getElementAt(index)).getDate();
94
95                 int x = 12 - size.length();
96
97                 for(int j = 0; j < x; j++)
98                 {
99                     size += " ";
100                 }
101             }
102             else if(Settings.showLocalDateNoSize &&
103                         (((DirEntry) list.getModel().getElementAt(index)).who instanceof LocalDir))
104             {
105                 size = ((DirEntry) list.getModel().getElementAt(index)).getDate();
106
107                 int x = 12 - size.length();
108
109                 for(int j = 0; j < x; j++)
110                 {
111                     size += " ";
112                 }
113             }
114
115             setText(size + s);
116         }
117         else
118         {
119             setFont(GUIDefaults.small);
120
121             String JavaDoc s = value.toString();
122             setText(s);
123         }
124
125         int ok = ((DirEntry) value).getPermission();
126
127         if(ok == DirEntry.DENIED)
128         {
129             setForeground(GUIDefaults.deniedColor);
130         }
131         else if(ok == DirEntry.W)
132         {
133             setForeground(GUIDefaults.writableColor);
134         }
135         else
136         {
137             setForeground(GUIDefaults.defaultColor);
138         }
139
140         if(((DirEntry) value).file.equals(".."))
141         {
142             setBackground(GUIDefaults.cdColor);
143         }
144
145         return this;
146     }
147 }
148
Popular Tags