www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+widget/BasicContainer.m

    classdef BasicContainer < mbcgui.widget.Component
%BASICCONTAINER Basic container component for MBC
%   mbcgui.widget.BasicContainer is a base class for components that want
%   to pass all of the basic Component properties to a single contained
%   component, e.g. a layout handle.
%
%   Subclasses normally set the ContentHandle property to be the handle of
%   the contained control in their constructor.  From this point on the
%   contained control will be given the correct Parent, Position, Visible
%   and Enable settings.  When the container is deleted, the contained
%   control is also deleted.
%
%   See also Component.

%   Copyright 2008-2015 The MathWorks, Inc.

properties(Dependent,Access=protected)
    %CONTENTHANDLE Handle to contained component
    ContentHandle = [];
end

properties(Access=private)
    %pContentHandle store Handle to contained component
    pContentHandle = [];
end


methods(Access=protected)
    function obj=BasicContainer(varargin)
    %BASICCONTAINER Construct a new BasicContainer object.
        obj@mbcgui.widget.Component(varargin{:});
    end
end


methods
    function set.ContentHandle(obj, H)
    obj.pContentHandle = H;
    set(H, 'Position', obj.Position, 'Visible', obj.Visible);
    end

    function H = get.ContentHandle(obj)
    H = obj.pContentHandle;
    end
    
    function delete(obj)
    try
        P = obj.Parent;
    catch E
        if strcmp(E.identifier, 'MATLAB:class:InvalidHandle')
            % Assume that the object is invalid
            return
        else
            rethrow(E);
        end
    end
    
    if isgraphics(P) && ~mbcgui.util.isBeingDestroyed(P)
        H = obj.ContentHandle;
        if mbcgui.util.isComponentHandle(H)
            delete(H);
        end
    end
    end
end


% Override the component property set methods to forward the values to the
% contained control.
methods(Access=protected)
    function setParent(obj, P)
    obj.setContentProperty('Parent', P);
    end
    
    function setPosition(obj, Pos)
    obj.setContentProperty('Position', Pos);
    end
    
    
    function setVisible(obj, Vis)
    obj.setContentProperty('Visible', Vis);
    end
    
    function setEnable(obj, En)
    obj.setContentProperty('Enable', En);
    end
    
    function attachContentHandle(obj,H)
    %attachContentHandle attach content without setting position
    obj.pContentHandle = H;
    end    
end

methods(Access=private)
    function setContentProperty(obj, Prop, Val)
    % Set a property on the contained object
    H = obj.ContentHandle;
    if ~isempty(H)
        set(H, Prop, Val);
    end
    end
end

end