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