net.sourceforge.msscodefactory.cflib.v2_1.CFLib.Swing.CFUInt32ColumnCellRenderer.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cflib.v2_1.CFLib.Swing.CFUInt32ColumnCellRenderer.java

Source

/*
 *  MSS Code Factory CFLib 2.1
 *
 *   Copyright (c) 2014-2015 Mark Sobkow
 *   
 *   This program is available as free software under the GNU LGPL v3, or
 *   under a commercial license from Mark Sobkow.  For commercial licensing
 *   details, please contact msobkow@sasktel.net.
 *
 *   Under the terms of the LGPL:
 *   
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU Lesser General Public License as
 *      published by the Free Software Foundation, either version 3 of
 *      the License, or (at your option) any later version.
 *     
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU Lesser General Public License for more details.
 *     
 *      You should have received a copy of the GNU Lesser General Public
 *      License along with this program.  If not,
 *      see http://www.gnu.org/licenses/.
 */

package net.sourceforge.msscodefactory.cflib.v2_1.CFLib.Swing;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;

import net.sourceforge.msscodefactory.cflib.v2_1.CFLib.*;

import org.apache.commons.codec.binary.Base64;

public class CFUInt32ColumnCellRenderer extends DefaultTableCellRenderer {
    protected static Format defaultFormat = null;

    public static Format getDefaultFormat() {
        if (defaultFormat == null) {
            defaultFormat = new DecimalFormat("#########0");
        }
        return (defaultFormat);
    }

    protected Object value = null;

    public CFUInt32ColumnCellRenderer() {
        super();
    }

    public Component getTableCellRendererComponent(JTable table, Object val, boolean isSelected, boolean hasFocus,
            int row, int column) {
        value = val;

        if (isSelected) {
            setBackground(Color.BLACK);
            setForeground(Color.LIGHT_GRAY);
        } else {
            setBackground(Color.LIGHT_GRAY);
            setForeground(Color.BLACK);
        }

        return (this);
    }

    public void paint(Graphics g) {
        if (g == null) {
            return;
        }
        Rectangle bounds = getBounds();
        g.setColor(getBackground());
        g.fill3DRect(0, 0, bounds.width, bounds.height, true);
        g.setColor(getForeground());
        String str;
        if (value instanceof Long) {
            Long val = (Long) value;
            Format fmt = getDefaultFormat();
            str = fmt.format(val);
        } else if (value instanceof String) {
            str = (String) value;
        } else {
            str = null;
        }
        if (str != null) {
            int firstNewline = str.indexOf('\n');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\r');
                if (firstNewline < 0) {
                    firstNewline = str.indexOf('\f');
                    if (firstNewline < 0) {
                        firstNewline = str.length();
                    }
                }
            }
            String firstLine = str.substring(0, firstNewline);
            FontMetrics fm = g.getFontMetrics();
            int ascent = fm.getAscent();
            int leading = fm.getLeading();
            int width = fm.stringWidth(firstLine);
            int x = (bounds.width - 8) - width;
            g.drawString(firstLine, x, leading + ascent + 4);
        }
    }

    public void setValue(Object o) {
        super.setValue(o);
        value = o;
    }
}