Changeset 36


Ignore:
Timestamp:
07/28/07 19:48:14 (5 years ago)
Author:
saua
Message:

This should fix Issue #13

Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • bbtracker/trunk/src/org/bbtracker/mobile/Preferences.java

    r17 r36  
    1313import javax.microedition.rms.RecordStoreNotFoundException; 
    1414 
     15import org.bbtracker.ImperialUnitConverter; 
     16import org.bbtracker.MetricUnitConverter; 
     17import org.bbtracker.UnitConverter; 
     18 
    1519public class Preferences { 
    1620        private static final String RECORD_STORE_NAME = "Preferences"; 
     
    2428        public static String[] START_ACTIONS = new String[] { "Do nothing", "Initialize GPS", "Start new track" }; 
    2529 
     30        public static final int EXPORT_KML = 0; 
     31 
     32        public static final int EXPORT_GPx = 1; 
     33 
    2634        public static String[] EXPORT_FORMATS = new String[] { "KML (Google Earth)", "GPX" }; 
     35 
     36        public static final int UNITS_METRIC = 0; 
     37 
     38        public static final int UNITS_IMPERIAL = 1; 
     39 
     40        public static String[] UNITS = new String[] { "Metric (km/h, km, m)", "Imperial (mph, miles, feet)" }; 
    2741 
    2842        private static Preferences instance; 
     
    5064        private int exportFormats = 0x03; // export format 0 and 1 are set 
    5165 
     66        private int units = UNITS_METRIC; 
     67 
    5268        private String exportDirectory; 
     69 
     70        private UnitConverter unitConverter; 
    5371 
    5472        private Preferences() { 
     
    6987        public void setStartAction(final int startAction) { 
    7088                this.startAction = startAction; 
     89        } 
     90 
     91        public String getExportDirectory() { 
     92                return exportDirectory; 
     93        } 
     94 
     95        public void setExportDirectory(final String exportDirectory) { 
     96                this.exportDirectory = exportDirectory; 
     97        } 
     98 
     99        public void setExportFormat(final int index, final boolean value) { 
     100                if (index >= EXPORT_FORMATS.length || index < 0) { 
     101                        throw new IllegalArgumentException(); 
     102                } 
     103                if (value) { 
     104                        exportFormats |= 1 << index; 
     105                } else { 
     106                        exportFormats &= ~(1 << index); 
     107                } 
     108        } 
     109 
     110        public boolean getExportFormat(final int index) { 
     111                return (exportFormats & (1 << index)) != 0; 
     112        } 
     113 
     114        public int getUnits() { 
     115                return units; 
     116        } 
     117 
     118        public void setUnits(final int units) { 
     119                if (units != UNITS_METRIC && units != UNITS_IMPERIAL) { 
     120                        throw new IllegalArgumentException(); 
     121                } 
     122                this.units = units; 
     123                unitConverter = null; 
     124        } 
     125 
     126        public UnitConverter getUnitsConverter() { 
     127                if (unitConverter == null) { 
     128                        switch (units) { 
     129                        case UNITS_METRIC: 
     130                                unitConverter = new MetricUnitConverter(); 
     131                                break; 
     132                        case UNITS_IMPERIAL: 
     133                                unitConverter = new ImperialUnitConverter(); 
     134                                break; 
     135                        default: 
     136                                throw new IllegalStateException(); 
     137                        } 
     138                } 
     139                return unitConverter; 
    71140        } 
    72141 
     
    103172                        } 
    104173                        exportFormats = in.readInt(); 
     174                        units = in.readInt(); 
    105175 
    106176                        in.close(); 
     
    140210                        } 
    141211                        out.writeInt(exportFormats); 
     212                        out.writeInt(units); 
    142213 
    143214                        out.close(); 
     
    168239                } 
    169240        } 
    170  
    171         public String getExportDirectory() { 
    172                 return exportDirectory; 
    173         } 
    174  
    175         public void setExportDirectory(final String exportDirectory) { 
    176                 this.exportDirectory = exportDirectory; 
    177         } 
    178  
    179         public void setExportFormat(final int index, final boolean value) { 
    180                 if (index >= EXPORT_FORMATS.length || index < 0) { 
    181                         throw new IllegalArgumentException(); 
    182                 } 
    183                 if (value) { 
    184                         exportFormats |= 1 << index; 
    185                 } else { 
    186                         exportFormats &= ~(1 << index); 
    187                 } 
    188         } 
    189  
    190         public boolean getExportFormat(final int index) { 
    191                 return (exportFormats & (1 << index)) != 0; 
    192         } 
    193241} 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/OptionsForm.java

    r24 r36  
    3434        private final ChoiceGroup exportFormatGroup; 
    3535 
     36        private final ChoiceGroup unitsGroup; 
     37 
    3638        public OptionsForm(final TrackManager trackManager) { 
    3739                super("Options"); 
     
    4345                sampleField = new TextField("Sample Interval in seconds: ", String.valueOf(pref.getSampleInterval()), 5, 
    4446                                TextField.NUMERIC); 
    45                 startTypeGroup = new ChoiceGroup("Startup action: ", Choice.EXCLUSIVE, Preferences.START_ACTIONS, null); 
     47 
     48                unitsGroup = new ChoiceGroup("Units: ", Choice.POPUP, Preferences.UNITS, null); 
     49                unitsGroup.setSelectedIndex(pref.getUnits(), true); 
     50 
     51                startTypeGroup = new ChoiceGroup("Startup action: ", Choice.POPUP, Preferences.START_ACTIONS, null); 
    4652                startTypeGroup.setSelectedIndex(pref.getStartAction(), true); 
     53 
    4754                directoryField = new TextField("Export directory: ", pref.getExportDirectory(), 100, TextField.URL); 
    4855                browseCommand = new Command("Browse", Command.ITEM, 1); 
     
    5663 
    5764                append(sampleField); 
     65                append(unitsGroup); 
    5866                append(startTypeGroup); 
    5967                append(directoryField); 
     
    8088                                        pref.setExportFormat(i, exportFormatGroup.isSelected(i)); 
    8189                                } 
     90 
     91                                pref.setUnits(unitsGroup.getSelectedIndex()); 
    8292 
    8393                                pref.store(); 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/StatusTile.java

    r31 r36  
    66import org.bbtracker.Track; 
    77import org.bbtracker.TrackPoint; 
     8import org.bbtracker.UnitConverter; 
    89import org.bbtracker.Utils; 
     10import org.bbtracker.mobile.Preferences; 
    911import org.bbtracker.mobile.TrackManager; 
    1012 
     
    5052                        point = (pi + 1) + "/" + track.getPointCount(); 
    5153                } 
    52                 final String lon; 
    53                 final String lat; 
    54                 final String speed; 
    55                 final String course; 
    56                 final String elevation; 
     54                double lonValue = Double.NaN; 
     55                double latValue = Double.NaN; 
     56                float speedValue = Float.NaN; 
     57                float courseValue = Float.NaN; 
     58                float elevationValue = Float.NaN; 
     59                double lengthValue = Double.NaN; 
    5760                if (p != null) { 
    58                         lon = Utils.longitudeToString(p.getLongitude()); 
    59                         lat = Utils.latitudeToString(p.getLatitude()); 
    60                         speed = Utils.speedToString(p.getSpeed()); 
    61                         course = Utils.courseToString(p.getCourse()); 
    62                         elevation = Utils.elevationToString(p.getElevation()); 
    63                 } else { 
    64                         lon = "-"; 
    65                         lat = "-"; 
    66                         speed = "- km/h"; 
    67                         course = "-" + Utils.DEGREE; 
    68                         elevation = "-m"; 
     61                        lonValue = p.getLongitude(); 
     62                        latValue = p.getLatitude(); 
     63                        speedValue = p.getSpeed(); 
     64                        courseValue = p.getCourse(); 
     65                        elevationValue = p.getElevation(); 
    6966                } 
    70                 final String length; 
    7167                if (track != null) { 
    72                         length = Utils.distanceToString(track.getLength()); 
    73                 } else { 
    74                         length = "-m"; 
     68                        lengthValue = track.getLength(); 
    7569                } 
     70 
     71                final String lon = Utils.longitudeToString(lonValue); 
     72                final String lat = Utils.latitudeToString(latValue); 
     73                final String course = Utils.courseToString(courseValue); 
     74 
     75                final UnitConverter unit = Preferences.getInstance().getUnitsConverter(); 
     76                final String speed = unit.speedToString(speedValue); 
     77                final String elevation = unit.elevationToString(elevationValue); 
     78                final String length = unit.distanceToString(lengthValue); 
    7679 
    7780                int y = MARGIN; 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/TrackTile.java

    r19 r36  
    1010import org.bbtracker.TrackSegment; 
    1111import org.bbtracker.Utils; 
     12import org.bbtracker.UnitConverter.ScaleConfiguration; 
     13import org.bbtracker.mobile.Preferences; 
    1214 
    1315public class TrackTile extends Tile { 
     
    3032        private static final int SCALE_HEIGTH = 5; 
    3133 
     34        private final Track track; 
     35 
    3236        private double minimumLongitude, minimumLatitude; 
    3337 
    3438        private double scale; 
    3539 
    36         private final Track track; 
    37  
    3840        private TrackPoint currentPoint; 
    3941 
    4042        private int scaleSizeInPixel; 
    4143 
    42         private int scaleSize; 
     44        private String scaleLabelCenter; 
     45 
     46        private String scaleLabelRight; 
    4347 
    4448        public TrackTile(final Track track) { 
     
    8791                                .distance(minimumLatitude, minimumLongitude, minimumLatitude, maximumLongitude); 
    8892                if (widthInMeter < 1) { 
    89                         scaleSize = 0; 
    90                         return; 
    91                 } 
    92  
    93                 final int metersAvailableForScale = (int) (widthInMeter * 0.9); 
    94  
    95                 scaleSize = 1; 
    96                 while (scaleSize < metersAvailableForScale / 10) { 
    97                         scaleSize = scaleSize * 10; 
    98                 } 
    99                 if (scaleSize * 5 < metersAvailableForScale) { 
    100                         scaleSize = scaleSize * 5; 
    101                 } else if (scaleSize * 2 < metersAvailableForScale) { 
    102                         scaleSize = scaleSize * 2; 
    103                 } 
    104                 scaleSizeInPixel = (int) (scaleSize * (width / widthInMeter)); 
     93                        scaleSizeInPixel = 0; 
     94                        return; 
     95                } 
     96 
     97                final double availableLengthInMeter = widthInMeter * 0.9; 
     98 
     99                final ScaleConfiguration conf = Preferences.getInstance().getUnitsConverter().getScaleConfiguration( 
     100                                availableLengthInMeter); 
     101 
     102                switch (conf.lengthInUnits) { 
     103                case 1: 
     104                        scaleLabelCenter = "0.5"; 
     105                        break; 
     106                case 5: 
     107                        scaleLabelCenter = "2.5"; 
     108                        break; 
     109                default: 
     110                        scaleLabelCenter = String.valueOf(conf.lengthInUnits / 2); 
     111                } 
     112                scaleLabelRight = conf.lengthInUnits + " " + conf.unit; 
     113 
     114                scaleSizeInPixel = (int) ((conf.lengthInMeter / widthInMeter) * width); 
    105115        } 
    106116 
     
    116126                        return; 
    117127                } 
    118                 final int segmentCount = track.getSegmentCount(); 
    119                 if (segmentCount < 1 || track.getSegment(0).getPointCount() == 0) { 
     128                if (track.getPointCount() == 0) { 
    120129                        return; 
    121130                } 
     
    183192 
    184193        private void drawScale(final Graphics g) { 
    185                 if (scaleSize == 0) { 
    186                         return; 
    187                 } 
    188  
    189                 String unit; 
    190                 int displayScaleSize; 
    191  
    192                 if (scaleSize >= 1000) { 
    193                         unit = "km"; 
    194                         displayScaleSize = scaleSize / 1000; 
    195                 } else { 
    196                         unit = "m"; 
    197                         displayScaleSize = scaleSize; 
    198                 } 
    199  
    200                 final String leftLabel = "0"; 
    201                 final String middleLabel; 
    202                 switch (displayScaleSize) { 
    203                 case 1: 
    204                         middleLabel = "0.5"; 
    205                         break; 
    206                 case 5: 
    207                         middleLabel = "2.5"; 
    208                         break; 
    209                 default: 
    210                         middleLabel = String.valueOf(displayScaleSize / 2); 
    211                 } 
    212                 final String rightLabel = displayScaleSize + " " + unit; 
     194                if (scaleSizeInPixel == 0) { 
     195                        return; 
     196                } 
    213197 
    214198                final Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); 
    215199 
    216                 final int left = (font.stringWidth(leftLabel) / 2) + 2; 
     200                final int left = (font.stringWidth("0") / 2) + 2; 
    217201                g.setFont(font); 
    218202                g.setColor(0x00000000); 
     
    221205 
    222206                final int textBottom = height - 4 - SCALE_HEIGTH; 
    223                 g.drawString(leftLabel, left, textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
    224                 g.drawString(middleLabel, left + (scaleSizeInPixel / 2), textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
    225                 g.drawString(rightLabel, left + scaleSizeInPixel, textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
     207                g.drawString("0", left, textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
     208                g.drawString(scaleLabelCenter, left + (scaleSizeInPixel / 2), textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
     209                g.drawString(scaleLabelRight, left + scaleSizeInPixel, textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
    226210        } 
    227211} 
  • bbtracker_common/trunk/src/org/bbtracker/Utils.java

    r30 r36  
    1616        public static final char SECOND = '\u2033'; 
    1717 
    18         private static final float MS_TO_KMH_FACTOR = 3.6f; 
    19  
    2018        private static final double WGS84_A = 6378137; 
    2119 
     
    3735 
    3836        public static String degreesToString(final double value, final char positiveChar, final char negativeChar) { 
     37                if (Double.isNaN(value)) { 
     38                        return "-"; 
     39                } 
    3940                char c; 
    4041                double d; 
     
    131132        } 
    132133 
    133         /** 
    134          * @param speed 
    135          *            the speed in m/s 
    136          * @return a human readable String containing the speed in km/h. 
    137          */ 
    138         public static String speedToString(final float speed) { 
    139                 final float value = speed * MS_TO_KMH_FACTOR; 
    140                 return String.valueOf(((int) (value * 10)) / 10f) + " km/h"; 
    141         } 
    142  
    143         public static String courseToString(final float course) { 
    144                 if (Float.isNaN(course)) { 
    145                         return "???" + DEGREE; 
    146                 } else { 
    147                         return String.valueOf((int) (Math.floor(course + 0.5d))) + DEGREE; 
    148                 } 
    149         } 
    150  
    151134        public static String dateToString(final Date date) { 
    152135                final String orig = date.toString(); 
     
    203186        } 
    204187 
    205         public static String elevationToString(final float elevation) { 
    206                 return ((int) elevation) + "m"; 
    207         } 
    208  
    209         public static String distanceToString(final double length) { 
    210                 if (length < 1000) { 
    211                         return ((int) length) + "m"; 
     188        /* 
     189         * (non-Javadoc) 
     190         *  
     191         * @see org.bbtracker.UnitConverter#courseToString(float) 
     192         */ 
     193        public static String courseToString(final float course) { 
     194                if (Float.isNaN(course)) { 
     195                        return "???" + DEGREE; 
    212196                } else { 
    213                         return String.valueOf(((int) (length / 100)) / 10f) + "km"; 
     197                        return String.valueOf((int) (Math.floor(course + 0.5d))) + DEGREE; 
    214198                } 
    215199        } 
Note: See TracChangeset for help on using the changeset viewer.