PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/browser/FileCellRenderer.java

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