]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Track.java
Version 4, January 2008
[GpsPrune.git] / tim / prune / data / Track.java
index 683cd21fcf6b3f07da472b64fc151970372ef51d..1188a7c5a83d6edef42a2d8f66c1b7bf59f608df 100644 (file)
@@ -1,6 +1,10 @@
 package tim.prune.data;
 
+import java.util.List;
+
 import tim.prune.UpdateMessageBroker;
+import tim.prune.edit.FieldEdit;
+import tim.prune.edit.FieldEditList;
 
 
 /**
@@ -28,9 +32,8 @@ public class Track
 
 
        /**
-        * Constructor giving arrays of Fields and Objects
-        * @param inFieldArray field array
-        * @param inPointArray 2d array of field values
+        * Constructor for empty track
+        * @param inBroker message broker object
         */
        public Track(UpdateMessageBroker inBroker)
        {
@@ -53,6 +56,11 @@ public class Track
         */
        public void load(Field[] inFieldArray, Object[][] inPointArray, int inAltFormat)
        {
+               if (inFieldArray == null || inPointArray == null)
+               {
+                       _numPoints = 0;
+                       return;
+               }
                // copy field list
                _masterFieldList = new FieldList(inFieldArray);
                // make DataPoint object from each point in inPointList
@@ -144,7 +152,8 @@ public class Track
                for (int i=0; i<_numPoints; i++)
                {
                        boolean keepPoint = true;
-                       if (!_dataPoints[i].isWaypoint())
+                       // Don't delete waypoints or photo points
+                       if (!_dataPoints[i].isWaypoint() && _dataPoints[i].getPhoto() == null)
                        {
                                // go through newPointArray to check for range
                                for (int j=0; j<numCopied && keepPoint; j++)
@@ -172,7 +181,6 @@ public class Track
                        System.arraycopy(newPointArray, 0, _dataPoints, 0, numCopied);
                        _numPoints = _dataPoints.length;
                        _scaled = false;
-                       _broker.informSubscribers();
                }
                return numDeleted;
        }
@@ -202,6 +210,7 @@ public class Track
 
        /**
         * Delete the specified point
+        * @param inIndex point index
         * @return true if successful
         */
        public boolean deletePoint(int inIndex)
@@ -238,7 +247,7 @@ public class Track
                        System.arraycopy(_dataPoints, inEnd + 1, newPointArray, inStart,
                                _numPoints - inEnd - 1);
                }
-               // Copy points over original array (careful!)
+               // Copy points over original array
                _dataPoints = newPointArray;
                _numPoints -= numToDelete;
                // needs to be scaled again
@@ -288,7 +297,6 @@ public class Track
                        _dataPoints = newPointArray;
                        _numPoints = _dataPoints.length;
                        _scaled = false;
-                       _broker.informSubscribers();
                }
                return numDupes;
        }
@@ -296,6 +304,8 @@ public class Track
 
        /**
         * Reverse the specified range of points
+        * @param inStart start index
+        * @param inEnd end index
         * @return true if successful, false otherwise
         */
        public boolean reverseRange(int inStart, int inEnd)
@@ -452,10 +462,26 @@ public class Track
 
                // Make array of points to insert
                DataPoint[] insertedPoints = startPoint.interpolate(endPoint, inNumPoints);
-               
+
                // Insert points into track
-               insertRange(insertedPoints, inStartIndex + 1);
-               return true;
+               return insertRange(insertedPoints, inStartIndex + 1);
+       }
+
+
+       /**
+        * Append the specified points to the end of the track
+        * @param inPoints DataPoint objects to add
+        */
+       public void appendPoints(DataPoint[] inPoints)
+       {
+               // Insert points into track
+               if (inPoints != null && inPoints.length > 0)
+               {
+                       insertRange(inPoints, _numPoints);
+               }
+               // needs to be scaled again to recalc x, y
+               _scaled = false;
+               _broker.informSubscribers();
        }
 
 
@@ -581,6 +607,48 @@ public class Track
        }
 
 
+       /**
+        * Collect all the waypoints into the given List
+        * @param inList List to fill with waypoints
+        */
+       public void getWaypoints(List inList)
+       {
+               // clear list
+               inList.clear();
+               // loop over points and copy all waypoints into list
+               for (int i=0; i<=_numPoints-1; i++)
+               {
+                       if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
+                       {
+                               inList.add(_dataPoints[i]);
+                       }
+               }
+       }
+
+
+       /**
+        * Search for the given Point in the track and return the index
+        * @param inPoint Point to look for
+        * @return index of Point, if any or -1 if not found
+        */
+       public int getPointIndex(DataPoint inPoint)
+       {
+               if (inPoint != null)
+               {
+                       // Loop over points in track
+                       for (int i=0; i<=_numPoints-1; i++)
+                       {
+                               if (_dataPoints[i] == inPoint)
+                               {
+                                       return i;
+                               }
+                       }
+               }
+               // not found
+               return -1;
+       }
+
+
        ///////// Internal processing methods ////////////////
 
 
@@ -604,7 +672,9 @@ public class Track
                                _longRange.addValue(point.getLongitude().getDouble());
                                _latRange.addValue(point.getLatitude().getDouble());
                                if (point.getAltitude().isValid())
+                               {
                                        _altitudeRange.addValue(point.getAltitude());
+                               }
                                if (point.isWaypoint())
                                        hasWaypoint = true;
                                else
@@ -778,6 +848,7 @@ public class Track
        /**
         * Replace the track contents with the given point array
         * @param inContents array of DataPoint objects
+        * @return true on success
         */
        public boolean replaceContents(DataPoint[] inContents)
        {
@@ -790,4 +861,41 @@ public class Track
                _broker.informSubscribers();
                return true;
        }
+
+
+       /**
+        * Edit the specified point
+        * @param inPoint point to edit
+        * @param inEditList list of edits to make
+        * @return true if successful
+        */
+       public boolean editPoint(DataPoint inPoint, FieldEditList inEditList)
+       {
+               if (inPoint != null && inEditList != null && inEditList.getNumEdits() > 0)
+               {
+                       // remember if coordinates have changed
+                       boolean coordsChanged = false;
+                       // go through edits one by one
+                       int numEdits = inEditList.getNumEdits();
+                       for (int i=0; i<numEdits; i++)
+                       {
+                               FieldEdit edit = inEditList.getEdit(i);
+                               inPoint.setFieldValue(edit.getField(), edit.getValue());
+                               // check coordinates
+                               coordsChanged |= (edit.getField().equals(Field.LATITUDE)
+                                       || edit.getField().equals(Field.LONGITUDE) || edit.getField().equals(Field.ALTITUDE));
+                       }
+                       // set photo status if coordinates have changed
+                       if (inPoint.getPhoto() != null && coordsChanged)
+                       {
+                               inPoint.getPhoto().setCurrentStatus(PhotoStatus.CONNECTED);
+                       }
+                       // point possibly needs to be scaled again
+                       _scaled = false;
+                       // trigger listeners
+                       _broker.informSubscribers();
+                       return true;
+               }
+               return false;
+       }
 }