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