001/**
002 * 
003 */
004package icy.gui.component.renderer;
005
006import icy.math.MathUtil;
007import icy.type.collection.array.Array1DUtil;
008import icy.type.collection.array.ArrayUtil;
009
010import javax.swing.SwingConstants;
011
012import org.pushingpixels.substance.api.renderers.SubstanceDefaultTableCellRenderer;
013
014/**
015 * @author Stephane
016 */
017public class NativeArrayTableCellRenderer extends SubstanceDefaultTableCellRenderer
018{
019    /**
020     * 
021     */
022    private static final long serialVersionUID = 7536618123117211456L;
023
024    final boolean signed;
025
026    public NativeArrayTableCellRenderer(boolean signed)
027    {
028        super();
029
030        this.signed = signed;
031
032        setHorizontalAlignment(SwingConstants.TRAILING);
033    }
034
035    public NativeArrayTableCellRenderer()
036    {
037        this(true);
038    }
039
040    @Override
041    protected void setValue(Object value)
042    {
043        if ((value != null) && (ArrayUtil.getDim(value) == 1))
044        {
045            final int len = ArrayUtil.getLength(value);
046
047            String s;
048
049            if (len == 0)
050                s = "";
051            else if (len == 1)
052                s = Double.toString(MathUtil.roundSignificant(Array1DUtil.getValue(value, 0, signed), 5));
053            else
054            {
055                s = "[" + Double.toString(MathUtil.roundSignificant(Array1DUtil.getValue(value, 0, signed), 5));
056                for (int i = 1; i < len; i++)
057                    s += " " + MathUtil.roundSignificant(Array1DUtil.getValue(value, i, signed), 5);
058                s += "]";
059            }
060
061            setText(s);
062        }
063        else
064            super.setValue(value);
065    }
066}