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

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