PageRenderTime 112ms CodeModel.GetById 87ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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