function HWIN = demo_javacomponent
% demo - Demo of modelit.ui.Jacontrol components
%
% CALL:
%  demo
%
% INPUT:
%  No input
%
% OUTPUT:
%  HWIN:
%    handle of the demo window.

%Compile/Build commands (R2013a)
% TARGET='d:\d\modelit\compiledExamples\exe2013a_demo_javacomponent';

%Compile/Build commands (R2021a)
% TARGET='d:\d\modelit\compiledExamples\exe2021a_demo_javacomponent';

%Compile/Build commands (R2013a/R2021a)
% mkdir(TARGET)
% eval(['mcc -C -m -d ',TARGET,' demo_javacomponent'])
% installPackage({'modelit'},TARGET);
% copyfile(which('demo_javacomponent.m'),TARGET)

% ZIJPP 20180423 rename demo to demo_javacomponent

if installjar(false)
    HWIN=[];
    return
end

% Create figure, delete existing figure if necessary
tag = mfilename;
HWIN = findobj('tag',tag,...
    'type','figure');
if ishandle(HWIN)
    return
end
HWIN = figure('menubar','none',...
    'visible','off',...
    'numbertitle','off',...
    'dockcontrols','off',...
    'units','normalized',...
    'position',[0.1 .2 .8 .7],...
    'name','Modelit JavaComponentToolbox Showcase',...
    'tag',tag);

% Tabbedpane takes all available space in figure
tabbedpane = jacontrol(HWIN,...
    'style','tabbedpane',...
    'units','normalized',...
    'tag','tabbedpane',...
    'position',[0 0 1 1]);

% First tab demo source code
if isdeployed
    fname = fullfile(pwd, 'demo_javacomponent.m');
    if isfile(fname)
        text = chararray2char(readstr(fname));
    else
        error('File %s not found',fname);
    end
else
    text = chararray2char(readstr(which('demo_javacomponent.m')));
end
texteditor = jacontrol(tabbedpane,...
    'style','texteditor',...
    'contenttype','text/matlab',...
    'infolabel',true,...
    'string',text);
addTab(tabbedpane, 'TextEditor', texteditor);
 
% Second tab with table
table = jacontrol(tabbedpane,...
    'style','table',...
    'iconset', 'filetypes.jar',...
    'infolabel',true,...
    table_data);
addTab(tabbedpane, 'Table', table);

% Third tab with treetable
treetable = jacontrol(tabbedpane,...
    'style','treetable',...
    'iconset', 'filetypes.jar',...
    'infolabel',true,...
    treetable_data);
expandAll(treetable,true);
addTab(tabbedpane, 'TreeTable', treetable);


% Tab with splitpanes
splitpane = jacontrol(tabbedpane,...
    'style','splitpane',...
    'OneTouchExpandable',true,...
    'DividerSize',10,...
    'orientation','horizontal');
leftsplitpane = jacontrol(splitpane,...
    'style','splitpane',...
    'orientation','horizontal');
rightsplitpane = jacontrol(splitpane,...
    'style','splitpane',...
    'orientation','vertical');

leftsplitpane.setLeftComponent(jacontrol(tabbedpane,...
    'style','panel',...
    'title','red',...
    'fontsize',12,...
    'borderstyle','line',...
    'background',[1 0 0]));
leftsplitpane.setRightComponent(jacontrol(tabbedpane,...
    'style','panel',...
    'title','green',...
    'fontsize',12,...
    'borderstyle','line',...
    'background',[0 1 0]));
splitpane.setLeftComponent(leftsplitpane);

rightsplitpane.setLeftComponent(jacontrol(tabbedpane,...
    'style','panel',...
    'title','blue',...
    'fontsize',12,...
    'background',[0 0 1]));
rightsplitpane.setRightComponent(jacontrol(tabbedpane,...
    'style','panel',...
    'title','yellow',...
    'fontsize',12,...
    'background',[1 1 0]));
splitpane.setRightComponent(rightsplitpane);

addTab(tabbedpane, 'Panels', splitpane);

% Fourth tab with scrollpane
scrollpane = jacontrol(tabbedpane,...
    'style','scrollpane');
addTab(tabbedpane, 'ScrollPane', scrollpane);
panel = jacontrol(scrollpane,...
    'style','panel');
scrollpane.add(panel);

nrows = 20;
ncols = 20;
for i=1:nrows
    for  j=1:ncols
        component = modelit.ui.Button(panel,...
            'string',sprintf('Button (%d, %d)', i, j));
        add(panel, component, i, j, 'fill', 'both', 'insets', [10 10 10 10]);
    end
end
 
% Tab with components
componenttabbedpane = jacontrol(tabbedpane,...
    'style','tabbedpane',...
    'tabplacement','top');

addTab(componenttabbedpane, 'Button', make_panel('button', componenttabbedpane, struct('String','Button','preferredsize',[200 20]))); 
addTab(componenttabbedpane, 'ToggleButton', make_panel('togglebutton', componenttabbedpane, struct('String','ToggleButton','preferredsize',[200 20]))); 

addTab(componenttabbedpane, 'RadioButton', make_panel('radiobutton', componenttabbedpane, struct('String','A RadioButton','preferredsize',[200 20]))); 
addTab(componenttabbedpane, 'CheckBox', make_panel('checkbox', componenttabbedpane, struct('String','A CheckBox','preferredsize',[200 20])));  

addTab(componenttabbedpane, 'ComboBox', make_panel('combobox', componenttabbedpane, struct('prompt','Enter text','preferredsize',[200 20]))); 
addTab(componenttabbedpane, 'DatePicker', make_panel('datepicker', componenttabbedpane,struct('value',now,'preferredsize',[200 20])));

addTab(componenttabbedpane, 'Label', make_panel('label', componenttabbedpane, struct('String','A label','preferredsize',[200 20])));  
addTab(componenttabbedpane, 'HyperLink', make_panel('hyperlink', componenttabbedpane, struct('String','http://www.modelit.nl','preferredsize',[200 20])));  

addTab(componenttabbedpane, 'Spinner', make_panel('spinner', componenttabbedpane, struct('content',{{'One';'Two';'Three'}},'preferredsize',[200 20])));  
addTab(componenttabbedpane, 'Slider', make_panel('slider', componenttabbedpane, struct('PaintTicks',true,'PaintLabels',true,'MajorTicks',10,'preferredsize',[200 60]))); 

addTab(componenttabbedpane, 'TextField', make_panel('textfield', componenttabbedpane, struct('prompt','Enter text','preferredsize',[200 20])));  
addTab(componenttabbedpane, 'PasswordField', make_panel('passwordfield', componenttabbedpane, struct('prompt','Enter text','preferredsize',[200 20])));  

addTab(componenttabbedpane, 'ProgressBar', make_panel('progressbar', componenttabbedpane, struct('value',45,'preferredsize',[200 20])));

addTab(tabbedpane, 'Components', componenttabbedpane);

set(HWIN, 'visible', 'on');
 
%__________________________________________________________________________
function panel = make_panel(type, parent, S)
% make_panel - Create a panel with one componennt
%
% CALL:
%  panel = make_panel(type, parent, S)

panel = jacontrol(parent,...
    'style','panel',...
    'title',sprintf('%s',type),...
    'borderstyle','line');

component = jacontrol(panel,...
    'style',type);
if nargin == 3
    set(component, S);
end
panel.add(component,1,1,'fill','none','weightx',1,'insets',[10 10 10 10]);

%__________________________________________________________________________
function S = table_data
% table_data - Generate data for the table
%
% CALL:
%  S = table_data

data = [];
randomstrings = {'Greece','Germany','Poland','Ukraine','Russia','Italy','Spain','Sweden','Denmark','France'};
for i=1:40
    data{i,1} = uint16(i);
    data{i,2} = rand(1)*100-50;
    data{i,3} = rand(1)*100-50;
    data{i,4} = randompiece;
    data{i,5} = rand(1) > .5;
    data{i,6} = randpick(randomstrings);
    data{i,7} = rand*10000000;
    data{i,8} = rand(1,3);
    data{i,9} = now + rand*i;
    data{i,10} = randpick({'a','b','c','d','e','f','g','h','i','j'});
    data{i,11} = randpick({'bullet_green.gif','bullet_grey.gif','bullet_red.gif','bullet_orange.png','bullet_star.png'});
    text = randpick({'bullet_green.gif','bullet_grey.gif','bullet_red.gif'});
    icon = text;
    data{i,12} = {text icon};
end

S.data = data;
S.columnnames = {'Index','Bar','AbsBar','PieceBar','Boolean','String','Filesize','Color','Date','Character','Icon','Text and icon'};
S.columntooltips = {'Column with an integer','Column with a bargraph','Column with an absolute bargraph','Column with a piecebar','Column with a boolean','Column with a string','Column with filesizes','Column with a color','Column with a date','Column with a character','Column with an icon','Column with ext and icon'};
S.columnformats = {'%d','bar','absbar','piecebar','logical',randomstrings,'filesize','Color','yyyyMMMdd', 'string', 'icon', 'textandicon'};

ncolors = 24;
key = [1:ncolors]';
colors = hsv(ncolors);
S.colorset = [key colors];

%__________________________________________________________________________
function S = treetable_data
% treetable_data - Generate data for the treetable
%
% CALL:
%  S = treetable_data

data = [];
icons = [];
for i=1:40
    data{i,1} = sprintf('%d row', uint32(i));
    data{i,2} = rand(1)*100-50;
    data{i,3} = rand(1)*100-50;
    data{i,4} = randompiece;
    data{i,5} = rand(1) > .5;
    data{i,6} = randpick({'Greece','Germany','Poland','Ukraine','Russia','Italy','Spain','Sweden','Denmark','France'});
    data{i,7} = rand*10000000;
    data{i,8} = rand(1,3);
    data{i,9} = now + rand*i;
    data{i,10} = randpick({'a','b','c','d','e','f','g','h','i','j'});
    data{i,11} = randpick({'bullet_green.gif','bullet_grey.gif','bullet_red.gif'});
    icons{i} = randpick({'bullet_green.gif','bullet_grey.gif','bullet_red.gif'});
end

S.data = data;
S.columnnames = {'Index','Bar','AbsBar','PieceBar','Boolean','String','Filesize','Color','Date','Character','icon'};
S.columntooltips = {'Index','Random','Random','Random','Boolean','String','Filesize','Color','Date','Character','Icon'};
S.columnformats = {'%d','bar','absbar','piecebar','logical','string','filesize','Color','yyyyMMMdd', 'string','icon'};
S.hierarchy =  zeros(size(data,1),1);
S.hierarchy(3:end) = 1;
S.hierarchy(5:end) = 2;
S.hierarchy(7:end) = 5;
S.hierarchy(8:end) = 6;
S.iconnames = icons;

%__________________________________________________________________________
function pieces = randompiece()
% randompiece - Make a random piece for a table piecebarrenderer
%
% CALL:
%  str = randompiece

npieces = randi([10 100]);
lengths = randi([1 10],npieces,1);
colors = randi([1 48],npieces,1);

pieces = [lengths colors]';

pieces = pieces(:)';

