Blame view

src/jsky/util/gui/SexagesimalTableCellRenderer.java 3.26 KB
fe0fb87e   Elodie Bourrec   First push to cre...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * ESO Archive
 *
 * $Id: SexagesimalTableCellRenderer.java,v 1.3 2009/02/25 16:10:08 abrighto Exp $
 *
 * who             when        what
 * --------------  ----------  ----------------------------------------
 * Allan Brighton  1999/07/23  Created
 */

package jsky.util.gui;

import java.awt.*;
import javax.swing.table.*;
import javax.swing.*;

import jsky.coords.HMS;
import jsky.coords.DMS;


/**
 * Used to reformat RA,DEC coordinates in a JTable in sexagesimal notation
 * for display.
 */
public class SexagesimalTableCellRenderer extends DefaultTableCellRenderer {

    // Set to true if this is for an RA column, false or Dec
    private boolean _isRA;

    // if true, display h:m:s, otherwise h:m
    private boolean _showSeconds = true;


    /**
     * Constructor.
     *
     * @param isRA true if this is for an RA column, false for Dec
     */
    public SexagesimalTableCellRenderer(boolean isRA) {
        _isRA = isRA;
        setHorizontalAlignment(JLabel.CENTER);
    }

    /**
     * Constructor.
     *
     * @param isRA true if this is for an RA column, false for Dec
     * @param showSeconds if true, display h:m:s, otherwise h:m
     */
    public SexagesimalTableCellRenderer(boolean isRA, boolean showSeconds) {
        this(isRA);
        _showSeconds = showSeconds;
        setHorizontalAlignment(JLabel.CENTER);
    }


    /**
     * This method is sent to the renderer by the drawing table to
     * configure the renderer appropriately before drawing.  Return
     * the Component used for drawing.
     *
     * @param    table        the JTable that is asking the renderer to draw.
     * This parameter can be null.
     * @param    value        the value of the cell to be rendered.  It is
     * up to the specific renderer to interpret
     * and draw the value.  eg. if value is the
     * String "true", it could be rendered as a
     * string or it could be rendered as a check
     * box that is checked.  null is a valid value.
     * @param    isSelected    true is the cell is to be renderer with
     * selection highlighting
     * @param    hasFocus    true if the cell has the focus
     * @param    row     the row index of the cell being drawn.  When
     * drawing the header the rowIndex is -1.
     * @param    column     the column index of the cell being drawn
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus,
                                                   int row, int column) {
        Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        double val = Double.NaN;
        if (value != null) {
            if (value instanceof Float) {
                val = ((Float) value).doubleValue();
            } else if (value instanceof Double) {
                val = (Double) value;
            }
        }
        if (!Double.isNaN(val)) {
            if (_isRA) {
                HMS hms = new HMS(val/15.);
                ((JLabel) component).setText(hms.toString(_showSeconds));
            } else {
                DMS dms = new DMS(val);
                ((JLabel) component).setText(dms.toString(_showSeconds));
            }
        }

        return component;
    }
}