SexagesimalTableCellEditor.java
3.28 KB
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
/*
* $Id: SexagesimalTableCellEditor.java,v 1.3 2009/04/21 13:31:17 abrighto Exp $
*/
package jsky.util.gui;
import java.awt.*;
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 SexagesimalTableCellEditor extends DefaultCellEditor {
private boolean hoursFlag;
/**
* Constructor.
*
* @param hoursFlag if true, divide the cell value by 15 and display hours : min : sec,
* otherwise display deg : min : sec.
*/
public SexagesimalTableCellEditor(boolean hoursFlag) {
super(new JTextField());
this.hoursFlag = hoursFlag;
}
/**
* This method is sent to the editor by the drawing table to
* configure the editor appropriately before drawing. Return
* the Component used for drawing.
*
* @param table the JTable that is asking the editor to draw.
* This parameter can be null.
* @param value the value of the cell to be rendered. It is
* up to the specific editor 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 editor with
* selection highlighting
* @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 getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row, int column) {
Component component = super.getTableCellEditorComponent(table, value, isSelected, 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 (hoursFlag) {
((JTextField) component).setText(new HMS(val / 15.).toString());
} else {
((JTextField) component).setText(new DMS(val).toString());
}
}
return component;
}
/**
* Returns the value contained in the editor
*/
public Object getCellEditorValue() {
Object o = super.getCellEditorValue();
if (o instanceof String) {
if (hoursFlag) {
try {
HMS hms = new HMS((String) o);
return hms.getVal() * 15.;
} catch (Exception e) {
DialogUtil.error("Invalid value: '" + o + "', expected decimal degrees or h:m:s");
}
} else {
try {
DMS dms = new DMS((String) o);
return dms.getVal();
} catch (Exception e) {
DialogUtil.error("Invalid value: '" + o + "', expected decimal degrees or d:m:s");
}
}
}
return o;
}
}