package to.etc.domui.component.dynaima;

import java.text.*;

import to.etc.domui.util.*;

/**
 * ChartField defines value of the field that will be displayed on the chart as well as it's label.
 *
 * @author <a href="mailto:nmaksimovic@execom.eu">Nemanja Maksimovic</a>
 * Created on 11 Jul 2011
 */
public final class ChartField implements Comparable<ChartField> {

	private static final int BRACES_LENGTH = 3;

	private static final int LIMIT = 30 - BRACES_LENGTH;

	private final double m_value;

	private final String m_label;
	
	public ChartField(double value, String label) {
		super();
		this.m_value = value;
		this.m_label = label;
	}
	public double getValue() {
		return m_value;
	}

	public String getLabel() {
		return m_label;
	}

	public String getShortLabel() {
		final String value = "[" + new DecimalFormat("#####,##0.##").format(m_value) + "] ";
		final String label = DomUtil.isBlank(m_label) || m_label.length() < LIMIT - value.length() ? m_label : m_label.substring(0, LIMIT - 3 - value.length()) + "...";
		return value + label;
	}

	@Override
	public int compareTo(ChartField o) {
		return Double.valueOf(getValue()).compareTo(Double.valueOf(o.getValue()));
	}

	@Override
	public String toString() {
		return "Label: " + m_label + "Value: " + m_value;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((m_label == null) ? 0 : m_label.hashCode());
		long temp;
		temp = Double.doubleToLongBits(m_value);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if(this == obj)
			return true;
		if(obj == null)
			return false;
		if(getClass() != obj.getClass())
			return false;
		ChartField other = (ChartField) obj;
		if(m_label == null) {
			if(other.m_label != null)
				return false;
		} else if(!m_label.equals(other.m_label))
			return false;
		if(Double.doubleToLongBits(m_value) != Double.doubleToLongBits(other.m_value))
			return false;
		return true;
	}

}

@@ -53,16 +53,64 @@
 .text.*;
+%0Aimport java.util.*;%0A%0Aimport javax.annotation.*;
 %0A%0Aimport
@@ -376,29 +376,318 @@
 eld 
-implements Comparable
+%7B%0A%0A%09public static final Comparator%3CChartField%3E COMPARATOR_BY_VALUE = new Comparator%3CChartField%3E() %7B%0A%09%09@Override%0A%09%09public int compare(ChartField o1, ChartField o2) %7B%0A%09%09%09return Double.compare(o1.getValue(), o2.getValue());%0A%09%09%7D%0A%09%7D;%0A%0A%09public static final Comparator%3CChartField%3E COMPARATOR_BY_LABEL = new Comparator
 %3CCha
@@ -694,18 +694,143 @@
 rtField%3E
+()
  %7B
+%0A%09%09@Override%0A%09%09public int compare(ChartField o1, ChartField o2) %7B%0A%09%09%09return o1.getLabel().compareTo(o2.getLabel());%0A%09%09%7D%0A%09%7D;
 %0A%0A%09priva
@@ -849,29 +849,38 @@
 nal int 
-BRACES_LENGTH
+VALUE_EXTRA_CHARACTERS
  = 3;%0A%0A%09
@@ -904,16 +904,22 @@
 nal int 
+LABEL_
 LIMIT = 
@@ -927,21 +927,30 @@
 0 - 
-BRACES_LENGTH
+VALUE_EXTRA_CHARACTERS
 ;%0A%0A%09
@@ -1045,16 +1045,25 @@
 e value,
+ @Nonnull
  String 
@@ -1084,21 +1084,116 @@
 er();%0A%09%09
-this.
+if(label == null) %7B%0A%09%09%09throw new IllegalArgumentException(%22ChartField.label cannot be null%22);%0A%09%09%7D%0A%09%09
 m_value 
@@ -1207,13 +1207,8 @@
 ;%0A%09%09
-this.
 m_la
@@ -1223,16 +1223,17 @@
 bel;%0A%09%7D%0A
+%0A
 %09public 
@@ -1270,24 +1270,34 @@
 _value;%0A%09%7D%0A%0A
+%09@Nonnull%0A
 %09public Stri
@@ -1524,16 +1524,22 @@
 gth() %3C 
+LABEL_
 LIMIT - 
@@ -1586,16 +1586,22 @@
 ring(0, 
+LABEL_
 LIMIT - 
@@ -1661,138 +1661,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public int compareTo(ChartField o) %7B%0A%09%09return Double.valueOf(getValue()).compareTo(Double.valueOf(o.getValue()));%0A%09%7D%0A%0A
 %09@Ov
@@ -1728,16 +1728,17 @@
 abel + %22
+ 
 Value: %22

Correct version of ChartField.java