PageRenderTime 70ms CodeModel.GetById 21ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/browser/FileCellRenderer.java

#
Java | 289 lines | 203 code | 38 blank | 48 comment | 28 complexity | 756f1d9a68080c31245f5ff2f78d70a2 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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 *
  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 java.awt.font.*;
 29import javax.swing.*;
 30import javax.swing.border.*;
 31import javax.swing.table.*;
 32import org.gjt.sp.jedit.io.VFS;
 33import org.gjt.sp.jedit.*;
 34//}}}
 35
 36public class FileCellRenderer extends DefaultTableCellRenderer
 37{
 38	public static Icon fileIcon = GUIUtilities.loadIcon("File.png");
 39	public static Icon openFileIcon = GUIUtilities.loadIcon("OpenFile.png");
 40	public static Icon dirIcon = GUIUtilities.loadIcon("Folder.png");
 41	public static Icon openDirIcon = GUIUtilities.loadIcon("OpenFolder.png");
 42	public static Icon filesystemIcon = GUIUtilities.loadIcon("DriveSmall.png");
 43	public static Icon loadingIcon = GUIUtilities.loadIcon("ReloadSmall.png");
 44
 45	//{{{ FileCellRenderer constructor
 46	public FileCellRenderer()
 47	{
 48		plainFont = UIManager.getFont("Tree.font");
 49		if(plainFont == null)
 50			plainFont = jEdit.getFontProperty("metal.secondary.font");
 51		boldFont = plainFont.deriveFont(Font.BOLD);
 52	} //}}}
 53
 54	//{{{ getTableCellRendererComponent() method
 55	public Component getTableCellRendererComponent(JTable table,
 56		Object value, boolean isSelected, boolean hasFocus, 
 57		int row, int column)
 58	{
 59		super.getTableCellRendererComponent(table,value,isSelected,
 60			hasFocus,row,column);
 61
 62		if(value instanceof VFSDirectoryEntryTableModel.Entry)
 63		{
 64			VFSDirectoryEntryTableModel.Entry entry =
 65				(VFSDirectoryEntryTableModel.Entry)value;
 66			VFS.DirectoryEntry file = entry.dirEntry;
 67
 68			setFont(file.type == VFS.DirectoryEntry.FILE
 69				? plainFont : boldFont);
 70
 71			this.isSelected = isSelected;
 72			this.file = file;
 73
 74			if(column == 0)
 75			{
 76				// while its broken to have a null
 77				// symlinkPath, some older plugins
 78				// might...
 79				String path;
 80				if(file.symlinkPath == null)
 81					path = file.path;
 82				else
 83					path = file.symlinkPath;
 84				openBuffer = (jEdit._getBuffer(path) != null);
 85
 86				setIcon(showIcons
 87					? getIconForFile(file,entry.expanded,
 88					openBuffer) : null);
 89				setText(file.name);
 90
 91				int state;
 92				if(file.type == VFS.DirectoryEntry.FILE)
 93					state = ExpansionToggleBorder.STATE_NONE;
 94				else if(entry.expanded)
 95					state = ExpansionToggleBorder.STATE_EXPANDED;
 96				else
 97					state = ExpansionToggleBorder.STATE_COLLAPSED;
 98
 99				setBorder(new ExpansionToggleBorder(
100					state,entry.level));
101			}
102			else
103			{
104				VFSDirectoryEntryTableModel model = (VFSDirectoryEntryTableModel)table.getModel();
105				String extAttr = model.getExtendedAttribute(column - 1);
106
107				openBuffer = false;
108				setIcon(null);
109				setText(file.getExtendedAttribute(extAttr));
110				setBorder(new EmptyBorder(1,1,1,1));
111			}
112		}
113
114		return this;
115	} //}}}
116
117	//{{{ paintComponent() method
118	public void paintComponent(Graphics g)
119	{
120		if(!isSelected)
121		{
122			Color color = file.getColor();
123
124			setForeground(color == null
125				? UIManager.getColor("Tree.foreground")
126				: color);
127		}
128
129		super.paintComponent(g);
130
131		if(openBuffer)
132		{
133			Font font = getFont();
134
135			FontMetrics fm = getFontMetrics(font);
136			int x, y;
137			if(getIcon() == null)
138			{
139				x = 0;
140				y = fm.getAscent() + 2;
141			}
142			else
143			{
144				x = getIcon().getIconWidth() + getIconTextGap();
145				y = Math.max(fm.getAscent() + 2,16);
146			}
147
148			Insets border = getBorder().getBorderInsets(this);
149			x += border.left;
150
151			g.setColor(getForeground());
152			g.drawLine(x,y,x + fm.stringWidth(getText()),y);
153		}
154	} //}}}
155
156	//{{{ getIconForFile() method
157	/**
158	 * @since jEdit 4.2pre7
159	 */
160	public static Icon getIconForFile(VFS.DirectoryEntry file,
161		boolean expanded)
162	{
163		return getIconForFile(file,expanded,
164			jEdit._getBuffer(file.symlinkPath) != null);
165	} //}}}
166
167	//{{{ getIconForFile() method
168	public static Icon getIconForFile(VFS.DirectoryEntry file,
169		boolean expanded, boolean openBuffer)
170	{
171		if(file.type == VFS.DirectoryEntry.DIRECTORY)
172			return (expanded ? openDirIcon : dirIcon);
173		else if(file.type == VFS.DirectoryEntry.FILESYSTEM)
174			return filesystemIcon;
175		else if(openBuffer)
176			return openFileIcon;
177		else
178			return fileIcon;
179	} //}}}
180
181	//{{{ Package-private members
182	Font plainFont;
183	Font boldFont;
184	boolean showIcons;
185
186	//{{{ propertiesChanged() method
187	void propertiesChanged()
188	{
189		showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons");
190	} //}}}
191
192	//{{{ getEntryWidth() method
193	int getEntryWidth(VFSDirectoryEntryTableModel.Entry entry,
194		Font font, FontRenderContext fontRenderContext)
195	{
196		String name = entry.dirEntry.name;
197		int width = (int)font.getStringBounds(name,fontRenderContext)
198			.getWidth();
199		width += ExpansionToggleBorder.ICON_WIDTH
200			+ entry.level * ExpansionToggleBorder.LEVEL_WIDTH
201			+ 3;
202		if(showIcons)
203		{
204			width += fileIcon.getIconWidth();
205			width += getIconTextGap();
206		}
207		return width;
208	} //}}}
209
210	//}}}
211
212	//{{{ Private members
213	private boolean openBuffer;
214	private boolean isSelected;
215	private VFS.DirectoryEntry file;
216	//}}}
217
218	//{{{ ExpansionToggleBorder class
219	static class ExpansionToggleBorder implements Border
220	{
221		static final Icon COLLAPSED_ICON;
222		static final Icon EXPANDED_ICON;
223		static final int ICON_WIDTH;
224
225		static final int LEVEL_WIDTH = 15;
226
227		static final int STATE_NONE = 0;
228		static final int STATE_COLLAPSED = 1;
229		static final int STATE_EXPANDED = 2;
230
231		//{{{ ExpansionToggleBorder constructor
232		public ExpansionToggleBorder(int state, int level)
233		{
234			this.state = state;
235			this.level = level;
236		} //}}}
237
238		//{{{ paintBorder() method
239		public void paintBorder(Component c, Graphics g,
240			int x, int y, int width, int height)
241		{
242			switch(state)
243			{
244			case STATE_COLLAPSED:
245				COLLAPSED_ICON.paintIcon(c,g,
246					x + level * LEVEL_WIDTH + 2,
247					y + (height - COLLAPSED_ICON.getIconHeight()) / 2);
248				break;
249			case STATE_EXPANDED:
250				EXPANDED_ICON.paintIcon(c,g,
251					x + level * LEVEL_WIDTH + 2,
252					y + 2 + (height - EXPANDED_ICON.getIconHeight()) / 2);
253				break;
254			}
255		} //}}}
256
257		//{{{ getBorderInsets() method
258		public Insets getBorderInsets(Component c)
259		{
260			return new Insets(1,level * LEVEL_WIDTH
261				+ ICON_WIDTH + 4,1,1);
262		} //}}}
263
264		//{{{ isBorderOpaque() method
265		public boolean isBorderOpaque()
266		{
267			return false;
268		} //}}}
269
270		//{{{ isExpansionToggle() method
271		public static boolean isExpansionToggle(int level, int x)
272		{
273			return (x >= level * LEVEL_WIDTH)
274				&& (x <= level * LEVEL_WIDTH + ICON_WIDTH);
275		} //}}}
276
277		//{{{ Private members
278		private int state;
279		private int level;
280
281		static
282		{
283			COLLAPSED_ICON = GUIUtilities.loadIcon("arrow1.png");
284			EXPANDED_ICON = GUIUtilities.loadIcon("arrow2.png");
285			ICON_WIDTH = Math.max(COLLAPSED_ICON.getIconWidth(),
286				EXPANDED_ICON.getIconWidth());
287		} //}}}
288	} //}}}
289}