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