function varargout = nic_rsdd_sortPara(varargin)
% NIC_RSDD_SORTPARA M-file for nic_rsdd_sortPara.fig
%      NIC_RSDD_SORTPARA, by itself, creates a new NIC_RSDD_SORTPARA or raises the existing
%      singleton*.
%
%      H = NIC_RSDD_SORTPARA returns the handle to a new NIC_RSDD_SORTPARA or the handle to
%      the existing singleton*.
%
%      NIC_RSDD_SORTPARA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NIC_RSDD_SORTPARA.M with the given input arguments.
%
%      NIC_RSDD_SORTPARA('Property','Value',...) creates a new NIC_RSDD_SORTPARA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before nic_rsdd_sortPara_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to nic_rsdd_sortPara_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help nic_rsdd_sortPara

% Last Modified by GUIDE v2.5 18-Jun-2010 19:04:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @nic_rsdd_sortPara_OpeningFcn, ...
                   'gui_OutputFcn',  @nic_rsdd_sortPara_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before nic_rsdd_sortPara is made visible.
function nic_rsdd_sortPara_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to nic_rsdd_sortPara (see VARARGIN)

% Choose default command line output for nic_rsdd_sortPara
handles.output = hObject;

handles.sortParaFig=hObject;
% Update handles structure
guidata(hObject, handles);

uiwait(hObject);%




% UIWAIT makes nic_rsdd_sortPara wait for user response (see UIRESUME)
% uiwait(handles.sortParaHdl);


% --- Outputs from this function are returned to the command line.
function varargout = nic_rsdd_sortPara_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.cfgpara;
varargout{2} = [];
close(hObject);



% --- Executes on selection change in CommCriteria.
function CommCriteria_Callback(hObject, eventdata, handles)
% hObject    handle to CommCriteria (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns CommCriteria contents as cell array
%        contents{get(hObject,'Value')} returns selected item from CommCriteria

optionC=get(hObject,'Value');
optionS=get(handles.CommType,'Value');
switch optionC
    case {1,2,3}
        if(optionS==2)
            set(handles.CommDatasetType,'Enable','off');
        elseif(optionS==1)
            set(handles.CommDatasetType,'Enable','on');
        end
    otherwise
end
if(optionC==3)
    set(handles.TextSelFileInfo,'Visible','off');
    set(handles.TextSelFile,'Visible','off');
    set(handles.ButtSelFile,'Visible','off');    
else
    set(handles.TextSelFileInfo,'Visible','on');
    set(handles.TextSelFile,'Visible','on');
    set(handles.ButtSelFile,'Visible','on');    
end

% --- Executes during object creation, after setting all properties.
function CommCriteria_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CommCriteria (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in CommType.
function CommType_Callback(hObject, eventdata, handles)
% hObject    handle to CommType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns CommType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from CommType
optionS=get(hObject,'Value');
optionC=get(handles.CommCriteria,'Value');
switch optionS
    case 1
        set(handles.CommDatasetType,'Enable','on');
    case 2
        set(handles.CommDatasetType,'Enable','off');
    otherwise
end
if(optionS==2)
    set(handles.TextSelFileInfo,'String','Select the spatial template:');
else
    set(handles.TextSelFileInfo,'String','Select the SPM file:');
end

% --- Executes during object creation, after setting all properties.
function CommType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CommType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in CommDatasetTYpe.
function CommDatasetTYpe_Callback(hObject, eventdata, handles)
% hObject    handle to CommDatasetTYpe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns CommDatasetTYpe contents as cell array
%        contents{get(hObject,'Value')} returns selected item from CommDatasetTYpe


% --- Executes during object creation, after setting all properties.
function CommDatasetTYpe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CommDatasetTYpe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in ButtSetOK.
function ButtSetOK_Callback(hObject, eventdata, handles)
% hObject    handle to ButtSetOK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
parameters=struct();
parameters.criteriaType=get(handles.CommCriteria,'Value');
parameters.CommType=get(handles.CommType,'Value');
parameters.CommDatasetType=get(handles.CommDatasetType,'Value');
parameters.TextSelFile=get(handles.TextSelFile,'String');

if(isempty(parameters.TextSelFile))
    errordlg('Please select the file');
    return;
end
% global cfgpara;

handles.cfgpara=parameters;
guidata(hObject,handles);

uiresume(handles.sortParaFig);
% if(ishandle(handles.output))
%     delete(handles.output);
% end


% --- Executes on selection change in CommDatasetType.
function CommDatasetType_Callback(hObject, eventdata, handles)
% hObject    handle to CommDatasetType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns CommDatasetType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from CommDatasetType


% --- Executes during object creation, after setting all properties.
function CommDatasetType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CommDatasetType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function TextSelFile_Callback(hObject, eventdata, handles)
% hObject    handle to TextSelFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TextSelFile as text
%        str2double(get(hObject,'String')) returns contents of TextSelFile as a double


% --- Executes during object creation, after setting all properties.
function TextSelFile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TextSelFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in ButtSelFile.
function ButtSelFile_Callback(hObject, eventdata, handles)
% hObject    handle to ButtSelFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

optionC=get(handles.CommType,'Value');

if(optionC==2)
[filename, pathname] = uigetfile({'*.img;*.nii', 'All Mask files (*.img; *.nii)'; ...
												'*.nii','NIFTI masks (*.nii)'; ...
												'*.img', 'Analyze or NIFTI masks(*.img)'}, ...
												'Please specify a file of mask');
    if ~(filename==0)
        set(handles.TextSelFile,'String',fullfile(pathname,filename));
    else
        set(handles.TextSelFile,'String','');
    end    
elseif(optionC==1)
    [filename, pathname] = uigetfile({'SPM.mat;SPM.MAT','SPM  Parameter File(SPM.mat)'}, ...
												'Please specify SPM.mat');
    if ~(filename==0)
        set(handles.TextSelFile,'String',fullfile(pathname,filename));
    else
        set(handles.TextSelFile,'String','');
    end    
end
guidata(hObject,handles);    

