/plugins/CodeBrowser/branches/ezust/com/bitart/codebrowser/CBListCellRenderer.java

#
Java | 175 lines | 100 code | 18 blank | 57 comment | 8 complexity | 3281f9e899e37e06d609700f471a5c0e MD5 | raw file

✨ Summary
  1. /******************************************************************************
  2. * Copyright 2002 BITart Gerd Knops. All rights reserved.
  3. *
  4. * Project : CodeBrowser
  5. * File : CBListCellRenderer.java
  6. * Author : Gerd Knops gerti@BITart.com
  7. *
  8. *******************************************************************************
  9. * :mode=java:folding=indent:collapseFolds=1:
  10. * History:
  11. * 020510 Creation of file
  12. *
  13. *******************************************************************************
  14. *
  15. * Description:
  16. * CBListCellRenderer is a ListCellRenderer displaying a buffers name as
  17. * follows:
  18. *
  19. * - The file name is displayed using the color the VFS assigns to it
  20. * - As much as possible from the END of the buffers path is displayed
  21. * in dark gray if the buffer is saved, and dark red if the buffer
  22. * has unsaved changes
  23. * - A bold font is used if the buffer is selected
  24. *
  25. * $Id: CBListCellRenderer.java 1655 2005-10-21 17:12:23Z ezust $
  26. *
  27. *******************************************************************************
  28. *
  29. * DISCLAIMER
  30. *
  31. * BITart and Gerd Knops make no warranties, representations or commitments
  32. * with regard to the contents of this software. BITart and Gerd Knops
  33. * specifically disclaim any and all warranties, wether express, implied or
  34. * statutory, including, but not limited to, any warranty of merchantability
  35. * or fitness for a particular purpose, and non-infringement. Under no
  36. * circumstances will BITart or Gerd Knops be liable for loss of data,
  37. * special, incidental or consequential damages out of the use of this
  38. * software, even if those damages were forseeable, or BITart or Gerd Knops
  39. * was informed of their potential.
  40. *
  41. ******************************************************************************/
  42. package com.bitart.codebrowser;
  43. /******************************************************************************
  44. * Imports
  45. ******************************************************************************/
  46. import java.util.*;
  47. import java.awt.*;
  48. import java.awt.font.*;
  49. import java.awt.geom.*;
  50. import javax.swing.*;
  51. import org.gjt.sp.jedit.*;
  52. import org.gjt.sp.jedit.io.*;
  53. /*****************************************************************************/
  54. public class CBListCellRenderer extends JPanel implements ListCellRenderer
  55. {
  56. /******************************************************************************
  57. * Vars
  58. ******************************************************************************/
  59. JList list;
  60. Buffer buf;
  61. boolean selected;
  62. int fontHeight=20;
  63. /******************************************************************************
  64. * Implementation
  65. ******************************************************************************/
  66. public Dimension getMinimumSize()
  67. {
  68. return new Dimension(10,fontHeight);
  69. }
  70. public Dimension getPreferredSize()
  71. {
  72. return new Dimension(10,fontHeight);
  73. }
  74. public void paintComponent(Graphics g)
  75. {
  76. int w=getWidth();
  77. //Font f=list.getFont();
  78. Font f=UIManager.getFont("Tree.font");
  79. if(selected) f=f.deriveFont(Font.BOLD);
  80. Font fi=f.deriveFont((selected)?Font.BOLD|Font.ITALIC:Font.ITALIC,f.getSize()-1);
  81. FontRenderContext frc=new FontRenderContext(null,true,false);
  82. LineMetrics lm=f.getLineMetrics("AZfj",frc);
  83. int baseline=2+(int)Math.ceil(lm.getAscent());
  84. Rectangle2D r2d=f.getStringBounds(" ",frc);
  85. int spacesWidth=(int)Math.ceil(r2d.getWidth());
  86. r2d=fi.getStringBounds("...",frc);
  87. int elipsesWidth=(int)Math.ceil(r2d.getWidth());
  88. String sep=java.io.File.separator;
  89. String name=buf.getName();
  90. String path=buf.getPath();
  91. int idx=path.lastIndexOf(sep);
  92. if(idx>0) path=path.substring(0,idx);
  93. r2d=f.getStringBounds(name,frc);
  94. int nl=(int)Math.ceil(r2d.getWidth());
  95. r2d=fi.getStringBounds(path,frc);
  96. int pl=(int)Math.ceil(r2d.getWidth());
  97. if(nl+pl+spacesWidth+elipsesWidth>w-10)
  98. {
  99. while(nl+pl+spacesWidth+elipsesWidth>w-10)
  100. {
  101. idx=path.indexOf(sep,1);
  102. if(idx<=0) break;
  103. path=path.substring(idx);
  104. r2d=fi.getStringBounds(path,frc);
  105. pl=(int)Math.ceil(r2d.getWidth());
  106. }
  107. path="..."+path;
  108. pl+=elipsesWidth;
  109. }
  110. if(nl+pl+spacesWidth>w-10)
  111. {
  112. path="...";
  113. pl=elipsesWidth;
  114. if(nl+pl+spacesWidth>w-10)
  115. {
  116. path="";
  117. pl=0;
  118. }
  119. }
  120. //g.setColor(Color.black);
  121. g.setColor(VFS.getDefaultColorFor(name));
  122. g.setFont(f);
  123. g.drawString(name,5,baseline);
  124. if(pl>0)
  125. {
  126. if(buf.isDirty())
  127. {
  128. g.setColor(new Color(0x800000));
  129. }
  130. else
  131. {
  132. g.setColor(Color.darkGray);
  133. }
  134. g.setFont(fi);
  135. g.drawString(path,w-pl-5,baseline);
  136. }
  137. }
  138. /******************************************************************************
  139. * ListCellRenderer interface
  140. ******************************************************************************/
  141. public Component getListCellRendererComponent(
  142. JList list,
  143. Object value,
  144. int index,
  145. boolean selected,
  146. boolean hasFocus
  147. )
  148. {
  149. this.list=list;
  150. buf=(Buffer)value;
  151. this.selected=selected;
  152. Rectangle2D r=list.getFont().getMaxCharBounds(new FontRenderContext(null,true,false));
  153. fontHeight=4+(int)Math.ceil(r.getHeight());
  154. return this;
  155. }
  156. }
  157. /*************************************************************************EOF*/