001    package nl.cwi.sen1.visplugin.table.model;
002    
003    import java.awt.Color;
004    import java.awt.Component;
005    import java.awt.Graphics;
006    import java.awt.geom.Rectangle2D;
007    
008    import javax.swing.JLabel;
009    import javax.swing.JTable;
010    import javax.swing.table.TableCellRenderer;
011    
012    /**
013     * Location button renderer. Class responisible for creating the buttons which
014     * are rendered when a location is specified in an Rtuple.
015     *
016     * @author Anton Lycklama a Nijeholt
017     * @author Bas Basten
018     * @author Anton Gerdessen (Javadoc + review)
019     * @date 13-03-2007
020     */
021    public class LocationButtonRenderer extends JLabel implements TableCellRenderer {
022    
023        private static final long serialVersionUID = 1L;
024    
025        private int m_lineWidth = 0;
026    
027        private int m_lineHeight = 0;
028    
029        /**
030         * Constructor.
031         *
032         * @author Anton Lycklama a Nijeholt
033         * @author Bas Basten
034         * @author Anton Gerdessen (Javadoc + review)
035         * @date 13-03-2007
036         */
037        public LocationButtonRenderer() {
038            initialize();
039        }
040    
041        /**
042         * Set the visible parameters for this label.
043         *
044         * @author Anton Lycklama a Nijeholt
045         * @author Bas Basten
046         * @author Anton Gerdessen (Javadoc + review)
047         * @date 13-03-2007
048         */
049        public void initialize() {
050            setOpaque(true);
051            setText("<b>Location</b>");
052            setForeground(Color.blue);
053        }
054    
055        /**
056         * Make the label invisible when its not selected.
057         *
058         * @author Anton Lycklama a Nijeholt
059         * @author Bas Basten
060         * @author Anton Gerdessen (Javadoc + review)
061         * @date 13-03-2007
062         * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
063         *      java.lang.Object, boolean, boolean, int, int)
064         */
065        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
066    
067            if (isSelected) {
068                setBackground(table.getSelectionBackground());
069            } else {
070                setBackground(table.getBackground());
071            }
072    
073            setText((value == null) ? "" : value.toString());
074            return this;
075        }
076    
077        /**
078         * Calculate the bounds of the underline. This method will be called one for
079         * each label. This method cannot be called in the initialize because the
080         * graphics bounds are not set at that time.
081         *
082         * @author Anton Gerdessen
083         * @date 14-03-2007
084         */
085        public void calculateUnderLineBounds() {
086            // Calculate the lineSize for the underline once and store it.
087            Rectangle2D textBounds = getFontMetrics(getFont()).getStringBounds(
088                    getText(), getGraphics());
089    
090            int textWidth = (int) textBounds.getWidth();
091            int iconWidth = (getIcon() == null ? 0 : getIcon().getIconWidth()
092                    + getIconTextGap());
093    
094            m_lineWidth = iconWidth + textWidth;
095            m_lineHeight = getHeight() / 2 + (int) (textBounds.getHeight() / 2);
096    
097        }
098    
099        /**
100         * Overwrite the paint method, call super to paint the label itself. Draw
101         * the underline afterwards.
102         *
103         * @author Anton Lycklama a Nijeholt
104         * @author Bas Basten
105         * @author Anton Gerdessen (Javadoc + review)
106         * @date 13-03-2
107         */
108        public void paint(Graphics g) {
109            super.paint(g);
110    
111            // If the bounds have not yet been calculated, calculate them.
112            if (m_lineWidth == 0 || m_lineWidth != 0) {
113                calculateUnderLineBounds();
114            }
115    
116            g.setColor(getForeground());
117            g.drawLine(0, m_lineHeight, m_lineWidth, m_lineHeight);
118        }
119    
120    }