package org.timepedia.chronoscope.client.render;

import org.timepedia.chronoscope.client.Cursor;
import org.timepedia.chronoscope.client.canvas.Layer;
import org.timepedia.chronoscope.client.gss.GssElement;
import org.timepedia.chronoscope.client.gss.GssProperties;
import org.timepedia.chronoscope.client.render.domain.TickFormatter;
import org.timepedia.chronoscope.client.render.domain.TickFormatterFactory;
import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.Exportable;

/**
 * Draws a date range (e.g. "11/25/1995 - 02/17/2007").
 * 
 * @author Chad Takahashi
 */
public class DateRangePanel extends AbstractPanel implements SelfResizing,
    GssElement, Exportable {

  public String getType() {
    return "daterange";
  }

  public String getTypeClass() {
    return null;
  }

  public final GssElement getParentGssElement() {
    return (LegendAxisPanel) this.parent;
  }

  private void initGssProperties(GssProperties props) {
    gssProperties = props;
  }

  DomainAxisPanel domainAxisPanel;
  GssProperties labelProperties;
  String dateRangeActive;

  public void init(Layer layer, DomainAxisPanel domainAxisPanel) {
    this.domainAxisPanel = domainAxisPanel;
    initGssProperties(domainAxisPanel.view.getGssProperties(this, ""));
    labelProperties = domainAxisPanel.view.getGssProperties(new GssElementImpl(
        "label", getParentGssElement()), "");
    resizeToIdealWidth();
  }

  public void draw() {
    layer.setStrokeColor(labelProperties.color);
    layer.drawText(bounds.x, bounds.y, dateRangeActive,
        labelProperties.fontFamily, labelProperties.fontWeight,
        labelProperties.fontSize, textLayerName, Cursor.DEFAULT);
  }
  
  private TickFormatter<?> getTickFormater() {
    TickFormatterFactory<?> fact = domainAxisPanel.getTickFormatterFactory();
    double domainWidth = domainAxisPanel.plot.getDomain().length();
    return fact.findBestFormatter(domainWidth);
  }
  
  public void resizeToMinimalWidth() {
    dateRangeActive = getTickFormater().getRangeLabelCompact(domainAxisPanel.plot.getDomain());
    bounds.width = stringSizer.getWidth(dateRangeActive, labelProperties);
  }

  public void resizeToIdealWidth() {
    dateRangeActive = getTickFormater().getRangeLabel(domainAxisPanel.plot.getDomain());
    bounds.width = stringSizer.getWidth(dateRangeActive, labelProperties);
  }

  @Export
  @Deprecated
  public void setDateRangeFormat(String dateFormat) {
  }
  

  @Export
  @Deprecated
  public void setCompactDateRangeFormat(String compactDateFormat) {
  }

  @Export
  @Deprecated
  public void setDateDelim(String dateDelim) {
  }

  @Export
  @Deprecated
  public void setCompactDateDelim(String compactDateDelim) {
  }

}

@@ -405,16 +405,199 @@
 actory;%0A
+import org.timepedia.chronoscope.client.util.DateFormatter;%0Aimport org.timepedia.chronoscope.client.util.Interval;%0Aimport org.timepedia.chronoscope.client.util.date.DateFormatHelper;%0A
 import o
@@ -712,72 +712,8 @@
 ange
- (e.g. %2211/25/1995 - 02/17/2007%22).%0A * %0A * @author Chad Takahashi
 %0A */
@@ -1208,16 +1208,146 @@
 eActive;
+%0A  DateFormatter dateFormater;%0A  String dateDelim = %22 - %22;%0A  DateFormatter dateFormaterCompact;%0A  String dateDelimCompact = %22 - %22;
 %0A%0A  publ
@@ -2224,32 +2224,249 @@
 inimalWidth() %7B%0A
+    Interval i = domainAxisPanel.plot.getDomain();%0A    if (dateFormater != null) %7B%0A      dateRangeActive = dateFormaterCompact.format(i.getStart()) + dateDelimCompact + dateFormater.format(i.getEnd());%0A    %7D else %7B%0A  
     dateRangeAct
@@ -2537,32 +2537,38 @@
 t.getDomain());%0A
+    %7D%0A
     bounds.width
@@ -2664,24 +2664,109 @@
 alWidth() %7B%0A
+    Interval i = domainAxisPanel.plot.getDomain();%0A    if (dateFormater != null) %7B%0A  
     dateRang
@@ -2771,39 +2771,36 @@
 ngeActive = 
-getTick
+date
 Formater
 ().getRangeL
@@ -2791,59 +2791,155 @@
 ater
-().getRangeLabel(domainAxisPanel.plot.getDomain());
+.format(i.getStart()) + dateDelim + dateFormater.format(i.getEnd());%0A    %7D else %7B%0A      dateRangeActive = getTickFormater().getRangeLabel(i);%0A    %7D
 %0A   
@@ -3021,38 +3021,24 @@
 %0A%0A  @Export%0A
-  @Deprecated%0A
   public voi
@@ -3085,105 +3085,333 @@
 %7B%0A  
-%7D%0A  %0A%0A  @Export%0A  @Deprecated%0A  public void setCompactDateRangeFormat(String compactDateFormat) %7B
+  if (!%22auto%22.equals(dateFormat)) %7B%0A      dateFormater = DateFormatHelper.getDateFormatter(dateFormat);%0A    %7D%0A  %7D%0A%0A  @Export%0A  public void setCompactDateRangeFormat(String compactDateFormat) %7B%0A    if (!%22auto%22.equals(compactDateFormat)) %7B%0A      dateFormaterCompact = DateFormatHelper.getDateFormatter(compactDateFormat);%0A    %7D
 %0A  %7D
@@ -3418,38 +3418,24 @@
 %0A%0A  @Export%0A
-  @Deprecated%0A
   public voi
@@ -3475,95 +3475,159 @@
 %7B%0A  
-%7D%0A%0A  @Export%0A  @Deprecated%0A  public void setCompactDateDelim(String compactDateDelim) %7B
+  this.dateDelim = dateDelim;%0A  %7D%0A%0A  @Export%0A  public void setCompactDateDelim(String dateDelimCompact) %7B%0A    this.dateDelimCompact = dateDelimCompact;
 %0A  %7D

allow to customize the dateRange format to be compatible with the old api, by default "auto" means select the most suitable format for each zoom