function pan4import(varargin)

% ========================================================================
% =============== Panel to import data from the workspace ================
% ========================================================================
%
%  $Id: pan4import.m,v 1.8 2008/08/30 12:27:08 nicola Exp $

global LTPDAinvar

ImportObjList='';
UpdateListbox();

currPanel   = varargin{1};
panelDimens = get(currPanel, 'Position');
backColor   = get(currPanel, 'BackgroundColor');

    DataPanPos   = [20 panelDimens(4)-470 panelDimens(3)-40 450];
  hDataPanel    =  uipanel(...
                      'Title','Data currently in the workspace',...
                      'BackgroundColor',[1,1,1],...
                      'FontSize',7,...
                      'Units','pixels',...
                      'Position',DataPanPos);
   
    nbuttons    = 3;
    buttonWidth = (DataPanPos(3)-10*(nbuttons+1))/nbuttons;
    buttonPos   = [10 DataPanPos(4)-45 buttonWidth 25];
  hPlotButton   =   uicontrol('Parent',hDataPanel,'Units','pixels','HorizontalAlignment','center','Position',buttonPos,'String','Plot','FontSize',8,'Visible','on','Callback','','Style','pushbutton','enable','off');
    buttonPos(1) = buttonPos(1)+buttonWidth+10;
  hRefreshButton =   uicontrol('Parent',hDataPanel,'Units','pixels','HorizontalAlignment','center','Position',buttonPos,'String','Refresh','FontSize',8,'Visible','on','Callback',@UpdateListbox,'Style','pushbutton','enable','on');
    buttonPos(1) = buttonPos(1)+buttonWidth+10;
  hImportButton =   uicontrol('Parent',hDataPanel,'Units','pixels','HorizontalAlignment','center','Position',buttonPos,'String','Import','FontSize',8,'Visible','on','Callback',@ImportCallback,'Style','pushbutton','enable','on');
%     buttonPos(1) = buttonPos(1)+buttonWidth+10;
%   hExploreButton =  uicontrol('Parent',hDataPanel,'Units','pixels','HorizontalAlignment','center','Position',buttonPos,'String','Explore','FontSize',8,'Visible','on','UserData',ImportObjList,'Callback',@hExploreButtonCallback,'Style','pushbutton','enable','off');
%     buttonPos(1) = buttonPos(1)+buttonWidth+10;
%   hSaveOutButton =  uicontrol('Parent',hDataPanel,'Units','pixels','HorizontalAlignment','center','Position',buttonPos,'String','Save','FontSize',8,'Visible','on','Enable','off','Callback',@SaveOutputCallback,'Style','pushbutton');
%     clear nbuttons buttonWidth buttonPos


    textWidth = (DataPanPos(3)-10*(3))/2;
    textPos   = [10 DataPanPos(4)-80 textWidth 25];
  uicontrol('Parent',hDataPanel,'BackgroundColor',get(hDataPanel,'BackgroundColor'),'HorizontalAlignment','center','Position',textPos,'String','Objects:','FontName','Times New Roman','FontSize',8,'FontWeight','normal','Visible','on','Style','text');
    clear textWidth textPos

    nlists    = 2;
    listWidth = (DataPanPos(3)-10*(nlists+1))/nlists;
    listPos   = [10 DataPanPos(4)-315 listWidth 240];
  ImportObjList   =   uicontrol('Parent',hDataPanel,'BackgroundColor',[0.95,0.95,0.95],'HorizontalAlignment','center','Position',listPos,'String',listInput,'Value',[],'Max',10,'Min',0,'FontSize',8,'FontWeight','normal','Visible','on','Enable','on','Tag','ImportObjList','Callback',@hImportListCallback,'Style','listbox');
    listPos(1) = listPos(1)+listWidth+10;
  % Info panel:
  uicontrol('Parent',hDataPanel,'BackgroundColor',[0.94,0.94,1],'Style','text','HorizontalAlignment', 'left','Fontsize', 7,'Units','pixels','Position',listPos,'Tag','infoPanel');
    hImportListCallback(ImportObjList,1)
    clear DataPanPos nlists listWidth listPos

        
%%
    %----------------------------------------------------------------------
    function UpdateListbox(varargin)
    % Updates the listbox of objects in the current workspace
    
        listInput = evalin('base','who');
        
        for i=numel(listInput):-1:1
            obj = evalin('base',listInput{i});
            if ~isa(obj,'ltpda_uo') || isa(obj,'cell'), listInput(i)=[]; end
        end
        
        if isempty(listInput)
            listInput = 'No LTPDA objects currently in the MATLAB workspace';
            set(findobj('Tag', 'ImportObjList'),'Enable', 'off','Value',[],'String',listInput)
        else
            set(findobj('Tag', 'ImportObjList'),'Enable', 'on','Value',[],'String',listInput)
        end
        
    end
    %----------------------------------------------------------------------

%%
    %----------------------------------------------------------------------
    function hImportListCallback(hObject, varargin)
    % This callback is called whenever the user click upon one of the
    % objects in the workspace
    
        if ~isempty(varargin{1}) && varargin{1}==1 && ~isa(listInput,'char')
              % This is a call only to update the info panel
                val = get(hObject, 'Value');
                if length(val) == 1
                    objLines = get(hObject, 'String');
                    if isempty(objLines)
                        objtxt = '';
                    else
                        objname = objLines{val};
                        obj = evalin('base',objname);
                        objtxt = display(obj);
                    end
                else
                    objtxt = '';
                end
                set(findobj('Tag', 'infoPanel'), 'String', objtxt)
        elseif ~isa(listInput,'char')
            if strcmp(get(gcf,'SelectionType'),'normal')
                val = get(hObject, 'Value');
                if length(val) == 1
                    objLines = get(hObject, 'String');
                    objname = objLines{val};
                    obj = evalin('base',objname);
                    objtxt = display(obj);
                else
                    objtxt = '';
                end
                set(findobj('Tag', 'infoPanel'), 'String', objtxt)
            end
            if strcmp(get(gcf,'SelectionType'),'open') && ~isempty(get(hObject, 'String'))
                val = get(hObject, 'Value');
                objLines = get(hObject, 'String');
                objname = objLines{val};
                obj = evalin('base',objname);
                try %#ok<ALIGN>
                    figure
                    plot(obj);
                catch
                    close(gcf)
                end
            end
        end
         
    end
    %----------------------------------------------------------------------

    %----------------------------------------------------------------------
    function ImportCallback(varargin)
    % This callback is called whenever the user click the 'Import' button

        objList = findobj('Tag', 'ImportObjList');
        val     = get(objList, 'Value');
        objLines   = get(objList, 'String');
        for i = 1:numel(val)
            objname = objLines{val(i)};
            obj = evalin('base',objname);
            nobj = numel(obj);
            if nobj>1
                for jj=1:nobj
                    objj = {obj(jj) , 0};
                    LTPDAinvar = [LTPDAinvar;objj];
                end
            else
                obj = {obj , 0};
                LTPDAinvar = [LTPDAinvar;obj];
            end
        end
        set(findobj('Tag', 'ImportObjList'),'Value',[])
             
    end
    %----------------------------------------------------------------------

%%
end