PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 159 lines | 101 code | 22 blank | 36 comment | 16 complexity | a7aaf67c9c4ffbfdd1769a6166f9395d 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 list and tree 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 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 javax.swing.*;
  27. import javax.swing.tree.*;
  28. import javax.swing.border.*;
  29. import org.gjt.sp.jedit.io.VFS;
  30. import org.gjt.sp.jedit.*;
  31. //}}}
  32. public class FileCellRenderer extends DefaultTreeCellRenderer
  33. {
  34. public static Icon fileIcon = GUIUtilities.loadIcon("file.gif");
  35. public static Icon dirIcon = GUIUtilities.loadIcon("closed_folder.gif");
  36. public static Icon openDirIcon = GUIUtilities.loadIcon("open_folder.gif");
  37. public static Icon filesystemIcon = GUIUtilities.loadIcon("drive.gif");
  38. public static Icon loadingIcon = GUIUtilities.loadIcon("drive.gif");
  39. //{{{ FileCellRenderer constructor
  40. public FileCellRenderer()
  41. {
  42. plainFont = UIManager.getFont("Tree.font");
  43. boldFont = plainFont.deriveFont(Font.BOLD);
  44. setBorder(new EmptyBorder(1,0,1,0));
  45. } //}}}
  46. //{{{ getTreeCellRendererComponent() method
  47. public Component getTreeCellRendererComponent(JTree tree, Object value,
  48. boolean sel, boolean expanded, boolean leaf, int row,
  49. boolean focus)
  50. {
  51. super.getTreeCellRendererComponent(tree,value,sel,expanded,
  52. leaf,row,focus);
  53. DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
  54. Object userObject = treeNode.getUserObject();
  55. if(userObject instanceof VFS.DirectoryEntry)
  56. {
  57. VFS.DirectoryEntry file = (VFS.DirectoryEntry)userObject;
  58. underlined = (jEdit.getBuffer(file.path) != null);
  59. setIcon(showIcons
  60. ? getIconForFile(file,expanded)
  61. : null);
  62. setFont(file.type == VFS.DirectoryEntry.FILE
  63. ? plainFont : boldFont);
  64. setText(file.name);
  65. if(!sel)
  66. {
  67. Color color = file.getColor();
  68. setForeground(color == null
  69. ? tree.getForeground()
  70. : color);
  71. }
  72. }
  73. else if(userObject instanceof BrowserView.LoadingPlaceholder)
  74. {
  75. setIcon(showIcons ? loadingIcon : null);
  76. setFont(plainFont);
  77. setText(jEdit.getProperty("vfs.browser.tree.loading"));
  78. underlined = false;
  79. }
  80. else if(userObject instanceof String)
  81. {
  82. setIcon(showIcons ? dirIcon : null);
  83. setFont(boldFont);
  84. setText((String)userObject);
  85. underlined = false;
  86. }
  87. else
  88. {
  89. // userObject is null?
  90. setIcon(null);
  91. setText(null);
  92. }
  93. return this;
  94. } //}}}
  95. //{{{ paintComponent() method
  96. public void paintComponent(Graphics g)
  97. {
  98. if(underlined)
  99. {
  100. Font font = getFont();
  101. FontMetrics fm = getFontMetrics(getFont());
  102. int x = (getIcon() == null ? 0
  103. : getIcon().getIconWidth()
  104. + getIconTextGap());
  105. g.setColor(getForeground());
  106. g.drawLine(x,fm.getAscent() + 2,
  107. x + fm.stringWidth(getText()),
  108. fm.getAscent() + 2);
  109. }
  110. super.paintComponent(g);
  111. } //}}}
  112. //{{{ Package-private members
  113. boolean showIcons;
  114. //{{{ propertiesChanged() method
  115. void propertiesChanged()
  116. {
  117. showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons");
  118. } //}}}
  119. //}}}
  120. //{{{ Private members
  121. //{{{ Instance members
  122. private Font plainFont;
  123. private Font boldFont;
  124. private boolean underlined;
  125. //}}}
  126. //{{{ getIconForFile() method
  127. private Icon getIconForFile(VFS.DirectoryEntry file, boolean expanded)
  128. {
  129. if(file.type == VFS.DirectoryEntry.DIRECTORY)
  130. return (expanded ? openDirIcon : dirIcon);
  131. else if(file.type == VFS.DirectoryEntry.FILESYSTEM)
  132. return filesystemIcon;
  133. else
  134. return fileIcon;
  135. } //}}}
  136. //}}}
  137. }