classdef FigureToolstripActionFactory < handle
    % FIGURETOOLSTRIPACTIONFACTORY Factory for figure toolstrip actions
    
    % Copyright 2019 The MathWorks, Inc.
    properties(Access=private, Constant)
        ActionMapping = {
            {...
            char("motw.embeddedfigures.newfigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Base',...
            @(~) figure,...
            false)
            },...
            {...
            char("motw.embeddedfigures.openfigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Base',...
            @(~) uiopen('figure'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.grid"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~) localGrid(ax,{"XGrid","YGrid","ZGrid"},{"on","on","off"}),...
            false)
            },...
            {...
            char("motw.embeddedfigures.xgrid"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~) localGrid(ax,{"XGrid","YGrid","ZGrid"},{"on","off","off"}),...
            false)
            },...
            {...
            char("motw.embeddedfigures.ygrid"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~) localGrid(ax,{"XGrid","YGrid","ZGrid"},{"off","on","off"}),...
            false)
            },...
            {...
            char("motw.embeddedfigures.removegrid"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~) localGrid(ax,{"XGrid","YGrid","ZGrid"},{"off","off","off"}),...
            false)            
            },...
            {...
            char("motw.embeddedfigures.title"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~)  localLabel(ax, 'motw.embeddedfigures.title'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.xlabel"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~)  localLabel(ax, 'motw.embeddedfigures.xlabel'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.ylabel"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~)  localLabel(ax, 'motw.embeddedfigures.ylabel'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.zlabel"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~)  localLabel(ax, 'motw.embeddedfigures.zlabel'),...
            false)
            },...            
            {...
            char("motw.embeddedfigures.legend"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localLegend(ax, value),...
            false)
            },...
            {...
            char("motw.embeddedfigures.removelegend"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~) legend(ax, 'hide'),...
            false)
            },...            
            {...
            char("motw.embeddedfigures.colorbar"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localColorbar(ax, value),...
            false)
            },...
            {...
            char("motw.embeddedfigures.removecolorbar"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, ~) colorbar(ax, 'hide'),...
            false)
            },...            
            {...
            char("motw.embeddedfigures.closefigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) close(fig),...
            false)
            },...
            {...
            char("motw.embeddedfigures.generatecode"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) makemcode(fig,'Output','-editor','showstatusbar',false),...
            false)
            },...                        
            {...
            char("motw.embeddedfigures.savefigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) filemenufcn(fig,'FileSave'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.saveasfigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) filemenufcn(fig,'FileSaveAs'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.basicfit"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) basicfitdatastat('bfit', fig, 'bf'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.datastats"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) basicfitdatastat('bfit', fig, 'ds'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.datalink"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) linkdata(fig,'showdialog',4),... % Stack depth 4 for linked brushing 
            true)
            },...
            {...
            char("motw.embeddedfigures.plotedit"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, options) matlab.graphics.internal.plotEditHelper(fig,options),...
            false)
            },...
            {...
            char("motw.embeddedfigures.plotedit.close"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.plotEditHelper(fig,'off'),...
            false)
            },...            
            {...
            char("motw.embeddedfigures.colormapeditor"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) localColormapFcn(fig),...
            false)
            },...
            {...
            char("motw.embeddedfigures.clearfigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  matlab.graphics.internal.clfHelper(fig),...
            false)
            },...
            {...
            char("motw.embeddedfigures.inspector"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, ~)  matlab.graphics.internal.inspectorHelper(selObj),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'show'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.close"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'close'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.orbit"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, val) matlab.graphics.internal.cameratoolbarHelper(fig,'orbit', val),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.pan"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, val) matlab.graphics.internal.cameratoolbarHelper(fig,'pan', val),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.dollyhv"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, val) matlab.graphics.internal.cameratoolbarHelper(fig,'dollyhv', val),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.dollyfb"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, val) matlab.graphics.internal.cameratoolbarHelper(fig,'dollyfb', val),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.zoom"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, val) matlab.graphics.internal.cameratoolbarHelper(fig,'zoom', val),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.roll"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, val) matlab.graphics.internal.cameratoolbarHelper(fig,'roll', val),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.resetx"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'x'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.resety"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'y'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.resetz"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'z'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.resetnone"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'none'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.togglescenelight"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'togglescenelight'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.orthographic"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'orthographic'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.perspective"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'perspective'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.resetcameralight"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'resetcameralight'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.camera.stop"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.cameratoolbarHelper(fig,'stop'),...
            false)
            },...
            {...
            char("motw.embeddedfigures.annotations.textarrow"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  startscribeobject('TextArrow', fig),...
            false)
            },...            
            {...
            char("motw.embeddedfigures.annotations.arrow"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  startscribeobject('Arrow', fig),...
            false)
            },...       
            {...
            char("motw.embeddedfigures.annotations.doublearrow"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  startscribeobject('DoubleArrow', fig),...
            false)
            },...    
            {...
            char("motw.embeddedfigures.annotations.line"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  startscribeobject('Line', fig),...
            false)
            },...    
            {...
            char("motw.embeddedfigures.linkedplot"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  locallinkedplotcallback(fig, true),...
            false)
            },...
            {...
            char("motw.embeddedfigures.copyfigure"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~)  copygraphics(fig),...
            false)
            },...             
            {...
            char("motw.embeddedfigures.font"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, value) localFontType(fig, value),...
            false)
            },...    
            {...
            char("motw.embeddedfigures.plotedit.text.font"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selectedObj, value) localPropFcn(selectedObj, 'FontName', value),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.scaleUpFont"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, value) localFontIncrease(fig),...
            false)
            },...               
            {...
            char("motw.embeddedfigures.scaleDownFont"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) localFontDecrease(fig),...
            false)
            },...
            {...
            char("motw.embeddedfigures.insertaxes"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, value) matlab.graphics.internal.insertAxes(fig, value),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.XScale"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localPropFcn(ax,'XScale', value),...
            false)
            },...       
            {...
            char("motw.embeddedfigures.YScale"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localPropFcn(ax,'YScale', value),...
            false)
            },...                   
            {...
            char("motw.embeddedfigures.ZScale"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localPropFcn(ax,'ZScale', value),...
            false)
            },...    
            {...
            char("motw.embeddedfigures.hold"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localHold(ax, value),...
            false)
            }... 
            {...
            char("motw.embeddedfigures.undo1"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) localUndo(fig),...
            false)
            }...
            {...
            char("motw.embeddedfigures.redo1"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) localRedo(fig),...
            false)
            },...                      
            {...
            char("motw.embeddedfigures.XMin"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localSetLimit(ax, 'X', 'Min', value),...
            false)
            },...                
            {...
            char("motw.embeddedfigures.YMin"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localSetLimit(ax, 'Y', 'Min', value),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.ZMin"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localSetLimit(ax, 'Z', 'Min', value),...
            false)
            },...  
            {...
            char("motw.embeddedfigures.XMax"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localSetLimit(ax, 'X', 'Max', value),...
            false)
            },...                
            {...
            char("motw.embeddedfigures.YMax"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localSetLimit(ax, 'Y', 'Max', value),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.ZMax"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Axes',...
            @(ax, value) localSetLimit(ax, 'Z', 'Max', value),...
            false)
            },...             
            {...
            char("motw.embeddedfigures.export"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.exportHelper(fig, ''),...
            false)
            },... 
            {...
            char("motw.embeddedfigures.exportPDF"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.exportHelper(fig, 'pdf'),...
            false)
            },... 
            {...
            char("motw.embeddedfigures.exportPNG"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) matlab.graphics.internal.exportHelper(fig, 'png'),...
            false)
            },... 
            {...
            char("motw.embeddedfigures.print"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Figure',...
            @(fig, ~) printdlg(fig),...
            false)
            },...
            {...
            char("motw.embeddedfigures.plotedit.bold"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'FontWeight', value),...
            false)
            },...            
            {...
            char("motw.embeddedfigures.plotedit.italics"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'FontAngle', value),...
            false)
            },...                 
            {...
            char("motw.embeddedfigures.plotedit.alignleft"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'left', value),...
            false)
            },...     
            {...
            char("motw.embeddedfigures.plotedit.aligncenter"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'center', value),...
            false)
            },...         
            {...
            char("motw.embeddedfigures.plotedit.alignright"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'right', value),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.plotedit.text.fontColor"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'FontColor', value),...
            false)
            },...  
            {...
            char("motw.embeddedfigures.plotedit.backgroundColor"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'BackgroundColor', value),...
            false)
            },...       
            {...
            char("motw.embeddedfigures.plotedit.edgeColor"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'EdgeColor', value),...
            false)
            },...               
            {...
            char("motw.embeddedfigures.lineStyle.solid"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, ~) localLinePropFcn(selObj, 'LineStyle', '-'),...
            false)
            },...               
            {...
            char("motw.embeddedfigures.lineStyle.dashed"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, ~) localLinePropFcn(selObj, 'LineStyle', '--'),...
            false)
            },...             
            {...
            char("motw.embeddedfigures.lineStyle.dotted"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, ~) localLinePropFcn(selObj, 'LineStyle', ':'),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.lineStyle.dashdot"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, ~) localLinePropFcn(selObj, 'LineStyle', '-.'),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.lineStyle.none"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, ~) localLinePropFcn(selObj, 'LineStyle', 'none'),...
            false)
            },...              
            {...
            char("motw.embeddedfigures.all.lineWidth"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localLinePropFcn(selObj, 'LineWidth', value),...
            false)
            },...             
            {...
            char("motw.embeddedfigures.plotedit.lineWidth"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localLinePropFcn(selObj, 'LineWidth', value),...
            false)
            },...                            
            {...
            char("motw.embeddedfigures.plotedit.fontSize"),...
            matlab.graphics.internal.FigureToolstripActionFactory.createRegistration(...
            'Selection',...
            @(selObj, value) localPropFcn(selObj, 'FontSize', value),...
            false)
            }...              
            };
    end
    
    properties (SetObservable=true, SetAccess='protected', GetAccess='public', Dependent=false, Hidden=false)
        ActionMap;
    end
    
    methods(Static, Access='public')
        
        function obj = getInstance()
            persistent factoryInstance;
            mlock;
            if isempty(factoryInstance)
                factoryInstance = matlab.graphics.internal.FigureToolstripActionFactory;
                factoryInstance.createFactoryMaps();
            end
            obj = factoryInstance;
            
        end
        
        function s = createRegistration(type, action, doMorph)
            s = struct('ActionType', type, 'Action', action, 'Morph', doMorph);
        end
        
    end
    
    methods(Access=private)
        function obj = FigureToolstripActionFactory()
        end
        
        function createFactoryMaps(this)
            this.ActionMap = this.createMapping(this.ActionMapping);
        end
        
        function map = createMapping(~, mapping)
            keyset = cell(1,length(mapping));
            valueset = cell(1,length(mapping));
            
            for i=1:length(mapping)
                keyset{i} = mapping{1,i}{1,1};
                valueset{i} = mapping{1,i}{1,2};
            end
            
            map = containers.Map(keyset',...
                valueset');
        end
    end
    
    methods(Access=public)
        function executeAction(this, key, varargin)
            
            try
                a = this.getAction(key);

                % If my action is somehow undefined, just return
                if isempty(a)
                    return;
                end

                % Get the current axes of the figure
                fig = get(groot,'CurrentFigure');
                ax = matlab.graphics.axis.Axes.empty;
                if ~isempty(fig)
                    ax = fig.CurrentAxes;
                end

                % If this action is unsupported in embedded figure, trigger a
                % morph
                if ~isempty(fig) && a.Morph
                    matlab.ui.internal.prepareFigureFor(fig, mfilename('fullpath'));
                end

                switch a.ActionType
                    case 'Base'
                        a.Action(varargin{:});
                    case 'Axes'
                        if ~isempty(ax)
                            a.Action(ax, varargin{:});
                        end
                    case 'Figure'
                        if ~isempty(fig)
                            a.Action(fig, varargin{:});
                        end
                    case 'Selection'
                        if ~isempty(fig)
                            selectedObjects = findall(fig, 'Selected', 'on');
                            a.Action(selectedObjects, varargin{:});
                        end                        
                end    
            catch 
                % Discuss best way to handle errors
            end
        end
        
        function action = getAction(this, key)
            action = [];
            
            keyList = string(keys(this.ActionMap));
            
            if any(key == keyList)
                action = this.ActionMap(key);
            end
        end
    end
end

function locallinkedplotcallback(fig, value)
if ~isempty(fig)
    if value
        datamanager.linkbtnoncallback('on',fig);
    else
        linkdata(fig, 'off');
    end
end

end

function localColorbar(ax, value)
    currentColorbar = [];
    % Find all the colorbars and see which one belongs to the current axes
    cbars = findobj(ancestor(ax, 'figure'), '-isa', 'matlab.graphics.illustration.ColorBar');
    for i=1:numel(cbars)
        cbar = cbars(i);

        if cbar.Axes == ax
            currentColorbar = cbar;
        end
    end
    
    if isempty(currentColorbar)    
        cb = colorbar(ax);
        matlab.graphics.annotation.internal.registerUndoLegendColorbar(cb);
    else
        
        oldValue = get(currentColorbar, 'Visible');
        
        % Set the new value
        set(currentColorbar, 'Visible', value);
        
        % Register to the undo stack
        fcn = @(ax,~) set(currentColorbar, 'Visible', value);
        invfcn = @(ax,~) set(currentColorbar, 'Visible', oldValue);
        localRegisterAsUndoable(currentColorbar, fcn, invfcn, 'colorbar');
    end
end

function localColormapFcn(fig)
% Pass the current axes to the colormap if available, 
% if not use the current figure
cmapObj = get(fig,'CurrentAxes');
if isempty(cmapObj)
   cmapObj = fig;
end
colormapeditor(cmapObj);
end

function localLegend(ax, value)
    if isempty(ax.Legend)
        cb = legend(ax);
        matlab.graphics.annotation.internal.registerUndoLegendColorbar(cb);
    else

        oldValue = get(ax.Legend, 'Visible');

        % Set the new value
        set(ax.Legend, 'Visible', value);
        
        % Register to the undo stack
        fcn = @(ax,~) set(ax.Legend, 'Visible', value);
        invfcn = @(ax,~) set(ax.Legend, 'Visible', oldValue);
        localRegisterAsUndoable(ax, fcn, invfcn, 'legend');
    end
end

function localLabel(ax, actionId)

% Get the label or Title currently editing
matlab.graphics.internal.labelHelper(ax, actionId);

end

function localGrid(ax,types,values)

% Create cell arrays for prop-val pairs to allow vectorized set in 
% Function, InverseFunction
lastValues = get(ax,cellstr(types));
fcn = @(ax,~) set(ax,cellstr(types),values);
invfcn = @(ax,~) set(ax,cellstr(types),lastValues);
localEvaluateAsUndoable(ax, fcn, invfcn, 'grid')

end

function localHold(ax,value)

% Turns off/on hold with undo/redo support
if value
   fcn = @(ax,~) hold(ax,"on");
   invfcn = @(ax,~) hold(ax,"off");
else
   fcn = @(ax,~) hold(ax,"off");
   invfcn = @(ax,~) hold(ax,"on");
end
localEvaluateAsUndoable(ax, fcn, invfcn, 'hold on')

end

function localFontIncrease(fig)

% Increases font with undo/redo support
fcn = @(fig,~) matlab.graphics.internal.fontHelper(fig, 'increase');
invfcn  = @(fig,~) matlab.graphics.internal.fontHelper(fig, 'decrease');
localEvaluateAsUndoable(fig, fcn, invfcn, 'font increase')

end

function localFontDecrease(fig)

% Decreases font with undo/redo support
fcn = @(fig,~) matlab.graphics.internal.fontHelper(fig, 'decrease');
invfcn  = @(fig,~) @(fig,~) matlab.graphics.internal.fontHelper(fig, 'increase');
localEvaluateAsUndoable(fig, fcn, invfcn, 'font decrease')
end

function localFontType(fig, value)

lastType = get(fig.CurrentAxes, 'FontName');
% Set the FontName for all axes and text object in the figure
matlab.graphics.internal.fontHelper(fig, value);
cmd.Name = ['FontName'];
cmd.Function = @(fig,~) matlab.graphics.internal.fontHelper(fig, value);
cmd.InverseFunction  = @(fig,~) matlab.graphics.internal.fontHelper(fig, lastType);
cmd.Varargin = {fig};
cmd.InverseVarargin = {fig};
uiundo(fig,'function',cmd);

end

function localSetLimit(ax, prop, minOrMax, value)
    try
        % Get the Limit property name and current value for undo/redo
        propName = localGetPropNameFromAxes(ax, prop);

        if ~isempty(propName)

            limits = get(ax, propName);

            inversePropValue = limits;

            % Convert he value of the incoming limits based on the Axes Ruler
            % e.g. Numeric, DateTime, Categorical or Duration
            val = localConvertValue(ax, prop, value);

            % Set either the Max or Min Limit
            if strcmp(minOrMax, 'Min')
                limits(1) = val;
            else
                limits(2) = val;
            end

            % Set the Limits
            set(ax, propName, limits);

            cmd.Name = propName;
            cmd.Function = @(ax,~) set(ax, propName, limits);
            cmd.InverseFunction  = @(ax,~) set(ax, propName, inversePropValue);
            cmd.Varargin = {ax};
            cmd.InverseVarargin = {ax};
            uiundo(ancestor(ax,'figure'),'function',cmd);
        end
    catch 
        % Catch errors here so they don't propogate
    end
end

function result = localGetPropNameFromAxes(ax, prop)    

    result = strcat(prop, 'Lim');
    
    switch class(ax)
        case 'matlab.graphics.axis.PolarAxes'
                if strcmp(prop, 'X')
                    result = 'RLim';
                elseif strcmp(prop, 'Y')
                    result = 'ThetaLim';
                end                
        case 'matlab.graphics.axis.GeographicAxes'
                if strcmp(prop, 'X')
                    result = 'LatitudeLimits';
                elseif strcmp(prop, 'Y')
                    result = 'LongitudeLimits';
                end
    end

    if ~isprop(ax, result)
        result = [];
    end
    
end

function result = localConvertValue(ax, prop, value)
% Get the Ruler Type and Ruler Handle
propName = strcat(prop, "Ruler");
ruler = get(ax, propName);

% Convert value based on Ruler Type
switch class(ruler)
    case 'matlab.graphics.axis.decorator.NumericRuler'
        result = str2double(value);
    case 'matlab.graphics.axis.decorator.DatetimeRuler'
        if strcmp(value, 'now') || ...
            strcmp(value, 'today') || ...
            strcmp(value, 'yesterday') || ...
            strcmp(value, 'tomorrow')
            result = datetime(value);
        else
            result = datetime(value, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
        end
    case 'matlab.graphics.axis.decorator.DurationRuler'                
        result = duration(numVal, 'InputFormat', 'dd:hh:mm:ss');
    case 'matlab.graphics.axis.decorator.CategoricalRuler'
        if ismember(value, limits)
            result  = value;
        end
end
end

function localLinePropFcn(selectedObjs, prop, value)
    % If I have no selected objects, then I am not in plot edit mode
    % and am using the controls that apply to all lines in the Figure Tab
    if isempty(selectedObjs)
        selectedObjs = findall(gcf, 'type', 'line', '-or', 'type', 'lineshape' );
    end
    
    localPropFcn(selectedObjs, prop, value);
end

function localPropFcn(selectedObjs, prop, value)
% TODO: Figure out how to make this work better for charts

propValue = value;
propName = prop;

switch prop
    case 'FontColor'
        propName = 'Color';
    case 'FontWeight'
        if value 
            propValue = 'bold';
        else
            propValue = 'normal';
        end
    case'FontAngle'
        if value 
            propValue = 'italic';
        else
            propValue = 'normal';
        end
    case {'left', 'center', 'right'}
        propValue = prop;
        propName = 'HorizontalAlignment';              
    case 'BackgroundColor'
        if all(~isprop(selectedObjs, 'BackgroundColor')) &&...
                all(isprop(selectedObjs, 'Color'))
            propName = 'Color';
        elseif all(~isprop(selectedObjs, 'BackgroundColor')) &&...
                all(isprop(selectedObjs, 'FaceColor'))
            propName = 'FaceColor';
        end    
    case 'EdgeColor'
        if all(~isprop(selectedObjs, 'EdgeColor')) &&...
                all(isprop(selectedObjs, 'Color'))
            propName = 'Color';
        end
    case 'XScale'
        if all(isa(selectedObjs, 'matlab.graphics.axis.PolarAxes'))
            propName = 'AlphaScale';
        end
end

% Update only those that have propname as a valid property on the object
% filter the rest out
selectedObjs = selectedObjs(isprop(selectedObjs, propName));

inversePropValue = get(selectedObjs, propName);
cmd.Name = [propName];
cmd.Function = @(selectedObjs,~) set(selectedObjs, propName, propValue);
cmd.InverseFunction  = @(selectedObjs,~) set(selectedObjs, propName, inversePropValue);
cmd.Varargin = {selectedObjs};
cmd.InverseVarargin = {selectedObjs};
% SelectedObjs should always have at least 1 member
fig = ancestor(selectedObjs(1), 'figure');

uiundo(fig,'function',cmd);

% Update with the new value
set(selectedObjs, propName, propValue)

% Not every property kicks off a redraw, make sure they do
% so it updates the figure
drawnow update;

end

function localUndo(f)
    f.uitools_FigureToolManager;
    cm = f.uitools_FigureToolManager.CommandManager;
    cm.undo;
    matlab.graphics.internal.FigureToolstripManager.updateActions...
                    ('motw.embeddedfigures.undo1','selected',false);
end

function localRedo(f)
    f.uitools_FigureToolManager;
    cm = f.uitools_FigureToolManager.CommandManager;
    cm.redo;
end

function localEvaluateAsUndoable(hObj, fcn, invfcn, name)
    
% Evaluate fcn and add an invertable operation representing the operation
% to the undo stack
feval(fcn,hObj);
localRegisterAsUndoable(hObj, fcn, invfcn, name);

end

function localRegisterAsUndoable(hObj, fcn, invfcn, name)

% Register the fcn and its inverse to the undo stack
cmd.Name = name;
cmd.Function = fcn;
cmd.InverseFunction  = invfcn;
cmd.Varargin = {hObj};
cmd.InverseVarargin = {hObj};
uiundo(ancestor(hObj,'figure'),'function',cmd);

end