; $Id: oodisplayecho__restoresession.pro,v 1.7 2006/07/28 14:46:14 kneller Exp $
;###############################################################################
;
;CLASS NAME:
;    pro ooDisplayEcho__restoresession
;
;PURPOSE:
;               To save the sessions and individual objects.
;CATEGORY:
;               Data Visualization, Analysis and Manipulation.
;SUPERCLASSES:
;               myProperties
;
;METHODS:
;    ooDisplayEcho::restoreSession
;
; AUTHOR:
; Larry Kneller
; NIST Center for Neutron Research
; 100 Bureau Drive, Gaithersburg, MD 20899
; United States
; kneller@nist.gov  301-975-8839
; July 25,2005
;
; LICENSE:
; The software in this file is written by an employee of
; National Institute of Standards and Technology
; as part of the DAVE software project.
;
; The DAVE software package is not subject to copyright protection
; and is in the public domain. It should be considered as an
; experimental neutron scattering data reduction, visualization, and
; analysis system. As such, the authors assume no responsibility
; whatsoever for its use, and make no guarantees, expressed or
; implied, about its quality, reliability, or any other
; characteristic. The use of certain trade names or commercial
; products does not imply any endorsement of a particular product,
; nor does it imply that the named product is necessarily the best
; product for the stated purpose. We would appreciate acknowledgment
; if the DAVE software is used of if the code in this file is
; included in another product.
;
;###############################################################################
function ooDisplayEcho::restoreSession,object=object
;
;NAME:
;        ooDisplayEcho::restoreSession
;
;PURPOSE:
;           Encapsulate all restore session functionality into one method
;           to be used from file.
;PARAMETERS:
;           none
;KEYWORDS:
;           object  Flag to indicate if only one object will be read from file.
;RETURN VALUE
;           1/0 TO INDICATE SUCCESS/FAILURE


                    if n_elements(object) eq 0 then object = 0
;void = dialog_message('Restore session test 1')
                    fn = dialog_pickfile(/read,/must_exist,$
                                            path=self.work_dir,$
                                            filter=['*.sav','*.xml'])

;void = dialog_message('Restore session test 2')


                    if fn eq '' then begin
                        void = dialog_message('NO INPUT FILE SELECTED.')
                        return,0
                    endif else begin

                            ;FIRST CHECK FOR VALID FILE EXTENSIONS, .xml and .sav
                            ;AND ESTABLISH WHICH TYPE IS BEING READ.
                            length = strlen(fn)
                            if length gt 4 then begin
                                ext = strmid(fn,length-4,4)
                                ;pos = stregex(fn,'.xml',/foldcase)
                                print,ext
                                if stregex(ext,'.sav',/fold_case) ne -1 then print,'.sav file'
                                if stregex(ext,'.xml',/fold_case) ne -1 then print,'.xml file'
                                extension = strlowcase(ext)
                                ;FOR TESTING
                                ;return,0
                            endif else begin
                                void = dialog_message('FILE MUST HAVE A ".xml" OR A ".sav" EXTENSION.')
                                return,0
                            endelse



                            ;if extension eq '.xml' then begin
                            case extension of
                            '.xml':begin
                                totallunlines = file_lines(fn)

                                self.work_dir = file_dirname(fn,/mark_directory)

                                if object eq 0 then begin
                                    ;031805
                                    ;DELETE ALL ACTIVE OBJECTS BEFORE RESTORING FROM FILE.
                                    killref = self.datacontainer->get(/all)
                                    self.datacontainer->remove,/all
                                    if obj_valid(killref[0]) then obj_destroy,killref
                                endif

    ;031805
    ;NEED TO ADD ERROR CHECKING HERE FOR THE DATA IN THE FILE.
    ;
    ;NO TIME TODAY...
    CATCH, Error_status
       ;HANDLE ERRORS IN READING THE STATE FILE.
       IF (Error_status NE 0) THEN BEGIN
          free_lun,lun,/force
          print,'4605 ooDisplayEcho::restoreSession PROBLEM OCCURRED.'
          print,readResult
          CATCH, /CANCEL
          if widget_info(b1,/valid_id) gt 0 then $
              widget_control,b1,/destroy
          return,0
       ENDIF

                                openr,lun,fn,/get_lun

                                ;
                                line=''
                                readf,lun,line  ;GET <xml>
                                readf,lun,line  ;GET THE WARNING MESSAGES.
                                readf,lun,line
                                readf,lun,line  ;GET "<session>"
                                if stregex(line,'<session>',/fold_case,/boolean) then begin
                                    readf,lun,line  ;GET "<nobj>"
                                    segs = strsplit(line,/extract)
                                    count = fix(segs[1])

                                    ;120304
                                    ;PROGRESS BAR
                                    b1 = widget_base(title='Session Import',xsize=200, $
                                        xoffset=400,yoffset=400,/floating,group_leader=self.atlb)
                                    p1 = cw_progress(b1,value=[count], $
                                                        title=['Obj # ='])
                                    widget_control,b1,/realize

                                    readf,lun,line ;GET "<gaussianwidth>"
                                    print,line
                                    segs = strsplit(line,/extract)
                                    ;print,'Gaussian width ='+segs[1]
                                    widget_control,self.atwwidth,$
                                                set_value = double(segs[1])

    ;MAYBE point_lun CAN MAKE THIS BACKWARD COMPATIBLE.
    ;TO CREATE BACKWARD COMPATIBILITY, ADD FUTURE NEW VALUES
    ;TO THE END OF THE FILE ON SAVE COMMANDS.
    ;THEN OLDER VERSIONS OF THE READER WILL NOT SEE THESE.
    ;ONCE IDL 6.1 IS USED FOR DAVE, THESE VALUES WILL BE
    ;RETRIEVED USING THE IDLffXMLDOM CLASSES.

                                    readf,lun,line ;GET "<period>"
;                                    print,line
                                    if stregex(line,'period',/fold_case,/boolean) then begin
                                        segs = strsplit(line,/extract)
                                        self.periodvalue = double(segs[1])
                                    endif
    ;060605
;                                    print,'self.periodvalue=',self.periodvalue
                                    widget_control,self.atwperiod,$
                                                set_value = strtrim(string(self.periodvalue),2)

                                    readf,lun,line ;GET "<data_dir>"
;                                    print,line
                                    segs = strsplit(line,/extract)
                                    self.data_dir = strtrim(segs[1],2)

                                    readf,lun,line ;GET "<work_dir>"
                                    segs = strsplit(line,/extract)

                            ;ELIMINATE THE NEXT LINE AND LET THE work_dir BE DETERMINED BY THE LOCATION OF THE DATA FILE.
                                    ;self.work_dir = strtrim(segs[1],2)

    ;for ii=0,40 do begin
    ;readf,lun,line
    ;print,line
    ;endfor;ii
    ;return,0
                                    for io = 0,count-1 do begin
                                        ;oblank = obj_new('ooecho',/blank)
                                        oblank = obj_new('ooechoMagnetic',/blank)
                                        readResult = oblank->readstate(lun=lun,totallunlines=totallunlines)
                                        ccount = self.datacontainer->count()
                                        if readResult eq 1 then begin
                                            self.datacontainer->add,oblank, $
                                                                position=ccount
                                        endif else begin
                                            free_lun,lun,/force
                                            if widget_info(b1,/valid_id) gt 0 then $
                                                widget_control,b1,/destroy

                                            void = dialog_message('Problem reading file: '+fn)
                                            return,0
                                        endelse
                                        if widget_info(p1,/valid_id) gt 0 then $
                                            widget_control,p1,set_value=[io+1]
                                    endfor;io
                                    readf,lun,line
                                    if not stregex(line,'</session>',/fold_case,/boolean) then $
                                        void = dialog_message('"</session>" NOT OBSERVED, '+ $
                                                                'MORE OBJECTS POSSIBLE.')
                                    free_lun,lun
                                    if widget_info(b1,/valid_id) gt 0 then $
                                        widget_control,b1,/destroy
                                newcount = self.datacontainer->count()
                                self.QChoiceIndex = 0

;                               MOVE NEXT 3 LINES TO CALLING ROUTINE
;                                self->reDefineWidgets,newcount,t=0,/newsize
;                                self->redefineTree
;                                self->draw

                                return,1
                            endif else begin


                                void = dialog_message('FILE FORMAT QUESTIONABLE: ' + $
                                                    'FIRST TAG NE "<session>".')

                                return,0
                            endelse
                        end;.xml
                        '.sav':begin
                            print,'RESTORING A .sav FILE.'
    CATCH, Error_status
       ;HANDLE ERRORS IN READING THE STATE FILE.
       IF (Error_status NE 0) THEN BEGIN
          free_lun,lun,/force
          print,'4827 ooDisplayEcho::restoreSession PROBLEM OCCURRED.'
          print,readResult
          CATCH, /CANCEL
          if widget_info(b1,/valid_id) gt 0 then $
              widget_control,b1,/destroy
          return,0
       ENDIF

;072505
;CREATE MACHINERY FOR RECONSTRUCTION AFTER .sav FILE FORMAT IS COMPLETED IN THE savesession SECTION.
;print,'CREATE MACHINERY FOR RECONSTRUCTION AFTER .sav FILE FORMAT IS COMPLETED IN THE savesession SECTION.'
;message = dialog_message('Test1')
                            b1 = widget_base(title='Session Import',xsize=200, $
                                xoffset=400,yoffset=400,/floating,group_leader=self.atlb)
                            p1 = cw_progress(b1,value=[1], $
                                                title=['PLEASE WAIT'])
                            widget_control,b1,/realize

;save,xindex,yindex,qchoiceindex,data_dir,work_dir,container,filename = fn,/compress
;message = dialog_message('Test2')

;print,'__restoreSession TESTING'
                            modtime = (file_info(fn)).mtime

                            if !version.release ge 6.1 then begin
                                restore,fn,/RELAXED_STRUCTURE_ASSIGNMENT;,description=description
                                ;print,'Session file description = ',description
                            endif else begin
                                restore,fn,/RELAXED_STRUCTURE_ASSIGNMENT
                            endelse
;print,'__restoreSession TESTING'

;                            help,xindex
;
;                            help,yindex
;                            help,qchoiceindex
;                            help,data_dir
;                            help,work_dir
;                            help,nse_container
;message = dialog_message('Test3')
                           if widget_info(b1,/valid_id) ne 0 then widget_control,p1,set_value=[1]
;print,'__restoreSession TESTING'

                            print,obj_valid(nse_container)
                            if obj_valid(nse_container) eq 0 then begin
                                message = dialog_message('.sav FILE MAY NOT BE VALID SESSION FILE.')
                                if widget_info(b1,/valid_id) ne 0 then widget_control,b1,/destroy
                                return,0
                            endif
;print,'__restoreSession TESTING'

;message = dialog_message('Test4')



                            if object eq 0 then begin
;message = dialog_message('Test5a')
                                self.xindex = xindex
;message = dialog_message('Test5b')
                                self.yindex = yindex
;message = dialog_message('Test5c')
                                self.qchoiceindex = 0;qchoiceindex
;message = dialog_message('Test5d')
                                self.data_dir = data_dir
;message = dialog_message('Test5e')
                                self.work_dir = work_dir
;message = dialog_message('Test5f')

                                ;DELETE ALL ACTIVE OBJECTS BEFORE RESTORING FROM FILE.
                                killref = self.datacontainer->get(/all)
;message = dialog_message('Test5g')
                                self.datacontainer->remove,/all
;message = dialog_message('Test5h')
                                if obj_valid(killref[0]) then obj_destroy,killref
;message = dialog_message('Test5i')
                                if obj_valid(self.datacontainer) then obj_destroy,self.datacontainer
;message = dialog_message('Test5j')


                                self.datacontainer = nse_container
;                            ;121405
;                                self.datacontainer = obj_new('idl_container')
;;message = dialog_message('Test5k')
;                                if nse_container->count() gt 0 then begin
;;message = dialog_message('Test5k1')
;                                    for ii=0,nse_container->count()-1 do begin
;;message = dialog_message('Test5k1a')
;                                        newref = (nse_container->get(position=ii))->clone(modtime=modtime)
;;message = dialog_message('Test5k1b')
;                                        self.datacontainer->add,newref
;;message = dialog_message('Test5k1c')
;                                    endfor;ii
;;message = dialog_message('Test5k2')
;                                    killref = nse_container->get(/all)
;;message = dialog_message('Test5k3')
;                                    nse_container->remove,/all
;;message = dialog_message('Test5k4')
;                                    if obj_valid(killref[0]) then obj_destroy,killref
;;message = dialog_message('Test5k5')
;                                    if obj_valid(self.datacontainer) then obj_destroy,nse_container
;;message = dialog_message('Test5k6')
;                                endif
;;message = dialog_message('Test5l')

                            endif else begin
;message = dialog_message('Test4a')
                                nobjects = nse_container->count()
;message = dialog_message('Test4b')
                                if nobjects gt 1 then begin
                                    void = dialog_message('Multiple objects in file, extracting only the first.')
                                endif
;                                ref = nse_container->get(position=0)
                                ref = (nse_container->get(position=0))->clone()
;message = dialog_message('Test4c')

;                                nse_container->remove,position=0
;message = dialog_message('Test4d')

                                killref = nse_container->get(/all)
;message = dialog_message('Test4e')
                                if obj_valid(killref[0]) gt 0 then obj_destroy,killref
;message = dialog_message('Test4f')
                                obj_destroy,nse_container
;message = dialog_message('Test4g')
                                count = self.datacontainer->count()
                                self.datacontainer->add,ref,position=count
                                self.QChoiceIndex = count
;message = dialog_message('Test4h')
                            endelse


                            if widget_info(b1,/valid_id) ne 0 then widget_control,b1,/destroy
;message = dialog_message('Test5')

                            return,1
                        end;'.sav'
                        else:begin
                            void = dialog_message('FILE MUST HAVE A ".xml" OR A ".sav" EXTENSION.')
                            return,0
                        end;else
                        endcase
                    endelse

end;restoreSession
