function cmHandle = ParetoMXPrightclickmenu(nmenu)
% Mouse right click context menu for Pareto graph
   cmHandle = uicontextmenu;
   if nmenu==1
       m = uimenu(cmHandle,'Label','Save design to a MXP-file...');
       m.MenuSelectedFcn = @save2mxp;
   elseif nmenu==2
       m = uimenu(cmHandle,'Label','Show design variable bounds for displayed designs');
       m.MenuSelectedFcn = @showbounds;
   end
 
    function save2mxp(src,event)
        Fig = gcf;
        data = get(Fig,'UserData');
        X = data.X;    % design parameters for each design
        Y = data.Y;    % objective values for each design
        initdesignfile = data.initdesignfile;
        motorProps = data.motorProps;
        designFunction = data.designFunction;
        Userdata = data.Userdata;
        savepath = data.savepath;
        point = event.Source.Parent.Parent.CurrentObject.Parent.CurrentPoint;  % selected point
        y1 = point(1,1);
        y2 = point(1,2);
        [~, i] = min(sqrt(((Y(:,1)-y1)/max(Y(:,1))).^2 + ((Y(:,2)-y2)/max(Y(:,2))).^2));   % closest point
        y = Y(i,:);   % objective values of the selected design
        x = X(i,:);   % design parameters of the selected design
        if ~exist(initdesignfile,'file')
            button = questdlg(['File ' initdesignfile ' not found. Do you want to select another base design MXP-file?'],'Base design MXP-file not found','Yes','No','No');
            if strcmp(button,'No')
                return
            end
            [filename, pathname] = uigetfile({'*.mxp'}, 'Select base design MXP-file...');
            if isequal([filename,pathname],[0,0])
                % If 'Cancel' was selected then return
                return
            else
                initdesignfile = fullfile(pathname,filename);
                data.initdesignfile = initdesignfile;   % save the new base design mxp-files
                set(Fig,'UserData',data);
            end
        end
        if ~exist(savepath,'dir') 
            savepath = fileparts(initdesignfile);
        end
        % write design parameters to the motorProps structure
        [~, motorProps] = designFunction(motorProps,[],[],0,x,[],Userdata);
        filename = ['design_f1=' num2str(y(1)) '_f2=' num2str(y(2))];
        cd_old=cd;
        cd(savepath);
        [filename, pathname] = uiputfile({'*.mxp'}, 'Save design to a MXP-file...', filename);
        cd(cd_old);
        if isequal([filename,pathname],[0,0])
            % If 'Cancel' was selected then return
            return
        else
            set(Fig,'Pointer','watch');
            MXPfile = fullfile(pathname,filename);
            saveDesign2MXPfile(initdesignfile, motorProps, MXPfile)
            set(Fig,'Pointer','arrow');
        end
        pathname = fileparts(MXPfile);   % remove '\' at the end of the path
        if ~strcmp(pathname,savepath)
            % if selected path is different from the default path for saving mxp-files
            data.savepath = pathname;   % change the default path for saving mxp-files
            set(Fig,'UserData',data);
        end
    end
    function showbounds(src,event)
        Fig = gcf;
        data = get(Fig,'UserData');
        X = data.X;    % design parameters for each design
        Y = data.Y;    % objective values for each design
        f1_lim = xlim;
        f2_lim = ylim;
        inds = find(Y(:,1)>=f1_lim(1) & Y(:,1)<=f1_lim(2) & Y(:,2)>=f2_lim(1) & Y(:,2)<=f2_lim(2));
        X = X(inds,:);
        disp(['Design variable #         Min                  Max'])
        for i=1:size(X,2)
            disp(sprintf('%8i%21.3f%21.3f',i,min(X(:,i)),max(X(:,i))));
        end
    end
end

