;------------------------------------------------------------------------------;
;+
; NAME:
;		     eis_import_title_gui
; PURPOSE:
;		     This object
; INPUTS:
;		     GROUP -
;-
;------------------------------------------------------------------------------;
FUNCTION eis_import_title_gui::Init , structure                ,            $
                                        parentClass            ,            $
                                        index         = index

Self.parentClass  = parentClass
Self.group_leader = parentClass->tlbID()
Self.structure    = PTR_NEW(structure)
IF KEYWORD_SET(index) THEN Self.index = index


;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle   = "EIS Input Title" + pt_version
TLB_EVENT                   = {eis_import_title_gui_Event}
TLB_EVENT.eis_import_title_gui       = Self
TLB_EVENT.Type              = "TLB"
Self.tlbID                  = WIDGET_BASE(                                  $
                                    TITLE       = widTitle,                 $
                                    /FLOATING      ,                        $
                                    /MODAL         ,                        $
                                    GROUP_LEADER = Self.group_leader ,      $
                                    COLUMN      = 1,                        $
                                    UValue      = TLB_EVENT ,               $
                                    /TLB_KILL_REQUEST_EVENTS)

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

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui::createGUI

event_pro = 'eis_import_title_gui_EV'
; ______________________________________________________________________
;
; Label base
;
; ______________________________________________________________________
labelBase                   = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                      $
                                    FRAME       = 1 )

; OK, check if TITLE is unique
struct_name = TAG_NAMES(*Self.structure ,/STRUCTURE_NAME)
CASE struct_name OF
    "EIS_LINELIST" : line = 'Line List'
    "EIS_RASTER"   : line = 'Raster'
    "EIS_STUDY"    : line = 'Study'
    ELSE           : stop
ENDCASE

value = "This " + line + " title already exists. Modify it!"
aLabel                      = WIDGET_LABEL(labelBase    ,                   $
                                    value       = value             ,       $
                                    /ALIGN_LEFT)
; ______________________________________________________________________
;
; TITLE base
;
; ______________________________________________________________________
titleBase                   = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                      $
                                    FRAME       = 1 )


TITLE_EVENT                 = {eis_import_title_gui_Event}
TITLE_EVENT.eis_import_title_gui      = Self
TITLE_EVENT.Type            = "TITLE"
value = (*Self.structure).title
Self.titleID                = WIDGET_TEXT(titleBase    ,                    $
                                    value       = value         ,           $
                                    xSize       = 80          ,             $
                                    Event_Pro   = event_pro       ,         $
                                    /EDITABLE                     ,         $
                                    uValue      = TITLE_EVENT)

aLabel                      = WIDGET_LABEL(titleBase      ,                 $
                                    value       = '(max 80 chars)' ,        $
                                    /ALIGN_LEFT)

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


ACCEPT_EVENT                = {eis_import_title_gui_Event}
ACCEPT_EVENT.eis_import_title_gui      = Self
ACCEPT_EVENT.Type           = "ACCEPT"
ACCEPTbutton                = WIDGET_BUTTON(bottomBase,                     $
                                    value       = 'Accept',                 $
                                    Event_Pro   = event_pro       ,         $
                                    uValue      = ACCEPT_EVENT)



END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_import_title_gui_Event
OBJ_DESTROY    , eis_import_title_gui_Event.eis_import_title_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_import_title_gui::CleanUp

PTR_FREE , Self.structure

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui::setProperty, author  = author

IF (N_ELEMENTS(author) GT 0) THEN stop;Self.eis_study.author = author

End
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui::GetProperty, structure = structure

structure = *Self.structure

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

    Return, Self.tlbID
End

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

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End

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

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

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_import_title_gui::Realize

Widget_Control, Self.tlbID, /Realize

End

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

XMANAGER , "eis_import_title_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_import_title_gui_EV",                                    $
    cleanup        = "eis_import_title_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                    $
    /NO_BLOCK

end

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui_Event__Define

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

;Get event handler structure containing eis_import_title_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_import_title_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_import_title_gui's class member procedure "Handler"
eis_import_title_gui_Event.eis_import_title_gui->Handler,Event

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

WIDGET_CONTROL, Event.ID, Get_UValue = eis_import_title_gui_Event

CASE eis_import_title_gui_Event.Type OF
    "TLB"          : Self->insist
    "ACCEPT"       : Self->accept
    "TITLE"        : Self->accept
    ELSE           : print,eis_import_title_gui_Event.Type
ENDCASE
END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui::insist

mText = "Oh no you don't. Try again!"
BEEP
d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
RETURN

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_import_title_gui::accept

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
ENDIF

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

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

; OK, check if TITLE is unique
struct_name = TAG_NAMES(*Self.structure ,/STRUCTURE_NAME)
CASE struct_name OF
    "EIS_LINELIST" : unique = eis_title_unique('' , widValue[0] , /LINELIST)
    "EIS_RASTER"   : unique = eis_title_unique('' , widValue[0] , /RASTER)
    "EIS_STUDY"    : unique = eis_title_unique('' , widValue[0] , /STUDY)
    ELSE           : stop
ENDCASE


IF (unique.unique_acron NE 1) THEN BEGIN
    mText = 'This is NOT a unique title. Try again!'
    BEEP
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF



CASE struct_name OF
    "EIS_LINELIST" : Self.parentClass->update_title , widValue[0] , Self.index , /LINELIST
    "EIS_RASTER"   : Self.parentClass->update_title , widValue[0] , Self.index , /RASTER
    "EIS_STUDY"    : Self.parentClass->update_title , widValue[0] , Self.index , /STUDY
    ELSE           : stop
ENDCASE


; And finally, kill the gui
Self->Control, /Destroy

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_import_title_gui__define

    struct          =	{ eis_import_title_gui                         ,     $
                                    tlbID           : 0L              ,     $
                                    group_leader    : 0L              ,     $
                                    parentClass     : OBJ_NEW()       ,     $
                                    titleID         : 0               ,     $
                                    index           : 0B              ,     $
                                    structure       : PTR_NEW()          $
                      }

END
