File: SegmentTableModel.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (137 lines) | stat: -rw-r--r-- 3,848 bytes parent folder | download | duplicates (4)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package tim.prune.function.comparesegments;

import java.util.ArrayList;
import java.util.TimeZone;

import javax.swing.table.AbstractTableModel;

import tim.prune.I18nManager;
import tim.prune.data.Distance;
import tim.prune.data.Timestamp;
import tim.prune.data.Unit;


/**
 * Table model for the segments in the comparison function
 */
public class SegmentTableModel extends AbstractTableModel
{
	/** list of segments */
	private ArrayList<SegmentSummary> _segmentList = null;
	/** Units to use for distances */
	private Unit _distUnits = null;
	/** objects shown in table */
	private ArrayList<TableValues> _tableValuesList = null;

	private static final int NUM_COLUMNS = 4;

	/** Class to represent a single row of the table */
	private static class TableValues
	{
		private final Object[] _values = new Object[NUM_COLUMNS];
		private TableValues(SegmentSummary inSegment, TimeZone inTimezone, Unit inDistUnits)
		{
			_values[0] = new DateForTable(inSegment.getStartTimestamp(), inTimezone);
			_values[1] = inSegment.getStartTimestamp().getTimeText(inTimezone);
			_values[2] = Distance.convertRadiansToDistance(inSegment.getDistanceInRadians(), inDistUnits);
			_values[3] = new Duration(inSegment.getDurationInSeconds());
		}
		private Object getValue(int inColumnIndex) {
			return _values[inColumnIndex];
		}
	}



	/**
	 * Initialize the table model with the segment list
	 * @param inSegments list of segments
	 * @param inTimezone timezone to use for dates and times
	 * @param inDistanceUnits units to use for distances
	 */
	public void init(ArrayList<SegmentSummary> inSegments, TimeZone inTimezone, Unit inDistanceUnits)
	{
		_segmentList = inSegments;
		_distUnits = inDistanceUnits;
		_tableValuesList = new ArrayList<>();
		for (SegmentSummary segment : inSegments) {
			_tableValuesList.add(new TableValues(segment, inTimezone, inDistanceUnits));
		}
		fireTableStructureChanged();
	}

	/**
	 * @return row count
	 */
	public int getRowCount() {
		return _segmentList == null ? 0 : _segmentList.size();
	}

	public int getColumnCount() {
		return NUM_COLUMNS;
	}

	/**
	 * Get the column class
	 * @param inColumnIndex column index
	 * @return Class of specified column
	 */
	public Class<?> getColumnClass(int inColumnIndex)
	{
		if (inColumnIndex == 0) {
			return DateForTable.class;
		}
		if (inColumnIndex == 2) {
			return Double.class;
		}
		if (inColumnIndex == 3) {
			return Duration.class;
		}
		return String.class;
	}

	public Object getValueAt(int inRowIndex, int inColIndex)
	{
		try {
			TableValues values = _tableValuesList.get(inRowIndex);
			return values.getValue(inColIndex);
		}
		catch (IndexOutOfBoundsException ignored) {}
		throw new IllegalArgumentException("Unknown column: " + inColIndex);
	}

	public String getColumnName(int inColIndex)
	{
		if (inColIndex == 0) {
			return I18nManager.getText("dialog.comparesegments.startdate");
		}
		if (inColIndex == 1) {
			return I18nManager.getText("dialog.comparesegments.starttime");
		}
		if (inColIndex == 2)
		{
			if (_distUnits == null) {
				return I18nManager.getText("fieldname.distance");
			}
			return I18nManager.getText("fieldname.distance") + " ("
				+ I18nManager.getText(_distUnits.getShortnameKey()) + ")";
		}
		if (inColIndex == 3) {
			return I18nManager.getText("fieldname.duration");
		}
		throw new IllegalArgumentException("Unknown column: " + inColIndex);
	}

	/** @return the start index of the requested segment */
	int getSegmentStartIndex(int inIndex) {
		return _segmentList.get(inIndex).getStartIndex();
	}

	/** @return true if the first segment is earlier */
	boolean areSegmentsInTimeOrder(int inIndex1, int inIndex2)
	{
		Timestamp timestamp1 = _segmentList.get(inIndex1).getStartTimestamp();
		Timestamp timestamp2 = _segmentList.get(inIndex2).getStartTimestamp();
		return timestamp1.isBefore(timestamp2);
	}
}