function [filename,user_canceled] = imgetfile
%IMGETFILE Displays Open Image dialog box.  
%   [FILENAME, USER_CANCELED] = IMGETFILE displays the Open Image dialog
%   box for the user to fill in and returns the full path to the file
%   selected in FILENAME. If the user presses the Cancel button,
%   USER_CANCELED will be TRUE. Otherwise, USER_CANCELED will be FALSE.
%   
%   The Open Image dialog box is modal; it blocks the MATLAB command line
%   until the user responds. The file types listed in the dialog are all
%   formats listed in IMFORMATS plus DICOM.
%   
%   See also IMFORMATS, IMTOOL, UIGETFILE.

%   Copyright 1993-2004 The MathWorks, Inc.  
%   $Revision: 1.1.8.1 $  $Date: 2004/08/10 01:49:06 $
    
s = javachk('swing');
if ~isempty(s)
    error(s.identifier,s.message)
end

filename       = '';          
user_canceled = false;      

persistent file_chooser;

import com.mathworks.mwswing.MJFileChooser;

need_new_file_chooser = ~isa(file_chooser,'MJFileChooser');
if need_new_file_chooser
    createFileChooser
end

returnVal = file_chooser.showOpenDialog(com.mathworks.mwswing.MJFrame);    
if (returnVal == MJFileChooser.APPROVE_OPTION)
  filename = char(file_chooser.getSelectedFile.getPath);
else
  user_canceled = true;
end

  %-------------------------
  function createFileChooser
  % Parse formats available in IMFORMATS and create filters that include
  % these formats plus DICOM. Use filters to initialize file chooser.
  
    import com.mathworks.mwswing.MJFileChooser;
    import com.mathworks.toolbox.images.ImformatsFileFilter;  
  
    file_chooser = MJFileChooser(pwd);
    file_chooser.setDialogTitle('Open Image');
    
    % Parse formats from IMFORMATS 
    formats = imformats;
    nformats = length(formats);
    desc = cell(nformats,1);
    [desc{:}] = deal(formats.description);
    ext = cell(nformats,1);
    [ext{:}] = deal(formats.ext);
  
    % Add other formats that are not part of IMFORMATS
    desc{end+1} = 'DICOM (DCM)';
    ext{end+1} = {'dcm'};
    nformats = size(desc,1);
    
    % Create a filter that includes all extensions
    ext_all = cell(0);
    for i = 1:nformats
      ext_i = ext{i};
      ext_all(end+1: end+numel(ext_i)) = ext_i(:);
    end
    ext{end+1,1} = ext_all;
    desc{end+1,1} = 'All image files'; 
    
    % Make a vector of String arrays
    extVector = java.util.Vector(nformats);
    for i = 1:nformats+1
      extVector.add(i-1,ext{i})
    end
  
    % Push formats into ImformatsFileFilter so instances of 
    % ImformatsFileFilter will be based on IMFORMATS.
    ImformatsFileFilter.initializeFormats(nformats,desc,extVector);
  
    % Create all_images_filter
    all_images_filter = ... 
        ImformatsFileFilter(ImformatsFileFilter.ACCEPT_ALL_IMFORMATS);
    file_chooser.addChoosableFileFilter(all_images_filter); 
    % Add one ChoosableFileFilter for each format in IMFORMATS
    for i = 1:nformats
      file_chooser.addChoosableFileFilter(ImformatsFileFilter(i-1))
    end
  
    % Put accept all files at end
    accept_all_filter = file_chooser.getAcceptAllFileFilter;
    file_chooser.removeChoosableFileFilter(accept_all_filter);
    file_chooser.addChoosableFileFilter(accept_all_filter);
  
    % Make default be all_images_filter
    file_chooser.setFileFilter(all_images_filter);
  
  end
  
end