function pal = update_log_palette(h,data)

% update_log_palette - update log palette when opening or closing logs
% --------------------------------------------------------------------
%
% g = update_log_palette(h,data)
%
% Input:
% ------
%  h - palette parent handle
%  data - parent userdata
%
% Output:
% -------
%  g - handle of updated palette figure

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 132 $
% $Date: 2004-12-02 15:58:47 -0500 (Thu, 02 Dec 2004) $
%--------------------------------

%------------------------------------------------------
% HANDLE INPUT
%------------------------------------------------------

%--
% set figure if needed
%--

if (nargin < 1)
	h = gcf;
end

%--
% get userdata if needed
%--

if ((nargin < 2) | isempty(data))
	data = get(h,'userdata');
end

%------------------------------------------------------
% MAKE LOG PALETTE AVAILABLE FROM MENU
%------------------------------------------------------

% tmp1 = findobj(h,'type','uimenu','label','Log');
% 
% tmp2 = cell2mat(get(tmp1,'parent'));
% 
% ix = find(tmp2 == findobj(tmp2,'flat','type','uimenu','label','Window'));
% 
% tmp1 = tmp1(ix);
% 
% set(tmp1,'enable','on');

%------------------------------------------------------
% CHECK FOR PALETTE
%------------------------------------------------------

pal = get_palette(h,'Log',data);

if (isempty(pal))
	return;
end

%------------------------------------------------------
% UPDATE LOG PALETTE
%------------------------------------------------------

%--
% get open log information
%--

if (isempty(data.browser.log))
	
	L = {'(No Open Logs)'};
	
	ms = 1; % popupmenu value, cannot be empty
	
	v = []; % listbox value should be empty
	
else
	
	L = file_ext(struct_field(data.browser.log,'file'));
	
	m = data.browser.log_active;
	
	v = find(struct_field(data.browser.log,'visible'));
	
	%--
	% sort the results
	%--
	
	tmp = length(data.browser.log);
	
	if (tmp > 1)
		
		[L,ix] = sort(L);
		
		% map active log using permutation
		
		ms = find(ix == m);
		
		% map values using permutation
		
		tmp = zeros(1,tmp);
		tmp(v) = 1;
		v = find(tmp(ix));
		
	else
		
		ms = m;
		
	end

end

%--
% update active control
%--

g = control_update(h,'Log','Active',[],data);

g = findobj(g,'flat','style','popupmenu');
set(g, ...
	'string',L, ...
	'value',ms ...
);

%--
% update display control
%--

g = control_update(h,'Log','Display',[],data);

g = findobj(g,'flat','style','listbox');
set(g, ...
	'string',L, ...
	'value',v ...
);

%--
% update log control
%--

g = control_update(h,'Log','Log',[],data);

g = findobj(g,'flat','style','popupmenu');
set(g, ...
	'string',L, ...
	'value',ms ...
);

%--
% update log option controls
%--

if (~isempty(data.browser.log))
	
% 	tmp = data.browser.log(m);
	
	tmp = data.browser.log(ms); % this is the case when the logs are sorted
	
	control_update(h,'Log','Color',rgb_to_color(tmp.color),data);
	
	control_update(h,'Log','Line Style',lt_to_linestyle(tmp.linestyle),data);
	
	control_update(h,'Log','Line Width',tmp.linewidth,data);

	control_update(h,'Log','Opacity',tmp.patch,data);
	
else
	
	control_update(h,'Log','Color','Red',data);
	
	control_update(h,'Log','Line Style','Solid',data);
	
	control_update(h,'Log','Line Width',1,data);

	control_update(h,'Log','Opacity',0,data);
	
end

%--
% enable and disable controls
%--

if (isempty(data.browser.log))
	state = '__DISABLE__';
else
	state = '__ENABLE__';
end

control = getfield(get(pal,'userdata'),'control');

for k = 1:length(control)
	
	if (~strcmp(control(k).style,'separator') & ~strcmp(control(k).style,'tabs'))
		
		if (~iscell(control(k).name))
			control_update(h,'Log',control(k).name,state,data);
		else
			for j = 1:prod(size(control(k).name))
				control_update(h,'Log',control(k).name{j},state,data);
			end
		end
	end
	
end

control_update(h,'Log','New Log','__ENABLE__',data);

control_update(h,'Log','Open Log ...','__ENABLE__',data);

if (strcmp(state,'__ENABLE__'))
	set(findobj(pal,'tag','Display','style','pushbutton'),'enable','off');
end