function plottools (varargin)
% PLOTTOOLS  Show or hide the plot-editing tools for a figure.
%    PLOTTOOLS ON shows all the tools for the current figure.
%    PLOTTOOLS OFF hides all the tools.
%    PLOTTOOLS TOGGLE toggles the visibility of the tools.
%    PLOTTOOLS with no arguments is the same as ON.
% 
% The first argument may be the handle to a figure, like so:
%    PLOTTOOLS (h, 'on')
%
% The last argument may be the name of a specific component, like so:
%    PLOTTOOLS ('on', 'figurepalette') or
%    PLOTTOOLS (h, 'on', 'figurepalette')
% The available components are named 'figurepalette', 'plotbrowser',
% and 'propertyeditor'.
%
% See also FIGUREPALETTE, PLOTBROWSER, and PROPERTYEDITOR.

%   Copyright 1984-2004 The MathWorks, Inc.



% plottools ('on')
% plottools ('on', 'figurepalette')
% plottools (h, 'on', 'figurepalette')
% plottools (h, 'on')
% plottools (h, 'on', 'figurepalette')

error (nargchk (0,3,nargin))

% Defaults:
fig = [];
action = 'on';
comp = 'all';

% Use the arguments:
if nargin == 1 
    if ishandle (varargin{1})
        fig = varargin{1};
    elseif ischar (varargin{1})
        action = varargin{1};
    end
elseif nargin == 2         % either (h, 'on') or ('on', 'panel')
    if ishandle (varargin{1})
        fig = varargin{1};
        action = varargin{2};
    elseif ischar (varargin{1})
        action = varargin{1};
        comp = varargin{2};
    end
elseif nargin == 3
    fig = varargin{1};
    action = varargin{2};
    comp = varargin{3};
end

if isempty(fig)
  fig = gcf;
  drawnow;
end

if isempty (get (fig, 'JavaFrame'))
    error (sprintf ('The plot tools are not supported on this platform.'));
end

% If we're only showing/hiding one of the plot tools, we might
% as well just call showplottool.  It'll do all the right things.
if (strcmp (comp, 'all') == 0)
    showplottool (fig, action, comp);
    return;
end

% Get the toolbar buttons to be enabled/disabled later:
onBtn  = uigettool (fig, 'Plottools.PlottoolsOn');
offBtn = uigettool (fig, 'Plottools.PlottoolsOff');

% Figure out if 'toggle' means 'on' or 'off':
if (strcmp (action, 'toggle') ~= 0)
    if ~isprop (fig, 'figurepalette')
        action = 'on';
    else
        compTmp = getplottool (fig, 'figurepalette');
        if (compTmp.isVisible)
            action = 'off';
        else
            action = 'on';
        end
    end
end

% Do it:
switch (action)
    case {'on', 'show'}
        oldptr = get (fig, 'Pointer');
        set (fig, 'Pointer', 'watch');
        plotedit (fig, 'on', oldptr);
        fp = getplottool (fig, 'figurepalette');
        pb = getplottool (fig, 'plotbrowser');
        pe = getplottool (fig, 'propertyeditor');
        sm = get (handle(fig), 'SelectionManager');
        sm.setShowingTools (true);
        if ~isempty(onBtn) && ~isempty(offBtn) && ishandle(onBtn) && ishandle(offBtn)
            set (onBtn, 'enable', 'off');
            set (offBtn, 'enable', 'on');
        end
        drawnow;
        fp.setVisible (true);
        pb.setVisible (true);
        pe.setVisible (true);
        set (fig, 'Pointer', oldptr);
    case {'off', 'hide'}
        plotedit (fig, 'off');
        fp = getplottool (fig, 'figurepalette');
        pb = getplottool (fig, 'plotbrowser');
        pe = getplottool (fig, 'propertyeditor');
        sm = get (handle(fig), 'SelectionManager');
        sm.setShowingTools (false);
        if ~isempty(onBtn) && ~isempty(offBtn) && ishandle(onBtn) && ishandle(offBtn)
            set (onBtn, 'enable', 'on');
            set (offBtn, 'enable', 'off');
        end
        drawnow;
        fp.setVisible (false);
        pb.setVisible (false);
        pe.setVisible (false);
    case 'get'
end
