gusucode.com > appdesigner工具箱matlab源码程序 > appdesigner/+appdesigner/+internal/+application/AppDesignEnvironment.m

    classdef AppDesignEnvironment < handle
    % APPDESIGNENVIRONMENT  Launches the App Designer
    %
    %    This class manages App Designer related settings, like index url,
    %    NameSpace, and dependent MATLAB services, like RTC, Debug.
    %    This class will launch App Designer browser window, and will open   
    %    an app if App Designer already launched
    
    %    Copyright 2013-2016 The MathWorks, Inc.
    
    properties (Access = private)
        % the PeerModel manager
        PeerModelManager
        
        % the App Designer browser window controller
        AppDesignerWindowController
        
        % listener to the AppDesignerWindowController being destroyed
        WindowControllerDestroyedListener
    end
    
    properties (SetAccess = private, ...
                GetAccess = ?appdesigner.internal.application.AppCodeTool)
        % the App Designer model
        AppDesignerModel        
    end
    
     properties (Constant)
        % URLs for different App Designer modes
        ReleaseUrl = 'toolbox/matlab/appdesigner/web/index.html'
        DebugUrl = 'toolbox/matlab/appdesigner/web/index-debug.html'
        
        % PeerModel manager namespace for App Designer
        NameSpace = '/appdesigner'
     end
    
    methods
        function obj = AppDesignEnvironment(peerModelManager, appDesignerModel)
            
            appdesigner.internal.application.AppDesignEnvironment.initializeMATLABServices();
            
            obj.PeerModelManager = peerModelManager;
            obj.AppDesignerModel = appDesignerModel;
        end
        
        function startAppDesigner(obj, varargin)
            % STARTAPPDESIGNER start App Designer 
            % 
            % If call this method without arugments, will just launching
            % App Designer with opening a default app
            %
            % The optional arguments:
            %    File path of the app to open
            %    URL to use for launching App Designer
            %    Browser for starting App Designer

            narginchk(1, 4);
            
            % file path of the app to open
            filePath = '';
            if nargin > 1
                filePath = varargin{1};
            end
                
            if ~isempty(obj.AppDesignerWindowController) ...
                    && isvalid(obj.AppDesignerWindowController)
                % App Designer already opened            
                obj.openApp(filePath);
            else
                obj.AppDesignerModel.InitialLoadingApp = filePath;
                
                % create Connection for AppDesignerWindowController
                pathToWebPage = appdesigner.internal.application.AppDesignEnvironment.ReleaseUrl;
                if nargin > 2
                    pathToWebPage = varargin{2};
                end
                connection = appdesservices.internal.peermodel.Connection(pathToWebPage);            

                % create the AppDesignerWindowController which will launch App Designer
                % browser window            
                obj.AppDesignerWindowController = appdesigner.internal.application.AppDesignerWindowController( ...
                    obj.PeerModelManager, obj.AppDesignerModel, connection);

                % listen to when the AppDesignerWindowController is destroyed which means
                % the App Designer is closed by the users
                obj.WindowControllerDestroyedListener = addlistener(obj.AppDesignerWindowController,'ObjectBeingDestroyed', ...
                    @(source, event)delete(obj));
                
                % Build query parameters for browser
                
                % The current working directory will be used to open the file
                % dialogs in a location that based on the pwd of MATLAB at the
                % time of open            
                currentWorkingDirectory = pwd;

                % Add a trailing file separator if necessary
                if ~strcmp(currentWorkingDirectory(end), filesep)
                    currentWorkingDirectory = [currentWorkingDirectory, filesep];
                end
                
                % Create the query parameters structure.
                queryParams = struct('CWD', currentWorkingDirectory);
                
                % Add file path as query param if it is not empty
                if ~isempty(filePath)
                   queryParams.FilePath = filePath;
                end
                
                % Add showIntroDialog as query param if it is not false
                % If it is empty, this means it is the first time launching
                % App Designer and so want to show the intro dialog.
                showIntroDialog = appdesigner.internal.application.getAppDesignerPref('ShowIntroDialog');  
                if isempty(showIntroDialog) || showIntroDialog == true
                    queryParams.ShowIntroDialog = 'true';
                end
                
                % Add showCodeViewTips as query param if it is not false
                % If it is empty, this means it is the first time launching
                % App Designer and so want to show the code view tips.
                showCodeViewTips = appdesigner.internal.application.getAppDesignerPref('ShowCodeViewTips');  
                if isempty(showCodeViewTips) || showCodeViewTips == true
                    queryParams.ShowCodeViewTips = 'true';
                end
                
                showAxesBanner = appdesigner.internal.application.getAppDesignerPref('ShowAxesBanner');
                if isempty(showAxesBanner) || showAxesBanner == true
                    queryParams.ShowAxesBanner = 'true';
                end
                
                % Add ShowCompatibilityDialog as query param if it is not false
                % If it is empty, this means it is the first time launching
                % App Designer and so want to show the code view tips.
                ShowCompatibilityDialog = appdesigner.internal.application.getAppDesignerPref('ShowCompatibilityDialog');  
                if isempty(ShowCompatibilityDialog) || ShowCompatibilityDialog == true
                    queryParams.ShowCompatibilityDialog = 'true';
                end
                
                % Add ShowProgrammingTips as query param if it is not false
                ShowProgrammingTips = appdesigner.internal.application.getAppDesignerPref('ShowProgrammingTips');  
                if isempty(ShowProgrammingTips) || ShowProgrammingTips == true
                    queryParams.ShowProgrammingTips = 'true';
                end
                
                % parse arguments to start App Designer: queryParams and browser
                inputArguments = {queryParams};

                if nargin > 3
                    % browser controller factory
                    inputArguments{end+1} = varargin{3};
                end
                obj.AppDesignerWindowController.startBrowser(inputArguments{:});
            end
        end
        
        function openApp(obj, filePath)
            %  OPENAPP Open the app into the started App Designer
            %     
            %   openApp() will bring the App Designer to front, and ask the
            %   App Designer to open the app if the file path is not empty
                        
            obj.AppDesignerWindowController.bringToFront();
            
            if ~isempty(filePath)
                obj.AppDesignerModel.openApp(filePath);
            end
        end
        
        function delete(obj)
            if ~isempty(obj.WindowControllerDestroyedListener)
                delete(obj.WindowControllerDestroyedListener);
                obj.WindowControllerDestroyedListener = [];
            end
            
            if ~isempty(obj.AppDesignerWindowController) ...
                    && isvalid(obj.AppDesignerWindowController)
                % The object would be empty if not calling
                % startAppDesigner()
                % If the user hits 'X' to close App Designer,
                % AppDesignerWindowController would already be destroyed
                delete(obj.AppDesignerWindowController);
            end
        end
    end
    
    methods (Static, Access = private)
        function initializeMATLABServices()
            % Initialize Editor Data Service in support of RTC
            import com.mathworks.services.editordataservice.*;
            edsManager = EditorDataServiceManager.getInstance();
            edsManager.initialize();
            
            % Todo remove this once done by default when breakpoint service is started 
            % G1284320
            javaMethod('initialized','com.mathworks.matlabserver.editordataservice.EditorDataServiceInitialize');
            
            % Todo remove this once done by default in connector code
            % initialize MatlabBreakpointMessageService manually
            com.mathworks.mde.editor.plugins.matlab.MatlabBreakpointMessageService.getInstance();
                    
            % Start the connector clipboard service to allow interaction
            % with the system clipboard
            com.mathworks.services.clipboardservice.ConnectorClipboardService.getInstance();
        end
    end
    
    methods (Static)
        function peerModelManager = getPeerModelManager(uniqueNameSpace)
            % This method will be called in the very beginning of starting
            % AppDesigner, and at that time connector probably would not be
            % fully on, related connector java class path not being set
            % correctly. getClientInstance() call would fail, especially in
            % the cluster, runlikebat much more likely to fail.
            % So ensure connector fully on, and the following call would be
            % no-op if connector already fully started, otherwise wait
            % until fully loaded
            connector.ensureServiceOn();
            
            % set up the peer model manager with appdesigner namespace            
            peerModelManager = com.mathworks.peermodel.PeerModelManagers.getClientInstance(uniqueNameSpace);
            peerModelManager.setSyncEnabled(true);
        end
    end
end