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