]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Track.java
Version 3, August 2007
[GpsPrune.git] / tim / prune / data / Track.java
index 683cd21fcf6b3f07da472b64fc151970372ef51d..c5a0f16f4d72a3707ebf4460088958c97cbf14c0 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;
 
 
 /**
@@ -53,6 +57,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 +153,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++)
@@ -238,7 +248,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
@@ -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
@@ -790,4 +860,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;
+       }
 }