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

    classdef Application < matlab.mixin.SetGet
%APPLICATION Application object.
%
%   MBCGUI.APPLICATION creates an Application object.  This is an interface
%   that wraps a figure and configures it for use as a main application
%   window.  Shortcuts for status message display and mouse pointer
%   changing are provided and there is support for a "sleep" mode to avoid
%   destroying and re-creating the application.
%
%   mbcgui.Application properties:
%      ContentHandle  - Content component handle.
%      Figure         - Handle to the application figure window.
%      SleepOnClose   - Boolean flag specifying whether to delete the
%                       figure or make it invisible when it is closed.
%
%   Methods:
%      close          - Close the application window.
%      restore        - Restore an application that is in "sleep" mode.
%      addSubFigure   - Register a new figure as a sub-figure of the
%                       application.
%
%      setStatusMessage    - Methods that alter the message in the
%      removeStatusMessage - statusbar.
%      clearStatusMessage  - 
%
%      setPointer     - 
%      removePointer  - Methods that alter the mouse pointer.
%      resetPointer   - 
%
%   Static methods:
%      find           - Find an application handle given a figure tag.
%
%   Protected methods:
%      createContent  - called during construction to generate the UI.
%      postCreate     - called after initial construction, when window is
%                       visible.
%      sleep          - called when the application is pushed to "sleep"
%                       mode.
%
%   Events:
%      PreClose       - Event that is sent before the application starts to
%                       close.
%      Close          - Event that is sent after the application closes
%                       sub-figures.
%      PostClose      - Event that is sent after the application is made
%                       invisible.

%   Copyright 2008-2015 The MathWorks, Inc.

properties
    %CONTENTHANDLE Handle to content component
    %   The ContentHandle property contains the handle to the component
    %   that implements the content of the Application.  The content may be
    %   set after constructing an Application, or provided during
    %   construction by Application subclasses by overriding the
    %   createContent method.
    %
    %   See also createContent.
    ContentHandle
    
    %SLEEPONCLOSE Control how the application figure is closed
    %   The SleepOnClose property is a boolean flag that indicates whether
    %   the application should destroy its figure window and itself when
    %   closed (SleepOnClose=false) or set the figure window to be
    %   invisible (SleepOnClose=true).  Applications that are set to
    %   "sleep" can be reopened using the restore method.  The default
    %   value of this property is "false".
    %
    %   See also restore.
    SleepOnClose = false;
    
    %USERDATA User-defined data field
    %   The UserData property is provided as a place for Application users
    %   to store associated data.
    UserData
end

properties(SetAccess=private)
    %FIGURE Handle to the application window
    Figure
end

properties(Access=protected)
    % General listener store
    Listeners
    
    % Statusbar handle 
    StatusBar
    
    % PointerRepository object
    PointerRepo
    
    % Figures that are "owned" by this application
    SubFigures = {};
end

properties(Dependent)
    %Title figure title
    Title
    %Visible
    Visible
end

events(NotifyAccess=private) 
    %PRECLOSE Pre-close event
    %   The PreClose event is sent at the beginning of the application
    %   close process.  This event can be used to stop a close from
    %   occurring before any sub-figures have been closed; however if the
    %   close action may depend on the sub-figures shutting down this may
    %   result in incorrect decisions.  In this case use the Close event.
    %
    %   The PreClose and Close events both provide a CloseEventData object
    %   that can be used to cancel the pending close.
    PreClose
    
    %CLOSE Close event
    %   The Close event is sent after any sub-figures are closed but before
    %   the application window is made invisible. 
    %
    %   The PreClose and Close events both provide a CloseEventData object
    %   that can be used to cancel the pending close.
    Close
    
    %POSTCLOSE Post-close event
    %   The PostClose event is sent after an application window has been
    %   closed.  If the application is not set to "sleep" on close, the
    %   application figure window will have been destroyed at this point.
    PostClose
end


methods
    function obj = Application(varargin)
    %APPLICATION Construct a new Application object
    %   H = mbcgui.Application(PROP1, VAL1, ...) constructs a new
    %   Application object.  The input arguments are passed to the figure
    %   construction and therefore may be any valid figure property-value
    %   pairs.
    
    obj.constructWindow(varargin{:}); 
    
    % Save a handle to the Pointer changing interface to save having to
    % find it all the time.
    obj.PointerRepo= xregGui.PointerRepository;
    
    % Create content
    C = obj.createContent();
    obj.ContentHandle = C;
    
    % Make window visible
    set(obj.Figure, 'Visible', 'on');
    drawnow('expose');
    
    % Call post-creation
    ptrID = obj.setPointer('watch');
    obj.postCreate();
    obj.removePointer(ptrID);
    
    % Allow the figure to be closed
    set(obj.Figure, 'CloseRequestFcn', {@iCloseApp, obj});
    end
  
    
    function delete(obj)
    %DELETE Destroy object
    %   delete(H) destroys the specified Application.  This will send close
    %   events and delete the associated figure window.
    
    if isgraphics(obj.Figure) && ~mbcgui.util.isBeingDestroyed(obj.Figure)
        deleteOwnedFigures(obj);
    	delete(obj.Figure);
    end
    end
end

methods
    function set.ContentHandle(obj, H)
    % Push the new content into the statusbar layout
    L = get(obj.Figure, 'LayoutManager');
    setBoolPackstatus(L, true);
    set(L, 'Elements', {H, obj.StatusBar});
    obj.ContentHandle = H;
    end
    
    function set.SleepOnClose(obj, state)
    if ~islogical(state) || ~isscalar(state)
        error(message('mbc:mbcgui:Application:InvalidPropertyValue'));
    end
    obj.SleepOnClose = state;
    end
    
    function set.Title(obj,t)
    
    set(obj.Figure,'Name',t);
    end
    
    function t = get.Title(obj)
    t = obj.Figure.Name;
    end
    
    function v=get.Visible(obj)
    if isgraphics(obj.Figure)
        v = obj.Figure.Visible;
    else
        v = 'off';
    end
    end
    
    function set.Visible(obj,v)
    if isgraphics(obj.Figure)
        obj.Figure.Visible = v;
    end
    
    end
end


methods(Access=protected)
    function C = createContent(obj)
    %CREATECONTENT Create the content handle
    %   hC = createContent(H) is called during the construction of a new
    %   Application.  The returned handle is inserted as the content of the
    %   application.  This method should be overridden in subclasses if you
    %   want to automatically create content during construction.
    
    C = [];
    end
    
    function postCreate(obj)
    %POSTCREATE Create content after the window is visible
    %   POSTCREATE(OBJ) is called after the window is constructed and
    %   visible.  This method can be overriden if you want to perform part
    %   of the window construction after it is first made visible.
    
    end
end


methods(Access=private)
    function constructWindow(obj, varargin)
    % Construct the application window.
    
    hFig = xregfigure('Visible', 'off', ...
        'KeyPressFcn', @(src, evt) [],...
        'CloseRequestFcn', '', ...
        varargin{:});
    if ~isempty(get(hFig, 'Tag'))
        xregpersistfigpos(hFig);
    end
    xregmoveonscreen(hFig);
    
    obj.Figure = hFig;
    
    % Create a hidden property that allows us to find the application form
    % the figure handle
    mbcgui.hgclassesutil.addprop(hFig, 'MBCApplicationHandle', 'Hidden', true);
    set(hFig, 'MBCApplicationHandle', obj);
    
    % Attach a listener that will synchronise destruction of this object
    % and the figure.
    Ldel = mbcgui.hgclassesutil.listener(hFig, 'ObjectBeingDestroyed', ...
        mbcutils.callback(@iFigDeletion, obj));
    obj.Listeners = Ldel;
    
    % Create the statusbar and position it ready for use during Content
    % construction
    sb = xregGui.statusbar('Parent', hFig);
    obj.StatusBar = sb;
    
    L = xreggridbaglayout(hFig, ...
        'Dimension', [2 1], ...
        'Rowsizes', [-1 20], ...
        'Elements', {[], sb});
    set(hFig, 'LayoutManager', L);
    end
    
    
    function closeWindow(obj, StayOpen)
    % Close the application window.  This makes it invisible and fires the
    % PostClose event
    if nargin<2 || ~StayOpen
        set(obj.Figure, 'Visible', 'off');
    end
    notify(obj, 'PostClose');
    end
    
    
    function closeOwnedFigures(obj)
    % Call close on all the sub-figures
    H = obj.SubFigures;
    for n = 1:length(H);
        if isgraphics(H{n})
            close(H{n});
        end
    end
    end
end
   
methods(Access=protected)
    %TODO: This should be private but an MCOS permissions bug means it cannot be
    function deleteOwnedFigures(obj)
    % Call delete on all the sub-figures
    H = obj.SubFigures;
    for n = 1:length(H);
        if isgraphics(H{n})
            delete(H{n});
        end
    end
    obj.SubFigures = {};
    end
end


% Sleep/restore functions
methods
    function OK = close(obj, StayOpen)
    %CLOSE Close the application
    %   CLOSE(OBJ) closes the application.  This will either destroy the
    %   application object or make the figure window invisible, depending
    %   on the value of the SleepOnClose property.
    %
    %   CLOSE(OBJ, STAYOPEN) allows the caller to specify that the window
    %   should stay open after the close request.  This allows subclasses
    %   to run through the close events if an application window needs to
    %   be reused.
    %
    %   OK = CLOSE(...)  returns a logical flag indicating whether the
    %   close was successful (OK = true) or whether the user cancelled it
    %   (OK = false).
    %
    %   See also SleepOnClose.
    
    if nargin<2
        StayOpen = false;
    end
    OK = false;
    
    % Send a pre-close notification
    E = mbcgui.CloseEventData;
    notify(obj, 'PreClose', E);
    if E.CancelClose
        % Do not close the application
        return
    end
    
    % Close sub-figures
    closeOwnedFigures(obj);
    
    % Send a close notification
    E = mbcgui.CloseEventData;
    notify(obj, 'Close', E);
    if E.CancelClose
        % Do not close the application
        return
    end
    
    % Make application invisible
    closeWindow(obj, StayOpen);
    
    if obj.SleepOnClose
        % Keep the object alive
        sleep(obj);
    else
        % Destroy the application object
        delete(obj);
    end
    OK = true;
    end
    
    
    function restore(obj)
    %RESTORE Re-open a closed application
    %   RESTORE(OBJ) will re-open an application that has been put into a
    %   "sleep" mode when closed.
    %
    %   See also close, SleepOnClose.
    
    obj.resetPointer();
    figure(obj.Figure);
    
    end
end

methods(Access=protected)
    function sleep(obj)
    %SLEEP Put the application into a "sleep" mode
    %   SLEEP(OBJ) is called when the application is being closed but will
    %   not be destroyed because the SleepOnClose property is set to true.
    %   When an application is in sleep mode, it can be quickly re-shown by
    %   calling the restore method.
    %
    %   Subclasses may want to override this method in order to perform
    %   additional processing when the application is made invisible.
    %
    %   See also restore.
    
    end
end

methods(Static)
    function h = find(Tag)
    %FIND Find an existing application handle.
    %
    %   H = FIND(TAG) finds and returns a handle to an existing application
    %   whose figure has the specified tag.  If no application is found, an
    %   empty matrix is returned.
    
    F = findobj(allchild(0),'flat','Tag',Tag);
    if isprop(F, 'MBCApplicationHandle')
        h = get(F, 'MBCApplicationHandle');
    else
        h = [];
    end
    end
end

end


function iFigDeletion(src, evt, obj)
    delete(obj);
end


function iCloseApp(src, evt, obj)
    close(obj);
end