;------------------------------------------------------------------------------;
;+
; NAME:
;		     eis_export_gui
; PURPOSE:
;		     This object
; INPUTS:
;		     GROUP -
;-
;------------------------------------------------------------------------------;
FUNCTION eis_export_gui::Init , parentClass , ll = ll , ra = ra , st = st

Self.parentClass  = parentClass
Self.group_leader = parentClass->tlbID()
;
; Get authorList
;
authorList = eis_read_study_lists( /AUTHOR)
PTR_FREE , Self.authorList
Self.authorList = PTR_NEW(authorList)
;
; Get targetList
;
targetList = eis_read_study_lists( /TARGET)
PTR_FREE , Self.targetList
Self.targetList = PTR_NEW(targetList)
;
; Get categoryList
;
categoryList = eis_read_study_lists( /CATEGORY)
PTR_FREE , Self.categoryList
Self.categoryList = PTR_NEW(categoryList)

; structType = 0/1/2/ = lineList/raster/study
IF (KEYWORD_SET(ll) EQ 1) THEN BEGIN
    parentClass->getProperty , eis_ll_obj = eis_ll_obj
    eis_ll_obj->getProperty , ll = struct
    Self.structType = 0
ENDIF
IF (KEYWORD_SET(ra) EQ 1) THEN BEGIN
    parentClass->getProperty , eis_raster = struct
    Self.structType = 1
ENDIF
IF (KEYWORD_SET(st) EQ 1) THEN BEGIN
    parentClass->getProperty , eis_study = struct
    Self.st_title     = STRTRIM(struct.title[0]    , 2)
    Self.st_target    = STRTRIM(struct.target      , 2)
    Self.st_category  = STRTRIM(struct.category[0] , 2)
    Self.structType = 2
ENDIF

Self.acronym = STRTRIM(struct.acronym   , 2 )
Self.author  = STRTRIM(struct.author[0] , 2 )

;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = 'EIS Study Export' + pt_version
TLB_EVENT                   = {eis_export_gui_Event}
TLB_EVENT.eis_export_gui     = Self
TLB_EVENT.Type              = "TLB"
Self.tlbID                  = WIDGET_BASE(                                  $
                                    TITLE       = widTitle,                 $
                                    COLUMN      = 1,                        $
                                    /FLOATING      ,                        $
                                    ;/MODAL         ,                        $
                                    GROUP_LEADER = Self.group_leader ,      $
                                    UValue      = TLB_EVENT)

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

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_export_gui::createGUI

Event_Pro   = 'eis_export_gui_EV'


topBase                     = WIDGET_BASE(Self.tlbID ,                      $
                                    COLUMN      = 1 ,                       $
                                    /FRAME )



; ______________________________________________________________________
;
; Acronym
;
; ______________________________________________________________________
acronymBase                 = WIDGET_BASE(topBase    ,                      $
                                    ROW         = 1)


value = 'Acronym:'
aLabel                      = WIDGET_LABEL(acronymBase ,                    $
                                    value       = value ,                   $
                                    xSize       = 60    ,                   $
                                    /ALIGN_LEFT )

DESC_EVENT                  = {eis_export_gui_Event}
DESC_EVENT.eis_export_gui   = Self
DESC_EVENT.Type             = "ACRONYM"
value = Self.acronym
acronymID                   = WIDGET_TEXT(acronymBase        ,              $
                                    value       = value      ,              $
                                    Event_Pro   = Event_Pro  ,              $
                                    uValue      = DESC_EVENT ,              $
                                    xSize       = 24 ,                      $
                                    /EDITABLE)
Self.acronymID = acronymID

value = ' (20 chars max)'
aLabel                      = WIDGET_LABEL(acronymBase ,                    $
                                    value       = value ,                   $
                                    xSize       = 150   ,                   $
                                    /ALIGN_LEFT )

; ______________________________________________________________________
;
; Study Details
;
; ______________________________________________________________________
IF (Self.structType EQ 2) THEN BEGIN

    ; __________________________________________________________________
    ;
    ; Title
    ;
    ; __________________________________________________________________
    titleBase               = WIDGET_BASE(topBase    ,                      $
                                    ROW         = 1)

    value = 'Title:'
    aLabel                  = WIDGET_LABEL(titleBase   ,                    $
                                    value       = value ,                   $
                                    xSize       = 60    ,                   $
                                    /ALIGN_LEFT )

    TITLE_EVENT             = {eis_export_gui_Event}
    TITLE_EVENT.eis_export_gui   = Self
    TITLE_EVENT.Type        = "TITLE"
    value = Self.st_title
    Self.titleID            = WIDGET_TEXT(titleBase          ,              $
                                    value       = value      ,              $
                                    Event_Pro   = Event_Pro  ,              $
                                    uValue      = TITLE_EVENT ,             $
                                    xSize       = 70 ,                      $
                                    /EDITABLE)

    value = ' (100 chars max)'
    aLabel                  = WIDGET_LABEL(titleBase   ,                    $
                                    value       = value ,                   $
                                    xSize       = 150   ,                   $
                                    /ALIGN_LEFT )

    ; __________________________________________________________________
    ;
    ; Category
    ;
    ; __________________________________________________________________
    categoryBase            = WIDGET_BASE(topBase    ,                      $
                                    ROW         = 1)

    value = 'Category:'
    aLabel                  = WIDGET_LABEL(categoryBase   ,                 $
                                    value       = value ,                   $
                                    xSize       = 60    ,                   $
                                    /ALIGN_LEFT )

    CATEGORY_EVENT          = {eis_export_gui_Event}
    CATEGORY_EVENT.eis_export_gui   = Self
    CATEGORY_EVENT.Type     = "CATEGORY"
    Self.categoryID         = WIDGET_DROPLIST(categoryBase        ,         $
                                    Event_Pro   = Event_Pro         ,       $
                                    value       = (*Self.categoryList)  ,   $
                                    uValue      = CATEGORY_EVENT)

    categoryIndex = WHERE(*Self.categoryList EQ Self.st_category)
    WIDGET_CONTROL , Self.categoryID , set_droplist_select =                $
                                                    categoryIndex[0] > 0

    ; __________________________________________________________________
    ;
    ; Target
    ;
    ; __________________________________________________________________
    targetBase              = WIDGET_BASE(topBase    ,                      $
                                    ROW         = 1)

    value = 'Target:'
    aLabel                  = WIDGET_LABEL(targetBase   ,                   $
                                    value       = value ,                   $
                                    xSize       = 60    ,                   $
                                    /ALIGN_LEFT )

    TARGET_EVENT            = {eis_export_gui_Event}
    TARGET_EVENT.eis_export_gui   = Self
    TARGET_EVENT.Type       = "TARGET"
    Self.targetID           = WIDGET_DROPLIST(targetBase        ,           $
                                    Event_Pro   = Event_Pro         ,       $
                                    value       = (*Self.targetList)  ,     $
                                    uValue      = TARGET_EVENT)

    targetIndex = WHERE(*Self.targetList EQ Self.st_target)
    WIDGET_CONTROL , Self.targetID , set_droplist_select =                  $
                                                    targetIndex[0] > 0

ENDIF
; ______________________________________________________________________
;
; Author
;
; ______________________________________________________________________
authorBase                  = WIDGET_BASE(topBase    ,                      $
                                    ROW         = 1)

aLabel                      = WIDGET_LABEL(authorBase ,                     $
                                    value       = 'Author:' ,               $
                                    xSize       = 60    ,                   $
                                    /ALIGN_LEFT )

AUTHOR_EVENT                 = {eis_export_gui_Event}
AUTHOR_EVENT.eis_export_gui   = Self
AUTHOR_EVENT.Type           = "AUTHOR"
Self.authorID               = WIDGET_DROPLIST(authorBase        ,           $
                                    Event_Pro   = Event_Pro         ,       $
                                    value       = (*Self.authorList)  ,     $
                                    uValue      = AUTHOR_EVENT)

authorIndex = WHERE(*Self.authorList EQ Self.author)
WIDGET_CONTROL , Self.authorID , set_droplist_select = authorIndex[0] > 1
; ______________________________________________________________________
;
; Apply & Quit Button Base
;
; ______________________________________________________________________
bottomBase                  = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1 ,                       $
                                    /FRAME           ,                      $
                                    /GRID_LAYOUT)

APPLY_EVENT                 = {eis_export_gui_Event}
APPLY_EVENT.eis_export_gui = Self
APPLY_EVENT.Type            = "APPLY"
APPLYbutton                 = WIDGET_BUTTON(bottomBase,                     $
                                    value       = ' Export... ' ,           $
                                    Event_Pro   = Event_Pro     ,           $
                                    uValue      = APPLY_EVENT)

QUIT_EVENT                  = {eis_export_gui_Event}
QUIT_EVENT.eis_export_gui = Self
QUIT_EVENT.Type             = "QUIT"
quitbutton                  = WIDGET_BUTTON(bottomBase,                     $
                                    value       = ' Dismiss ',              $
                                    Event_Pro   = Event_Pro  ,              $
                                    uValue      = QUIT_EVENT)


END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_export_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_export_gui_Event
OBJ_DESTROY    , eis_export_gui_Event.eis_export_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_export_gui::CleanUp

PTR_FREE , Self.authorList
PTR_FREE , Self.targetList
PTR_FREE , Self.categoryList

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_export_gui::setProperty, autoScaleStr  = autoScaleStr

IF (N_ELEMENTS(autoScaleStr) GT 0) THEN BEGIN
    stop
ENDIF

End
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_export_gui::GetProperty, tlbID   = tlbID

tlbID = Self.tlbID

End
;______________________________________________________________________________
; Return widget id
;______________________________________________________________________________
Function eis_export_gui::tlbID

    Return, Self.tlbID
End

;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_export_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End

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

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

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_export_gui::Realize

Widget_Control, Self.tlbID, /Realize

End

;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_export_gui::XManager

XMANAGER , "eis_export_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_export_gui_EV",                                    $
    cleanup        = "eis_export_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                   $
    /NO_BLOCK

END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_export_gui_Event__Define

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

;Get event handler structure containing eis_export_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_export_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_export_gui's class member procedure "Handler"
eis_export_gui_Event.eis_export_gui->Handler,Event

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

WIDGET_CONTROL, Event.ID, Get_UValue = eis_export_gui_Event

CASE eis_export_gui_Event.Type OF
    "QUIT"      : Self->Control, /Destroy
    "APPLY"     : Self->export
    "AUTHOR"    : Self->updateAuthor
    "TARGET"    : Self->updateTarget
    "CATEGORY"  : Self->updateCategory
    "ACRONYM"   : ;Self->updateDescription
    ELSE        : print,eis_export_gui_Event.Type
ENDCASE

END
;______________________________________________________________________________
; Update Target field
;______________________________________________________________________________
PRO eis_export_gui::updateTarget

droplistSelect = WIDGET_INFO(Self.targetID , /DROPLIST_SELECT)
Self.st_target = (*Self.targetList)[droplistSelect]

END
;______________________________________________________________________________
; Update Category field
;______________________________________________________________________________
PRO eis_export_gui::updateCategory

droplistSelect = WIDGET_INFO(Self.categoryID , /DROPLIST_SELECT)
Self.st_category = (*Self.categoryList)[droplistSelect]

END
;______________________________________________________________________________
; Update Author field
;______________________________________________________________________________
PRO eis_export_gui::updateAuthor

droplistSelect = WIDGET_INFO(Self.authorID , /DROPLIST_SELECT)
Self.author = (*Self.authorList)[droplistSelect]

END
;______________________________________________________________________________
; Apply
;______________________________________________________________________________
FUNCTION eis_export_gui::getStudyDetails

; Read acronym from text widget
WIDGET_CONTROL , Self.titleID , get_value = widValue

; Make sure we don't have a null string
IF (STRLEN(widValue[0]) LT 5) THEN BEGIN
    mText = 'Title must have at least 5 characters. Try again!'
    BEEP
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN , 0
ENDIF

; Make sure we don't have > 100 characters
IF (STRLEN(widValue[0]) GT 100) THEN BEGIN
    mText = 'Title must a maximum of 100 characters. Try again!'
    BEEP
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN , 0
ENDIF


st_info = { title     : widValue[0]           ,                             $
            category  : Self.st_category      ,                             $
            target    : Self.st_target                                      $
          }

RETURN , st_info

END
;______________________________________________________________________________
; Apply
;______________________________________________________________________________
PRO eis_export_gui::export

; Read acronym from text widget
WIDGET_CONTROL , Self.acronymID , get_value = newAcronym

; Make sure we don't have a null string
IF (STRLEN(newAcronym[0]) LT 5) THEN BEGIN
    mText = 'Acronym must have at least 5 characters. Try again!'
    BEEP
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

; Make sure there aren't any spaces
split = STRSPLIT(newAcronym[0] , count = count)
IF (count NE 1) THEN BEGIN
    mText    = "I'm replacing your spaces with underscores!"
    widValue = str_replace(newAcronym[0],' ','_')
    WIDGET_CONTROL , Self.acronymID , set_value = newAcronym[0]
ENDIF

; Make sure we don't have > 20 characters
IF (STRLEN(newAcronym[0]) GT 20) THEN BEGIN
    mText = 'Acronym must a maximum of 20 characters. Try again!'
    BEEP
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

;
; OK, acronym appears to be OK
;
CASE Self.structType OF
        ; LineList
    0:  Self.parentClass->export_ll , newAcronym[0] , Self.author
        ; Raster
    1:  Self.parentClass->export_ra , newAcronym[0] , Self.author
        ; Study
    2:  BEGIN
            st_info = Self->getStudyDetails()
            IF (N_TAGS(st_info) EQ 0) THEN BEGIN
                RETURN
            ENDIF ELSE BEGIN
                Self.parentClass->export , newAcronym[0] , Self.author ,    $
                                            st_info = st_info
            ENDELSE
        END
ENDCASE


; And finally, kill widget!
Self->Control, /Destroy

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO	eis_export_gui__Define

    struct =	{ eis_export_gui                               ,               $
                    tlbID                 : 0L              ,               $
                    group_leader          : 0L              ,               $
                    parentClass           : OBJ_NEW()       ,               $
                    acronym               : ''              ,               $
                    author                : ''              ,               $
                    structType            : 0B              ,               $
;                    lineListType          : 0B              ,               $
;                    rasterType            : 0B              ,               $
;                    studyType             : 0B              ,               $
                    st_title              : ''              ,               $
                    titleID               : 0L              ,               $
                    st_category           : ''              ,               $
                    categoryID            : 0L              ,               $
                    st_target             : ''              ,               $
                    targetID              : 0L              ,               $
                    acronymID             : 0L              ,               $
                    authorID              : 0L              ,               $
                    authorList            : PTR_NEW()       ,               $
                    targetList            : PTR_NEW()       ,               $
                    categoryList          : PTR_NEW()                       $
              }

END
