function buildXJViewBrowser()
%user interface to merge a browseXJ and a xjView version
%you will need a browseXJ and xjView version ready

%in order to build your version of xjViewBrowser/xjView just run this function
%and follow the instructions

%created in 2012 by Christoph Hofstetter (c) ch_hofstetter@hotmail.com


colour = [ 200/255 233/255 200/255];

f = figure('unit','pixels','Name','build xjViewBrowser','NumberTitle','off', 'MenuBar', 'none','Color', colour);
pos = get(f,'Position');
pos(3:4) = [640 320];
set(f,'Position',pos);

handles = guihandles(f);
handles.figure = f;

leftBorder = 35;

thisPos = [0 pos(4)-80 pos(3) 60];
handles.labelTitle = uicontrol(handles.figure,'style','text',...
    'String','Select an xjView and xjViewBrowser version in order to merge them',...
    'FontSize', 12,...
    'BackgroundColor', colour, ...
    'Position', thisPos);

% xjView
thisPos = [leftBorder  pos(4)- 120 250 30];
handles.selectXJViewPath = uicontrol(handles.figure,'style','edit',...
    'String','',...
    'Position',thisPos);
thisPos = [leftBorder + 270  pos(4)- 120 130 30];
handles.selectXJViewButton = uicontrol(handles.figure,'style','pushbutton',...
    'String','select xjView',...
    'callback',@selectXJView_Callback,...
    'Position',thisPos);

thisPos = [leftBorder + 420  pos(4)- 120 130 30];
handles.webLinkXJView = uicontrol(handles.figure,'style','pushbutton',...
    'String','get xjView',...
    'callback','web http://www.alivelearn.net/xjview8/ -browser',...
    'Position',thisPos);



% xjViewBrowser
thisPos = get(handles.selectXJViewPath,'Position');
thisPos(2) = thisPos(2) - 50;
handles.selectxjViewBrowserPath = uicontrol(handles.figure,'style','edit',...
    'String','',...
    'Position',thisPos);
thisPos = get(handles.selectXJViewButton,'Position');
thisPos(2) = thisPos(2) - 50;
handles.selectxjViewBrowserButton = uicontrol(handles.figure,'style','pushbutton',...
    'String','select xjViewBrowser',...
    'callback',@selectxjViewBrowser_Callback,...
    'Position',thisPos);
thisPos = get(handles.webLinkXJView,'Position');
thisPos(2) = thisPos(2) - 50;
handles.webLinkxjViewBrowser = uicontrol(handles.figure,'style','pushbutton',...
    'String','get xjViewBrowser',...
    'callback','web http://www.virtualhome.ch/christoph/xjViewBrowser/ -browser',...
    'Position',thisPos);

% path
thisPos = get(handles.selectxjViewBrowserPath,'Position');
thisPos(2) = thisPos(2) - 50;
handles.defineNewFilePath = uicontrol(handles.figure,'style','edit',...
    'String','',...
    'Position',thisPos);
thisPos = get(handles.selectxjViewBrowserButton,'Position');
thisPos(2) = thisPos(2) - 50;
handles.defineNewFileButton = uicontrol(handles.figure,'style','pushbutton',...
    'String','save new file as',...
    'callback',@defineNewFile_Callback,...
    'Position',thisPos);







thisPos = [180 20 70 30];
handles.buildButton = uicontrol(handles.figure,'style','pushbutton',...
    'String','build now ',...
    'callback',@buildNewFile_Callback,...
    'Position',thisPos);


guidata(f, handles);
return


function selectXJView_Callback(hObject, eventdata)
handles = guidata(hObject);
[FileName,PathName] = uigetfile('*.m','select xjView');
set(handles.selectXJViewPath,'String',fullfile(PathName,FileName));


function selectxjViewBrowser_Callback(hObject, eventdata)
handles = guidata(hObject);
[FileName,PathName] = uigetfile('*.m','select xjViewBrowser');
set(handles.selectxjViewBrowserPath,'String',fullfile(PathName,FileName));


function defineNewFile_Callback(hObject, eventdata)
handles = guidata(hObject);
[FileName,PathName] = uiputfile('*.m','save new file as','myxjViewBrowser');
set(handles.defineNewFilePath,'String',fullfile(PathName,FileName));

function buildNewFile_Callback(hObject, eventdata)
handles = guidata(hObject);

%check files are set
PathXJView = get(handles.selectXJViewPath,'String');
PathxjViewBrowser = get(handles.selectxjViewBrowserPath,'String');
PathNewFile = get(handles.defineNewFilePath,'String');

if ~exist(PathXJView,'file') || ~exist(PathxjViewBrowser,'file')
    msgbox('One or both source files do not exist.','warning');
    return;
end

if strcmp(PathNewFile,'')
    msgbox('Please choose a target file name.','warning');
    return;
end

option = 'Yes';
if exist(PathNewFile,'file')
    option = questdlg('The target file already exists, would you like to overwrite?','Overwrite existing file?','Yes','No','Cancel');
end

switch option
    case 'No'
        return;
    case 'Yes'
        %check entry point in xjView
        if ~checkXJViewFile(PathXJView)
            msgbox('Unable to find entry point in xjView file.','warning');
            return
        else
            %do insertion
            buildFile(PathXJView, PathxjViewBrowser, PathNewFile);
            msgbox('The new file has been created.','warning');            
        end
end



function foundEntryPointFlag = checkXJViewFile(thePath)
fidXJVIEW = fopen(thePath,'r');

foundEntryPointFlag = 0;

targetLine = 'CallBack_loadImagePush(handles.loadImagePush, [], thisStruct);';

tline = fgetl(fidXJVIEW);
while ischar(tline)
    tline = fgetl(fidXJVIEW);
    
    if strcmp(strtrim(tline),targetLine)
        foundEntryPointFlag = 1;
        break;
    end
end
fclose(fidXJVIEW);

function buildFile(PathXJView, PathxjViewBrowser, PathTarget)
targetLine = 'CallBack_loadImagePush(handles.loadImagePush, [], thisStruct);';

h = waitbar(0,'processing');
fidXJVIEW = fopen(PathXJView,'r');
fidNEWFILE = fopen(PathTarget,'w');

tline = fgetl(fidXJVIEW);
while ischar(tline)
    
    fprintf(fidNEWFILE,'%s',tline); %quote line
    fprintf(fidNEWFILE,'\n'); %end line
    
    %if ~isempty(strfind(tline,'entrypointInject'))
    if strcmp(strtrim(tline),targetLine)
        
        %add end
        tline = fgetl(fidXJVIEW);
        fprintf(fidNEWFILE,'%s',tline); %quote line
        fprintf(fidNEWFILE,'\n\n\n\n'); %end line
        
        fprintf(fidNEWFILE,'%s','%%%%%%%%%%%%%%%%%%%%%%%%%%%'); %quote line
        fprintf(fidNEWFILE,'\n'); %end line
        fprintf(fidNEWFILE,'%s','%%%xjViewBrowser START%%%%%%%%%%'); %quote line
        fprintf(fidNEWFILE,'\n'); %end line
        fprintf(fidNEWFILE,'%s','%%%%%%%%%%%%%%%%%%%%%%%%%%%'); %quote line
        fprintf(fidNEWFILE,'\n\n'); %end line
        fprintf(fidNEWFILE,'%s','xjViewBrowser'); %quote line
        fprintf(fidNEWFILE,'\n\n'); %end line
        
        %load from file
        fidxjViewBrowser = fopen(PathxjViewBrowser,'r');
        tlinexjViewBrowser = fgetl(fidxjViewBrowser);
        while ischar(tlinexjViewBrowser)
            
            fprintf(fidNEWFILE,'%s',tlinexjViewBrowser); %quote line
            fprintf(fidNEWFILE,'\n'); %end line
            tlinexjViewBrowser = fgetl(fidxjViewBrowser);
        end
        fclose(fidxjViewBrowser);
        
        fprintf(fidNEWFILE,'\n\n\n\n\n'); %end line
        fprintf(fidNEWFILE,'%s','%%%%%%%%%%%%%%%%%%%%%%%%%%%'); %quote line
        fprintf(fidNEWFILE,'\n'); %end line
        fprintf(fidNEWFILE,'%s','%%%xjViewBrowser END%%%%%%%%%%'); %quote line
        fprintf(fidNEWFILE,'\n'); %end line
        fprintf(fidNEWFILE,'%s','%%%%%%%%%%%%%%%%%%%%%%%%%%%'); %quote line
        fprintf(fidNEWFILE,'\n\n\n\n\n'); %end line
    end
    
    
    tline = fgetl(fidXJVIEW);
end

fclose(fidXJVIEW);
fclose(fidNEWFILE);
delete(h);




