KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > browser > FileCellRenderer


1 /*
2  * FileCellRenderer.java - renders table cells for the VFS browser
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999 Jason Ginchereau
7  * Portions copyright (C) 2001, 2003 Slava Pestov
8  * Portions copyright (C) 2007 Matthieu Casanova
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */

24
25 package org.gjt.sp.jedit.browser;
26
27 //{{{ Imports
28
import java.awt.*;
29 import java.awt.font.*;
30 import javax.swing.*;
31 import javax.swing.border.*;
32 import javax.swing.table.*;
33 import org.gjt.sp.jedit.io.VFSFile;
34 import org.gjt.sp.jedit.*;
35 //}}}
36

37 /**
38  * Local filesystem VFS.
39  * @version $Id: FileCellRenderer.java 8581 2007-01-13 18:42:18Z kpouer $
40  */

41 public class FileCellRenderer extends DefaultTableCellRenderer
42 {
43     public static Icon fileIcon = GUIUtilities.loadIcon("File.png");
44     public static Icon openFileIcon = GUIUtilities.loadIcon("OpenFile.png");
45     public static Icon dirIcon = GUIUtilities.loadIcon("Folder.png");
46     public static Icon openDirIcon = GUIUtilities.loadIcon("OpenFolder.png");
47     public static Icon filesystemIcon = GUIUtilities.loadIcon("DriveSmall.png");
48     public static Icon loadingIcon = GUIUtilities.loadIcon("ReloadSmall.png");
49
50     //{{{ FileCellRenderer constructor
51
public FileCellRenderer()
52     {
53         plainFont = UIManager.getFont("Tree.font");
54         if(plainFont == null)
55             plainFont = jEdit.getFontProperty("metal.secondary.font");
56         boldFont = plainFont.deriveFont(Font.BOLD);
57     } //}}}
58

59     //{{{ getTableCellRendererComponent() method
60
public Component getTableCellRendererComponent(JTable table,
61         Object JavaDoc value, boolean isSelected, boolean hasFocus,
62         int row, int column)
63     {
64         super.getTableCellRendererComponent(table,value,isSelected,
65             hasFocus,row,column);
66
67         if(value instanceof VFSDirectoryEntryTableModel.Entry)
68         {
69             VFSDirectoryEntryTableModel.Entry entry =
70                 (VFSDirectoryEntryTableModel.Entry)value;
71             VFSFile file = entry.dirEntry;
72
73             setFont(file.getType() == VFSFile.FILE
74                 ? plainFont : boldFont);
75
76             this.isSelected = isSelected;
77             this.file = file;
78
79             if(column == 0)
80             {
81                 // while its broken to have a null
82
// symlinkPath, some older plugins
83
// might...
84
String JavaDoc path;
85                 if(file.getSymlinkPath() == null)
86                     path = file.getPath();
87                 else
88                     path = file.getSymlinkPath();
89                 openBuffer = jEdit._getBuffer(path) != null;
90
91                 setIcon(showIcons
92                     ? getIconForFile(file,entry.expanded,
93                     openBuffer) : null);
94                 setText(file.getName());
95
96                 int state;
97                 if(file.getType() == VFSFile.FILE)
98                     state = ExpansionToggleBorder.STATE_NONE;
99                 else if(entry.expanded)
100                     state = ExpansionToggleBorder.STATE_EXPANDED;
101                 else
102                     state = ExpansionToggleBorder.STATE_COLLAPSED;
103
104                 setBorder(new ExpansionToggleBorder(
105                     state,entry.level));
106             }
107             else
108             {
109                 VFSDirectoryEntryTableModel model = (VFSDirectoryEntryTableModel)table.getModel();
110                 String JavaDoc extAttr = model.getExtendedAttribute(column);
111
112                 openBuffer = false;
113                 setIcon(null);
114                 setText(file.getExtendedAttribute(extAttr));
115                 setBorder(new EmptyBorder(1,1,1,1));
116             }
117         }
118
119         return this;
120     } //}}}
121

122     //{{{ paintComponent() method
123
public void paintComponent(Graphics g)
124     {
125         if(!isSelected)
126         {
127             Color color = file.getColor();
128
129             setForeground(color == null
130                 ? UIManager.getColor("Tree.foreground")
131                 : color);
132         }
133
134         super.paintComponent(g);
135
136         if(openBuffer)
137         {
138             Font font = getFont();
139
140             FontMetrics fm = getFontMetrics(font);
141             int x, y;
142             if(getIcon() == null)
143             {
144                 x = 0;
145                 y = fm.getAscent() + 2;
146             }
147             else
148             {
149                 x = getIcon().getIconWidth() + getIconTextGap();
150                 y = Math.max(fm.getAscent() + 2,16);
151             }
152
153             Insets border = getBorder().getBorderInsets(this);
154             x += border.left;
155
156             g.setColor(getForeground());
157             g.drawLine(x,y,x + fm.stringWidth(getText()),y);
158         }
159     } //}}}
160

161     //{{{ getIconForFile() method
162
/**
163      * @since jEdit 4.3pre2
164      */

165     public static Icon getIconForFile(VFSFile file,
166         boolean expanded)
167     {
168         return getIconForFile(file,expanded,
169             jEdit._getBuffer(file.getSymlinkPath()) != null);
170     } //}}}
171

172     //{{{ getIconForFile() method
173
public static Icon getIconForFile(VFSFile file,
174         boolean expanded, boolean openBuffer)
175     {
176         if (defaultIcons)
177             return file.getDefaultIcon(expanded, openBuffer);
178         return file.getIcon(expanded, openBuffer);
179     } //}}}
180

181     //{{{ Package-private members
182
Font plainFont;
183     Font boldFont;
184     boolean showIcons;
185     private static boolean defaultIcons = true;
186
187     //{{{ propertiesChanged() method
188
void propertiesChanged()
189     {
190         showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons");
191         defaultIcons = jEdit.getBooleanProperty("vfs.browser.useDefaultIcons");
192     } //}}}
193

194     //{{{ getEntryWidth() method
195
int getEntryWidth(VFSDirectoryEntryTableModel.Entry entry,
196         Font font, FontRenderContext fontRenderContext)
197     {
198         String JavaDoc name = entry.dirEntry.getName();
199         int width = (int)font.getStringBounds(name,fontRenderContext)
200             .getWidth();
201         width += ExpansionToggleBorder.ICON_WIDTH
202             + entry.level * ExpansionToggleBorder.LEVEL_WIDTH
203             + 3;
204         if(showIcons)
205         {
206             width += fileIcon.getIconWidth();
207             width += getIconTextGap();
208         }
209         return width;
210     } //}}}
211

212     //}}}
213

214     //{{{ Private members
215
private boolean openBuffer;
216     private boolean isSelected;
217     private VFSFile file;
218     //}}}
219

220     //{{{ ExpansionToggleBorder class
221
static class ExpansionToggleBorder implements Border
222     {
223         static final Icon COLLAPSED_ICON;
224         static final Icon EXPANDED_ICON;
225         static final int ICON_WIDTH;
226
227         static final int LEVEL_WIDTH = 15;
228
229         static final int STATE_NONE = 0;
230         static final int STATE_COLLAPSED = 1;
231         static final int STATE_EXPANDED = 2;
232
233         //{{{ ExpansionToggleBorder constructor
234
ExpansionToggleBorder(int state, int level)
235         {
236             this.state = state;
237             this.level = level;
238         } //}}}
239

240         //{{{ paintBorder() method
241
public void paintBorder(Component c, Graphics g,
242             int x, int y, int width, int height)
243         {
244             switch(state)
245             {
246             case STATE_COLLAPSED:
247                 COLLAPSED_ICON.paintIcon(c,g,
248                     x + level * LEVEL_WIDTH + 2,
249                     y + (height - COLLAPSED_ICON.getIconHeight()) / 2);
250                 break;
251             case STATE_EXPANDED:
252                 EXPANDED_ICON.paintIcon(c,g,
253                     x + level * LEVEL_WIDTH + 2,
254                     y + 2 + (height - EXPANDED_ICON.getIconHeight()) / 2);
255                 break;
256             }
257         } //}}}
258

259         //{{{ getBorderInsets() method
260
public Insets getBorderInsets(Component c)
261         {
262             return new Insets(1,level * LEVEL_WIDTH
263                 + ICON_WIDTH + 4,1,1);
264         } //}}}
265

266         //{{{ isBorderOpaque() method
267
public boolean isBorderOpaque()
268         {
269             return false;
270         } //}}}
271

272         //{{{ isExpansionToggle() method
273
public static boolean isExpansionToggle(int level, int x)
274         {
275             return (x >= level * LEVEL_WIDTH)
276                 && (x <= level * LEVEL_WIDTH + ICON_WIDTH);
277         } //}}}
278

279         //{{{ Private members
280
private int state;
281         private int level;
282
283         static
284         {
285             COLLAPSED_ICON = GUIUtilities.loadIcon("arrow1.png");
286             EXPANDED_ICON = GUIUtilities.loadIcon("arrow2.png");
287             ICON_WIDTH = Math.max(COLLAPSED_ICON.getIconWidth(),
288                 EXPANDED_ICON.getIconWidth());
289         } //}}}
290
} //}}}
291
}
292
Popular Tags