Changeset 7


Ignore:
Timestamp:
07/25/07 00:07:45 (6 years ago)
Author:
saua
Message:
  • Add KML export
  • some major cleanup & nicer error messages (that can actually be read)
Files:
4 added
13 edited

Legend:

Unmodified
Added
Removed
  • bbtracker/trunk/bbtracker.jad

    r4 r7  
    1 MIDlet-1: BBTracker,bbtracker.png,org.dyndns.rentar.bbtracker.mobile.BBTracker 
     1MIDlet-1: BBTracker,bbtracker.png,org.bbtracker.mobile.BBTracker 
    22MIDlet-Jar-URL: bbtracker.jar 
    33MIDlet-Name: BBTracker 
    4 MIDlet-Permissions: javax.microedition.location.Location, javax.microedition.location.Orientation 
     4MIDlet-Permissions: javax.microedition.location.Location, javax.microedition.location.Orientation, javax.microedition.io.Connector.file.read, javax.microedition.io.Connector.file.write 
    55MIDlet-Vendor: Joachim Sauer 
    6 MIDlet-Version: 0.1.5 
     6MIDlet-Version: 0.1.9 
    77MicroEdition-Configuration: CLDC-1.1 
    88MicroEdition-Profile: MIDP-2.0 
  • bbtracker/trunk/src/org/bbtracker/mobile/BBTracker.java

    r4 r7  
    33import javax.microedition.lcdui.Alert; 
    44import javax.microedition.lcdui.AlertType; 
     5import javax.microedition.lcdui.Command; 
     6import javax.microedition.lcdui.CommandListener; 
    57import javax.microedition.lcdui.Display; 
    68import javax.microedition.lcdui.Displayable; 
     9import javax.microedition.lcdui.Form; 
    710import javax.microedition.location.LocationException; 
    811import javax.microedition.midlet.MIDlet; 
    912import javax.microedition.midlet.MIDletStateChangeException; 
     13import javax.microedition.rms.RecordStoreException; 
    1014 
    1115import org.bbtracker.mobile.gui.MainCanvas; 
     
    2327        private final TrackManager trackManager; 
    2428 
    25         private MainCanvas mainCanvas; 
     29        private final MainCanvas mainCanvas; 
    2630 
    2731        private boolean firstStart = true; 
     
    3539                trackManager = new TrackManager(); 
    3640 
     41                mainCanvas = new MainCanvas(trackManager); 
     42 
    3743                try { 
    3844                        switch (Preferences.getInstance().getStartAction()) { 
     
    4551                        } 
    4652                } catch (final LocationException e) { 
    47                         nonFatal(e, "Initializing Location Provider"); 
     53                        nonFatal(e, "Initializing Location Provider", mainCanvas); 
    4854                } 
    4955        } 
    5056 
    51         public void shutdown() { 
     57        public void shutdown(final boolean destroy) { 
    5258                if (trackManager != null) { 
    5359                        trackManager.shutdown(); 
    5460                } 
    55                 Preferences.getInstance().store(); 
    56                 notifyDestroyed(); 
     61                try { 
     62                        Preferences.getInstance().store(); 
     63                } catch (final RecordStoreException ignored) { 
     64                        // ignore 
     65                } 
     66                if (destroy) { 
     67                        notifyDestroyed(); 
     68                } 
    5769        } 
    5870 
    5971        protected void destroyApp(final boolean arg0) throws MIDletStateChangeException { 
    60                 shutdown(); 
     72                shutdown(true); 
    6173        } 
    6274 
    6375        protected void pauseApp() { 
    64                 mainCanvas = null; 
    6576        } 
    6677 
     
    7788                } 
    7889 
    79         } 
    80  
    81         public static void alert(final Alert alert) { 
    82                 final Display d = getDisplay(); 
    83                 Displayable next = d.getCurrent(); 
    84                 if (next == null) { 
    85                         final BBTracker i = getInstance(); 
    86                         if (i.mainCanvas == null) { 
    87                                 i.mainCanvas = new MainCanvas(i.trackManager); 
    88                         } 
    89                         next = i.mainCanvas; 
    90                 } 
    91                 d.setCurrent(alert, next); 
    9290        } 
    9391 
     
    112110        } 
    113111 
    114         public static void nonFatal(final Throwable t, final String action) { 
    115                 t.printStackTrace(); 
    116                 alert(new Alert("Non-fatal Exception", "Non-fatal Exception while " + action + ": " + t.getMessage(), null, 
    117                                 AlertType.WARNING)); 
     112        public static void nonFatal(final Throwable t, final String action, final Displayable next) { 
     113                log(t); 
     114                final Alert alert = new Alert("Non-fatal Exception", "Non-fatal Exception while " + action + ": " + 
     115                                t.getMessage(), null, AlertType.WARNING); 
     116                alert(alert, next); 
     117        } 
     118 
     119        public static void alert(final Alert alert, final Displayable next) { 
     120                getDisplay().setCurrent(alert, next != null ? next : getInstance().mainCanvas); 
    118121        } 
    119122 
    120123        public static void fatal(final Throwable t, final String action) { 
    121                 // TODO 
    122                 getInstance().shutdown(); 
     124                log(t); 
     125                final BBTracker i = getInstance(); 
     126                i.shutdown(false); 
     127                final Form errorForm = new Form("Fatal Exception!"); 
     128                errorForm.append("Fatal Exception while " + action + ":"); 
     129                errorForm.append(t.toString()); 
     130                errorForm.addCommand(new Command("Exit", Command.EXIT, 0)); 
     131                errorForm.setCommandListener(new CommandListener() { 
     132 
     133                        public void commandAction(final Command cmd, final Displayable displayable) { 
     134                                i.notifyDestroyed(); 
     135                        } 
     136                }); 
     137                getDisplay().setCurrent(errorForm); 
    123138        } 
    124139 
    125140        public void showMainCanvas() { 
    126                 if (mainCanvas == null) { 
    127                         mainCanvas = new MainCanvas(trackManager); 
    128                 } 
    129141                getDisplay().setCurrent(mainCanvas); 
    130142        } 
     143 
     144        public static void log(final Throwable e) { 
     145                // used only for debugging 
     146                e.printStackTrace(); 
     147        } 
     148 
     149        public static void log(final String m) { 
     150                // used only for debugging 
     151                System.err.println(m); 
     152        } 
    131153} 
  • bbtracker/trunk/src/org/bbtracker/mobile/Preferences.java

    r4 r7  
    2929                if (instance == null) { 
    3030                        instance = new Preferences(); 
    31                         instance.load(); 
     31                        try { 
     32                                instance.load(); 
     33                        } catch (final RecordStoreException ignored) { 
     34                                // ignore 
     35                        } 
    3236                } 
    3337                return instance; 
     
    4145 
    4246        private int trackNumber = 1; 
     47 
     48        private String exportDirectory; 
    4349 
    4450        private Preferences() { 
     
    6571        } 
    6672 
    67         private void load() { 
     73        private void load() throws RecordStoreException { 
    6874                RecordStore rs = null; 
    6975                try { 
     
    8995                        sampleInterval = in.readInt(); 
    9096                        trackNumber = in.readInt(); 
     97                        if (in.readByte() != 0) { 
     98                                exportDirectory = in.readUTF(); 
     99                        } 
    91100 
    92101                        in.close(); 
     
    95104                } catch (final InvalidRecordIDException e) { 
    96105                        // ignore, don't load anything 
    97                 } catch (final RecordStoreException e) { 
    98                         BBTracker.nonFatal(e, "loading preferences"); 
    99                         e.printStackTrace(); 
    100106                } catch (final IOException e) { 
    101                         BBTracker.nonFatal(e, "loading preferences"); 
    102                         e.printStackTrace(); 
     107                        throw new RecordStoreException(e.getMessage()); 
    103108                } finally { 
    104109                        if (rs != null) { 
     
    112117        } 
    113118 
    114         public void store() { 
     119        public void store() throws RecordStoreException { 
    115120                RecordStore rs = null; 
    116121                try { 
     
    123128                        out.writeInt(sampleInterval); 
    124129                        out.writeInt(trackNumber); 
     130                        if (exportDirectory == null) { 
     131                                out.writeByte(0); 
     132                        } else { 
     133                                out.writeByte(1); 
     134                                out.writeUTF(exportDirectory); 
     135                        } 
    125136 
    126137                        out.close(); 
     
    139150                                enumerateRecords.destroy(); 
    140151                        } 
    141                 } catch (final RecordStoreException e) { 
    142                         BBTracker.nonFatal(e, "storing preferences"); 
    143                         e.printStackTrace(); 
    144152                } catch (final IOException e) { 
    145                         BBTracker.nonFatal(e, "storing preferences"); 
    146                         e.printStackTrace(); 
     153                        throw new RecordStoreException(e.getMessage()); 
    147154                } finally { 
    148155                        if (rs != null) { 
     
    155162                } 
    156163        } 
     164 
     165        public String getExportDirectory() { 
     166                return exportDirectory; 
     167        } 
     168 
     169        public void setExportDirectory(final String exportDirectory) { 
     170                this.exportDirectory = exportDirectory; 
     171        } 
    157172} 
  • bbtracker/trunk/src/org/bbtracker/mobile/TrackManager.java

    r6 r7  
    1414import org.bbtracker.Track; 
    1515import org.bbtracker.TrackPoint; 
    16 import org.bbtracker.TrackSegment; 
    1716 
    1817public class TrackManager implements LocationListener { 
     
    165164                } catch (final IllegalArgumentException e) { 
    166165                        provider.setLocationListener(TrackManager.this, -1, -1, -1); 
    167                         BBTracker.nonFatal(e, "Trying to set sample interval " + sampleInterval); 
     166                        BBTracker.log(e); 
    168167                } 
    169168        } 
     
    173172                state = STATE_STATIC; 
    174173                track = newTrack; 
    175                 if (track == null) { 
     174                if (track == null || track.getPointCount() == 0) { 
    176175                        currentPoint = null; 
    177176                } else { 
    178                         if (track.getSegmentCount() > 0) { 
    179                                 final TrackSegment seg = track.getSegment(0); 
    180                                 if (seg.getPointCount() > 0) { 
    181                                         currentPoint = seg.getPoint(0); 
    182                                 } 
    183                         } 
     177                        currentPoint = track.getPoint(0); 
    184178                } 
    185179        } 
     
    219213                                TrackStore.getInstance().store(track); 
    220214                        } catch (final RecordStoreException e) { 
    221                                 BBTracker.nonFatal(e, "Storing track"); 
     215                                BBTracker.log(e); 
    222216                        } 
    223217                } 
  • bbtracker/trunk/src/org/bbtracker/mobile/TrackStore.java

    r4 r7  
    1010import javax.microedition.rms.RecordStore; 
    1111import javax.microedition.rms.RecordStoreException; 
     12import javax.microedition.rms.RecordStoreNotFoundException; 
    1213 
    1314import org.bbtracker.Track; 
     
    3637        } 
    3738 
    38         public String[] getTrackNames() { 
     39        public String[] getTrackNames() throws RecordStoreException { 
    3940                if (names == null) { 
    4041                        RecordStore rs = null; 
     
    6364                                } 
    6465                                enumeration.destroy(); 
    65                         } catch (final RecordStoreException e) { 
    66                                 e.printStackTrace(); 
     66                        } catch (final RecordStoreNotFoundException e) { 
     67                                names = new String[0]; 
     68                                indices = new int[0]; 
    6769                        } finally { 
    6870                                if (rs != null) { 
     
    7880        } 
    7981 
    80         public Track getTrack(final int index) { 
     82        public Track getTrack(final int index) throws RecordStoreException { 
    8183                if (indices == null) { 
    8284                        throw new IllegalStateException("Must not call getTrack() without calling getTrackNames() before!"); 
     
    9395                        return track; 
    9496                } catch (final IOException e) { 
    95                         BBTracker.nonFatal(e, "Loading Track"); 
    96                 } catch (final RecordStoreException e) { 
    97                         BBTracker.nonFatal(e, "Loading Track"); 
     97                        throw new RecordStoreException(e.getMessage()); 
    9898                } finally { 
    9999                        if (rs != null) { 
     
    112112                        } 
    113113                } 
    114                 return null; 
    115114        } 
    116115 
     
    150149        } 
    151150 
    152         public void deleteTrack(final int selectedIndex) { 
     151        public void deleteTrack(final int selectedIndex) throws RecordStoreException { 
    153152                RecordStore rs = null; 
    154153                try { 
     
    159158                        indices = null; 
    160159                        names = null; 
    161                 } catch (final RecordStoreException e) { 
    162                         BBTracker.nonFatal(e, "deleting track"); 
    163160                } finally { 
    164161                        if (rs != null) { 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/MainCanvas.java

    r4 r7  
    77import javax.microedition.lcdui.Font; 
    88import javax.microedition.lcdui.Graphics; 
     9import javax.microedition.rms.RecordStoreException; 
    910 
    1011import org.bbtracker.Track; 
     
    8687 
    8788        public void newPoint(final TrackPoint newPoint, final boolean boundsChanged, final boolean newSegment) { 
     89                trackTile.setCurrentPoint(newPoint); 
    8890                if (boundsChanged) { 
    8991                        trackTile.onResize(); // XXX Make that nicer 
     
    129131                        nextDisplayable = new NewTrackForm(manager); 
    130132                } else if (command == tracksCommand) { 
    131                         nextDisplayable = new TracksForm(manager); 
     133                        try { 
     134                                nextDisplayable = new TracksForm(manager); 
     135                        } catch (final RecordStoreException e) { 
     136                                BBTracker.nonFatal(e, "getting list of stored tracks", this); 
     137                        } 
    132138                } else if (command == exitCommand) { 
    133                         BBTracker.getInstance().shutdown(); 
     139                        BBTracker.getInstance().shutdown(true); 
    134140                        return; 
    135141                } 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/NewTrackForm.java

    r4 r7  
    4141                        try { 
    4242                                trackManager.newTrack(nameField.getString()); 
     43                                BBTracker.getInstance().showMainCanvas(); 
    4344                        } catch (final LocationException e) { 
    44                                 BBTracker.nonFatal(e, "Starting new track"); 
     45                                BBTracker.nonFatal(e, "Starting new track", null); 
    4546                        } 
     47                } else if (command == cancelCommand) { 
     48                        BBTracker.getInstance().showMainCanvas(); 
    4649                } 
    47                 BBTracker.getInstance().showMainCanvas(); 
    4850        } 
    4951} 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/OptionsForm.java

    r4 r7  
    55import javax.microedition.lcdui.Command; 
    66import javax.microedition.lcdui.CommandListener; 
     7import javax.microedition.lcdui.Display; 
    78import javax.microedition.lcdui.Displayable; 
    89import javax.microedition.lcdui.Form; 
     10import javax.microedition.lcdui.Item; 
     11import javax.microedition.lcdui.ItemCommandListener; 
    912import javax.microedition.lcdui.TextField; 
     13import javax.microedition.rms.RecordStoreException; 
    1014 
    1115import org.bbtracker.mobile.BBTracker; 
     
    1317import org.bbtracker.mobile.TrackManager; 
    1418 
    15 public class OptionsForm extends Form implements CommandListener { 
     19public class OptionsForm extends Form implements CommandListener, ItemCommandListener { 
    1620        private final TrackManager trackManager; 
    1721 
    18         private final Command okCommand = new Command("OK", Command.OK, 0); 
     22        private final Command okCommand; 
    1923 
    20         private final Command cancelCommand = new Command("Cancel", Command.CANCEL, 1); 
     24        private final Command cancelCommand; 
     25 
     26        private final Command browseCommand; 
    2127 
    2228        private final TextField sampleField; 
    2329 
    2430        private final ChoiceGroup startTypeGroup; 
     31 
     32        private final TextField directoryField; 
    2533 
    2634        public OptionsForm(final TrackManager trackManager) { 
     
    3543                startTypeGroup = new ChoiceGroup("Startup action: ", Choice.EXCLUSIVE, Preferences.START_ACTIONS, null); 
    3644                startTypeGroup.setSelectedIndex(pref.getStartAction(), true); 
     45                directoryField = new TextField("Export directory: ", pref.getExportDirectory(), 100, TextField.URL); 
     46                browseCommand = new Command("Browse", Command.ITEM, 1); 
     47                directoryField.addCommand(browseCommand); 
     48                directoryField.setItemCommandListener(this); 
    3749 
    3850                append(sampleField); 
    3951                append(startTypeGroup); 
     52                append(directoryField); 
    4053 
     54                okCommand = new Command("OK", Command.OK, 0); 
    4155                addCommand(okCommand); 
     56                cancelCommand = new Command("Cancel", Command.CANCEL, 1); 
    4257                addCommand(cancelCommand); 
    4358                setCommandListener(this); 
     
    5166                                pref.setSampleInterval(sampleInterval); 
    5267                                pref.setStartAction(startTypeGroup.getSelectedIndex()); 
     68                                pref.setExportDirectory(directoryField.getString()); 
    5369 
    5470                                pref.store(); 
    5571                        } catch (final NumberFormatException e) { 
    5672                                // should not happen 
     73                        } catch (final RecordStoreException e) { 
     74                                BBTracker.nonFatal(e, "storing preferences", null); 
    5775                        } 
    5876 
     
    6179                BBTracker.getInstance().showMainCanvas(); 
    6280        } 
     81 
     82        public void commandAction(final Command command, final Item item) { 
     83                if (command == browseCommand) { 
     84                        final BrowseForm browser = new BrowseForm("Save Directory", directoryField.getString()); 
     85                        final Display display = BBTracker.getDisplay(); 
     86                        browser.setCallback(new Runnable() { 
     87 
     88                                public void run() { 
     89                                        final String selectedPath = browser.getPath(); 
     90                                        if (selectedPath != null) { 
     91                                                directoryField.setString(selectedPath); 
     92                                        } 
     93                                        display.setCurrent(OptionsForm.this); 
     94                                } 
     95 
     96                        }); 
     97                        display.setCurrent(browser); 
     98                        System.out.println(browser.getPath()); 
     99                } 
     100        } 
    63101} 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/TrackTile.java

    r4 r7  
    1212 
    1313public class TrackTile extends Tile { 
    14         private static final int POINT_COLOR = 0x00bbbbbb; 
    15  
    1614        private static final int MARKED_POINT_COLOR = 0x00bb0000; 
    1715 
     
    2018        private static final int LAST_MARKED_POINT_COLOR = 0x00550000; 
    2119 
    22         private static final int LINK_COLOR = 0x00008800; 
     20        private static final int LINK_COLOR = 0x00003300; 
    2321 
    2422        private static final int SEGMENT_LINK_COLOR = 0x00aaaaaa; 
     
    3735 
    3836        private final Track track; 
     37 
     38        private TrackPoint currentPoint; 
    3939 
    4040        private int scaleSizeInPixel; 
     
    4747                minimumLongitude = minimumLatitude = Double.NaN; 
    4848                scale = Float.NaN; 
     49        } 
     50 
     51        public void setCurrentPoint(final TrackPoint currentPoint) { 
     52                this.currentPoint = currentPoint; 
     53        } 
     54 
     55        public TrackPoint getCurrentPoint() { 
     56                return currentPoint; 
    4957        } 
    5058 
     
    130138                        final TrackSegment segment = (TrackSegment) segments.nextElement(); 
    131139                        final Enumeration points = segment.getPoints(); 
     140                        TrackPoint prev = null; 
    132141                        while (points.hasMoreElements()) { 
    133142                                final TrackPoint point = (TrackPoint) points.nextElement(); 
     
    137146 
    138147                                        g.drawLine(prevX, prevY, curX, curY); 
    139                                         drawPoint(g, prevX, prevY, prevIsWaypoint, false); 
     148                                        drawPoint(g, prevX, prevY, prevIsWaypoint, prev == currentPoint); 
    140149                                        if (newSegment) { 
    141150                                                g.setColor(SEGMENT_LINK_COLOR); 
     
    147156                                prevX = curX; 
    148157                                prevY = curY; 
     158                                prev = point; 
    149159                        } 
    150                         drawPoint(g, prevX, prevY, prevIsWaypoint, true); 
    151                 } 
    152         } 
    153  
    154         private void drawPoint(final Graphics g, final int x, final int y, final boolean waypoint, final boolean last) { 
    155                 final int color = last ? (waypoint ? LAST_MARKED_POINT_COLOR : LAST_POINT_COLOR) 
    156                                 : (waypoint ? MARKED_POINT_COLOR : POINT_COLOR); 
    157                 g.setColor(color); 
    158                 g.drawLine(x - 2, y - 2, x + 2, y + 2); 
    159                 g.drawLine(x - 2, y + 2, x + 2, y - 2); 
     160                        drawPoint(g, prevX, prevY, prevIsWaypoint, prev == currentPoint); 
     161                } 
     162        } 
     163 
     164        private void drawPoint(final Graphics g, final int x, final int y, final boolean waypoint, final boolean current) { 
     165                if (!(waypoint || current)) { 
     166                        return; 
     167                } 
     168                if (current) { 
     169                        g.setColor(waypoint ? LAST_MARKED_POINT_COLOR : LAST_POINT_COLOR); 
     170                        g.drawLine(x, y - 3, x + 3, y); 
     171                        g.drawLine(x + 3, y, x, y + 3); 
     172                        g.drawLine(x, y + 3, x - 3, y); 
     173                        g.drawLine(x - 3, y, x, y - 3); 
     174 
     175                } else { 
     176                        g.setColor(MARKED_POINT_COLOR); 
     177                        g.drawLine(x - 2, y - 2, x + 2, y + 2); 
     178                        g.drawLine(x - 2, y + 2, x + 2, y - 2); 
     179                } 
    160180        } 
    161181 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/TracksForm.java

    r4 r7  
    11package org.bbtracker.mobile.gui; 
    22 
     3import java.io.IOException; 
     4import java.io.OutputStream; 
     5 
     6import javax.microedition.io.Connector; 
     7import javax.microedition.io.file.FileConnection; 
     8import javax.microedition.lcdui.Alert; 
     9import javax.microedition.lcdui.AlertType; 
    310import javax.microedition.lcdui.Choice; 
    411import javax.microedition.lcdui.Command; 
     
    613import javax.microedition.lcdui.Displayable; 
    714import javax.microedition.lcdui.List; 
     15import javax.microedition.rms.RecordStoreException; 
    816 
    917import org.bbtracker.Track; 
    1018import org.bbtracker.mobile.BBTracker; 
     19import org.bbtracker.mobile.Preferences; 
    1120import org.bbtracker.mobile.TrackManager; 
    1221import org.bbtracker.mobile.TrackStore; 
     22import org.bbtracker.mobile.exporter.KmlTrackExporter; 
     23import org.bbtracker.mobile.exporter.TrackExporter; 
    1324 
    1425public class TracksForm extends List implements CommandListener { 
     
    1728        private final Command selectCommand; 
    1829 
     30        private final Command exportCommand; 
     31 
    1932        private final Command deleteCommand; 
    2033 
    2134        private final Command cancelCommand; 
    2235 
    23         public TracksForm(final TrackManager trackManager) { 
     36        public TracksForm(final TrackManager trackManager) throws RecordStoreException { 
    2437                super("Tracks", Choice.IMPLICIT); 
    2538 
     
    2841                selectCommand = new Command("Select", Command.OK, 0); 
    2942                addCommand(selectCommand); 
     43 
     44                exportCommand = new Command("Export", Command.ITEM, 2); 
     45                addCommand(exportCommand); 
    3046 
    3147                deleteCommand = new Command("Delete", Command.ITEM, 3); 
     
    4460        } 
    4561 
    46         private void loadNames() { 
     62        private void loadNames() throws RecordStoreException { 
    4763                deleteAll(); 
    4864                final TrackStore store = TrackStore.getInstance(); 
     
    5470 
    5571        public void commandAction(final Command command, final Displayable displayable) { 
    56                 if (command == selectCommand) { 
    57                         final Track track = TrackStore.getInstance().getTrack(getSelectedIndex()); 
    58                         trackManager.setTrack(track); 
    59                         BBTracker.getInstance().showMainCanvas(); 
    60                 } else if (command == deleteCommand) { 
    61                         TrackStore.getInstance().deleteTrack(getSelectedIndex()); 
    62                         loadNames(); 
     72                if (command == deleteCommand) { 
     73                        try { 
     74                                TrackStore.getInstance().deleteTrack(getSelectedIndex()); 
     75                                loadNames(); 
     76                        } catch (final RecordStoreException e) { 
     77                                BBTracker.nonFatal(e, "deleting track", null); 
     78                                return; 
     79                        } 
     80                        return; 
    6381                } else if (command == cancelCommand) { 
    6482                        BBTracker.getInstance().showMainCanvas(); 
     83                } else { 
     84                        final Track track; 
     85                        try { 
     86                                track = TrackStore.getInstance().getTrack(getSelectedIndex()); 
     87                        } catch (final RecordStoreException e) { 
     88                                BBTracker.nonFatal(e, "loading track", this); 
     89                                return; 
     90                        } 
     91                        if (command == selectCommand) { 
     92                                trackManager.setTrack(track); 
     93                                BBTracker.getInstance().showMainCanvas(); 
     94                        } else if (command == exportCommand) { 
     95                                final Preferences preferences = Preferences.getInstance(); 
     96                                final String dir = preferences.getExportDirectory(); 
     97                                if (dir == null) { 
     98                                        final Alert alert = new Alert("No export directory defined!", 
     99                                                        "Please define an export directory in the options screen.", null, AlertType.WARNING); 
     100                                        BBTracker.alert(alert, null); 
     101                                        return; 
     102                                } 
     103                                try { 
     104                                        exportTrack(dir, track); 
     105                                } catch (final IOException e) { 
     106                                        BBTracker.nonFatal(e, "exporting track", this); 
     107                                        return; 
     108                                } 
     109 
     110                                final Alert alert = new Alert("Finished exporting", "The track " + track.getName() + 
     111                                                " has been exported successfully!", null, AlertType.INFO); 
     112                                BBTracker.alert(alert, null); 
     113                        } 
     114                } 
     115        } 
     116 
     117        private void exportTrack(final String dir, final Track track) throws IOException { 
     118                final TrackExporter exporter = new KmlTrackExporter(); 
     119                final String fileName = exporter.getFileName(track); 
     120                final String fullName = dir.endsWith("/") ? dir + fileName : dir + "/" + fileName; 
     121                FileConnection connection = null; 
     122                OutputStream out = null; 
     123                try { 
     124                        connection = (FileConnection) Connector.open("file:///" + fullName, Connector.READ_WRITE); 
     125                        connection.create(); 
     126                        out = connection.openOutputStream(); 
     127                        exporter.export(out, track); 
     128                } finally { 
     129                        if (out != null) { 
     130                                try { 
     131                                        out.close(); 
     132                                } catch (final IOException ignored) { 
     133                                        // ignore 
     134                                } 
     135                        } 
     136                        if (connection != null) { 
     137                                try { 
     138                                        connection.close(); 
     139                                } catch (final IOException ignored) { 
     140                                        // ignore 
     141                                } 
     142                        } 
    65143                } 
    66144        } 
  • bbtracker_common/trunk/.classpath

    r4 r7  
    22<classpath> 
    33        <classpathentry kind="src" path="src"/> 
    4         <classpathentry kind="src" path="tests"/> 
    54        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/CDC-1.1%Foundation-1.1"/> 
    65        <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> 
  • bbtracker_common/trunk/src/org/bbtracker/Track.java

    r5 r7  
    5353        public int getPointCount() { 
    5454                return pointCount; 
     55        } 
     56 
     57        public TrackPoint getPoint(final int nr) { 
     58                if (nr < 0 || nr > pointCount) { 
     59                        throw new IndexOutOfBoundsException("no such point: " + nr + ", must be >= 0 and < " + pointCount); 
     60                } 
     61                int i = nr; 
     62                final Enumeration segs = segments.elements(); 
     63                while (segs.hasMoreElements()) { 
     64                        final TrackSegment seg = (TrackSegment) segs.nextElement(); 
     65                        final int count = seg.getPointCount(); 
     66                        if (i < count) { 
     67                                return seg.getPoint(i); 
     68                        } else { 
     69                                i -= count; 
     70                        } 
     71                } 
     72                throw new IllegalStateException("pointCount corrupt!"); 
    5573        } 
    5674 
  • bbtracker_common/trunk/src/org/bbtracker/Utils.java

    r5 r7  
    153153                return orig.substring(0, 20) + orig.substring(size - 4, size); 
    154154        } 
     155 
     156        public static String escapeXml(final String xml) { 
     157                final StringBuffer escaped = new StringBuffer(xml.length() + 4); 
     158                final char[] chars = xml.toCharArray(); 
     159                for (int i = 0; i < chars.length; i++) { 
     160                        final char c = chars[i]; 
     161                        switch (c) { 
     162                        case '<': 
     163                                escaped.append("&lt;"); 
     164                                break; 
     165                        case '&': 
     166                                escaped.append("&amp;"); 
     167                                break; 
     168                        default: 
     169                                escaped.append(c); 
     170                        } 
     171                } 
     172                return escaped.toString(); 
     173        } 
    155174} 
Note: See TracChangeset for help on using the changeset viewer.