PageRenderTime 54ms CodeModel.GetById 41ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 159 lines | 101 code | 22 blank | 36 comment | 16 complexity | a7aaf67c9c4ffbfdd1769a6166f9395d 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.gif");
 38	public static Icon dirIcon = GUIUtilities.loadIcon("closed_folder.gif");
 39	public static Icon openDirIcon = GUIUtilities.loadIcon("open_folder.gif");
 40	public static Icon filesystemIcon = GUIUtilities.loadIcon("drive.gif");
 41	public static Icon loadingIcon = GUIUtilities.loadIcon("drive.gif");
 42
 43	//{{{ FileCellRenderer constructor
 44	public FileCellRenderer()
 45	{
 46		plainFont = UIManager.getFont("Tree.font");
 47		boldFont = plainFont.deriveFont(Font.BOLD);
 48		setBorder(new EmptyBorder(1,0,1,0));
 49	} //}}}
 50
 51	//{{{ getTreeCellRendererComponent() method
 52	public Component getTreeCellRendererComponent(JTree tree, Object value,
 53		boolean sel, boolean expanded, boolean leaf, int row,
 54		boolean focus)
 55	{
 56		super.getTreeCellRendererComponent(tree,value,sel,expanded,
 57			leaf,row,focus);
 58
 59		DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
 60		Object userObject = treeNode.getUserObject();
 61		if(userObject instanceof VFS.DirectoryEntry)
 62		{
 63			VFS.DirectoryEntry file = (VFS.DirectoryEntry)userObject;
 64
 65			underlined = (jEdit.getBuffer(file.path) != null);
 66
 67			setIcon(showIcons
 68				? getIconForFile(file,expanded)
 69				: null);
 70			setFont(file.type == VFS.DirectoryEntry.FILE
 71				? plainFont : boldFont);
 72			setText(file.name);
 73
 74			if(!sel)
 75			{
 76				Color color = file.getColor();
 77
 78				setForeground(color == null
 79					? tree.getForeground()
 80					: color);
 81			}
 82		}
 83		else if(userObject instanceof BrowserView.LoadingPlaceholder)
 84		{
 85			setIcon(showIcons ? loadingIcon : null);
 86			setFont(plainFont);
 87			setText(jEdit.getProperty("vfs.browser.tree.loading"));
 88			underlined = false;
 89		}
 90		else if(userObject instanceof String)
 91		{
 92			setIcon(showIcons ? dirIcon : null);
 93			setFont(boldFont);
 94			setText((String)userObject);
 95			underlined = false;
 96		}
 97		else
 98		{
 99			// userObject is null?
100			setIcon(null);
101			setText(null);
102		}
103
104		return this;
105	} //}}}
106
107	//{{{ paintComponent() method
108	public void paintComponent(Graphics g)
109	{
110		if(underlined)
111		{
112			Font font = getFont();
113
114			FontMetrics fm = getFontMetrics(getFont());
115			int x = (getIcon() == null ? 0
116				: getIcon().getIconWidth()
117				+ getIconTextGap());
118			g.setColor(getForeground());
119			g.drawLine(x,fm.getAscent() + 2,
120				x + fm.stringWidth(getText()),
121				fm.getAscent() + 2);
122		}
123
124		super.paintComponent(g);
125	} //}}}
126
127	//{{{ Package-private members
128	boolean showIcons;
129
130	//{{{ propertiesChanged() method
131	void propertiesChanged()
132	{
133		showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons");
134	} //}}}
135
136	//}}}
137
138	//{{{ Private members
139
140	//{{{ Instance members
141	private Font plainFont;
142	private Font boldFont;
143
144	private boolean underlined;
145	//}}}
146
147	//{{{ getIconForFile() method
148	private Icon getIconForFile(VFS.DirectoryEntry file, boolean expanded)
149	{
150		if(file.type == VFS.DirectoryEntry.DIRECTORY)
151			return (expanded ? openDirIcon : dirIcon);
152		else if(file.type == VFS.DirectoryEntry.FILESYSTEM)
153			return filesystemIcon;
154		else
155			return fileIcon;
156	} //}}}
157
158	//}}}
159}