package Debrief.ReaderWriter.XML.Shapes;

import MWC.GenericData.WorldDistance;
import MWC.Utilities.ReaderWriter.XML.PlottableExporter;
import MWC.Utilities.ReaderWriter.XML.MWCXMLReader.HandleAttribute;
import MWC.Utilities.ReaderWriter.XML.Util.LocationHandler;

abstract public class VectorHandler extends ShapeHandler implements PlottableExporter
{

  private final static String ARROW_AT_END="ArrowAtEnd";
	
  MWC.GenericData.WorldLocation _start;
  double _bearing;
  WorldDistance _distance;

  protected boolean _arrowAtEnd = false;

  public VectorHandler()
  {
    // inform our parent what type of class we are
    super("vector");


    addHandler(new LocationHandler("tl"){
      public void setLocation(MWC.GenericData.WorldLocation res)
      {
        _start = res;
      }
    });
    addAttributeHandler(new HandleAttribute("Bearing")
	{
		public void setValue(String name, String val)
		{
			_bearing = Double.valueOf(val);
		}
	});
    addAttributeHandler(new HandleBooleanAttribute(ARROW_AT_END)
    {
      public void setValue(String name, boolean value)
      {
        _arrowAtEnd = value;
      }
    });
    addAttributeHandler(new HandleAttribute("Distance")
	{
		public void setValue(String name, String val)
		{
			try
			{
				double _inner = readThisDouble(val);
				_distance = new WorldDistance(_inner, WorldDistance.YARDS);
			}
			catch (java.text.ParseException pe)
			{
				MWC.Utilities.Errors.Trace.trace(pe, "Failed reading in:" + name + " value is:"
						+ val);
			}

		}
	});

  }

  public final MWC.GUI.Shapes.PlainShape getShape()
  {
    MWC.GUI.Shapes.LineShape ls = new MWC.GUI.Shapes.VectorShape(_start, _bearing,  _distance);
    ls.setArrowAtEnd(_arrowAtEnd);
    return ls;
  }

  public final void elementClosed()
  {
    super.elementClosed();

    // reset the local parameters
    _start =  null;
    _distance = null;
    _bearing = 0;
    _arrowAtEnd = false;
  }

  public final void exportThisPlottable(MWC.GUI.Plottable plottable,org.w3c.dom.Element parent, org.w3c.dom.Document doc)
  {
    // output the shape related stuff first
    org.w3c.dom.Element ePlottable = doc.createElement(_myType);

    super.exportThisPlottable(plottable, ePlottable, doc);

    // now our circle related stuff

    // get the circle
    Debrief.Wrappers.ShapeWrapper sw = (Debrief.Wrappers.ShapeWrapper)plottable;
    MWC.GUI.Shapes.PlainShape ps = sw.getShape();
    if(ps instanceof MWC.GUI.Shapes.VectorShape)
    {
      // export the attributes
      MWC.GUI.Shapes.VectorShape cs = (MWC.GUI.Shapes.VectorShape)ps;
      MWC.Utilities.ReaderWriter.XML.Util.LocationHandler.exportLocation(cs.getLine_Start(), "tl", ePlottable, doc);
      ePlottable.setAttribute("Distance", writeThis(cs.getDistance().getValueIn(
				WorldDistance.YARDS)));
      ePlottable.setAttribute("Bearing", writeThis(cs.getBearing()));
      ePlottable.setAttribute(ARROW_AT_END, writeThis(cs.getArrowAtEnd()));
    }
    else
    {
      throw new java.lang.RuntimeException("wrong shape passed to line exporter");
    }

    // add ourselves to the output
    parent.appendChild(ePlottable);
  }


}

@@ -134,76 +134,8 @@
 er;%0A
-import MWC.Utilities.ReaderWriter.XML.MWCXMLReader.HandleAttribute;%0A
 impo
@@ -290,16 +290,158 @@
 private 
+static final String DISTANCE = %22Distance%22;%0A%09private static final String BEARING = %22Bearing%22;%0A%09private static final String TL = %22tl%22;%0A%09private 
 final st
@@ -748,20 +748,18 @@
 Handler(
-%22tl%22
+TL
 )%7B%0A     
@@ -908,25 +908,23 @@
 tribute(
-%22Bearing%22
+BEARING
 )%0A%09%7B%0A%09%09p
@@ -1239,26 +1239,24 @@
 tribute(
-%22Distance%22
+DISTANCE
 )%0A%09%7B%0A%09%09p
@@ -2723,20 +2723,18 @@
 tart(), 
-%22tl%22
+TL
 , ePlott
@@ -2775,26 +2775,24 @@
 tribute(
-%22Distance%22
+DISTANCE
 , writeT
@@ -2882,25 +2882,23 @@
 tribute(
-%22Bearing%22
+BEARING
 , writeT

use constants for editable attributes