;+
; PROJECT
;          Solar-B EIS
;
; NAME
;          eis_obev_gui__DEFINE
;
; PURPOSE
;          Preferences GUI
;
; INPUTS
;          parentClass - the eis_mk_plan object
;
; CATEGORY
;          EIS Timeline Planning
;
; WRITTEN
;          John A Rainnie RAL 6-Jun-2008
;
; HISTORY
;          v0.1 JAR 06-Nov-2008
;               Catch case where last index is EQ the number of array
;               elements (in which case, subtract 1.0)
;          v0.2 JAR 18-Nov-2008
;               Allow buffer to be negative. In which case, catch where
;               stop_time is less than start_time.
;          v0.3 JAR 5-May-2009
;               Added XTW tags to GUI.
;          v0.4 JAR 18-Jun-2009
;               1. Commented out ETW (DRW request)
;               2. Added "Ignore SAA" radio button (DRW request)
;          v0.5 JAR 15-Jun-2010
;               Modified XTW buffer from 0 to 2 minutes (DRW request)
;          v0.6 JAR 07-Sep-2010
;               Increased size of Offset combobox in OBEV Tool. Also
;               disable widgets if no obev entries are found.
;
;-
;______________________________________________________________________________
FUNCTION eis_obev_gui::Init , parentClass

Self.parentClass  = parentClass
Self.group_leader = parentClass->tlbID()

;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = "Observing Windows" + pt_version
TLB_EVENT                  = {eis_obev_gui_Event}
TLB_EVENT.eis_obev_gui     = Self
TLB_EVENT.Type             = "TLB"
Self.tlbID                 = WIDGET_BASE(                                  $
                                   TITLE       = widTitle            ,     $
                                   COLUMN      = 1                   ,     $
                                   /FLOATING                         ,     $
                                   GROUP_LEADER = Self.group_leader  ,     $
                                   UValue      = TLB_EVENT )

Event_Pro = 'eis_obev_gui_EV'
; ______________________________________________________________________
;
; Widget Tab
;
; ______________________________________________________________________
tabBase                    = WIDGET_TAB(Self.tlbID,/frame)
Self.tabBaseID = tabBase
value = ' SAA and Eclipse Free Periods '
allBase                   = WIDGET_BASE(tabBase                      ,     $
                                   Title      = value                ,     $
                                   COLUMN     = 1)

value = ' SAA Periods '
saaBase                    = WIDGET_BASE(tabBase                     ,     $
                                   Title      = value                ,     $
                                   COLUMN     = 1)

value = ' X-Ray Eclipses (XTW) '
xtwBase                    = WIDGET_BASE(tabBase                     ,     $
                                   Title      = value                ,     $
                                   COLUMN     = 1)

;value = ' EUV Eclipses (ETW) '
;etwBase                    = WIDGET_BASE(tabBase                     ,     $
;                                   Title      = value                ,     $
;                                   COLUMN     = 1)

value = ' Optical Eclipses (NGT) '
ngtBase                    = WIDGET_BASE(tabBase                     ,     $
                                   Title      = value                ,     $
                                   COLUMN     = 1)

; ______________________________________________________________________
;
; SAA Widget Text
;
; ______________________________________________________________________
tab_topline = STRING(format='(A-27,A-27,A-10)'                       ,     $
                       'Start Time','Stop Time','Duration')
aLabel                     = WIDGET_LABEL(saaBase                    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   value       = tab_topline)

saaText_EVENT              = {eis_obev_gui_Event}
saaText_EVENT.eis_obev_gui = Self
saaText_EVENT.Type         = "SAATEXT"
Self.saaTextID                = WIDGET_LIST(saaBase                  ,     $
                                   value       = 'nowt'              ,     $
                                   ySize       = 20                  ,     $
                                   xSize       = 100                 ,     $
                                   ;/SCROLL)
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = saaText_EVENT)

; ______________________________________________________________________
;
; XTW Text
;
; ______________________________________________________________________
tab_topline = STRING(format='(A-27,A-27,A-10)'                       ,     $
                       'Start Time','Stop Time','Duration')
aLabel                     = WIDGET_LABEL(xtwBase                    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   value       = tab_topline)

xtwText_EVENT              = {eis_obev_gui_Event}
xtwText_EVENT.eis_obev_gui = Self
xtwText_EVENT.Type         = "XTWTEXT"
Self.xtwTextID                = WIDGET_LIST(xtwBase                  ,     $
                                   value       = 'nowt'              ,     $
                                   ySize       = 20                  ,     $
                                   xSize       = 100                 ,     $
;                                   /SCROLL)
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = xtwText_EVENT)

; ______________________________________________________________________
;
; ETW Text
;
; ______________________________________________________________________
;Self.etwTextID                = WIDGET_TEXT(etwBase                  ,     $
;                                   value       = 'nowt'              ,     $
;                                   ySize       = 30                  ,     $
;                                   xSize       = 100                 ,     $
;                                   /SCROLL)
; ______________________________________________________________________
;
; NGT Text
;
; ______________________________________________________________________
tab_topline = STRING(format='(A-27,A-27,A-10)'                       ,     $
                       'Start Time','Stop Time','Duration')
aLabel                     = WIDGET_LABEL(ngtBase                    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   value       = tab_topline)

ngtText_EVENT              = {eis_obev_gui_Event}
ngtText_EVENT.eis_obev_gui = Self
ngtText_EVENT.Type         = "NGTTEXT"
Self.ngtTextID             = WIDGET_LIST(ngtBase                  ,     $
                                   value       = 'nowt'              ,     $
                                   ySize       = 20                  ,     $
                                   xSize       = 100                 ,     $
;                                   /SCROLL)
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = ngtText_EVENT)
; ______________________________________________________________________
;
; Widget Text
;
; ______________________________________________________________________
tab_topline = STRING(format='(A-27,A-27,A-10)'                       ,     $
                       'Start Time','Stop Time','Duration')
aLabel                     = WIDGET_LABEL(allBase                    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   value       = tab_topline)

allText_EVENT              = {eis_obev_gui_Event}
allText_EVENT.eis_obev_gui = Self
allText_EVENT.Type         = "ALLTEXT"
Self.allTextID             = WIDGET_LIST(allBase                     ,     $
                                   value       = 'nowt'              ,     $
                                   ySize       = 20                  ,     $
                                   xSize       = 100                 ,     $
                                   ;/SCROLL                           ,     $
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = allText_EVENT)





; ______________________________________________________________________
;
; Cursor Time base
;
; ______________________________________________________________________

curtimeBase                = WIDGET_BASE(Self.tlbID                  ,     $
                                   ROW         = 1                   ,     $
                                   FRAME       = 1)
Self.curtimeBase = curtimeBase

startstop_EVENT            = {eis_obev_gui_Event}
startstop_EVENT.eis_obev_gui  = Self
startstop_EVENT.Type       = "STARTSTOP"
Self.startstopID           = WIDGET_DROPLIST(curtimeBase             ,     $
                                   title       = 'Time:'             ,     $
                                   value       = ['Start','Stop']    ,     $
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = startstop_EVENT)

aLabel                     = WIDGET_LABEL(curtimeBASE                ,     $
                                   value       = '   Offset (s):')

offset_EVENT               = {eis_obev_gui_Event}
offset_EVENT.eis_obev_gui  = Self
offset_EVENT.Type          = "OFFSET"
value = ['0','30','60','90','120','1800']
Self.offsetID              = WIDGET_COMBOBOX(curtimeBase             ,     $
                                   /EDITABLE                         ,     $
                                   value       = value               ,     $
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   xSize       = 90                  ,     $
                                   uValue      = startstop_EVENT)

aLabel                     = WIDGET_LABEL(curtimeBASE                ,     $
                                   value       = '  ')

Self.curtimeLabelID        = WIDGET_LABEL(curtimeBASE                ,     $
                                   value       = 'none'              ,     $
                                   /SUNKEN_FRAME                     ,     $
                                   /ALIGN_LEFT                       ,     $
                                   xSize       = 160)

SETCURT_EVENT              = {eis_obev_gui_Event}
SETCURT_EVENT.eis_obev_gui = Self
SETCURT_EVENT.Type         = "SETCURT"
value = ' Set as Cursor Time '
SETCURTbutton              = WIDGET_BUTTON(curtimeBASE               ,     $
                                    value       = value              ,     $
                                    Event_Pro   = 'eis_obev_gui_EV'  ,     $
                                    uValue      = SETCURT_EVENT)

; ______________________________________________________________________
;
; SLIDER base
;
; ______________________________________________________________________
sliderBase                 = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                     $
                                    /FRAME)
Self.sliderBase = sliderBase

value = "Select Buffers (mins):"
aLabel                     = WIDGET_LABEL(sliderBase                 ,     $
                                    value      = value)

SAA_SL_EVENT               = {eis_obev_gui_Event}
SAA_SL_EVENT.eis_obev_gui  = Self
SAA_SL_EVENT.Type          = "SAA_SL"
Self.saa_sliderID           = WIDGET_SLIDER(sliderBase               ,     $
                                   title       = "SAA"               ,     $
                                   value       = 5.0                 ,     $
                                   minimum     = -5.0                ,     $
                                   maximum     = 15.0                ,     $
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = SAA_SL_EVENT)

XTW_SL_EVENT               = {eis_obev_gui_Event}
XTW_SL_EVENT.eis_obev_gui  = Self
XTW_SL_EVENT.Type          = "XTW_SL"
Self.xtw_sliderID          = WIDGET_SLIDER(sliderBase                ,     $
                                   title       = "X-Ray (XTW)"       ,     $
                                   value       = 2.0                 ,     $
                                   minimum     = 0.0                 ,     $
                                   maximum     = 15.0                ,     $
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = XTW_SL_EVENT)

;ETW_SL_EVENT               = {eis_obev_gui_Event}
;ETW_SL_EVENT.eis_obev_gui  = Self
;ETW_SL_EVENT.Type          = "ETW_SL"
;Self.etw_sliderID          = WIDGET_SLIDER(sliderBase                ,     $
;                                   title       = "EUV (ETW)"         ,     $
;                                   value       = 10.0                ,     $
;                                   minimum     = 0.0                 ,     $
;                                   maximum     = 15.0                ,     $
;                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
;                                   uValue      = ETW_SL_EVENT)

NGT_SL_EVENT               = {eis_obev_gui_Event}
NGT_SL_EVENT.eis_obev_gui  = Self
NGT_SL_EVENT.Type          = "NGT_SL"
Self.ngt_sliderID          = WIDGET_SLIDER(sliderBase                ,     $
                                   title       = "Optical (NGT)"     ,     $
                                   value       = 10.0                ,     $
                                   minimum     = 0.0                 ,     $
                                   maximum     = 15.0                ,     $
                                   Event_Pro   = 'eis_obev_gui_EV'   ,     $
                                   uValue      = NGT_SL_EVENT)

DEFAULT_EVENT              = {eis_obev_gui_Event}
DEFAULT_EVENT.eis_obev_gui = Self
DEFAULT_EVENT.Type         = "DEFAULT"
quitbutton                 = WIDGET_BUTTON(sliderBase                ,     $
                                    value       = ' Default '        ,     $
                                    Event_Pro   = 'eis_obev_gui_EV'  ,     $
                                    uValue      = DEFAULT_EVENT)


IGNORESAA_BASE             = WIDGET_BASE(sliderBase                  ,     $
                                    ROW         = 1                  ,     $
                                    /NONEXCLUSIVE)


IGNORESAA_EVENT              = {eis_obev_gui_Event}
IGNORESAA_EVENT.eis_obev_gui = Self
IGNORESAA_EVENT.Type         = "IGNORESAA"
Self.ignore_saa_buttonID     = WIDGET_BUTTON(IGNORESAA_BASE          ,     $
                                    value       = 'Ignore SAA'       ,     $
                                    Event_Pro   = 'eis_obev_gui_EV'  ,     $
                                    uValue      = IGNORESAA_EVENT)

; ______________________________________________________________________
;
; Bottom base
;
; ______________________________________________________________________
bottomBase                  = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                      $
                                    ;/FRAME           ,                      $
                                    /GRID_LAYOUT)

REFRESH_EVENT              = {eis_obev_gui_Event}
REFRESH_EVENT.eis_obev_gui = Self
REFRESH_EVENT.Type         = "REFRESH"
quitbutton                 = WIDGET_BUTTON(bottomBase,                      $
                                    value       = ' Refresh ',              $
                                    Event_Pro   = 'eis_obev_gui_EV',     $
                                    uValue      = REFRESH_EVENT)

QUIT_EVENT                 = {eis_obev_gui_Event}
QUIT_EVENT.eis_obev_gui    = Self
QUIT_EVENT.Type            = "QUIT"
quitbutton                 = WIDGET_BUTTON(bottomBase,                      $
                                    value       = ' Quit ',                  $
                                    Event_Pro   = 'eis_obev_gui_EV',     $
                                    uValue      = QUIT_EVENT)

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_obev_gui_Event
OBJ_DESTROY    , eis_obev_gui_Event.eis_obev_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_obev_gui::CleanUp

PTR_FREE , Self.allLine , Self.xtwLine , Self.saaLine , Self.ngtLine

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::setProperty, start_time = start_time

IF (N_ELEMENTS(start_time) GT 0) THEN BEGIN

    stop

ENDIF

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::GetProperty, tlbID      = tlbID

tlbID      = Self.tlc_saaTextID

END
;______________________________________________________________________________
; Return widget id
;______________________________________________________________________________
FUNCTION eis_obev_gui::tlbID

    RETURN, Self.tlbID
END
;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_obev_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End
;______________________________________________________________________________
; Widget_Info wrapper
;______________________________________________________________________________
FUNCTION eis_obev_gui::Info, _Ref_Extra=Widget_Info_Keywords

Return, Widget_Info(Self.tlbID, _Extra=Widget_Info_Keywords)

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_obev_gui::Realize

Widget_Control, Self.tlbID, /Realize

End

;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_obev_gui::XManager;,group=group

XMANAGER , "eis_obev_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_obev_gui_EV",                                    $
    cleanup        = "eis_obev_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                    $
    /NO_BLOCK

end

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui_Event__Define

    struct = {  eis_obev_gui_Event,                                              $
                Type            : "",        $ ;Unique identifier for event
                eis_obev_gui   : OBJ_NEW(), $ ;Reference to object passed
                extra           : 0          $ ;to - often object associated
             }
END
;______________________________________________________________________________
; Event Handler wrapper
;______________________________________________________________________________
PRO eis_obev_gui_EV , Event

;Get event handler structure containing eis_obev_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_obev_gui_Event
;If a WIDGET_TAB event , just quit
IF (TAG_NAMES(event, /STRUCTURE_NAME) EQ 'WIDGET_TAB') THEN BEGIN
    ;print,'tab event'
    RETURN
ENDIF

;Call eis_obev_gui's class member procedure "Handler"
eis_obev_gui_Event.eis_obev_gui->Handler,Event

END
;______________________________________________________________________________
;
; * THIS is the routine for putting in event handling NOT eis_obev_gui_Ev above.
;
; Event Handler - handle events.  Derived classes must
; contain their own implementation in order to handle events or else pass in an
; procedure name/function name using the Event_Pro/Event_Func keywords to
; CWidget::Init()
;______________________________________________________________________________
PRO eis_obev_gui::Handler, Event

WIDGET_CONTROL, Event.ID, Get_UValue = eis_obev_gui_Event
CASE eis_obev_gui_Event.Type OF
   "QUIT"       : Self->Control, /Destroy
   "REFRESH"    : Self->refresh
   "SAA_SL"     : Self->refresh
   "XTW_SL"     : Self->refresh
   ;"ETW_SL"     : Self->refresh
   "NGT_SL"     : Self->refresh
   "DEFAULT"    : Self->set_defaults
   "IGNORESAA"  : Self->ignore_saa
   "ALLTEXT"    : Self->display_cursor_time
   "SAATEXT"    : Self->display_cursor_time
   "XTWTEXT"    : Self->display_cursor_time
   "NGTTEXT"    : Self->display_cursor_time
   "STARTSTOP"  : Self->display_cursor_time
   "SETCURT"    : Self->set_cursor_time
   ELSE         : print,eis_obev_gui_Event.Type
ENDCASE
END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::set_cursor_time

WIDGET_CONTROL , Self.curtimeLabelID , get_value = cur_time
IF (VALID_TIME(cur_time[0]) EQ 0) THEN BEGIN
    BEEP
    mText = DIALOG_MESSAGE( "Not a valid time. Aborting!"            ,     $
                     DIALOG_PARENT = Self.tlbID                      ,     $
                     /ERROR )
    RETURN
ENDIF

new_cur_time = anytim2tai(cur_time[0])
Self.parentClass->getProperty , tl_graphics = tl_graphics
disp_start   = tl_graphics.axes.x_axis_day_tai
disp_stop    = disp_start + (tl_graphics.axes.x_axis_length * 3600.)
; Clip the new cursor time to limits of display period
new_cur_time = disp_start > new_cur_time < disp_stop


Self.parentClass->updateCursorTime , new_cur_time
Self.parentClass->initialize_plot

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::display_cursor_time

tab_index = WIDGET_INFO(Self.tabBaseID , /TAB_CURRENT)

CASE tab_index OF
   0 :    index = WIDGET_INFO(Self.allTextID,/LIST_SELECT)
   1 :    index = WIDGET_INFO(Self.saaTextID,/LIST_SELECT)
   2 :    index = WIDGET_INFO(Self.xtwTextID,/LIST_SELECT)
   3 :    index = WIDGET_INFO(Self.ngtTextID,/LIST_SELECT)
ENDCASE

; If an entry is NOT selected, then complain and bail out!
IF (index LT 0) THEN BEGIN
    BEEP
    mText = DIALOG_MESSAGE( "Time entry NOT selected. Aborting!"     ,     $
                     DIALOG_PARENT = Self.tlbID                      ,     $
                     /ERROR )
    RETURN
ENDIF

; If it is, then get the relevant line of text
CASE tab_index OF
   0 :    line  = (*Self.allLine)[index]
   1 :    line  = (*Self.saaLine)[index]
   2 :    line  = (*Self.xtwLine)[index]
   3 :    line  = (*Self.ngtLine)[index]
ENDCASE



split = STRSPLIT(line,/EXTRACT)
start_time = STRJOIN([split[0],split[1]] , /SINGLE , ' ')
stop_time  = STRJOIN([split[2],split[3]] , /SINGLE , ' ')

combo_text = WIDGET_INFO(Self.offsetID,/COMBOBOX_GETTEXT)
IF (is_number(combo_text) EQ 1) THEN BEGIN
   combo_value = LONG(combo_text)
ENDIF ELSE BEGIN
   combo_value = 0L
ENDELSE

start_time = anytim2utc(anytim2tai(start_time) + combo_value,/STIME)
stop_time  = anytim2utc(anytim2tai(stop_time)  + combo_value,/STIME)

; Start or Stop?
index = WIDGET_INFO(Self.startStopID,/DROPLIST_SELECT)
IF (index EQ 0) THEN value = start_time ELSE value = stop_time
WIDGET_CONTROL , Self.curtimeLabelID , set_value = value


END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::ignore_saa

saa_ignore = WIDGET_INFO(Self.ignore_saa_buttonID , /BUTTON_SET)
CASE saa_ignore OF
     1    : sensitive = 0
     ELSE : sensitive = 1
ENDCASE

WIDGET_CONTROL , Self.saa_sliderID , sensitive = sensitive
Self->refresh

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::set_defaults

WIDGET_CONTROL , Self.saa_sliderID , set_value = 5
WIDGET_CONTROL , Self.xtw_sliderID , set_value = 2
;WIDGET_CONTROL , Self.etw_sliderID , set_value = 10
WIDGET_CONTROL , Self.ngt_sliderID , set_value = 10
Self->refresh

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::report_no_obev_events_found

WIDGET_CONTROL , Self.allTextID , sensitive = 0 , set_value = ''
WIDGET_CONTROL , Self.saaTextID , sensitive = 0 , set_value = ''
WIDGET_CONTROL , Self.xtwTextID , sensitive = 0 , set_value = ''
WIDGET_CONTROL , Self.ngtTextID , sensitive = 0 , set_value = ''
WIDGET_CONTROL , Self.curtimeLabelID , set_value = 'none'

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_obev_gui::refresh

WIDGET_CONTROL , /HOURGLASS
Self.parentClass->getProperty , tl_graphics = tl_graphics            ,     $
                                obev        = obev
obev = *obev
IF (N_TAGS(obev[0]) EQ 0) THEN BEGIN
   Self->report_no_obev_events_found
   ; De-sensitize widgets if no entries are found!!
   WIDGET_CONTROL , Self.curtimeBase , sensitive = 0
   WIDGET_CONTROL , Self.sliderBase  , sensitive = 0
   RETURN
ENDIF

; Sensitize widgets if entries are present.
WIDGET_CONTROL , Self.curtimeBase , sensitive = 1
WIDGET_CONTROL , Self.sliderBase  , sensitive = 1

; Create a vector whose size is the number of seconds in the interval;
; first_start_time -> last_stop_time
disp_start = MIN(obev[*].start_time)
disp_stop  = MAX(obev[*].stop_time)
series     = BYTARR(disp_stop - disp_start) + 1
nSeries    = N_ELEMENTS(series)
; Get SAA and NGT structure arrays

saa_index_list = WHERE(STRLOWCASE(obev[*].name) EQ 'saa' , count_saa)
ngt_index_list = WHERE(STRLOWCASE(obev[*].name) EQ 'ngt' , count_ngt)
etw_index_list = WHERE(STRLOWCASE(obev[*].name) EQ 'etw' , count_etw)
xtw_index_list = WHERE(STRLOWCASE(obev[*].name) EQ 'xtw' , count_xtw)

WIDGET_CONTROL , Self.saa_sliderID , get_value = saa_buffer_slider
WIDGET_CONTROL , Self.xtw_sliderID , get_value = xtw_buffer_slider
;WIDGET_CONTROL , Self.etw_sliderID , get_value = etw_buffer_slider
WIDGET_CONTROL , Self.ngt_sliderID , get_value = ngt_buffer_slider


saa_ignore = WIDGET_INFO(Self.ignore_saa_buttonID , /BUTTON_SET)
IF (saa_ignore EQ 0) THEN BEGIN

saaLine = ''
FOR i = 0, count_saa - 1 DO BEGIN

;   item = obev[saa_start_time_sort[i]]
   item = obev[saa_index_list[i]]
   start_index = item.start_time - disp_start
   stop_index  = item.stop_time  - disp_start
   stop_index  = stop_index - (stop_index GE nSeries)
   duration    = sec2dhms(item.stop_time-item.start_time)
   IF (start_index GT 0) AND                                               $
      (start_index LE nSeries) THEN BEGIN

;          subtract 5 minutes off start_time
;          add      5 minutes off stop_time
           saa_buffer  = saa_buffer_slider[0] * 60.
           start_line  = start_index
           stop_line   = stop_index
           start_index = start_index - saa_buffer > 0
           stop_index  = stop_index  + saa_buffer < nSeries - 1

           IF (stop_index GT start_index) THEN BEGIN
               series[start_index:stop_index] = 0

               start_time  = anytim2utc(disp_start+start_line,/stime)
               stop_time   = anytim2utc(disp_start+stop_line ,/stime)
               line        = STRJOIN([start_time                     ,     $
                                      stop_time                      ,     $
                                      duration]  ,'    ',/SINGLE)
               CASE STRLEN(saaLine[0]) OF
                   0    : saaLine = line
                   ELSE : saaLine = [saaLine , line]
               ENDCASE

           ENDIF
   ENDIF
ENDFOR
WIDGET_CONTROL , Self.saaTextID , set_value = saaLine
IF (is_blank(saaLine[0]) EQ 1) THEN sensitive = 0 ELSE sensitive = 1
WIDGET_CONTROL , Self.saaTextID , sensitive = sensitive
PTR_FREE , Self.saaLine
IF ~(is_blank(saaLine[0])) THEN Self.saaLine = PTR_NEW(saaLine)

ENDIF

xtwLine = ''
FOR i = 0, count_xtw - 1 DO BEGIN

   item = obev[xtw_index_list[i]]
   start_index = item.start_time - disp_start
   stop_index  = item.stop_time  - disp_start
   stop_index  = stop_index - (stop_index GE nSeries)
   duration    = sec2dhms(item.stop_time-item.start_time)
   IF (start_index GT 0) AND                                               $
      (start_index LE nSeries) THEN BEGIN

;          subtract 10 minutes off start_time
;          add      10 minutes off stop_time
           xtw_buffer  = xtw_buffer_slider[0] * 60.
           start_line  = start_index
           stop_line   = stop_index
           start_index = start_index - xtw_buffer > 0
           stop_index  = stop_index  + xtw_buffer < nSeries - 1

           IF (stop_index GT start_index) THEN BEGIN
               series[start_index:stop_index] = 0

               start_time  = anytim2utc(disp_start+start_line,/stime)
               stop_time   = anytim2utc(disp_start+stop_line ,/stime)
               line        = STRJOIN([start_time                     ,     $
                                      stop_time                      ,     $
                                      duration]  ,'    ',/SINGLE)
               CASE STRLEN(xtwLine[0]) OF
                   0    : xtwLine = line
                   ELSE : xtwLine = [xtwLine , line]
               ENDCASE

           ENDIF
   ENDIF
ENDFOR
WIDGET_CONTROL , Self.xtwTextID , set_value = xtwLine
IF (is_blank(xtwLine[0]) EQ 1) THEN sensitive = 0 ELSE sensitive = 1
WIDGET_CONTROL , Self.xtwTextID , sensitive = sensitive
PTR_FREE , Self.xtwLine
IF ~(is_blank(xtwLine[0])) THEN Self.xtwLine = PTR_NEW(xtwLine)

ngtLine = ''
FOR i = 0, count_ngt - 1 DO BEGIN

   item = obev[ngt_index_list[i]]
   start_index = item.start_time - disp_start
   stop_index  = item.stop_time  - disp_start
   stop_index  = stop_index - (stop_index GE nSeries)
   duration    = sec2dhms(item.stop_time-item.start_time)
   IF (start_index GT 0) AND                                               $
      (start_index LE nSeries) THEN BEGIN

;          subtract 10 minutes off start_time
;          add      10 minutes off stop_time
           ngt_buffer  = ngt_buffer_slider[0] * 60.
           start_line  = start_index
           stop_line   = stop_index
           start_index = start_index - ngt_buffer > 0
           stop_index  = stop_index  + ngt_buffer < nSeries - 1

           IF (stop_index GT start_index) THEN BEGIN
               series[start_index:stop_index] = 0

               start_time  = anytim2utc(disp_start+start_line,/stime)
               stop_time   = anytim2utc(disp_start+stop_line ,/stime)
               line        = STRJOIN([start_time                     ,     $
                                      stop_time                      ,     $
                                      duration]  ,'    ',/SINGLE)
               CASE STRLEN(ngtLine[0]) OF
                   0    : ngtLine = line
                   ELSE : ngtLine = [ngtLine , line]
               ENDCASE
           ENDIF
   ENDIF
ENDFOR
WIDGET_CONTROL , Self.ngtTextID , set_value = ngtLine
IF (is_blank(ngtLine[0]) EQ 1) THEN sensitive = 0 ELSE sensitive = 1
WIDGET_CONTROL , Self.ngtTextID , sensitive = sensitive
PTR_FREE , Self.ngtLine
IF ~(is_blank(ngtLine[0])) THEN Self.ngtLine = PTR_NEW(ngtLine)

;FOR i = 0, count_etw - 1 DO BEGIN
;
;   item = obev[etw_index_list[i]]
;   start_index = item.start_time - disp_start
;   stop_index  = item.stop_time  - disp_start
;   stop_index  = stop_index - (stop_index GE nSeries)
;   duration    = sec2dhms(item.stop_time-item.start_time)
;   IF (start_index GT 0) AND                                               $
;      (start_index LE nSeries) THEN BEGIN
;
;;          subtract 10 minutes off start_time
;;          add      10 minutes off stop_time
;           etw_buffer  = etw_buffer_slider[0] * 60.
;           start_line  = start_index
;           stop_line   = stop_index
;           start_index = start_index - etw_buffer > 0
;           stop_index  = stop_index  + etw_buffer < nSeries - 1
;
;           IF (stop_index GT start_index) THEN BEGIN
;               series[start_index:stop_index] = 0
;
;               start_time  = anytim2utc(disp_start+start_line,/stime)
;               stop_time   = anytim2utc(disp_start+stop_line ,/stime)
;               line        = STRJOIN([start_time                     ,     $
;                                      stop_time                      ,     $
;                                      duration]  ,'    ',/SINGLE)
;               WIDGET_CONTROL , Self.etwTextID , set_value = line , /APPEND
;           ENDIF
;   ENDIF
;ENDFOR

buffered    = [0 , series , 0]
whereChange = WHERE(buffered[1:*] NE buffered, count)
whereChange = REFORM(whereChange, 2, count/2, /Overwrite)
duration    = REFORM(whereChange[1,*] - whereChange[0,*])

allLine = STRARR(N_ELEMENTS(duration))

FOR i = 0, N_ELEMENTS(duration) - 1 DO BEGIN

   start_time = anytim2utc(whereChange[0,i]+disp_start,/stime)
   stop_time  = anytim2utc(whereChange[1,i]+disp_start,/stime)
   line       = STRJOIN([start_time                      ,$
                         stop_time                       ,$
                         sec2dhms(duration[i])],'    ',/SINGLE)
   allLine[i] = line
ENDFOR
WIDGET_CONTROL , Self.allTextID , set_value = allLine
IF (is_blank(allLine[0]) EQ 1) THEN sensitive = 0 ELSE sensitive = 1
WIDGET_CONTROL , Self.allTextID , sensitive = sensitive
PTR_FREE , Self.allLine
IF ~(is_blank(allLine[0])) THEN Self.allLine = PTR_NEW(allLine)

; OK, want to set top line to zero. Also, set list selection to zero
; if there are any entries. If not, then set to -1.
allLine_select = PTR_VALID(Self.allLine) - 1
saaLine_select = PTR_VALID(Self.saaLine) - 1
xtwLine_select = PTR_VALID(Self.xtwLine) - 1
ngtLine_select = PTR_VALID(Self.ngtLine) - 1

WIDGET_CONTROL , Self.allTextID , set_list_top    = 0                ,     $
                                  set_list_select = allLine_select
WIDGET_CONTROL , Self.saaTextID , set_list_top    = 0                ,     $
                                  set_list_select = saaLine_select
WIDGET_CONTROL , Self.xtwTextID , set_list_top    = 0                ,     $
                                  set_list_select = xtwLine_select
WIDGET_CONTROL , Self.ngtTextID , set_list_top    = 0                ,     $
                                  set_list_select = ngtLine_select
                                  
;WIDGET_CONTROL , Self.etwTextID , set_list_top = 0 , set_list_select = 0



WIDGET_CONTROL , Self.curtimeLabelID , set_value = 'Hammer Time'


END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_obev_gui__define

    struct   = { eis_obev_gui                                        ,     $
                   tlbID                : 0L                         ,     $
                   group_leader         : 0L                         ,     $
                   parentClass          : OBJ_NEW()                  ,     $
                   allLine              : PTR_NEW()                  ,     $
                   xtwLine              : PTR_NEW()                  ,     $
                   saaLine              : PTR_NEW()                  ,     $
                   ngtLine              : PTR_NEW()                  ,     $
                   curtimeBase          : 0L                         ,     $
                   sliderBase           : 0L                         ,     $
                   tabBaseID            : 0L                         ,     $
                   saaTextID            : 0L                         ,     $
                   xtwTextID            : 0L                         ,     $
                   etwTextID            : 0L                         ,     $
                   ngtTextID            : 0L                         ,     $
                   saa_sliderID         : 0L                         ,     $
                   xtw_sliderID         : 0L                         ,     $
                   etw_sliderID         : 0L                         ,     $
                   ngt_sliderID         : 0L                         ,     $
                   ignore_saa_buttonID  : 0L                         ,     $
                   startstopID          : 0L                         ,     $
                   offsetID             : 0L                         ,     $
                   curtimeLabelID       : 0L                         ,     $
                   allTextID            : 0L                              $
               }


END