function [c,t] = uitoolfactory(p,id)
% This undocumented function may change in a future release.

%UITOOLFACTORY Creates built-in toolbar components
% uitoolfactory 
%     With no arguments, displays registered toolbar 
%     components to the command window.
%
% [C] = uitoolfactory(H,'GroupName.ComponentName') 
%     H is the parent toolbar handle. 
%     'GroupName' is the name of the toolbar group
%     'ComponentName' is the name of the toolbar component
%     C is the toolbar component
%
% [C] = uitoolfactory(H,'GroupName')
%     Specifying a 'GroupName' without a 'ComponentName' will create
%     all the components, C, registered in that group.
%
% NOTE:
% C will return empty if the component is no longer supported as a
% built-in component.
%
% [INFO] = uitoolfactory(H,'getinfo')
%     Returns a struct containing information on all registered tools.
%
% Example: 
%
%  % Create a figure with a simple toolbar
%  FIG = figure('toolbar','none');
%  H = uitoolbar('parent',FIG);
%  C = uitoolfactory(H,'Standard.EditPlot'); 
%  C = uitoolfactory(H,'Exploration.ZoomIn'); 
%  set(C,'Separator','on');
%  C = uitoolfactory(H,'Exploration.ZoomOut');
%  C = uitoolfactory(H,'Exploration.Pan');
%
% See also UIGETTOOL.

%   Copyright 1984-2003 The MathWorks, Inc.
%   $Revision: 1.1.6.11 $ $Date: 2004/01/15 21:15:44 $


if nargin==0
    % Pretty print output
    info = localGetToolbarComponentInfo;
    localPrettyPrint(info); 
    
% info = uitoolfactory('getinfo')    
elseif nargin==1 & nargout==1
    info = localGetToolbarComponentInfo;    
    % Convert cell of structs to structure
    for n = 1:length(info)
       c(n) = info{n};
    end    

elseif nargin==2 
    c = localMainCreate(p,id);
end

%---------------------------------------------------%
function [hComponent] = localMainCreate(p,fullid)
% Creates one or more uitoolbar components

hComponent = [];

% Get group and name from full ID
[idgroup,idname] = strtok(fullid,'.');
if ~isempty(idname)
    idname = idname(2:end);
end
    
% p must be a toolbar
if strcmpi(get(p,'Type'),'uitoolbar')
    hToolbar = p;
else
    error('uitools:uitoolbarfactory','Handle must be a toolbar');
end

% Get full listing of registered components
dat = localGetToolbarComponentInfo;

% No name specified, so load group of toolbar components
if isempty(idname)
    
    % Cycle through each item and add appropriately
    for n = 1:length(dat);
        info = dat{n};
        if strcmp(info.group,idgroup)
             hComponent(end+1) = localCreateToolbarComponent(hToolbar,info);    
        end
    end
    
% Load individual toolbar components
else
    % Cycle through each item 
    for n = 1:length(dat);
        info = dat{n};
        if strcmp(info.group,idgroup) && strcmp(info.name,idname);
             hComponent = localCreateToolbarComponent(hToolbar,info);
             break;
        end
    end    
end

%---------------------------------------------------%
function [h] = localCreateToolbarComponent(p,info)
% Creates individual components (i.e. uitoggletool)

%mlroot = 'S:/A/matlab'; % for sandbox testing
mlroot = matlabroot;

ICONROOT = [mlroot, '/toolbox/matlab/icons/'];
props = info.properties;

% Create tag based on group & name. It is better to keep this tag unique as
% we do know. It is used to improve the performance of creating the icon
% from CData in Java Figures. It is used as the key to search for a match
% in a table with Java icons created for component generated by this
% uitoolfactory. 
props.tag = [info.group,'.',info.name];

% Load cdata from *.gif file
filename = info.icon;
if length(filename)>3 && strncmp(filename(end-3:end),'.gif',4)
    [cdata,map] = imread([ICONROOT,info.icon]);
    % Set all white (1,1,1) colors to be transparent (nan)
    ind = find(map(:,1)+map(:,2)+map(:,3)==3);
    map(ind) = nan;
    props.cdata = ind2rgb(cdata,map);

% Load cdata from *.mat file
else
    dat = load([ICONROOT,info.icon],'cdata');
    props.cdata = dat.cdata;
end

% Specify PARENT property
props.parent = p;

% Create toolbar component
h = feval(info.constructor,props);

% Properties common to all uitool components
set(h,'HandleVisibility','off','Serializable','off');

%---------------------------------------------------%
function localPrettyPrint(uidata)
% Pretty prints toolbar data to command window
% in a similar manner as the PATH command.

% Header
disp(sprintf('\n\t\tTOOLBAR ITEMS\n'))     

info = {};
for n = 1:length(uidata)
    info{n} = [uidata{n}.group,'.',uidata{n}.name];
end

% Display items
ch= strvcat(info);
tabspace = ones(size(ch,1),1);
tabspace(:) = sprintf('\t');
s = [tabspace, ch];
disp(s)

% Footer
disp(sprintf('\n'))

%---------------------------------------------------%
function [uidata] = localGetToolbarComponentInfo
% Loads all toolbar data

uidata = {};

% STANDARD TOOLS ------------------------------------------%
% New Figure
info = [];
info.name = 'NewFigure';
info.group = 'Standard';
info.constructor = 'uipushtool';
info.properties.ClickedCallback = 'filemenufcn(gcbf,''FileNew'')'; 
info.properties.ToolTip = 'New Figure';
info.icon = 'newdoc';
uidata{end+1} = info;

% Open File
info = [];
info.name = 'FileOpen';
info.group = 'Standard';
info.constructor = 'uipushtool';
info.properties.ClickedCallback = 'filemenufcn(gcbf,''FileOpen'')'; 
info.properties.ToolTip = 'Open File';
info.icon = 'opendoc';
uidata{end+1} = info;

% Save Figure
info = [];
info.name = 'SaveFigure';
info.group = 'Standard';
info.constructor = 'uipushtool';
info.properties.ClickedCallback = 'filemenufcn(gcbf,''FileSave'')'; 
info.properties.ToolTip = 'Save Figure';
info.icon = 'savedoc';
uidata{end+1} = info;

% Print
info = [];
info.name = 'PrintFigure';
info.group = 'Standard';
info.constructor = 'uipushtool';
info.properties.ClickedCallback = 'printdlg(gcbf)'; 
info.properties.ToolTip = 'Print Figure';
info.icon = 'printdoc';
uidata{end+1} = info;

% Plot Edit
info = [];
info.name = 'EditPlot';
info.group = 'Standard';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'plotedit(gcbf,''toggle'')';
info.properties.ToolTip =  'Edit Plot';
info.icon = 'pointer';
uidata{end+1} = info;

% EXPLORE TOOLS ------------------------------------------%

% Zoom In
info = [];
info.name = 'ZoomIn';
info.group = 'Exploration';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'putdowntext(''zoomin'',gcbo)';
%info.properties.ClickedCallback = 'zoom inmode';
info.properties.ToolTip = 'Zoom In';
info.icon = 'view_zoom_in.gif';
uidata{end+1} = info;

% Zoom Out
info = [];
info.name = 'ZoomOut';
info.group = 'Exploration';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'putdowntext(''zoomout'',gcbo)';
%info.properties.ClickedCallback = 'zoom outmode';
info.properties.ToolTip = 'Zoom Out';
info.icon = 'view_zoom_out.gif';
uidata{end+1} = info;

% Pan
info = [];
info.name = 'Pan';
info.group = 'Exploration';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'putdowntext(''pan'',gcbo)';
%info.properties.ClickedCallback = 'pan on';
info.properties.ToolTip = 'Pan';
info.icon = 'pan';
uidata{end+1} = info;

% Rotate
info = [];
info.name = 'Rotate';
info.group = 'Exploration';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'putdowntext(''rotate3d'',gcbo)';
%info.properties.ClickedCallback = 'rotate3d on';
info.properties.ToolTip = 'Rotate 3D';
info.icon = 'tool_rotate_3d.gif';
uidata{end+1} = info;

% Data Cursor
info = [];
info.name = 'DataCursor';    
info.group = 'Exploration';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'putdowntext(''datatip'',gcbo)';
info.properties.ToolTip = 'Data Cursor';
info.icon = 'tool_data_cursor.gif';
uidata{end+1} = info;

% ANNOTATION TOOLS ------------------------------------------%

% Colorbar
info = [];
info.name = 'InsertColorbar';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'insertmenufcn(gcbf,''Colorbar'')';
info.properties.ToolTip = 'Insert Colorbar';
info.icon = 'tool_colorbar.gif';
uidata{end+1} = info;

% Legend
info = [];
info.name = 'InsertLegend';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.ClickedCallback = 'insertmenufcn(gcbf,''Legend'')';
info.properties.ToolTip = 'Insert Legend';
info.icon = 'tool_legend.gif';
uidata{end+1} = info;

% Rectangle
info = [];
info.name = 'InsertRectangle';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''Rectangle'')';
info.properties.ToolTip = 'Insert Rectangle';
info.icon = 'tool_rectangle.gif';
uidata{end+1} = info;

% Ellipse
info = [];
info.name = 'InsertEllipse';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''Ellipse'')';
info.properties.ToolTip = 'Insert Ellipse';
info.icon = 'tool_ellipse.gif';
uidata{end+1} = info;

% Textbox
info = [];
info.name = 'InsertTextbox';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''Textbox'')';
info.properties.ToolTip = 'Insert Textbox';
info.icon = 'tool_text.gif';
uidata{end+1} = info;

% Text Arrow
info = [];
info.name = 'InsertTextArrow';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''TextArrow'')';
info.properties.ToolTip = 'Insert Text Arrow';
info.icon = 'tool_text_arrow.gif';
uidata{end+1} = info;

% 2 Headed Arrow
info = [];
info.name = 'InsertDoubleArrow';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''DoubleArrow'')';
info.properties.ToolTip = 'Insert Double Arrow';
info.icon = 'tool_double_arrow.gif';
uidata{end+1} = info;

% 1 Headed Arrow
info = [];
info.name = 'InsertArrow';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''Arrow'')';
info.properties.ToolTip = 'Insert Arrow';
info.icon = 'tool_arrow.gif';
uidata{end+1} = info;

% Line
info = [];
info.name = 'InsertLine';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'insertmenufcn(gcbf,''Line'')';
info.properties.ToolTip = 'Insert Line';
info.icon = 'tool_line.gif';
uidata{end+1} = info;

% Pin
info = [];
info.name = 'Pin';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'startscribepinning(gcbf)';
info.properties.OffCallback = 'startscribepinning(gcbf,''off'')';
info.properties.ToolTip = 'Pin to axes';
info.icon = 'pin_icon.gif';
uidata{end+1} = info;

% Align Distribute
info = [];
info.name = 'AlignDistribute';
info.group = 'Annotation';
info.constructor = 'uitoggletool';
info.properties.OnCallback = 'scribealign(gcbf)';
info.properties.ToolTip =  'Align/Distribute';
info.icon = 'tool_align.gif';
uidata{end+1} = info;


% PLOTTOOL-RELATED TOOLS ------------------------------------------%

% Plottools Off
info = [];
info.name = 'PlottoolsOff';
info.group = 'Plottools';
info.constructor = 'uipushtool';
info.properties.ClickedCallback = 'plottools (gcbf, ''hide'');';
info.properties.ToolTip = 'Hide Plot Tools';
info.properties.Enable = 'off';
info.properties.RequireJavaFigures = 'on';
info.icon = 'plottoolsoff';
uidata{end+1} = info;

% Plottools On
info = [];
info.name = 'PlottoolsOn';
info.group = 'Plottools';
info.constructor = 'uipushtool';
info.properties.ClickedCallback = 'plottools (gcbf, ''show'');'; 
info.properties.ToolTip = 'Show Plot Tools';
info.properties.Enable = 'on';
info.properties.RequireJavaFigures = 'on';
info.icon = 'plottoolson';
uidata{end+1} = info;
