/******************************************************************************
* KmlBuilder
* KML writer and construction class for creating rich KML 
* of genealogeo mappings
* 
* Author:       Mitchell Bowden <mitchellbowden AT gmail DOT com>
* License:      MIT License: http://creativecommons.org/licenses/MIT/
******************************************************************************/

package com.msbmsb.genealogeo.kml;

import com.msbmsb.genealoj.*;
import com.msbmsb.gogogeocode.*;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import de.micromata.opengis.kml.v_2_2_0.Icon;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.Placemark;
import de.micromata.opengis.kml.v_2_2_0.Style;

import java.io.File;
import java.util.List;

public class KmlBuilder {
  public Kml m_kml;
  public Document m_doc;
  public Folder m_folder;
  public Style m_defaultStyle;

  public KmlBuilder(String kmlName) {
    m_kml = new Kml();
    m_doc = m_kml.createAndSetDocument().withName(kmlName).withOpen(true);
    m_folder = m_doc.createAndAddFolder();
    m_defaultStyle = m_doc.createAndAddStyle();
    m_defaultStyle.withId("style_default")
      .createAndSetIconStyle().withScale(10.0);
    m_defaultStyle.createAndSetLabelStyle().withColor("000000").withScale(10.0);
  }

  public void addAndLink(IndividualNode curr) {
    addAndLink(curr, null, null);
  }

  public void addAndLink(IndividualNode curr, IndividualNode prev, Coordinates prevCoords) {
    // add curr, link to prev
    // then recurse

    String fullName = curr.getFullName();
    Coordinates currCoords = GoGoGeocode.geocode(curr.getChildrenWithTag("BIRT").get(0).getChildrenWithTag("PLAC").get(0).data());

    Placemark placemark = m_folder.createAndAddPlacemark();
    placemark.withName(curr.getSurname())
      .withStyleUrl("#style_default")
      .withDescription(fullName + " - birth")
      .createAndSetLookAt().withLongitude(currCoords.longitude).withLatitude(currCoords.latitude).withAltitude(0).withRange(12000000);
    placemark.createAndSetPoint().addToCoordinates(currCoords.longitude, currCoords.latitude);

    // if prev coords available, make connecting line
    if(prevCoords != null)
      placemark.createAndSetLineString().withExtrude(true).withTessellate(true)
        .addToCoordinates(currCoords.toString())
        .addToCoordinates(prevCoords.toString());

    List<IndividualNode> parents = curr.getParents();
    for(IndividualNode i : parents) {
      addAndLink(i, curr, currCoords);
    }
  }

  public void marshal(String fileName) {
    try {
      m_kml.marshal(new File(fileName));
    } catch(Exception e) {
    }
  }
}

@@ -426,16 +426,52 @@
 o.kml;%0A%0A
+import com.msbmsb.genealogeo.kml.*;%0A
 import c
@@ -488,24 +488,24 @@
 genealoj.*;%0A
-
 import com.m
@@ -812,16 +812,67 @@
 0.Style;
+%0Aimport de.micromata.opengis.kml.v_2_2_0.LineStyle;
 %0A%0Aimport
@@ -935,29 +935,30 @@
 uilder %7B%0A  p
-ublic
+rivate
  Kml m_kml;%0A
@@ -960,21 +960,22 @@
 kml;%0A  p
-ublic
+rivate
  Documen
@@ -986,21 +986,22 @@
 doc;%0A  p
-ublic
+rivate
  Folder 
@@ -1013,21 +1013,22 @@
 der;%0A  p
-ublic
+rivate
  Style m
@@ -1357,24 +1357,24 @@
 cale(10.0);%0A
-
     m_defaul
@@ -1442,16 +1442,112 @@
 (10.0);%0A
+    m_defaultStyle.createAndSetLineStyle()%0A      .withColor(%22760000ff%22)%0A      .withWidth(3.0d);%0A
   %7D%0A%0A  p

add linestyle to default style