PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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