PageRenderTime 133ms CodeModel.GetById 80ms app.highlight 38ms RepoModel.GetById 12ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/browser/FileCellRenderer.java

#
Java | 164 lines | 110 code | 20 blank | 34 comment | 20 complexity | 0f82f83ed74ed93fd77875b3059a63a9 MD5 | raw file
  1/*
  2 * FileCellRenderer.java - renders list and tree 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 Slava Pestov
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit.browser;
 25
 26//{{{ Imports
 27import java.awt.*;
 28import javax.swing.*;
 29import javax.swing.tree.*;
 30import javax.swing.border.*;
 31import org.gjt.sp.jedit.io.VFS;
 32import org.gjt.sp.jedit.*;
 33//}}}
 34
 35public class FileCellRenderer extends DefaultTreeCellRenderer
 36{
 37	public static Icon fileIcon = GUIUtilities.loadIcon("File.png");
 38	public static Icon openFileIcon = GUIUtilities.loadIcon("OpenFile.png");
 39	public static Icon dirIcon = GUIUtilities.loadIcon("Folder.png");
 40	public static Icon openDirIcon = GUIUtilities.loadIcon("OpenFolder.png");
 41	public static Icon filesystemIcon = GUIUtilities.loadIcon("DriveSmall.png");
 42	public static Icon loadingIcon = GUIUtilities.loadIcon("ReloadSmall.png");
 43
 44	//{{{ FileCellRenderer constructor
 45	public FileCellRenderer()
 46	{
 47		plainFont = UIManager.getFont("Tree.font");
 48		boldFont = plainFont.deriveFont(Font.BOLD);
 49		setBorder(new EmptyBorder(1,0,1,0));
 50	} //}}}
 51
 52	//{{{ getTreeCellRendererComponent() method
 53	public Component getTreeCellRendererComponent(JTree tree, Object value,
 54		boolean sel, boolean expanded, boolean leaf, int row,
 55		boolean focus)
 56	{
 57		super.getTreeCellRendererComponent(tree,value,sel,expanded,
 58			leaf,row,focus);
 59
 60		DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
 61		Object userObject = treeNode.getUserObject();
 62		if(userObject instanceof VFS.DirectoryEntry)
 63		{
 64			VFS.DirectoryEntry file = (VFS.DirectoryEntry)userObject;
 65
 66			underlined = (jEdit.getBuffer(file.path) != null);
 67
 68			setIcon(showIcons
 69				? getIconForFile(file,expanded)
 70				: null);
 71			setFont(file.type == VFS.DirectoryEntry.FILE
 72				? plainFont : boldFont);
 73			setText(file.name);
 74
 75			if(!sel)
 76			{
 77				Color color = file.getColor();
 78
 79				setForeground(color == null
 80					? UIManager.getColor("Tree.foreground")
 81					: color);
 82			}
 83		}
 84		else if(userObject instanceof BrowserView.LoadingPlaceholder)
 85		{
 86			setIcon(showIcons ? loadingIcon : null);
 87			setFont(plainFont);
 88			setText(jEdit.getProperty("vfs.browser.tree.loading"));
 89			underlined = false;
 90		}
 91		else if(userObject instanceof String)
 92		{
 93			setIcon(showIcons ? dirIcon : null);
 94			setFont(boldFont);
 95			setText((String)userObject);
 96			underlined = false;
 97		}
 98		else
 99		{
100			// userObject is null?
101			setIcon(null);
102			setText(null);
103		}
104
105		return this;
106	} //}}}
107
108	//{{{ paintComponent() method
109	public void paintComponent(Graphics g)
110	{
111		if(underlined)
112		{
113			Font font = getFont();
114
115			FontMetrics fm = getFontMetrics(getFont());
116			int x, y;
117			if(getIcon() == null)
118			{
119				x = 0;
120				y = fm.getAscent() + 2;
121			}
122			else
123			{
124				x = getIcon().getIconWidth() + getIconTextGap();
125				y = Math.max(fm.getAscent() + 2,16);
126			}
127			g.setColor(getForeground());
128			g.drawLine(x,y,x + fm.stringWidth(getText()),y);
129		}
130
131		super.paintComponent(g);
132	} //}}}
133
134	//{{{ getIconForFile() method
135	public static Icon getIconForFile(VFS.DirectoryEntry file, boolean expanded)
136	{
137		if(file.type == VFS.DirectoryEntry.DIRECTORY)
138			return (expanded ? openDirIcon : dirIcon);
139		else if(file.type == VFS.DirectoryEntry.FILESYSTEM)
140			return filesystemIcon;
141		else if(jEdit.getBuffer(file.path) != null)
142			return openFileIcon;
143		else
144			return fileIcon;
145	} //}}}
146
147	//{{{ Package-private members
148	boolean showIcons;
149
150	//{{{ propertiesChanged() method
151	void propertiesChanged()
152	{
153		showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons");
154	} //}}}
155
156	//}}}
157
158	//{{{ Private members
159	private Font plainFont;
160	private Font boldFont;
161
162	private boolean underlined;
163	//}}}
164}