package org.helioviewer.jhv.gui.components.base;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JTextField;

import org.helioviewer.jhv.base.time.TimeUtils;

/**
 * This offers a text field to edit the time at a day. A normal
 * JFormattedTextField is for some reason badly from the user interface
 * (especially then backspace tends to delete more than one item).
 * <p>
 * It is based on the parts of the ObservationDialog from Stephan Pagel and I
 * therefore will assume a format from the user as HH:mm:ss.
 * <p>
 * It will validates when the focus leaves, and defaults to 00:00:00; like
 * before maybe worth changing later
 *
 * @author Helge Dietert
 */
@SuppressWarnings({ "serial" })
public class TimeTextField extends JTextField {
    /**
     * Default value used to set
     */
    private static final String defaultTime = "00:00:00";

    /**
     * Used time formatter
     */

    /**
     * Creates a new time text field
     */
    public TimeTextField() {
        super(defaultTime);
        addFocusListener(new FocusListener() {
            /**
             * Nothing to do
             *
             * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
             */
            @Override
            public void focusGained(FocusEvent arg0) {
            }

            /**
             * Validate the input
             *
             * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
             */
            @Override
            public void focusLost(FocusEvent arg0) {
                validateInput();
            }
        });
    }

    /**
     * Gives the formatted input (normalized, e.g. 0:61 becomes 1:01)
     *
     * @return formatted input or default time if its not valid
     */
    public String getFormattedInput() {
        String time = getText();
        try {
            return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(time));
        } catch (ParseException e) {
            try {
                return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(time + ":00"));
            } catch (ParseException e2) {
                return defaultTime;
            }
        }
    }

    /**
     * Gives a date object with the selected time
     *
     * @return Date with selected time (or defaultTime if invalid)
     */
    public Date getValue() {
        String time = getText();
        try {
            return TimeUtils.timeDateFormat.parse(time);
        } catch (ParseException e) {
            try {
                return TimeUtils.timeDateFormat.parse(time + ":00");
            } catch (ParseException e2) {
                try {
                    return TimeUtils.timeDateFormat.parse(defaultTime);
                } catch (ParseException e4) {
                    return null;
                }

            }
        }
    }

    /**
     * Validates the fields and resets its if necessary
     */
    private void validateInput() {
        setText(getFormattedInput());
    }

    /**
     * Sets the time to the time given in the date
     *
     * @param time
     *            new time to set
     */
    public void setValue(Date time) {
        setText(TimeUtils.timeDateFormat.format(time));
    }

}

@@ -2234,32 +2234,213 @@
 Exception e2) %7B%0A
+                try %7B%0A                    return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(time + %22:00:00%22));%0A                %7D catch (ParseException e3) %7B%0A    
                 
@@ -2459,16 +2459,34 @@
 ltTime;%0A
+                %7D%0A
         
@@ -2961,32 +2961,184 @@
           try %7B%0A
+                    return TimeUtils.timeDateFormat.parse(time + %22:00:00%22);%0A                %7D catch (ParseException e3) %7B%0A                    try %7B%0A    
                 
@@ -3185,32 +3185,36 @@
 e(defaultTime);%0A
+    
                 
@@ -3259,24 +3259,28 @@
             
+    
 return null;
@@ -3280,16 +3280,39 @@
 n null;%0A
+                    %7D%0A%0A
         

Extend time more