function updateResultSummary(this,DialogPanel)
% updateResultSummary - Update the text field with the result summary
%  Author(s): John Glass
%  Copyright 2004 The MathWorks, Inc.
%	$Revision: 1.1.6.4 $  $Date: 2004/12/26 22:24:38 $

%% Get the summary area handle and lti system
sa = this.SummaryAreaUDD;

%% Get the index to the selected linearization
if DialogPanel.isSelectorComboEnabled
    combo_index = DialogPanel.getSelectedModelIndex;
else
    combo_index = 1;
end
sys = this.LinearizedModel(:,:,combo_index);

%% Label
data = {'<font face="monospaced"; size=3>'};
data{end+1} = sprintf('<B>Linearization Result:</B><BR>');
data{end+1} = '<BR>';
data{end+1} = sprintf('&nbsp;-&nbsp;To plot the response of this result click on the node labeled Custom Views.<BR>');
data{end+1} = sprintf('&nbsp;-&nbsp;To export the result click on the Export To Workspace button below.<BR>');

%% Get the font metric and the number of pixels per character.  This is
%% a monospaced font so all characters will have the same widths.
fnt = sa.getFont;
fm = sa.getFontMetrics(fnt);
stringWidth = fm.stringWidth('x');
[Explorer,Workspace,TreeManager] = slctrlexplorer;

%% This is the number of characters that are allowed in the display
panelWidth = floor(TreeManager.ExplorerPanel.getDisplayer.getWidth/stringWidth*0.9);

%% Get the selected element
mode = DialogPanel.getLinearAnalysisSummaryPanel.getLTITypeCombo.getSelectedIndex;

if mode == 0
    %% Get the number of states
    nstates = length(sys.a);
    statelabels = cell(nstates,1);
    for ct = 1:nstates
        statelabels{ct} = sprintf('x%d',ct);
    end
    %% Replace state names with the indecies x1, x2,... that are summarized
    %% below.
    ReducedStateNames = sys.StateName;
    sys.StateNames = statelabels;
    
    %% Find the full state names in the model  
    if isempty(this.IOStructure)
        FullStateName = ReducedStateNames;
    else
        FullStateName = this.IOStructure(1).FullStateName;
    end

    %% State space mode
    data = localParseCharArray(evalc(sprintf('display(sys,%d)',panelWidth)),data);
    data{end+1} = '<BR>';
    data{end+1} = sprintf('<B>State Names:</B><BR>');
    for ct = 1:length(sys.StateName)
        block_red = regexprep(ReducedStateNames{ct},'\n',' ');
        if ~strcmp(FullStateName{ct},'?') 
            block = regexprep(FullStateName{ct},'\n',' ');            
            data{end+1} = sprintf('x%d - <a href="block:%s">%s</a><BR>',ct,block,block_red);
        elseif strcmp(FullStateName{ct},'?') && ... 
                strcmp(this.LinearizationOptions.RateConversionMethod,'zoh')
            data{end+1} = sprintf('x%d - State added during rate conversion.<BR>',ct);
        else
            data{end+1} = sprintf('x%d - State name lost in rate conversion.<BR>',ct);
        end
    end
elseif mode == 1
    %% Zero pole gain mode
    data = localParseCharArray(evalc(sprintf('display(zpk(sys),%d)',panelWidth)),data);
else
    %% transfer function mode
    data = localParseCharArray(evalc(sprintf('display(tf(sys),%d)',panelWidth)),data);   
end

data{end+1} = '<BR>';
data{end+1} = sprintf('<A><B>Input Channel Names:</B></A><BR>');
if ~isempty(this.IOStructure)
    for ct = 1:length(sys.InputName)
        block = regexprep(sys.InputName{ct},'\n',' ');
        data{end+1} = sprintf('<a href="signal:%s:%d">%s</a><BR>',...
            this.IOStructure(1).FullInputName{ct},...
            this.IOStructure(1).FullInputPort(ct),...
            block);
    end
else
    for ct = 1:length(sys.InputName)
        block = regexprep(sys.InputName{ct},'\n',' ');
        data{end+1} = block;
    end
end

data{end+1} = '<BR>';
data{end+1} = sprintf('<B>Output Channel Names:</B><BR>');
if ~isempty(this.IOStructure)
    for ct = 1:length(sys.OutputName)
        block = regexprep(sys.OutputName{ct},'\n',' ');
        data{end+1} = sprintf('<a href="signal:%s:%d">%s</a><BR>',...
            this.IOStructure(1).FullOutputName{ct},...
            this.IOStructure(1).FullOutputPort(ct),...
            block);
    end
else
    for ct = 1:length(sys.OutputName)
        block = regexprep(sys.OutputName{ct},'\n',' ');
        data{end+1} = block;
    end
end

%% Clear the text
sa.setText('');
%% Add the new text
sa.append(data);
%% Set the point to the top of the view
pnt = java.awt.Point(0,0);
scrollpane = sa.getParent;
awtinvoke(scrollpane,'setViewPosition',pnt);

%% LocalFunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LocalParseCharArray - parses to convert char array with carriage returns
%% to create cell array.
function data = localParseCharArray(strin,data)

%% Find the carriage returns
ind = regexp(strin,'\n');

%% Loop over every line
for ct = 1:length(ind);
   if ct == 1
       data{end+1} = [sprintf('<A>%s</A>',regexprep(strin(1:ind(ct)),' ','&nbsp;')),'<BR>'];;
   else
       data{end+1} = [sprintf('<A>%s</A>',regexprep(strin(ind(ct-1):ind(ct)),' ','&nbsp;')),'<BR>'];
   end
end