File: WeatherTableModel.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (124 lines) | stat: -rw-r--r-- 3,328 bytes parent folder | download | duplicates (6)
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
package tim.prune.function.weather;

import javax.swing.table.AbstractTableModel;

import tim.prune.I18nManager;


/**
 * Table model for results of weather forecast
 */
public class WeatherTableModel extends AbstractTableModel
{
	/** Weather results */
	private WeatherResults _results;

	/** Row indices */
	public static final int ROW_DAY  = 0;
	public static final int ROW_DESC = 1;
	public static final int ROW_WIND = 2;
	public static final int ROW_ICON = 3;
	public static final int ROW_TEMP = 4;
	public static final int ROW_HUMID = 5;

	/** String for degrees Celsius */
	private static final String UNITS_DEGC = I18nManager.getText("units.degreescelsius.short");
	/** String for degrees Fahrenheit */
	private static final String UNITS_DEGF = I18nManager.getText("units.degreesfahrenheit.short");

	/**
	 * @return column count
	 */
	public int getColumnCount()
	{
		if (_results == null) {return 0;}
		return _results.getNumForecasts();
	}

	/**
	 * @param inColNum column number
	 * @return column label for given column
	 */
	public String getColumnName(int inColNum)
	{
		if (_results != null && inColNum >= 0 && inColNum < getColumnCount())
		{
			SingleForecast forecast = _results.getForecast(inColNum);
			if (!forecast.hasTimes() || forecast.getTimeFrom().startsWith("00")) {
				return forecast.getDate();
			}
			return forecast.getTimeFrom();
		}
		return "";
	}

	/**
	 * @return number of rows
	 */
	public int getRowCount()
	{
		return 6;
	}

	/** @return true if there are no columns */
	public boolean isEmpty()
	{
		return getColumnCount() == 0;
	}

	/**
	 * @param inRowNum row number
	 * @param inColNum column number
	 * @return cell entry at given row and column
	 */
	public Object getValueAt(int inRowNum, int inColNum)
	{
		if (inColNum < 0 || inColNum >= getColumnCount()) {return "";}
		SingleForecast forecast = _results.getForecast(inColNum);
		if (forecast != null)
		{
			switch (inRowNum)
			{
				case ROW_DAY:  {
					final String dayDesc = forecast.getDayDesc() == null ? "now" : forecast.getDayDesc();
					return buildDisplayString(null, I18nManager.getText("dialog.weather.day." + dayDesc));
				}
				case ROW_DESC: return buildDisplayString(null, forecast.getDescription());
				case ROW_WIND: return buildDisplayString(I18nManager.getText("dialog.weather.wind"), forecast.getWindDescription());
				case ROW_ICON: return forecast.getImageName();
				case ROW_TEMP: return buildDisplayString(I18nManager.getText("dialog.weather.temp"), forecast.getTemps()
					+ (_results.isCelsius() ? UNITS_DEGC : UNITS_DEGF));
				case ROW_HUMID: return buildDisplayString(I18nManager.getText("dialog.weather.humidity"), forecast.getHumidity());
			}
		}
		return "";
	}

	/**
	 * Build a html string from the given title and value
	 */
	private static final String buildDisplayString(String inTitle, String inValue)
	{
		if (inValue == null) {return null;}
		return "<html>" + (inTitle == null ? "" : (inTitle + ":&nbsp;"))
			+ "<big>" + inValue.replaceAll(" ", "&nbsp;") + "</big></html>";
	}

	/**
	 * Set the results
	 * @param inResults weather results including all forecasts
	 */
	public void setResults(WeatherResults inResults)
	{
		_results = inResults;
		fireTableStructureChanged();
	}

	/**
	 * Clear the list of forecasts
	 */
	public void clear()
	{
		setResults(null);
	}
}