1
2
3
4
5
6
7 package net.sourceforge.jsh3modtool.gui.imagetable;
8
9 import java.awt.*;
10
11 import javax.swing.*;
12 import javax.swing.table.*;
13
14 /***
15 * A cell renderer for FolderCells, displays a Folder and its short name under it
16 *
17 * @author erma
18 */
19 public class FolderCellRenderer extends JComponent implements TableCellRenderer
20 {
21 private Font messageFont = (Font) UIManager.get("Label.font");
22
23 private ImageIcon folderIcon;
24 private int folderIconWidth;
25 private int folderIconHeight;
26
27 private FolderCell folderCell;
28
29 /*** Creates a new instance of FolderCellRenderer */
30 public FolderCellRenderer()
31 {
32
33
34
35 }
36
37 /*** Returns the component used for drawing the cell. This method is
38 * used to configure the renderer appropriately before drawing.
39 * @param table the <code>JTable</code> that is asking the
40 * renderer to draw; can be <code>null</code>
41 * @param value the value of the cell to be rendered. It is
42 * up to the specific renderer to interpret
43 * and draw the value. For example, if
44 * <code>value</code>
45 * is the string "true", it could be rendered as a
46 * string or it could be rendered as a check
47 * box that is checked. <code>null</code> is a
48 * valid value
49 * @param isSelected true if the cell is to be rendered with the
50 * selection highlighted; otherwise false
51 * @param hasFocus if true, render cell appropriately. For
52 * example, put a special border on the cell, if
53 * the cell can be edited, render in the color used
54 * to indicate editing
55 * @param row the row index of the cell being drawn. When
56 * drawing the header, the value of
57 * <code>row</code> is -1
58 * @param column the column index of the cell being drawn
59 * @return an AWT component, can be null
60 */
61 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
62 {
63 Component component = null;
64
65 if ( ( value != null ) && ( value instanceof FolderCell ) )
66 {
67 folderCell = (FolderCell) value;
68 setToolTipText( folderCell.getTargetPath() );
69 component = this;
70 }
71
72 return component;
73 }
74
75 /*** Paints the component, ie paints the background, the imagepanel, the text
76 * and the checkbox
77 * @param graphics graphics 2D object
78 */
79 public void paintComponent(Graphics graphics)
80 {
81 int width = getWidth();
82 int height = getHeight();
83
84 Graphics2D graph = (Graphics2D) graphics;
85
86 FontMetrics fontmetrics = graph.getFontMetrics( messageFont );
87 int maxWidth = width;
88 if ( folderCell.getCachedNameMaxWidth() != maxWidth )
89 {
90 folderCell.recacheName( folderCell.getName(), maxWidth, fontmetrics );
91 }
92
93 folderIcon.paintIcon( this, graph, width / 2 - folderIconWidth / 2, height / 2 - folderIconHeight / 2 );
94
95 String cachedName = folderCell.getCachedName();
96 graph.drawString( cachedName, width / 2 - fontmetrics.stringWidth(cachedName) / 2, height - 5);
97 }
98
99
100
101
102 /*** Dummy implementation for performace reasons. */
103 public void validate() {}
104 /*** Dummy implementation for performace reasons. */
105 public void revalidate() {}
106 /*** Dummy implementation for performace reasons. {@inheritDoc} */
107 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
108 /*** Dummy implementation for performace reasons. {@inheritDoc} */
109 public void repaint(long tm, int x, int y, int width, int height){}
110 /*** Dummy implementation for performace reasons. {@inheritDoc} */
111 public void repaint(Rectangle r) {}
112 }