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() {
        try {
            return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(getText()));
        } catch (ParseException e) {
            return defaultTime;
        }
    }

    /**
     * Gives a date object with the selected time
     *
     * @return Date with selected time (or defaultTime if invalid)
     */
    public Date getValue() {
        try {
            return TimeUtils.timeDateFormat.parse(getText());
        } catch (ParseException e) {
            try {
                return TimeUtils.timeDateFormat.parse(defaultTime);
            } catch (ParseException e1) {
                // The default time should always parseable
                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));
    }

}

@@ -779,16 +779,17 @@
 gs(%7B
+ 
 %22serial%22
 %7D)%0Ap
@@ -784,16 +784,17 @@
 %22serial%22
+ 
 %7D)%0Apubli
@@ -1898,32 +1898,65 @@
 mattedInput() %7B%0A
+        String time = getText();%0A
         try %7B%0A  
@@ -2039,21 +2039,541 @@
 rse(
-getText()));%0A
+time));%0A        %7D catch (ParseException e) %7B%0A            try %7B%0A                return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(%220%22 + time));%0A            %7D catch (ParseException e1) %7B%0A                try %7B%0A                    return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(time + %22:00%22));%0A                %7D catch (ParseException e2) %7B%0A                    try %7B%0A                        return TimeUtils.timeDateFormat.format(TimeUtils.timeDateFormat.parse(%220%22 + time + %22:00%22));%0A            
     
@@ -2593,32 +2593,33 @@
 ParseException e
+3
 ) %7B%0A            
@@ -2614,24 +2614,36 @@
             
+            
 return defau
@@ -2650,16 +2650,70 @@
 ltTime;%0A
+                    %7D%0A                %7D%0A            %7D%0A
         
@@ -2886,24 +2886,57 @@
 etValue() %7B%0A
+        String time = getText();%0A
         try 
@@ -2987,25 +2987,142 @@
 t.parse(
-getText()
+time);%0A        %7D catch (ParseException e) %7B%0A            try %7B%0A                return TimeUtils.timeDateFormat.parse(%220%22 + time
 );%0A     
@@ -3116,32 +3116,36 @@
  time);%0A        
+    
 %7D catch (ParseEx
@@ -3145,32 +3145,33 @@
 ParseException e
+1
 ) %7B%0A            
@@ -3162,32 +3162,36 @@
 ) %7B%0A            
+    
 try %7B%0A          
@@ -3188,32 +3188,36 @@
                 
+    
 return TimeUtils
@@ -3238,30 +3238,189 @@
 t.parse(
-defaultTime);%0A
+time + %22:00%22);%0A                %7D catch (ParseException e2) %7B%0A                    try %7B%0A                        return TimeUtils.timeDateFormat.parse(%220%22 + time + %22:00%22);%0A   
         
@@ -3415,32 +3415,37 @@
 %0A               
+     
 %7D catch (ParseEx
@@ -3445,33 +3445,33 @@
 ParseException e
-1
+3
 ) %7B%0A            
@@ -3478,52 +3478,160 @@
     
-// The 
+        try %7B%0A                            return TimeUtils.timeDateFormat.parse(
 default
- time should always parseable%0A
+Time);%0A                        %7D catch (ParseException e4) %7B%0A    
     
@@ -3638,24 +3638,32 @@
             
+        
 return null;
@@ -3663,16 +3663,82 @@
 n null;%0A
+                        %7D%0A                    %7D%0A                %7D%0A
         

Allow different time formats