package de.roboshock.gui.util;

import java.awt.Component;
import java.text.DecimalFormat;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

@SuppressWarnings("serial")
public class UsefullTableCellRenderer extends DefaultTableCellRenderer {

	private int	anzNachkommaBeiDoubles	= 1;
	private boolean stringsLeft = false;
	private boolean expectDate = false;
	private boolean withTooltip = false;

	public UsefullTableCellRenderer() {
		this(1);
	}

	public UsefullTableCellRenderer(int anzNachkommaBeiDoubles) {
		super();
		this.anzNachkommaBeiDoubles = anzNachkommaBeiDoubles;
	}

	public UsefullTableCellRenderer(int anzNachkommaBeiDoubles, boolean expectDate) {
		super();
		this.anzNachkommaBeiDoubles = anzNachkommaBeiDoubles;
		this.expectDate = expectDate;
	}

	/**
	* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
	*/
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Component c = super.getTableCellRendererComponent(table, value, isSelected,
				hasFocus, row, column);

		try {

			// Sonderbehandlung f�r double
			if (value instanceof Double) {
				JLabel c2 = (JLabel) c;
				c2.setHorizontalAlignment(JLabel.RIGHT);
				DecimalFormat df = new DecimalFormat("#,###,##0.00");
				if (anzNachkommaBeiDoubles == 0)
					df = new DecimalFormat("#,###,##0");
				if (anzNachkommaBeiDoubles == 1)
					df = new DecimalFormat("#,###,##0.0#");
				String dblValue = df.format((Double) value);
				c2.setText(dblValue);
			}

			if (value instanceof Long) {
				JLabel c2 = (JLabel) c;
				c2.setHorizontalAlignment(JLabel.RIGHT);
			}

			if (value instanceof JComponent) {
				return (JComponent) value;
			}

			// Sonderbehandlung f�r String
			if ((value instanceof String) && (c instanceof JLabel)) {
				JLabel c2 = (JLabel) c;
				// f�r dat�mer
				if (expectDate) {
					c2.setText(getDateStringForTable((String)value));
				}

				if (stringsLeft) {
					c2.setHorizontalAlignment(JLabel.LEFT);
					// Um einen Abstand zu rechts stehenden Inhalten zu garantieren, ein blank einf�gen
					c2.setText(" "+c2.getText());
				}
				else {
					c2.setHorizontalAlignment(JLabel.RIGHT);
				}
				
				if (withTooltip) {
					// VALUE als Tooltip �bernehmen
					String tooltip = value.toString().length() > 0 ? value.toString() : null;
					// Tooltip setzen
					setToolTipText(tooltip);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return c;
	}
	
	public static String getDateStringForTable(String s) {
		if (s == null) {
			return "";
		} 
		if ((s.indexOf(".") > -1) || s.length() != 8)
			return s; // steht was verkehrtes drin
		
		if (s.equals("01011111"))
			return "01.01.1111";
		
		try {
			return s; //DateConverter.dateWithPoints(s);
		} catch (Exception e) {
			return s; // steht was verkehrtes drin
		}
	}
	
	public void setStringsLeft(boolean stringsLeft) {
		this.stringsLeft = stringsLeft;
	}


	public void setWithTooltip(boolean tooltip) {
	  this.withTooltip = tooltip;
  }
}
