function busdemoscript(varargin)
%BUSDEMOSCRIPT Controls the Bus Block Demo HTML page
%   BUSDEMOSCRIPT(action) executes the hyperlink callback associated with the 
%   the string action.
%
%   BUSDEMOSCRIPT(action,blockName) is used for the hiliteblock action. blockName
%   contains the string name of the block to be highlighted using hilite_system.
%
%   The complete set of demo files is as follows.
%      1) busdemo.mdl
%      2) busdemo.htm 
%      3) sldemo1_fig1.gif
%      4) sldemo1_fig2.gif
%      5) sldemo1_fig3.gif
%      6) sldemo1_fig4.gif
%      7) busdemoscript.m

%   Karen Gondoly
%	Copyright 1986-2000 The MathWorks, Inc. 
%	$Revision: 1.1 $  $Date: 2000/08/23 14:08:38 $

persistent BUSDEMO_STEP_COUNTER
try % Wrap the whole thing in a try catch, in case the user doesn't go in order.
	
	ni=nargin;
	
	if ~ni,
		step = 'initialize';
	else 
		step = varargin{1};
	end
	
	% If first time running the demo, or they closed the model
	% Open the model and initialize the counter
	if isempty(find_system('Name','busdemo')) | ~ni
		busdemo;
		BUSDEMO_STEP_COUNTER=zeros(1,14);
	end
	
	switch step
	case 'initialize',
		% Open the web browser with the html file
		fullPathName=which('busdemoscript');
		tok=filesep;
		indtok = findstr(fullPathName,tok);
		pathname = fullPathName(1:indtok(end));
		web([pathname,'busdemo.htm']);
		
	case 'step1',
		% Open the Simulink model
		BUSDEMO_STEP_COUNTER(1)=1;
		
	case 'step2',
		% Run the model
		BUSDEMO_STEP_COUNTER(2)=1;
		sim('busdemo'); % Executing just this line will automatically open the model
		
	case 'step3',
		BUSDEMO_STEP_COUNTER(3)=1;
		% Turn on signal dimensions
		set_param('busdemo','ShowLineDimensions','on');
		set_param('busdemo','SimulationCommand','update'); % In case they didn't run the diagram
		
	case 'step4',
		BUSDEMO_STEP_COUNTER(4)=1;
		% Turn on wide vector lines
		%-This should actually be step 8, so make sure up to step 7 has been done
		if ~BUSDEMO_STEP_COUNTER(7),
			busdemoscript('step7');
		end	
		set_param('busdemo','WideLines','on');
		set_param('busdemo','SimulationCommand','update'); % Needed when invoked from command line
		
	case 'step5',
		BUSDEMO_STEP_COUNTER(5)=1;
		hilite_system('busdemo/Sine Wave','find');
		open_system('busdemo/Sine Wave')
		
	case 'step6',
		BUSDEMO_STEP_COUNTER(6)=1;
		set_param('busdemo/Sine Wave','Amplitude','[1 2]')
		%---If they come here, make sure to reset later steps that were already clicked
		BUSDEMO_STEP_COUNTER(7:14)=0;
		
	case 'step7',
		BUSDEMO_STEP_COUNTER(7)=1;
		% Make sure the signal dimensions are already shown
		if ~BUSDEMO_STEP_COUNTER(3),
			busdemoscript('step3');
		end	
		
		close_system('busdemo/Sine Wave')
		hilite_system('busdemo/Sine Wave','none')
		set_param('busdemo/Sine Wave','Amplitude','[1 2]');
		%---If they come here, make sure to reset later steps that were already clicked
		BUSDEMO_STEP_COUNTER(8:14)=0;
		
	case 'step8',
		BUSDEMO_STEP_COUNTER(8)=1;
		set_param('busdemo/Sine Wave','VectorParams1D','off');
		
	case 'step9',
		BUSDEMO_STEP_COUNTER(9)=1;
		% Make sure the wide vector lines are already shown
		if ~BUSDEMO_STEP_COUNTER(4),
			busdemoscript('step4');
		end	
		close_system('busdemo/Sine Wave')
		hilite_system('busdemo/Sine Wave','none')
		set_param('busdemo/Sine Wave','VectorParams1D','off');
		
	case 'step9_5',
		BUSDEMO_STEP_COUNTER(10)=1;
		% Make sure the wide vector lines are already shown
		if ~BUSDEMO_STEP_COUNTER(4),
			busdemoscript('step4');
		end	
		close_system('busdemo/Sine Wave') % Close the dialog, if opened so changes are applied
		set_param('busdemo/Sine Wave','Amplitude','[1 2; 3 4]');
		set_param('busdemo','SimulationCommand','update');
		
	case 'step10',
		BUSDEMO_STEP_COUNTER(11)=1;
		%---Get demo up to the point where a matrix has been entered
		if ~BUSDEMO_STEP_COUNTER(10),
			busdemoscript('step9_5');
		end	
		open_system(find_system('busdemo','BlockType','BusSelector'))
		
	case 'step11',
		BUSDEMO_STEP_COUNTER(12)=1;
		if ~BUSDEMO_STEP_COUNTER(11),
			busdemoscript('step10');
		end	
		
		% Animate the steps in this piece of the demo.
		Fig = ...
			findobj(allchild(0),'Name',['Block Parameters: Bus',char(10),'Selector']);
		Data = get(Fig,'UserData');	
		
		% 1) Select the Chirp signal
		set(Data.SelectedList,'Value',2) 
		set([Data.RemoveButton,Data.UpButton],'Enable','on')
		pause(0.75)
		
		% 2) Simulate removing the Chirp signal
		set(Data.SelectedList,'String',{'Pulse'},'Value',1)
		set(Data.UpButton,'Enable','off')
		
	case 'step12'
		BUSDEMO_STEP_COUNTER(13)=1;
		if ~BUSDEMO_STEP_COUNTER(11),
			busdemoscript('step10');
		end	
		
		% Animate the steps in this piece of the demo.
		Fig = ...
			findobj(allchild(0),'Name',['Block Parameters: Bus',char(10),'Selector']);
		Data = get(Fig,'UserData');
		
		% 1) Select the Sine signal
		set(Data.TreeList,'Value',5)
		pause(0.75)
		
		% 2) Simulate adding the signal to the Selected signal list
		set(Data.SelectedList,'String',{'Pulse';'Sine'})
		
	case 'step13',
		BUSDEMO_STEP_COUNTER(14)=1;
		if ~BUSDEMO_STEP_COUNTER(13),
			busdemoscript('step12');
		end	
		Fig = ...
			findobj(allchild(0),'Name',['Block Parameters: Bus',char(10),'Selector']);
		Data = get(Fig,'UserData');
		
		% Copied from busselect.m ApplyFunction
		blockH = Data.BlockHandle;
		signalList = get(Data.SelectedList, 'string');
		
		signals = StripSelection(signalList);
		signals = signals(:)';
		signals(2,:) = {','};
		selSignals = cat(2, signals{:});
		selSignals(end) = [];
		
		% Perform the set_param only if parameters have changed
		CurrentOutSignals = get_param(blockH, 'OutputSignals');
		
		set_param(blockH, ...
			'OutputSignals',selSignals)
		
		close(Fig)
		
	case 'step14',
		% Final run of the diagram
		if ~BUSDEMO_STEP_COUNTER(14),
			busdemoscript('step13');
		end	
		sim('busdemo'); 
		
	case 'update',
		type = varargin{2};
		switch type
		case 'vector',
			% Make sure the Sine Wave contains a vector param
			if ~BUSDEMO_STEP_COUNTER(7),
				busdemoscript('step7');
			end	
		case 'matrix',
			% Make sure the up to step 9 is done
			if ~BUSDEMO_STEP_COUNTER(9),
				busdemoscript('step9');
			end	
		end
		set_param('busdemo','SimulationCommand','update')
		
	case 'hiliteblock'
		%---hilite appropriate blocks
		blockName = varargin{2};
		b=find_system('busdemo','Type','block');
		
		% Hilite the specific block for 2 seconds
		hilite_system(['busdemo/',blockName],'find')
		pause(2)	
		hilite_system(['busdemo/',blockName],'none')
		
	case 'closemodel',
		close_system('busdemo',0);
		
	otherwise
		warning('The requested action could not be taken.')
		
	end	 % switch action
catch
	warndlg({'There was an error executing the link you selected.';
		'Please, restart the demonstration as additional links ';
		'may be broken due to this problem.';
		'';
		'To avoid future errors, please run the links in the order ';
		'in which they occur in the demo.'})
end % Try/catch

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function modArray = StripSelection(inArray)

modArray = strrep(inArray, '??? ', '');
