;+
; NAME
;          eis_xrt_flare_gui
;
; PURPOSE
;          This (object) GUI allows the user/planner to edit the EIS Event
;          trigger control parameters.
;
; INPUTS
;          parentClass - object reference of parent object gui
;
; WRITTEN
;          John Rainnie, RAL, some time ago now
;
; HISTORY
;          v0.1 JAR 2-Apr-2008
;          v0.2 JAR 9-Jan-2011
;               Set x0 to -35
;          v0.3 JAR 19-Jan-2012
;               Updated XRTFlare gui design to force different states of
;               pointing widgets when verify_fov is toggled. Also made
;               it modal.
;          v0.4 JAR 30-Jul-2012.
;               Change default x0 value from -35 to -36
;               Change default y0 value from +45 to +16
;          v0.5 JAR 07-Aug-2012.
;               Inherit xcen and ycen pointing (ra1_xcen, ra1_ycen)
;               from hunter study. Also includes MIP, YIP and s/c
;               pointing.
;          v0.6 JAR 12-Dec-2012.
;               Change default x0 value from -36 to +36
;               Change default y0 value from +16 to -18
;          v0.7 JAR 01-Jun-2013
;               Get x0 and y0 default values from function;
;               "eis_get_default_x0y0"
;          v0.8 JAR 11-Feb-2014
;               + Fixed case where a study whose id = 0 existed
;                 (only at MSSL)
;               + Disabled use of filler study functionality
;               + Read, check and save x0 and y0 values when "Done", so that
;                 the user doesn't have to press enter.
;          v0.9 JAR 04-Dec-2014
;               When toggling "Verify XRT in FOV", update the response
;               study pointing by re-inheriting those of the hunter.
;
;-
;______________________________________________________________________________
FUNCTION eis_xrt_flare_gui::Init , parentClass , resp_id , ra_index

; Check resp_id is valid. If not, bail out
r = db_read_response_entry(id=resp_id)
IF (N_TAGS(r) EQ 0) THEN BEGIN
   BEEP
   mText = "This Response Entry does not exist. Bailing out!"
   complain = DIALOG_MESSAGE( mText ,                        $
                              DIALOG_PARENT = Self.tlbID,                $
                              /ERROR )
   RETURN , 0
ENDIF

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

;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = 'XRT Flare Trigger Control Parameters' + pt_version
TLB_EVENT                   = {eis_xrt_flare_gui_Event}
TLB_EVENT.eis_xrt_flare_gui = Self
TLB_EVENT.Type              = "TLB"
Self.tlbID                  = WIDGET_BASE(                                  $
                                    TITLE       = widTitle,                 $
                                    COLUMN      = 1,                        $
                                    /FLOATING      ,                        $
                                    /MODAL         ,                        $
                                    GROUP_LEADER = Self.group_leader ,      $
                                    UValue      = TLB_EVENT,                $
                                    xOffset     = 800)

;-------------------create the GUI------------------------------;
Self->createGUI
Self->get_response_db_values

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_xrt_flare_gui::createGUI

Event_Pro = 'eis_xrt_flare_gui_EV'

;______________________________________________________________________________
; Hunter Study Selection
;______________________________________________________________________________
HunterStudyBaseID          = WIDGET_BASE(Self.tlbID                  ,     $
                                   COLUMN      = 1                   ,     $
                                   FRAME       = 1)

;______________________________________________________________________________
; X0 and Y0
;______________________________________________________________________________
xy0Base                    = WIDGET_BASE(HunterStudyBaseID                 ,     $
                                   ROW         = 1)

aLabel                     = WIDGET_LABEL(xy0Base                      ,   $
                                    value       = ' EIS-XRT Offset: '  ,   $
                                    ;xSize       = 30                  ,   $
                                    /ALIGN_LEFT)

aLabel                     = WIDGET_LABEL(xy0Base                      ,   $
                                    value       = ' X0:'               ,   $
                                    xSize       = 30                   ,   $
                                    /ALIGN_LEFT)


X0_EVENT                   = {eis_xrt_flare_gui_Event}
X0_EVENT.eis_xrt_flare_gui = Self
X0_EVENT.Type              = "X0_EVENT"
Self.x0ID                  = WIDGET_TEXT(xy0Base                       ,   $
                                   value       = '0'                   ,   $
                                   Event_Pro   = Event_Pro             ,   $
                                   xSize       = 6                     ,   $
                                   /EDITABLE                           ,   $
                                   uValue      = X0_EVENT)

aLabel                     = WIDGET_LABEL(xy0Base                      ,   $
                                    value       = ' Y0:'               ,   $
                                    xSize       = 30                   ,   $
                                    /ALIGN_LEFT)

Y0_EVENT                   = {eis_xrt_flare_gui_Event}
Y0_EVENT.eis_xrt_flare_gui = Self
Y0_EVENT.Type              = "Y0_EVENT"
Self.y0ID                  = WIDGET_TEXT(xy0Base                       ,   $
                                   value       = '0'                   ,   $
                                   Event_Pro   = Event_Pro             ,   $
                                   xSize       = 6                     ,   $
                                   /EDITABLE                           ,   $
                                   uValue      = Y0_EVENT)

;value = "  (Allowed range is -4095 to 4095)"
x0y0      = eis_get_default_x0y0()
x0y0_text = STRJOIN([x0y0[0],x0y0[1]],'/',/SINGLE)
value     = STRJOIN(['  Default values are '    ,               $
                  STRCOMPRESS(x0y0_text,/REMOVE_ALL),           $
                  ' when Verify FOV is enabled'],/SINGLE)
aLabel                     = WIDGET_LABEL(xy0Base                      ,   $
                                    value       = value                ,   $
                                    ;xSize       = 30                  ,   $
                                    /ALIGN_LEFT)



;______________________________________________________________________________
; Response Study Selection
;______________________________________________________________________________
RespSTBaseID          = WIDGET_BASE(Self.tlbID                         ,   $
                                   COLUMN      = 1                     ,   $
                                   FRAME       = 1)

;______________________________________________________________________________
; Response Study Selection
;______________________________________________________________________________
R_STBaseID                 = WIDGET_BASE(RespSTBaseID                  ,   $
                                   ROW         = 1)

R_ST_EVENT                 = {eis_xrt_flare_gui_Event}
R_ST_EVENT.eis_xrt_flare_gui   = Self
R_ST_EVENT.Type            = "RESPONSE_STUDY"
value = ' Select Response study... '
response_st_button         = WIDGET_BUTTON(R_STBaseID                  ,   $
                                   value       = value                 ,   $
                                   Event_Pro   = Event_Pro             ,   $
                                   uValue      = R_ST_EVENT            ,   $
                                   ;xSize       = 150                  ,   $
                                   ySize       = 30)

aLabel                     = WIDGET_LABEL(R_STBaseID                   ,   $
                                    value       = '  ID: '             ,   $
                                    xSize       = 50                   ,   $
                                    /ALIGN_LEFT)

Self.rst_idID              = WIDGET_LABEL(R_STBaseID                   ,   $
                                    value       = idValue              ,   $
                                    xSize       = 50                   ,   $
                                    /ALIGN_LEFT)

aLabel                     = WIDGET_LABEL(R_STBaseID                   ,   $
                                    value       = ' Acronym:  '        ,   $
                                    /ALIGN_LEFT)

Self.rst_acronymID         = WIDGET_LABEL(R_STBaseID                   ,   $
                                    value       = '-'                  ,   $
                                    xSize       = 220                  ,   $
                                    /ALIGN_LEFT)



;______________________________________________________________________________
; Repoint Raster Selection
;______________________________________________________________________________
RABaseID                   = WIDGET_BASE(RespSTBaseID                  ,   $
                                   ROW         = 1)

value = ' Repoint Raster: '
aLabel                     = WIDGET_LABEL(RABaseID                     ,   $
                                   value       = value                 ,   $
                                   xSize       = 100                   ,   $
                                   /ALIGN_LEFT)

RA_EVENT                   = {eis_xrt_flare_gui_Event}
RA_EVENT.eis_xrt_flare_gui = Self
RA_EVENT.Type              = "RA_SELECT"
ra_list = '                                                          '
Self.raDroplistID          = WIDGET_DROPLIST(RABaseID                  ,   $
                                   value       = ra_list               ,   $
                                   Event_Pro   = Event_Pro             ,   $
                                   uValue      = RA_EVENT              ,   $
                                   xSize       = 600                   ,   $
                                   sensitive   = 0)
WIDGET_CONTROL , Self.raDroplistID , set_droplist_select = 0


; Repoint Base
;______________________________________________________________________________
RepointBaseID              = WIDGET_BASE(RespSTBaseID                  ,   $
                                   COLUMN      = 1                     ,   $
                                   FRAME       = 1)

Self.RepointBaseID = RepointBaseID
;---------------------------------------------------------------------------
;
; Create Repoint X & Y widgets
;
;---------------------------------------------------------------------------

;______________________________________________________________________________
; Check for XRT in EIS FOV
;______________________________________________________________________________
xrtFovBase                 = WIDGET_BASE(RepointBaseID           ,     $
                                   ROW         = 1                   ,     $
                                   /NONEXCLUSIVE)

XRT_FOV_EVENT              = {eis_xrt_flare_gui_Event}
XRT_FOV_EVENT.eis_xrt_flare_gui = Self
XRT_FOV_EVENT.Type         = "XRT_FOV"
value = 'Verify XRT Flare is in the EIS FOV'
Self.xrt_eisFovID           = WIDGET_BUTTON(xrtFovBase  ,                   $
                                    value       = value               ,     $
                                    Event_Pro   = Event_Pro           ,     $
                                    uValue      = XRT_FOV_EVENT ,           $
                                    SENSITIVE   = 1)
;
; X
;
X_RA1PointingBaseID        = WIDGET_BASE(RepointBaseID                 ,   $
                                   ROW         = 1)

x_value = '           X Repoint:'
aLabel                     = WIDGET_LABEL(X_RA1PointingBaseID     ,     $
                                   value       = x_value          ,     $
                                   /ALIGN_LEFT)

;REPOINT_X_EVENT            = {eis_xrt_flare_gui_Event}
;REPOINT_X_EVENT.eis_xrt_flare_gui = Self
;REPOINT_X_EVENT.Type       = "X_REPOINT"
;value = ['Manual','Automatic (default)']
;Self.x_repointID            = WIDGET_DROPLIST(X_RA1PointingBaseID     ,     $
;                                   value       = value               ,     $
;                                   Event_Pro   = Event_Pro           ,     $
;                                   uValue      = REPOINT_X_EVENT)
Self.x_repointID           = WIDGET_LABEL(X_RA1PointingBaseID          ,   $
                                   value       = 'Automatic (default)' ,   $
                                   /ALIGN_LEFT)


X_RA1PointingRHSBaseID       = WIDGET_BASE(X_RA1PointingBaseID           ,     $
                                   ROW         = 1)

Self.X_RA1PointingRHSBaseID = X_RA1PointingRHSBaseID


x_value = 'X:'
aLabel                     = WIDGET_LABEL(X_RA1PointingRHSBaseID     ,     $
                                   value       = x_value)


; Manual Repointing
RA1_X_EVENT                = {eis_xrt_flare_gui_Event}
RA1_X_EVENT.eis_xrt_flare_gui  = Self
RA1_X_EVENT.Type           = "RA1_X"
Self.ra1_xID               = WIDGET_TEXT(X_RA1PointingRHSBaseID           ,     $
                                   value       = '0'                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   xSize       = 11                  ,     $
                                   /EDITABLE                         ,     $
                                   ;/ALL_EVENTS                       ,     $
                                   uValue      = RA1_X_EVENT)

x_value = ' (solar coordinates)'
aLabel                     = WIDGET_LABEL(X_RA1PointingRHSBaseID     ,     $
                                   value       = x_value)

;
; Y
;
Y_RA1PointingBaseID          = WIDGET_BASE(RepointBaseID                ,     $
                                   ROW         = 1)

y_value = '           Y Repoint:'
aLabel                     = WIDGET_LABEL(Y_RA1PointingBaseID     ,     $
                                   value       = y_value          ,     $
                                   /ALIGN_LEFT)

;REPOINT_Y_EVENT            = {eis_xrt_flare_gui_Event}
;REPOINT_Y_EVENT.eis_xrt_flare_gui = Self
;REPOINT_Y_EVENT.Type       = "Y_REPOINT"
;value = ['Manual','Automatic (default)']
;Self.y_repointID            = WIDGET_DROPLIST(Y_RA1PointingBaseID     ,     $
;                                   value       = value               ,     $
;                                   Event_Pro   = Event_Pro           ,     $
;                                   uValue      = REPOINT_Y_EVENT)
Self.y_repointID           = WIDGET_LABEL(Y_RA1PointingBaseID          ,   $
                                   value       = 'Automatic (default)' ,   $
                                   /ALIGN_LEFT)


Y_RA1PointingRHSBaseID       = WIDGET_BASE(Y_RA1PointingBaseID           ,     $
                                   ROW         = 1)

Self.Y_RA1PointingRHSBaseID = Y_RA1PointingRHSBaseID

y_value = 'Y:'
aLabel                     = WIDGET_LABEL(Y_RA1PointingRHSBaseID     ,     $
                                   value       = y_value)


; Manual Repointing
RA1_Y_EVENT                = {eis_xrt_flare_gui_Event}
RA1_Y_EVENT.eis_xrt_flare_gui  = Self
RA1_Y_EVENT.Type           = "RA1_Y"
Self.ra1_yID               = WIDGET_TEXT(Y_RA1PointingRHSBaseID           ,     $
                                   value       = '0'                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   xSize       = 11                  ,     $
                                   /EDITABLE                         ,     $
                                   ;/ALL_EVENTS                       ,     $
                                   uValue      = RA1_Y_EVENT)

y_value = ' (solar coordinates)'
aLabel                     = WIDGET_LABEL(Y_RA1PointingRHSBaseID     ,     $
                                   value       = x_value)


; Filler Study Selection
;______________________________________________________________________________
FillSTBaseID          = WIDGET_BASE(RespSTBaseID                  ,     $
                                   COLUMN      = 1                   ,     $
                                   FRAME       = 1)

;______________________________________________________________________________
; Filler Study Toggle
;______________________________________________________________________________
fillerOnBaseID             = WIDGET_BASE(FillSTBaseID                 ,     $
                                   ROW         = 1)

nonExcBaseID               = WIDGET_BASE(fillerOnBaseID               ,     $
                                   ROW         = 1                   ,     $
                                   /NONEXCLUSIVE)

F_OFF_EVENT                = {eis_xrt_flare_gui_Event}
F_OFF_EVENT.eis_xrt_flare_gui  = Self
F_OFF_EVENT.Type           = "FILLER_OFF"
value = 'Use a Filler study'
Self.fillerOffButtonID     = WIDGET_BUTTON(nonExcBaseID              ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = F_OFF_EVENT         ,     $
                                   SENSITIVE   = 0)



;______________________________________________________________________________
; Filler Study Selection
;______________________________________________________________________________
F_STBaseID                 = WIDGET_BASE(FillSTBaseID                ,     $
                                   ROW         = 1)

Self.F_STBaseID = F_STBaseID

F_ST_EVENT                 = {eis_xrt_flare_gui_Event}
F_ST_EVENT.eis_xrt_flare_gui   = Self
F_ST_EVENT.Type            = "FILLER_STUDY"
value = ' Select Filler study...  '
filler_st_button           = WIDGET_BUTTON(F_STBaseID                ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = F_ST_EVENT          ,     $
                                   ;xSize       = 150                 ,     $
                                   ySize       = 30)

aLabel                     = WIDGET_LABEL(F_STBaseID                 ,      $
                                    value       = '  ID: '          ,      $
                                    xSize       = 50 ,                      $
                                    /ALIGN_LEFT)

Self.fst_idID              = WIDGET_LABEL(F_STBaseID ,                $
                                    value       = idValue ,                $
                                    xSize       = 50 ,                      $
                                    /ALIGN_LEFT)

aLabel                     = WIDGET_LABEL(F_STBaseID ,                $
                                    value       = ' Acronym:  ' ,      $
                                    /ALIGN_LEFT)

Self.fst_acronymID         = WIDGET_LABEL(F_STBaseID ,                $
                                    value       = '-' ,                   $
                                    xSize       = 220 ,                     $
                                    /ALIGN_LEFT)

;______________________________________________________________________________
; Bottom Base
;______________________________________________________________________________
bottomBaseID               = WIDGET_BASE(Self.tlbID                    ,   $
                                   ROW         = 1                     ,   $
                                   FRAME       = 1                     ,   $
                                   /GRID_LAYOUT)
;---------------------------------------------------------------------------
;
; ACCEPT button
;
;---------------------------------------------------------------------------
ACCEPT_EVENT               = {eis_xrt_flare_gui_Event}
ACCEPT_EVENT.eis_xrt_flare_gui = Self
ACCEPT_EVENT.Type          = "ACCEPT"
value = ' Done '
ACCEPTbutton               = WIDGET_BUTTON(bottomBaseID              ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = ACCEPT_EVENT)

ACCEPT_EVENT               = {eis_xrt_flare_gui_Event}
ACCEPT_EVENT.eis_xrt_flare_gui = Self
ACCEPT_EVENT.Type          = "STRUCT"
value = ' Check structure '
ACCEPTbutton               = WIDGET_BUTTON(bottomBaseID              ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = ACCEPT_EVENT)

END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_xrt_flare_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_xrt_flare_gui_Event
OBJ_DESTROY    , eis_xrt_flare_gui_Event.eis_xrt_flare_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_xrt_flare_gui::CleanUp


END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_xrt_flare_gui::setProperty , x_repoint   = x_repoint         ,     $
                                     y_repoint   = y_repoint

IF (N_ELEMENTS(x_repoint) GT 0) THEN BEGIN
   stop
   ;Self.eis_xrt_flare_control.adjust_x = x_repoint
ENDIF

IF (N_ELEMENTS(y_repoint) GT 0) THEN BEGIN
   stop
   ;Self.eis_xrt_flare_control.adjust_y = y_repoint
ENDIF

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_xrt_flare_gui::GetProperty, eis_xrt_flare_control = eis_xrt_flare_control

stop
eis_xrt_flare_control = Self.eis_xrt_flare_control

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

RETURN , Self.tlbID

END
;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_xrt_flare_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End

;______________________________________________________________________________
; Widget_Info wrapper
;______________________________________________________________________________
Function eis_xrt_flare_gui::Info, _Ref_Extra=Widget_Info_Keywords

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

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_xrt_flare_gui::Realize

Widget_Control, Self.tlbID, /Realize

End

;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_xrt_flare_gui::XManager

XMANAGER , "eis_xrt_flare_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_xrt_flare_gui_EV",                                    $
    cleanup        = "eis_xrt_flare_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                   $
    /NO_BLOCK

END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_xrt_flare_gui_Event__Define

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

;Get event handler structure containing eis_xrt_flare_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_xrt_flare_gui_Event

;Call eis_xrt_flare_gui's class member procedure "Handler"
eis_xrt_flare_gui_Event.eis_xrt_flare_gui->Handler,Event

END
;______________________________________________________________________________
;
; * THIS is the routine for putting in event handling NOT
; eis_xrt_flare_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_xrt_flare_gui::Handler, Event

WIDGET_CONTROL, Event.ID, Get_UValue = eis_xrt_flare_gui_Event
CASE eis_xrt_flare_gui_Event.Type OF
    "ACCEPT"          : Self->accept
    "RESPONSE_STUDY"  : eis_xstudy , parentClass = Self , /MODAL
    "FILLER_STUDY"    : Self->get_filler_study
    "FILLER_OFF"      : Self->filler_study_off
    "RA_SELECT"       : Self->select_raster , Event
    "X_REPOINT"       : ;Self->update_adjust_x , event
    "Y_REPOINT"       : ;Self->update_adjust_y , event
    "LOCATE_X"        : Self->update_locate_x , event
    "LOCATE_Y"        : Self->update_locate_y , event
    "XRT_FOV"         : Self->toggle_verify_fov
    "STRUCT"          : Self->help_struct
    "RA1_X"           : Self->update_ra1_x
    "RA1_Y"           : Self->update_ra1_y
    "X0_EVENT"        : Self->update_xy0 , 'x'
    "Y0_EVENT"        : Self->update_xy0 , 'y'
    ELSE              : print,eis_xrt_flare_gui_Event.Type
ENDCASE

END
;______________________________________________________________________________
; Update x0 value
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_xy0 , x_or_y

; OK, get the x0 and y0 values currently stored in the response DB
priv          = !PRIV
!PRIV         = 2
status        = eis_open_db(/RESPONSE)
x0_old_value  = dbval(Self.resp_id, "LOCATE_X")
y0_old_value  = dbval(Self.resp_id, "LOCATE_Y")
dbclose
!PRIV         = priv

; Get x0 and y0 values from text box widgets
IF (x_or_y EQ 'x') THEN BEGIN
   widID     = Self.x0ID
   old_value = x0_old_value
ENDIF ELSE BEGIN
   widID     = Self.y0ID
   old_value = y0_old_value
ENDELSE
WIDGET_CONTROL , widID , get_value = value
new_value  = value[0]

; Check the value is actually a number
IF ~is_number(new_value) THEN BEGIN
   mText = STRJOIN([x_or_y,'0 is not a valid number. Aborting!'],/SINGLE)
   BEEP
   complain = DIALOG_MESSAGE( mText                                    ,   $
                              DIALOG_PARENT = Self.tlbID , /ERROR )
   RETURN
ENDIF

; Recast value to LONG
new_value = LONG(new_value)

; Ignore if the value hasn't changed
IF (new_value EQ old_value) THEN RETURN

; Check value is in range; -4095 -> +4095
IF (new_value LT -4095) OR (new_value GT 4095) THEN BEGIN
   mText = STRJOIN([x_or_y,                                                $
           '0 is out of range [-4095:4095]. Aborting!'],/SINGLE)
   BEEP
   complain = DIALOG_MESSAGE(mText , DIALOG_PARENT = Self.tlbID , /ERROR)
   RETURN
ENDIF

; Tag name is either "LOCATE_X" or "LOCATE_Y"
dbtagname = STRJOIN(['LOCATE_',STRUPCASE(x_or_y)],/SINGLE)


; OK, the new x0 or y0 value is valid. Now save it in the response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, dbtagname , new_value , Self.resp_id
dbclose
!PRIV = priv


END
;______________________________________________________________________________
; OK, check and update x0 and y0 values
;______________________________________________________________________________
FUNCTION eis_xrt_flare_gui::check_and_update_xy0

; Get x0 and y0 values from text box widgets
WIDGET_CONTROL , Self.x0ID , get_value = x0_value
WIDGET_CONTROL , Self.y0ID , get_value = y0_value
x0_new_value  = x0_value[0]
y0_new_value  = y0_value[0]

; Check the values are actually numbers
IF ~is_number(x0_new_value) OR ~is_number(y0_new_value) THEN BEGIN
   mText = "EIS-XRT Offsets are not valid numbers. Aborting!"
   BEEP
   complain = DIALOG_MESSAGE( mText                                    ,   $
                              DIALOG_PARENT = Self.tlbID , /ERROR )
   RETURN , 0
ENDIF

; Recast new values from strings into longs
x0_new_value = LONG(x0_new_value)
y0_new_value = LONG(y0_new_value)

; Check they are in range; -4095 -> +4095
IF (x0_new_value LT -4095) OR (x0_new_value GT 4095) OR            $
   (y0_new_value LT -4095) OR (y0_new_value GT 4095) THEN BEGIN
   mText = "EIS-XRT Offsets are out of range. Aborting!"
   BEEP
   complain = DIALOG_MESSAGE( mText                                    ,   $
                              DIALOG_PARENT = Self.tlbID , /ERROR )
   RETURN , 0
ENDIF

; OK, get the x0 and y0 values currently stored in the response DB
priv          = !PRIV
!PRIV         = 2
status        = eis_open_db(/RESPONSE)
x0_old_value  = dbval(Self.resp_id, "LOCATE_X")
y0_old_value  = dbval(Self.resp_id, "LOCATE_Y")
dbclose
!PRIV         = priv

; OK, the new x0 and y0 values are valid. Now save it in the response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, "LOCATE_X" , x0_new_value , Self.resp_id
dbput, "LOCATE_Y" , y0_new_value , Self.resp_id
dbclose
!PRIV = priv

RETURN , 1

END
;______________________________________________________________________________
; OK, get raster X position
;______________________________________________________________________________
PRO eis_xrt_flare_gui::help_struct

r = db_read_response_entry(id = Self.resp_id)
print,'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
help,r,/str
print,'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'

END
;______________________________________________________________________________
; OK, get updated MIP and YIP values
;______________________________________________________________________________
PRO eis_xrt_flare_gui::get_new_mip_and_yip

; Get ra_id and pointing info from response DB
r        = db_read_response_entry(id = Self.resp_id)
ra_id    = r.raster_identifier
ra1_xcen = r.ra1_xcen
ra1_ycen = r.ra1_ycen
ra1_sc_x = r.ra1_sc_x
ra1_sc_y = r.ra1_sc_y

; Get differences in x and y
delx = r.ra1_sc_x - r.ra1_xcen
dely = r.ra1_sc_y - r.ra1_ycen

; Calculate new mip and yip
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; JAR 1-Apr-2014. Add "obs_id" and "aia_time" inputs to routine
; "eis_get_mirror_position"
; Using hunter study seq_id, get obs_id and start time of that TL entry
tl       = eis_get_tl_entry(r.trig_st_seqid)
obs_id   = tl.science_component.obs_id
aia_time = tl.time_component.start_time
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; JAR 27-Aug-2014. Get sot_offsets from tl.sc.disturbance
sot_offset = tl.science_component.disturbance
sot_offset = FLOAT(STRSPLIT(sot_offset,',',/EXTRACT))

dummy = eis_get_mirror_position(delx , dely , ra_id ,       $
                          obs_id , aia_time , sot_offset)

; Save results to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RA1_MIP' , dummy.mip    , Self.resp_id
dbput, 'RA1_YIP' , dummy.yStart , Self.resp_id
dbclose
!PRIV = priv

END
;______________________________________________________________________________
; OK, get raster X position
;______________________________________________________________________________
FUNCTION eis_xrt_flare_gui::check_and_update_xycen

; If verify_fov is on (default condition) then nothing to do here
button_set = WIDGET_INFO(Self.xrt_eisFovID , /BUTTON_SET)
IF (button_set EQ 1) THEN RETURN , 1

; Get solar coordinates from widget text boxes
WIDGET_CONTROL , Self.ra1_xID , get_value = new_x
WIDGET_CONTROL , Self.ra1_yID , get_value = new_y

; Check the values are actually numbers
IF ~is_number(new_x[0]) OR ~is_number(new_y[0]) THEN BEGIN
   mText = "X/Y Repoint (solar) Coordinates are not valid numbers. Aborting!"
   BEEP
   complain = DIALOG_MESSAGE( mText                                    ,   $
                              DIALOG_PARENT = Self.tlbID , /ERROR )
   RETURN , 0
ENDIF

; Save results to response DB (recast as floating point numbers)
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RA1_XCEN' , FLOAT(new_x[0]) , Self.resp_id
dbput, 'RA1_YCEN' , FLOAT(new_y[0]) , Self.resp_id
dbclose
!PRIV = priv

; Calculate and store the corresponding MIP value - ONLY if a response
; study has been selected (needs raster ID)
status = eis_open_db(/RESPONSE)
ra_id  = dbval(Self.resp_id, 'RESPONSE_RA_ID')
dbclose
IF (ra_id GE 1) THEN Self->get_new_mip_and_yip

RETURN , 1

END
;______________________________________________________________________________
; OK, get raster X position
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_ra1_x

WIDGET_CONTROL , Self.ra1_xID , get_value = newValue

; Get original value from Response DB
priv = !PRIV
!PRIV = 2
status   = eis_open_db(/RESPONSE)
ra1_xcen = dbval(Self.resp_id, 'RA1_XCEN')
dbclose
!PRIV = priv

; arguments-> name , newValue , oldValue , minValue , maxValue , multiple
valid  = eis_validate_input('X_POINT' , newValue[0] ,                     $
                           ra1_xcen , -2000. , 2000.)
CASE valid.valid OF
    0:  BEGIN
            ;newValue = Self.eis_xrt_flare_control.ra1_xcen
            newValue = ra1_xcen
            BEEP
            complain = DIALOG_MESSAGE( valid.mText ,                        $
                                 DIALOG_PARENT = Self.tlbID,                $
                                 /ERROR )
        END
    1:  newValue = FLOAT(newValue[0])
ENDCASE

; Save results to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RA1_XCEN' , newValue , Self.resp_id
dbclose
!PRIV = priv

;Self.eis_xrt_flare_control.ra1_xcen = newValue

; Update widget value
x_point = STRTRIM(STRING(format='(F-10.2)',newValue),2)
WIDGET_CONTROL , Self.ra1_xID , set_value = x_point

; Calculate and store the corresponding MIP value - ONLY if a response
; study has been selected (needs raster ID)
status = eis_open_db(/RESPONSE)
ra_id  = dbval(Self.resp_id, 'RESPONSE_RA_ID')
dbclose

IF (ra_id GE 1) THEN Self->get_new_mip_and_yip

END
;______________________________________________________________________________
; OK, get raster Y position
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_ra1_y

WIDGET_CONTROL , Self.ra1_yID , get_value = newValue

; Get original value from Response DB
priv = !PRIV
!PRIV = 2
status   = eis_open_db(/RESPONSE)
ra1_ycen = dbval(Self.resp_id, 'RA1_YCEN')
dbclose
!PRIV = priv

; arguments-> name , newValue , oldValue , minValue , maxValue , multiple
valid  = eis_validate_input('Y_POINT' , newValue[0] ,                     $
                           ra1_ycen , -2000. , 2000.)
CASE valid.valid OF
    0:  BEGIN
            newValue = ra1_ycen
            BEEP
            complain = DIALOG_MESSAGE( valid.mText ,                        $
                                 DIALOG_PARENT = Self.tlbID,                $
                                 /ERROR )
        END
    1:  newValue = FLOAT(newValue[0])
ENDCASE

; Save results to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RA1_YCEN' , newValue , Self.resp_id
dbclose
!PRIV = priv

;Self.eis_xrt_flare_control.ra1_ycen = newValue

; Update widget value
y_point = STRTRIM(STRING(format='(F-10.2)',newValue),2)
WIDGET_CONTROL , Self.ra1_yID , set_value = y_point

; Calculate and store the corresponding YIP value - ONLY if a response
; study has been selected (needs raster ID)
status = eis_open_db(/RESPONSE)
ra_id  = dbval(Self.resp_id, 'RESPONSE_RA_ID')
dbclose

IF (ra_id GE 1) THEN Self->get_new_mip_and_yip

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_xrt_flare_gui::get_response_db_values

; Does an entry exist in the RESPONSE DB? Yes. Always!!!! Now.
r = db_read_response_entry(id = Self.resp_id)

; update x0 and y0 values
x0_text = STRCOMPRESS(r.locate_x,/REMOVE_ALL)
y0_text = STRCOMPRESS(r.locate_y,/REMOVE_ALL)
;print,STRJOIN([x0_string,y0_string],',',/SINGLE)
WIDGET_CONTROL , Self.x0ID , set_value = x0_text
WIDGET_CONTROL , Self.y0ID , set_value = y0_text

; Response study values
Self->update_response_study
; Filler study values
Self->update_filler_study

; Verify_fov button
verify_fov = r.line_index
WIDGET_CONTROL , Self.xrt_eisFovID , set_button = verify_fov

; Pointing Info
ra1_xcen_text = STRTRIM(STRING(format='(F-8.2)',r.ra1_xcen),2)
ra1_ycen_text = STRTRIM(STRING(format='(F-8.2)',r.ra1_ycen),2)
WIDGET_CONTROL , Self.ra1_xID , set_value = ra1_xcen_text
WIDGET_CONTROL , Self.ra1_yID , set_value = ra1_ycen_text

; Adjust_x and adjust_y - depends on verify_fov value
; If FOV_verify is 1, then these should be also set to 1

priv   = !PRIV
!PRIV  = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'LINE_INDEX' , verify_fov , Self.resp_id
dbput, 'ADJUST_X'   , verify_fov , Self.resp_id
dbput, 'ADJUST_Y'   , verify_fov , Self.resp_id
dbclose
!PRIV  = priv

IF (verify_fov EQ 1) THEN BEGIN

   WIDGET_CONTROL , Self.x_repointID , set_value = 'Automatic (default)'
   WIDGET_CONTROL , Self.y_repointID , set_value = 'Automatic (default)'
   WIDGET_CONTROL , Self.X_RA1PointingRHSBaseID , sensitive = 0
   WIDGET_CONTROL , Self.Y_RA1PointingRHSBaseID , sensitive = 0
   WIDGET_CONTROL , Self.x0ID                   , sensitive = 1
   WIDGET_CONTROL , Self.y0ID                   , sensitive = 1

ENDIF ELSE BEGIN

   WIDGET_CONTROL , Self.x_repointID , set_value = 'Manual             '
   WIDGET_CONTROL , Self.y_repointID , set_value = 'Manual             '
   WIDGET_CONTROL , Self.X_RA1PointingRHSBaseID , sensitive = 1
   WIDGET_CONTROL , Self.Y_RA1PointingRHSBaseID , sensitive = 1
   WIDGET_CONTROL , Self.x0ID                   , sensitive = 0
   WIDGET_CONTROL , Self.y0ID                   , sensitive = 0

ENDELSE


END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::reinherit_hunter_pointing

; Get response study parameters
resp = db_read_response_entry(id = Self.resp_id)

; Has a response study been selected? If not, then bail out
IF (resp.response_st_id LT 1) THEN RETURN

; Get hunter study pointing
tl       = eis_get_tl_entry(resp.trig_st_seqid)
ra1_xcen = tl.science_component.studyRasters[Self.ra_index].x_point
ra1_ycen = tl.science_component.studyRasters[Self.ra_index].y_point
ra1_sc_x = tl.science_component.studyRasters[Self.ra_index].sc_x
ra1_sc_y = tl.science_component.studyRasters[Self.ra_index].sc_y
ra1_mip  = tl.science_component.studyRasters[Self.ra_index].mip
ra1_yip  = 306 ;tl.science_component.studyRasters[Self.ra_index].yip

; Save results to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RA1_XCEN' , ra1_xcen , Self.resp_id
dbput, 'RA1_YCEN' , ra1_ycen , Self.resp_id
dbput, 'RA1_SC_X' , ra1_sc_x , Self.resp_id
dbput, 'RA1_SC_Y' , ra1_sc_y , Self.resp_id
dbput, 'RA1_MIP'  , ra1_mip  , Self.resp_id
dbput, 'RA1_YIP'  , ra1_yip  , Self.resp_id
dbclose
!PRIV = priv

; Update Pointing Info widgets
ra1_xcen_text = STRTRIM(STRING(format='(F-8.2)',ra1_xcen),2)
ra1_ycen_text = STRTRIM(STRING(format='(F-8.2)',ra1_ycen),2)
WIDGET_CONTROL , Self.ra1_xID , set_value = ra1_xcen_text
WIDGET_CONTROL , Self.ra1_yID , set_value = ra1_ycen_text


END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::toggle_verify_fov

button_set = WIDGET_INFO(Self.xrt_eisFovID , /BUTTON_SET)
mText_0    = "By default, this parameter should be switched on!"
mText_1    = "     Are you sure you want to switch it off?"
mText_2    = ""
mText_3    = "       The EIS-XRT Offsets will be reset."
mText      = [mText_0,mText_1,mText_2,mText_3]
IF (button_set EQ 0) THEN BEGIN
    BEEP
    question = DIALOG_MESSAGE(       mText                           ,     $
                     DIALOG_PARENT = Self.tlbID                      ,     $
                     /QUESTION                                       ,     $
                     /DEFAULT_NO)
    IF (question EQ 'No') THEN $
       WIDGET_CONTROL , Self.xrt_eisFovID , set_button = 1
ENDIF

; Get the state of the button again - it may have changed!
button_set = WIDGET_INFO(Self.xrt_eisFovID , /BUTTON_SET)


; Adjust_x and adjust_y - depends on verify_fov value
; If FOV_verify is 1, then these should be also set to 1
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'LINE_INDEX' , button_set , Self.resp_id
dbput, 'ADJUST_X'   , button_set , Self.resp_id
dbput, 'ADJUST_Y'   , button_set , Self.resp_id
dbclose
!PRIV = priv

; Does an entry exist in the RESPONSE DB? Yes. Always!!!! Now.
r = db_read_response_entry(id = Self.resp_id)

; update x0 and y0 values
WIDGET_CONTROL , Self.x0ID , set_value = STRCOMPRESS(r.locate_x,/REMOVE_ALL)
WIDGET_CONTROL , Self.y0ID , set_value = STRCOMPRESS(r.locate_y,/REMOVE_ALL)


IF (button_set EQ 0) THEN BEGIN

   ; verify_fov = OFF. Set label = 'Manual' and enable text boxes
   WIDGET_CONTROL , Self.x_repointID , set_value = 'Manual             '
   WIDGET_CONTROL , Self.y_repointID , set_value = 'Manual             '
   WIDGET_CONTROL , Self.X_RA1PointingRHSBaseID , sensitive = 1
   WIDGET_CONTROL , Self.Y_RA1PointingRHSBaseID , sensitive = 1
   WIDGET_CONTROL , Self.x0ID                   , sensitive = 0
   WIDGET_CONTROL , Self.y0ID                   , sensitive = 0

ENDIF ELSE BEGIN

   ; Refresh response study pointing to hunter study pointing
   ; That is, re-inherit the pointing.
   ; What is the hunter study?
   ; Re-inherit hunter study pointing
   Self->reinherit_hunter_pointing



   ; verify_fov = ON. Set label = 'Automatic' and disable text boxes
   WIDGET_CONTROL , Self.x_repointID , set_value = 'Automatic (default)'
   WIDGET_CONTROL , Self.y_repointID , set_value = 'Automatic (default)'
   WIDGET_CONTROL , Self.X_RA1PointingRHSBaseID , sensitive = 0
   WIDGET_CONTROL , Self.Y_RA1PointingRHSBaseID , sensitive = 0
   WIDGET_CONTROL , Self.x0ID                   , sensitive = 1
   WIDGET_CONTROL , Self.y0ID                   , sensitive = 1

ENDELSE

END
;______________________________________________________________________________
; Update adjust_x
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_adjust_x , event

stop
;Self.eis_xrt_flare_control.adjust_x = event.index
;WIDGET_CONTROL , Self.X_RA1PointingRHSBaseID , sensitive = event.index

END
;______________________________________________________________________________
; Update adjust_y
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_adjust_y , event

stop
;Self.eis_xrt_flare_control.adjust_y = event.index
;WIDGET_CONTROL , Self.Y_RA1PointingRHSBaseID , sensitive = event.index

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::get_filler_study

toggle = WIDGET_INFO(Self.fillerOffButtonID , /BUTTON_SET)
IF (toggle NE 1) THEN BEGIN
   ; NO filler study
    BEEP
    mText_1 = 'The use of a filler study has been disabled.'
    mText_2 = 'Use the radio button (above) to enable it.'
    mText   = [mText_1 , mText_2]
    complain = DIALOG_MESSAGE( mText                                 ,     $
                     DIALOG_PARENT = Self.tlbID                      ,     $
                     /INFORMATION)
    RETURN
ENDIF
; Button is on, so invoke eis_xstudy to select filler study
eis_xstudy , parentClass = Self , /MODAL , /FILLER

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::filler_study_off

; Get state of button
toggle = WIDGET_INFO(Self.fillerOffButtonID , /BUTTON_SET)
IF (toggle EQ 0) THEN BEGIN
   ; NO filler study. Save response raster_id to response DB
   priv = !PRIV
   !PRIV = 2
   status = eis_open_db(/RESPONSE,/UPDATE)
   dbput, 'FILLER_ST_ID' , 0 , Self.resp_id
   dbclose
   !PRIV = priv
   ; Update response study values
   WIDGET_CONTROL , Self.fst_idID      , set_value = 'None'
   WIDGET_CONTROL , Self.fst_acronymID , set_value = 'None'
   WIDGET_CONTROL , Self.F_STBaseID    , sensitive = 0
ENDIF ELSE BEGIN
   WIDGET_CONTROL , Self.F_STBaseID    , sensitive = 1
ENDELSE

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::reset_widgets

; Update response study values
WIDGET_CONTROL , Self.rst_idID      , set_value = '-'
WIDGET_CONTROL , Self.rst_acronymID , set_value = '-'
WIDGET_CONTROL , Self.fst_idID      , set_value = '-'
WIDGET_CONTROL , Self.fst_acronymID , set_value = '-'
WIDGET_CONTROL , Self.raDroplistID  , sensitive = 0 , set_value = 'None'

END
;______________________________________________________________________________
; Select Repoint Raster
;______________________________________________________________________________
PRO eis_xrt_flare_gui::select_raster , event

; Get ra_id
WIDGET_CONTROL , Self.raDroplistID , get_value = list
item  = list[event.index]
ra_id = FIX(STRMID(item , 0 , 6))

; Save results to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RESPONSE_RA_ID' , ra_id , Self.resp_id
dbclose
!PRIV = priv

END
;______________________________________________________________________________
; Kill eis_xStudy
;______________________________________________________________________________
PRO eis_xrt_flare_gui::kill_xstudy , xstudy_obj

xstudy_obj->Control, /Destroy

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_filler_study

; Get response study_id from response DB
priv = !PRIV
!PRIV = 2
status     = eis_open_db(/RESPONSE)
st_id      = dbval(Self.resp_id, 'FILLER_ST_ID')
dbclose
!PRIV = priv
; Valid st_id should be greater than 1
IF (st_id LT 1) THEN BEGIN
   WIDGET_CONTROL , Self.fillerOffButtonID , set_button = 0
   Self->filler_study_off
   RETURN
ENDIF
; Read study. Check study is valid (should be an {eis_study} structure)
st         = db_read_study_entry(st_id)
IF (N_TAGS(st) EQ 0) THEN BEGIN
   WIDGET_CONTROL , Self.fillerOffButtonID , set_button = 0
   Self->filler_study_off
   RETURN
ENDIF

; If ID is non-zero then toggle button ON
WIDGET_CONTROL , Self.fillerOffButtonID , set_button = 1

eis_study = db_read_study_entry(st_id)
id        = STRING(format='(I06)' , st_id)
acronym   = STRTRIM(eis_study.acronym , 2)

; Update response study values
WIDGET_CONTROL , Self.fst_idID      , set_value = id
WIDGET_CONTROL , Self.fst_acronymID , set_value = STRTRIM(acronym,2)

END
;______________________________________________________________________________
; Load Filler study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::loadStudy , st_id , filler = filler

; OK, save response study_id to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)

; Update structure with new response/filler study ID
IF (KEYWORD_SET(filler) EQ 1) THEN BEGIN
   dbput, 'FILLER_ST_ID' , st_id , Self.resp_id
   dbclose
   !PRIV = priv
   ; Now update widgets
   Self->update_filler_study
ENDIF ELSE BEGIN
   dbput, 'RESPONSE_ST_ID' , st_id , Self.resp_id
   dbput, 'RESPONSE_RA_ID' , 0     , Self.resp_id
   dbclose
   !PRIV = priv
   ; Now update widgets
   Self->update_response_study
ENDELSE

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_xrt_flare_gui::update_response_study

; Get response study_id from response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE)
st_id  = dbval(Self.resp_id, 'RESPONSE_ST_ID')
ra_id  = dbval(Self.resp_id, 'RESPONSE_RA_ID')
dbclose
!PRIV = priv

; Check st_id is valid (1 or greater)
IF (st_id LT 1) THEN BEGIN
   WIDGET_CONTROL , Self.RepointBaseID , sensitive = 0
   Self->reset_widgets
   RETURN
ENDIF
; Read study. Check study is valid (should be an {eis_study} structure)
st     = db_read_study_entry(st_id)
IF (N_TAGS(st) EQ 0) THEN BEGIN
   WIDGET_CONTROL , Self.RepointBaseID , sensitive = 0
   Self->reset_widgets
   RETURN
ENDIF

; Update Study ID and acronym values
id        = STRING(format='(I06)' , st_id)
acronym   = STRTRIM(st.acronym , 2)
WIDGET_CONTROL , Self.rst_idID      , set_value = id
WIDGET_CONTROL , Self.rst_acronymID , set_value = STRTRIM(acronym,2)

; Time to get the study raster id
IF (ra_id EQ 0) THEN BEGIN
   ; ra_id = 0 (not set). Use the ra_id of the first studyRaster
   ra_id    = st.studyRasters[0].ra_id
   ra_index = 0
ENDIF ELSE BEGIN
   ; Get list of raster IDs - and find its studyRaster index (need for
   ; "set_droplist_select")
   ra_id_list = st.studyRasters[0:st.nRasters-1].ra_id
   ra_index   = WHERE(ra_id_list EQ ra_id , ra_count)
ENDELSE

; OK, save response raster_id to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RESPONSE_RA_ID' , ra_id , Self.resp_id
dbclose
!PRIV = priv

; OK, populate raster droplist
ra_list = Self->get_ra_droplist(st)

; OK, update widgets
WIDGET_CONTROL , Self.raDroplistID  , set_value = ra_list
WIDGET_CONTROL , Self.raDroplistID  , set_droplist_select = ra_index[0]
WIDGET_CONTROL , Self.raDroplistID  , sensitive = 1
WIDGET_CONTROL , Self.RepointBaseID , sensitive = 1

; Update solar coordinates
Self->get_new_solar_coords

END
;______________________________________________________________________________
; OK, new solar coords, based on current sc pointing and MIP/YIP values
;______________________________________________________________________________
PRO eis_xrt_flare_gui::get_new_solar_coords

; Get ra_id and pointing info from response DB
r     = db_read_response_entry(id = Self.resp_id)
ra_id = r.raster_identifier
mip   = r.ra1_mip
yip   = r.ra1_yip
sc_x  = r.ra1_sc_x
sc_y  = r.ra1_sc_y

; Calculate new solar coords

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; JAR 1-Apr-2014. Add "obs_id" and "aia_time" inputs to routine
; "eis_get_xy_point"
; Using hunter study seq_id, get obs_id and start time of that TL entry
tl       = eis_get_tl_entry(r.trig_st_seqid)
obs_id   = tl.science_component.obs_id
aia_time = tl.time_component.start_time
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; JAR 27-Aug-2014. Get sot_offsets from tl.sc.disturbance
sot_offset = tl.science_component.disturbance
sot_offset = FLOAT(STRSPLIT(sot_offset,',',/EXTRACT))

xycen = eis_get_xy_point(sc_x , sc_y , mip , yip , ra_id ,     $
                         obs_id , aia_time , sot_offset)

; Save results to response DB
priv = !PRIV
!PRIV = 2
status = eis_open_db(/RESPONSE,/UPDATE)
dbput, 'RA1_XCEN' , xycen.x_point , Self.resp_id
dbput, 'RA1_YCEN' , xycen.y_point , Self.resp_id
dbclose
!PRIV = priv

END
;______________________________________________________________________________
; OK, get the raster list
;______________________________________________________________________________
FUNCTION eis_xrt_flare_gui::get_ra_droplist , eis_study

; 000001  Test_raster_3 [RA:000002][SNS:]
; O VI   184.12
; Create a list of studyRaster details (id, acronym and titles)
ra_list = STRARR(eis_study.nRasters)
FOR i = 0 , eis_study.nRasters - 1 DO BEGIN

   sra        = eis_study.studyRasters[i]
   idValue    = STRING(format='(I06)',sra.ra_id)
   ra_list[i] = STRING(format='(A-8,A-20,A-60)'                        ,   $
                 idValue,sra.ra_acronym,sra.ra_title)

ENDFOR

RETURN , ra_list

END
;______________________________________________________________________________
; Check values
;______________________________________________________________________________
FUNCTION eis_xrt_flare_gui::check_values

valid = {valid:1B , mText:['','','','','']}
valid.mText[0] = 'This Event Trigger is NOT valid!'
valid.mText[4] = 'Do you want to quit anyway?'

; Get values from response DB
r = db_read_response_entry(id = Self.resp_id)

; Check Response and Filler Study IDs and Repointing Raster IDs are valid
IF (r.response_st_id LT 1) THEN BEGIN
   valid.mText[2] = '(Error: Response Study is NOT selected)'
   valid.valid    = 0
   RETURN , valid
ENDIF
IF (r.raster_identifier LT 1) THEN BEGIN
   valid.mText[2] = '(Error: : Repointing Raster is NOT selected)'
   valid.valid    = 0
   RETURN , valid
ENDIF

; Filler Study - only an option.
toggle    = WIDGET_INFO(Self.fillerOffButtonID , /BUTTON_SET)
; If using a filler study, then make sure it's ID is non-zero
IF (toggle EQ 1) AND (r.filler_st_id EQ 0) THEN BEGIN
   valid.mText[2] = '(Error: Follow on Study is NOT selected)'
   valid.valid    = 0
   RETURN , valid
ENDIF

RETURN , valid

END
;______________________________________________________________________________
; Accept properties and quit
;______________________________________________________________________________
PRO eis_xrt_flare_gui::accept

; Check x0 and y0 widget text boxes
valid = Self->check_and_update_xy0()
IF (valid EQ 0) THEN RETURN

; Check solar coords widget text boxes
valid = Self->check_and_update_xycen()
IF (valid EQ 0) THEN RETURN

; Do some other checks
valid = Self->check_values()
IF (valid.valid NE 1) THEN BEGIN
    BEEP
    question = DIALOG_MESSAGE( valid.mText                           ,     $
                     DIALOG_PARENT = Self.tlbID                      ,     $
                     /QUESTION                                       ,     $
                     /DEFAULT_NO)
    IF (question EQ 'No') THEN RETURN
ENDIF

; Quit GUI
Self->Control, /Destroy

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_xrt_flare_gui__Define

    struct = { eis_xrt_flare_gui                                       ,     $
                    tlbID                 : 0L                         ,     $
                    group_leader          : 0L                         ,     $
                    parentClass           : OBJ_NEW()                  ,     $
                    resp_id               : 0L                         ,     $
                    ra_index              : 0L                         ,     $
                    raDroplistID          : 0L                         ,     $
                    F_STBaseID            : 0L                         ,     $
                    RepointBaseID         : 0L                         ,     $
                    xrt_eisFovID          : 0L                         ,     $
                    fillerOffButtonID     : 0L                         ,     $
                    rst_idID              : 0L                         ,     $
                    rst_acronymID         : 0L                         ,     $
                    fst_idID              : 0L                         ,     $
                    x_repointID           : 0L                         ,     $
                    y_repointID           : 0L                         ,     $
                    ra1_xID               : 0L                         ,     $
                    ra1_yID               : 0L                         ,     $
                    x0ID                  : 0L                         ,     $
                    y0ID                  : 0L                         ,     $
                    X_RA1PointingRHSBaseID: 0L                         ,     $
                    Y_RA1PointingRHSBaseID: 0L                         ,     $
                    fst_acronymID         : 0L                               $
             }

END
