]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/CoordDisplay.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / gui / CoordDisplay.java
1 package tim.prune.gui;
2
3 import tim.prune.data.Coordinate;
4
5 /**
6  * Functions for display of coordinates in gui
7  */
8 public abstract class CoordDisplay
9 {
10
11         /**
12          * Construct an appropriate coordinate label using the selected format
13          * @param inCoordinate coordinate
14          * @param inFormat selected display format
15          * @return language-sensitive string
16          */
17         public static String makeCoordinateLabel(Coordinate inCoordinate, int inFormat)
18         {
19                 String coord = inCoordinate.output(inFormat);
20                 // Fix broken degree signs (due to unicode mangling)
21                 final char brokenDeg = 65533;
22                 if (coord.indexOf(brokenDeg) >= 0)
23                 {
24                         coord = coord.replaceAll(String.valueOf(brokenDeg), "\u00B0");
25                 }
26                 return restrictDP(coord);
27         }
28
29
30         /**
31          * Restrict the given coordinate to a limited number of decimal places for display
32          * @param inCoord coordinate string
33          * @return chopped string
34          */
35         private static String restrictDP(String inCoord)
36         {
37                 final int DECIMAL_PLACES = 7;
38                 if (inCoord == null) return "";
39                 String result = inCoord;
40                 final int dotPos = Math.max(inCoord.lastIndexOf('.'), inCoord.lastIndexOf(','));
41                 if (dotPos >= 0)
42                 {
43                         final int chopPos = dotPos + DECIMAL_PLACES;
44                         if (chopPos < (inCoord.length()-1))
45                         {
46                                 result = inCoord.substring(0, chopPos);
47                                 // Maybe there's an exponential in there too which needs to be appended
48                                 int expPos = inCoord.toUpperCase().indexOf("E", chopPos);
49                                 if (expPos > 0 && expPos < (inCoord.length()-1))
50                                 {
51                                         result += inCoord.substring(expPos);
52                                 }
53                         }
54                 }
55                 return result;
56         }
57
58 }