]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Field.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / data / Field.java
1 package tim.prune.data;
2
3 import tim.prune.I18nManager;
4
5 /**
6  * Class to represent a field of a data point
7  */
8 public class Field
9 {
10         private String _labelKey = null;
11         private String _customLabel = null;
12         private boolean _builtin = false;
13
14         public static final Field LATITUDE = new Field("fieldname.latitude", true);
15         public static final Field LONGITUDE = new Field("fieldname.longitude", true);
16         public static final Field ALTITUDE = new Field("fieldname.altitude", true);
17         public static final Field TIMESTAMP = new Field("fieldname.timestamp", true);
18         public static final Field WAYPT_NAME = new Field("fieldname.waypointname", true);
19         public static final Field WAYPT_TYPE = new Field("fieldname.waypointtype", true);
20         public static final Field DESCRIPTION = new Field("fieldname.description", true);
21         public static final Field NEW_SEGMENT = new Field("fieldname.newsegment", true);
22
23         public static final Field SPEED          = new Field("fieldname.speed", true);
24         public static final Field VERTICAL_SPEED = new Field("fieldname.verticalspeed", true);
25
26         // TODO: Field for photo filename, ability to load (from text) and save (to text)
27
28         /** List of all the available fields */
29         private static final Field[] ALL_AVAILABLE_FIELDS = {
30                 LATITUDE, LONGITUDE, ALTITUDE, TIMESTAMP, WAYPT_NAME, WAYPT_TYPE, DESCRIPTION, NEW_SEGMENT,
31                 SPEED, VERTICAL_SPEED,
32                 new Field(I18nManager.getText("fieldname.custom"))
33         };
34
35         /**
36          * Private constructor
37          * @param inLabelKey Key for label texts
38          * @param inBuiltin true for built-in types, false for custom
39          */
40         private Field(String inLabelKey, boolean inBuiltin)
41         {
42                 if (inBuiltin) {
43                         _labelKey = inLabelKey;
44                         _customLabel = null;
45                 }
46                 else {
47                         _labelKey = null;
48                         _customLabel = inLabelKey;
49                 }
50                 _builtin = inBuiltin;
51         }
52
53
54         /**
55          * Public constructor for custom fields
56          * @param inLabel label to use for display
57          */
58         public Field(String inLabel)
59         {
60                 this(inLabel, false);
61         }
62
63         /**
64          * @return the name of the field
65          */
66         public String getName()
67         {
68                 if (_labelKey != null)
69                         return I18nManager.getText(_labelKey);
70                 return _customLabel;
71         }
72
73         /**
74          * Change the name of the (non built-in) field
75          * @param inName new name
76          */
77         public void setName(String inName)
78         {
79                 if (!isBuiltIn()) _customLabel = inName;
80         }
81
82         /**
83          * @return true if this is a built-in field
84          */
85         public boolean isBuiltIn()
86         {
87                 return _builtin;
88         }
89
90         /**
91          * Checks if the two fields are equal
92          * @param inOther other Field object
93          * @return true if Fields identical
94          */
95         public boolean equals(Field inOther)
96         {
97                 return (isBuiltIn() == inOther.isBuiltIn() && getName().equals(inOther.getName()));
98         }
99
100         /**
101          * Get the field for the given field name
102          * @param inFieldName name of field to look for
103          * @return Field if found, or null otherwise
104          */
105         public static Field getField(String inFieldName)
106         {
107                 for (int i=0; i<ALL_AVAILABLE_FIELDS.length; i++)
108                 {
109                         Field field = ALL_AVAILABLE_FIELDS[i];
110                         if (field.getName().equals(inFieldName)) {
111                                 return field;
112                         }
113                 }
114                 // not found
115                 return null;
116         }
117
118         /**
119          * @return array of field names
120          */
121         public static String[] getFieldNames()
122         {
123                 String[] names = new String[ALL_AVAILABLE_FIELDS.length];
124                 for (int i=0; i<ALL_AVAILABLE_FIELDS.length; i++) {
125                         names[i] = ALL_AVAILABLE_FIELDS[i].getName();
126                 }
127                 return names;
128         }
129 }