function rootnode = getInspectorPanelData(this);
%GETINSPECTORPANELDATA

%  Author(s): John Glass
%  Revised:
% Copyright 1986-2004 The MathWorks, Inc.
% $Revision: 1.1.6.11 $ $Date: 2004/12/26 22:24:37 $

import javax.swing.*;

%% Get the Jacobian data.  It is assumed and is the case that the Jacobian
%% structure will always be the same.  Only A,B,C,D will be different.
J = this.ModelJacobian;
J1 = J(1);

%% Create an empty block list
BlockNameList = cell(size(J1.Mi.BlockHandles));

%% Get the block handles and the other information such as sample time.
BlockHandles = J1.Mi.BlockHandles;
BlocksInPath = J1.Mi.BlocksInPath;
InputIdx = [J1.Mi.InputIdx+1;size(J1.B,2)+1];
OutputIdx = [J1.Mi.OutputIdx+1;size(J1.C,1)+1];
StateIdx = [J1.Mi.StateIdx+1; length(J1.A)+1];
SampleTimes = J1.Ts(1:length(J1.A));
StateName = J1.StateName;

%% Find the hidden buffers and remove carriage
HiddenBuffers = [];
for ct = 1:length(BlockNameList)
    try
        BlockNameList{ct} = regexprep(getfullname(J1.Mi.BlockHandles(ct)),'\n',' ');
    catch
        HiddenBuffers = [HiddenBuffers;ct];
    end
end

%% Remove references to hidden buffers
BlockNameList(HiddenBuffers) = [];
BlockHandles(HiddenBuffers) = [];
BlocksInPath(HiddenBuffers) = [];

%% Sort the block list
[BlockNameList,ind] = sort(BlockNameList);

%% Create empty objects for the tree structure
Block = GenericLinearizationNodes.BlockInspectorLinearization;

%% Tree Creation: Find the unique subsystem parents
UniqueParents = unique(regexprep(find_system(this.Model,...
    'FollowLinks','on','LookUnderMasks','all',...
    'BlockType','SubSystem'),'\n',' '));
UniqueParents = {this.model,UniqueParents{:}};
Parents = regexprep(get_param(BlockHandles,'Parent'),'\n',' ');
BlockList = regexprep(get_param(BlockHandles,'Name'),'\n',' ');

%% Tree Creation: Create their node objects
root = [];
for ct = 1:length(UniqueParents)
    if ct == 1;
        root = GenericLinearizationNodes.SubsystemMarker('root');
    else
        root = [root; GenericLinearizationNodes.SubsystemMarker('sdaf')];
    end

    %% Find its direct parent
    if (ct == 1)
        ParentInd = 1;
        root(ct).Label = UniqueParents{ct};
    else
        %% Get the subsystem's parent
        SSParent = regexprep(get_param(UniqueParents{ct},'Parent'),'\n',' ');
        ParentInd = find(strcmp(SSParent,UniqueParents));
        %% Get the label
        root(ct).Label = get_param(UniqueParents{ct},'Name');
        %% Connect the subsystem to its parent
        connect(root(ct), root(ParentInd), 'up');
    end

    %% Find the blocks that are direct children
    ind = find(strcmp(Parents,UniqueParents{ct}));
    
    %% Remove any subsystem or signal viewer scope blocks in the linearization
    Block_Type = get_param(BlockHandles(ind),'BlockType');
    ind_subsys = find(strcmp(Block_Type,'SubSystem'));
    ind_scope = find(strcmp(Block_Type,'Scope'));
    if ~isempty(ind_scope)
        for ct = length(ind_scope):-1:1
            if strcmp(get_param(BlockHandles(ind(ind_scope(ct))),'IOType'),'none')
                ind_scope(ct) = [];
            end
        end
    end
    ind([ind_subsys(:);ind_scope(:)]) = [];
    
    %% Create an empty default list model
    nchild = length(ind);
    BlockListData = cell(nchild,1);
    
    %% Create objects to be parented to the subsystem
    ChildBlocks = [];
    for ct2 = 1:nchild
        ChildBlocks = [ChildBlocks; Block.copy];

        %% Find the index in the original list
        ind_original = find(J1.Mi.BlockHandles == BlockHandles(ind(ct2)));

        state_ind = find(strcmp(getfullname(BlockHandles(ind(ct2))),StateName));
        if ~isempty(state_ind)
            indx = state_ind;
        else
            indx = [];
        end
        if (InputIdx(ind_original) ~= InputIdx(ind_original+1))
            indu = [InputIdx(ind_original):(InputIdx(ind_original+1)-1)];
        else
            indu = [];
        end
        if (OutputIdx(ind_original) ~= OutputIdx(ind_original+1))
            indy = [OutputIdx(ind_original):(OutputIdx(ind_original+1)-1)];
        else
            indy = [];
        end
        
        %% Store the sample times
        ChildBlocks(ct2).DiscardUpdate = false;
        ChildBlocks(ct2).SampleTimes = SampleTimes(indx);
        if BlocksInPath(ind(ct2))
            ChildBlocks(ct2).InLinearizationPath = 'Yes';
            %% Store each of the block's linearizations
            ChildBlocks(ct2).setAllABCD(J,indx,indu,indy);
        else
            ChildBlocks(ct2).InLinearizationPath = 'No';
        end
        ChildBlocks(ct2).FullBlockName = getfullname(BlockHandles(ind(ct2)));
        ChildBlocks(ct2).DiscardUpdate = true;
        
        %% Add all blocks to the list
        BlockListData{ct2} = regexprep(get_param(BlockHandles(ind(ct2)),'Name'),'\n',' ');
    end

    %% Store the blocks in the subsystem node
    root(ct).Blocks = ChildBlocks;
    root(ct).ListData = BlockListData;
end

%% Set the panel information
this.InspectorNode = root(1);
