source: an2_juigetfiles.m @ 44

Last change on this file since 44 was 39, checked in by tjniskan, 11 years ago
  • Added "Recent Files" quick access menu in File-menu
  • Fixed a bug in plugins/copy_data_to_workspace.m that made writing a

new name for a variable difficult

  • Fixed a bug in the file prefix editbox in an2_export_gui.m
  • Changed references to Analyze 2.0 in all license notices to Aedes.

The name change should now be complete...

M an2_export_gui.m
M an2_cellsprintf.m
M an2_calc_wait.m
M an2_check_file_exist.m
M an2_iseven.m
M an2_cellwrite.m
M an2_wbar.m
M an2_rot3d.m
M an2_readfdf.m
M an2_revision.m
M an2_viewprocpar.m
M an2_checkcthdr.m
M an2_readprocpar.m
M an2_fitmaps.m
M an2_read_nifti.m
M an2_data_read.m
M an2_resviewer.m
M an2_maptool.m
M aedes.m
M an2_res2table.m
M an2_copy_roi.m
M plugins/save_roi_as_mask.m
M plugins/write_difference_images.m
M plugins/plot_profile.m
M plugins/calculate_t2_map.m
M plugins/calculate_t1r_map.m
M plugins/view_kspace.m
M plugins/copy_data_to_workspace.m
M plugins/take_snapshot.m
M an2_inputdlg.m
M an2_roi_copy_gui.m
M an2_readctdata.m
M an2_readfid.m
M an2_readfidprefs.m
M an2_readtab.m
M an2_check_updates.m
M an2_killfigs.m
M an2_roi_stats.m
M an2_saveres.m
M an2_rotateflip.m
M an2_juigetfiles.m
M an2_gui_defaults.m
M an2_editstack.m
M an2_update.m
M an2_write_nifti.m

File size: 31.1 KB
Line 
1function [filename,filepath,filterindex] = an2_juigetfiles(filefilter,ftitle,defaultdir)
2% AN2_JUIGETFILES - A Java based file browser for selecting multiple files in
3% non-sorted order from different folders.
4%   
5%
6% Synopsis:
7%       [FILENAME, PATHNAME, FILTERINDEX] = AN2_JUIGETFILES(FILTERSPEC,TITLE, DEFAULTPATH)
8%
9% Description:
10%       The behavior of AN2_JUIGETFILES is very similar to UIGETFILE with a few
11%       exceptions. The UIGETFILE properties 'location' and 'multiselect'
12%       are not available. Output arguments FILENAME and PATHNAME will
13%       always be cell arrays, if cancel is not pressed. The last input
14%       argument is not the default file but the default
15%       directory. FILTERSPEC must always be a cell array.
16%
17%       NOTE: Utilizes heavily the Matlab Java interface and undocumented
18%       and unsupported functions. This is due to change from version to
19%       version and, thus, this function will probably be broken in future
20%       Matlab releases. At the moment, however, AN2_JUIGETFILES should
21%       work with Matlab version from R14SP2 to R2007b.
22%
23% Examples:
24%       [filename, pathname, filterindex] = an2_juigetfiles( ...
25%       {'*.m;*.fig;*.mat;*.mdl', 'All MATLAB Files (*.m, *.fig, *.mat, *.mdl)';
26%        '*.m',  'M-files (*.m)'; ...
27%        '*.fig','Figures (*.fig)'; ...
28%        '*.mat','MAT-files (*.mat)'; ...
29%        '*.mdl','Models (*.mdl)'; ...
30%        '*.*',  'All Files (*.*)'}, ...
31%        'Pick a file(s)',pwd);
32%
33% See also:
34%       UIGETFILE
35
36% This function is a part of Aedes - A graphical tool for analyzing
37% medical images
38%
39% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
40%
41% Department of Physics, (or Department of Neurobiology)
42% University of Kuopio, FINLAND
43%
44% This program may be used under the terms of the GNU General Public
45% License version 2.0 as published by the Free Software Foundation
46% and appearing in the file LICENSE.TXT included in the packaging of
47% this program.
48%
49% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
50% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
51
52
53%% Parse input arguments
54if nargin==0
55  filefilter = {'*.*','All Files (*.*)'};
56  ftitle = 'Select File(s)';
57  defaultdir = 0;
58elseif nargin==1
59  ftitle = 'Select File(s)';
60  defaultdir = 0;
61elseif nargin==2
62  defaultdir = 0;
63elseif nargin>3
64  error('Too many input arguments')
65end
66%keyboard
67%% Default colors and fonts for gui
68%FigColor=get(0,'DefaultUicontrolBackgroundcolor');
69GD=an2_gui_defaults;
70FigColor = GD.col.mainfig;
71
72%% Draw main figure
73scrsz=get(0,'screensize');
74fig_w = 870;%780;
75fig_h = 535;
76fig_pos = [scrsz(3)/2-fig_w/2 scrsz(4)/2-fig_h/2 fig_w fig_h];
77fh=figure('position',fig_pos,...
78          'Name',ftitle, ...
79          'Numbertitle','off', ...
80          'Tag','an2_juigetfiles_main_fig', ...
81          'Color',FigColor,...%[236 233 216]./255,...%GD.col.mainfig, ...
82          'Toolbar','none', ...
83          'Menubar','none', ...
84          'DoubleBuffer','on', ...
85          'DockControls','off',...
86          'renderer','painters',...
87          'closereq','uiresume(gcbf)',...
88          'windowstyle','modal',...
89          'handlevisibility','off',...
90          'resize','off');
91H.fh=fh;
92%pos = [5 60 200 420];
93pos=[5 60 320 420];
94%% Check if defaultdir exists
95if ischar(defaultdir)
96  if ~isdir(defaultdir)
97    defaultdir=0;
98  end
99end
100
101%% Try to find icon path
102tmp = which('an2_juigetfiles');
103if ~isempty(tmp)
104  [fp,fn,fe]=fileparts(tmp);
105  iconpath = [fp,filesep,'icons',filesep];
106else
107  iconpath = [pwd,filesep,'icons',filesep];
108end
109
110%% Create uitree for directory browsing --------------
111rooticonpath = [iconpath,'MyComputer.gif'];
112rootnode = uitreenode('My Computer','My Computer',rooticonpath,false);
113tree = uitree(fh,'Root',rootnode,'position',pos);%,'ExpandFcn',@l_ExpFcn);
114set(tree,'NodeExpandedCallback',{@l_ExpFcnAlt,tree})
115
116
117%% Create uitree for files ---------------------------
118filetree = uitree(fh,'position',[pos(1)+pos(3)+5 pos(2) 220 pos(4)]);
119set(filetree,'MultipleSelectionEnabled',true,...
120             'NodeCollapsedCallback',{@l_FileTreeNodeCollapsed,filetree},...
121             'NodeExpandedCallback',{@l_OpenDirectory,tree,1});
122%'NodeSelectedCallback',{@l_FileTreeNodeSelected,filetree},...
123fileroot = uitreenode('Loading','Loading...','',false);
124filetree.setRoot(fileroot);
125filetree.reloadNode(fileroot);
126
127%% Set NodeSelected callback for directory tree
128set(tree,'NodeSelectedCallback',{@l_DirNodeSelected,tree,filetree});
129
130%% Create selected files uitree
131pos2=get(filetree,'position');
132selfiletreeroot = uitreenode('Selected Files','0 Files Selected','',false);
133selfiletree = uitree(fh,'Root',selfiletreeroot,'position',[pos(1)+pos2(3)+pos(3)+100 pos(2) 220 pos(4)]);
134set(selfiletree,'DndEnabled',true);
135set(selfiletree,'MultipleSelectionEnabled',true);
136set(selfiletree,'NodeDroppedCallback',{@l_SelFileTreeNodeDropped,selfiletree});
137
138
139% Expand My Computer
140tree.expand(rootnode);
141
142%% UICONTROLS ----------------------------------
143% Folders text
144tmp=get(tree,'position');
145dir_tx = uicontrol('parent',fh,...
146                   'position',[tmp(1)+5 tmp(2)+tmp(4) 150 13],...
147                   'style','text',...
148                   'string','Folders',...
149                   'fontweight','bold',...
150                   'horizontalalign','left',...
151                   'backgroundcolor',FigColor);%[236 233 216]./255);
152
153% Files text
154tmp=get(filetree,'position');
155files_tx = uicontrol('parent',fh,...
156                     'position',[tmp(1) tmp(2)+tmp(4) 150 13],...
157                     'style','text',...
158                     'string','Files',...
159                     'fontweight','bold',...
160                     'horizontalalign','left',...
161                     'backgroundcolor',FigColor);%[236 233 216]./255);
162
163% Selected Files text
164tmp=get(selfiletree,'position');
165selfiles_tx = uicontrol('parent',fh,...
166                        'position',[tmp(1) tmp(2)+tmp(4) 150 13],...
167                        'style','text',...
168                        'string','Selected Files',...
169                        'fontweight','bold',...
170                        'horizontalalign','left',...
171                        'backgroundcolor',FigColor);%[236 233 216]./255);
172
173
174
175% Add and Remove buttons
176tmp=get(filetree,'position');
177addbtn = uicontrol('parent',fh,...
178                   'units','pixel',...
179                   'position',[tmp(1)+tmp(3)+5 (tmp(4))/2+tmp(2) 85 25],...
180                   'string','Add >>',...
181                   'style','pushbutton',...
182                   'callback',{@l_AddRemFiles,'add',filetree,selfiletree});
183tmp=get(addbtn,'position');
184rembtn = uicontrol('parent',fh,...
185                   'units','pixel',...
186                   'position',[tmp(1) tmp(2)-25-5 85 25],...
187                   'string','<< Remove',...
188                   'style','pushbutton',...
189                   'callback',{@l_AddRemFiles,'remove',filetree,selfiletree});
190
191
192% Show path -checkbox
193tmp=get(selfiletree,'pos');
194showpathcb = uicontrol('parent',fh,...
195                       'units','pixel',...
196                       'position',[tmp(1) tmp(2)-17 150 15],...
197                       'style','checkbox',...
198                       'value',0,...
199                       'backgroundcolor',FigColor,...%[236 233 216]./255,...
200                       'string','Show full path',...
201                       'callback',{@l_ShowHidePathNames,selfiletree});
202
203% Back/Forward buttons
204tmp=get(tree,'position');
205back_btn = uicontrol('parent',fh,...
206                     'units','pixel',...
207                     'position',[tmp(1) tmp(2)+tmp(4)+20 80 25],...
208                     'string','<< Back',...
209                     'style','pushbutton',...
210                     'callback',{@l_BackForward,'back',tree},...
211                     'enable','off',...
212                     'busyaction','cancel',...
213                     'Interruptible','off');
214forward_btn = uicontrol('parent',fh,...
215                        'units','pixel',...
216                        'position',[tmp(1)+85 tmp(2)+tmp(4)+20 80 25],...
217                        'string','Forward >>',...
218                        'style','pushbutton',...
219                        'callback',{@l_BackForward,'forward',tree},...
220                        'enable','off',...
221                        'busyaction','cancel',...
222                        'Interruptible','off');
223
224% Path edit
225tmp=get(forward_btn,'position');
226pathedit = uicontrol('parent',fh,...
227                     'units','pixel',...
228                     'position',[tmp(1)+tmp(3)+5 tmp(2) 690 25],...
229                     'string','',...
230                     'style','edit',...
231                     'horizontalalign','left',...
232                     'backgroundcolor','w',...
233                     'callback',{@l_OpenDirectory,tree,[]});
234
235
236% Open and Cancel buttons
237tmp=get(selfiletree,'pos');
238openbtn = uicontrol('parent',fh,...
239                    'units','pixel',...
240                    'position',[tmp(1)+tmp(3)-80 10 80 25],...
241                    'string','Open',...
242                    'style','pushbutton',...
243                    'userdata',0,...
244                    'callback','set(gcbo,''userdata'',1),uiresume(gcbf)',...
245                    'enable','off');
246tmp=get(openbtn,'pos');
247cancelbtn = uicontrol('parent',fh,...
248                      'units','pixel',...
249                      'position',[tmp(1)-85 10 80 25],...
250                      'string','Cancel',...
251                      'style','pushbutton',...
252                      'userdata',1,...
253                      'callback','uiresume(gcbf)');
254
255% File Filter popup
256tmp=get(tree,'position');
257filefilter_popup = uicontrol('parent',fh,...
258                             'units','pixel',...
259                             'position',[tmp(1)+105 tmp(2)-35 440 25],...
260                             'string',{' '},...
261                             'value',1,...
262                             'style','popup',...
263                             'backgroundcolor','w',...
264                             'callback',{@l_DirNodeSelected,tree,filetree});
265ff_tx = uicontrol('parent',fh,...
266                  'units','pixel',...
267                  'position',[tmp(1) tmp(2)-35 100 20],...
268                  'string','Show Files of Type:',...
269                  'horizontalalign','left',...
270                  'style','text',...
271                  'backgroundcolor',FigColor);%[236 233 216]./255);
272
273ffstr = {filefilter{:,2}};
274
275% Construct find string for regexp
276regexp_str = strrep(strrep({filefilter{:,1}},';','|'),'*.','\.');
277set(filefilter_popup,'string',ffstr,'userdata',regexp_str)
278
279H.tree = tree;
280H.filetree = filetree;
281H.selfiletree = selfiletree;
282H.openbtn = openbtn;
283H.cancelbtn = cancelbtn;
284H.filefilter_popup = filefilter_popup;
285H.pathedit = pathedit;
286H.back_btn = back_btn;
287H.forward_btn = forward_btn;
288H.showpathcb = showpathcb;
289H.addbtn = addbtn;
290H.rembtn = rembtn;
291H.history = {};
292H.historyind = 1;
293H.updateHistory = true;
294H.iconpath = iconpath;
295setappdata(H.fh,'H',H)
296
297% Try to open default directory
298if ~ischar(defaultdir)
299  if isunix
300        defaultdir = getenv('HOME');
301  else
302        defaultdir = getenv('USERPROFILE');
303  end
304  %defaultdir=pwd;
305end
306l_OpenDirectory([],[],tree,defaultdir)
307drawnow
308
309% Wait for exit
310uiwait(H.fh)
311if get(H.openbtn,'userdata')==0
312  % Action canceled
313  filename=0;
314  filepath=0;
315  filterindex=0;
316  delete(H.fh)
317  return
318end
319
320% Get selected files
321filename = {};
322filepath = {};
323filterindex = get(H.filefilter_popup,'value');
324selfileroot = handle(selfiletree.getRoot);
325childcount=selfileroot.ChildCount;
326for ii=1:childcount
327  node = selfileroot.getChildAt(ii-1);
328  value = node.getValue;
329  [fpath,fname,fext]=fileparts(value);
330  filename{ii}=[fname,fext];
331  filepath{ii}=[fpath,filesep];
332end
333
334fig_h = H.fh;
335clear H tree filetree selfiletree
336
337% Close window
338delete(fig_h)
339clear an2_juigetfiles
340return
341
342
343% $$$ %%%%%%%%%%%%%%%%%%%%%%%%%
344% $$$ % Expand function
345% $$$ %%%%%%%%%%%%%%%%%%%%%%%%%
346% $$$ function nodes=l_ExpFcn(tree,value)
347% $$$ %disp('Calling ExpFcn...')
348% $$$ set(gcf,'pointer','watch')
349% $$$ drawnow
350% $$$ if strcmpi(value,'My Computer')
351% $$$   
352% $$$   %% Use Java to determine available drives
353% $$$   drives = {};
354% $$$   r=java.io.File.listRoots;
355% $$$   for n=1:length(r)
356% $$$     drives{n} = char(r(n).toString);
357% $$$   end
358% $$$   
359% $$$   % Add drives to the tree
360% $$$   %nodes = [];
361% $$$   for ii=1:length(drives)
362% $$$     if any(strcmpi(drives{ii},{'A:\','B:\'}))
363% $$$       iconpath = '.\icons\floppyicon.gif';
364% $$$     else
365% $$$       iconpath = '.\icons\driveicon.gif';
366% $$$     end
367% $$$     nodes(ii) = uitreenode(drives{ii},drives{ii},iconpath,0);
368% $$$   end
369% $$$ else
370% $$$   count = 0;
371% $$$   ch = dir(value);
372% $$$   if isempty(ch)
373% $$$     tmp=tree.SelectedNodes;
374% $$$     node = handle(tmp(1));
375% $$$     
376% $$$     % Set loaded state for the node back to false
377% $$$     tree.setLoaded(node,false)
378% $$$     
379% $$$     % Set leafing back to "off" position
380% $$$     %node.Leaf(false)
381% $$$     
382% $$$     % Refresh node
383% $$$     tree.reloadNode(node)
384% $$$     
385% $$$     nodes=[];
386% $$$     h=warndlg(['Cannot access "' value '"'],'Access error');
387% $$$     set(gcf,'pointer','arrow')
388% $$$     return
389% $$$   else
390% $$$     directories = {ch([ch(:).isdir]).name};
391% $$$   end
392% $$$   %files = {ch(~[ch(:).isdir]).name};
393% $$$   
394% $$$   for ii=1:length(directories)
395% $$$     if (any(strcmp(directories{ii}, {'.', '..', ''})) == 0)
396% $$$       count = count + 1;
397% $$$       iconpath = '.\icons\Folder.gif';
398% $$$       
399% $$$       leafing=l_CheckLeafing([value, directories{ii}, filesep]);
400% $$$       nodes(count) = uitreenode([value, directories{ii}, filesep], ...
401% $$$                                 directories{ii}, iconpath, leafing);
402% $$$     end
403% $$$   end
404% $$$   
405% $$$   if count==0
406% $$$     nodes = [];
407% $$$   end
408% $$$ end
409% $$$ set(gcf,'pointer','arrow')
410
411
412
413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414% Alternate expand function
415%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416function l_ExpFcnAlt(h,evd,tree)
417
418H=getappdata(findall(0,'tag','an2_juigetfiles_main_fig'),'H');
419set(H.fh,'pointer','watch')
420drawnow
421
422evdnode = evd.getCurrentNode;
423value=evdnode.getValue;
424
425if ~tree.isLoaded(evdnode)
426
427  if strcmpi(value,'My Computer')
428   
429    %% Use Java to determine available drives, as using DIR is veeryy
430    %% slooow
431    drives = {};
432    r=java.io.File.listRoots;
433    for n=1:length(r)
434      drives{n} = char(r(n).toString);
435    end
436   
437    % Add drives to the tree
438    %nodes = [];
439    for ii=1:length(drives)
440      if any(strcmpi(drives{ii},{'A:\','B:\'}))
441      iconpath = [H.iconpath,'floppyicon.gif'];
442      else
443        iconpath = [H.iconpath,'driveicon.gif'];
444      end
445      nodes(ii) = uitreenode(drives{ii},drives{ii},iconpath,0);
446    end
447  else
448    count = 0;
449    ch = dir(value);
450    if isempty(ch)
451      %tmp=tree.SelectedNodes;
452      %node = handle(tmp(1));
453     
454      % Set loaded state for the node back to false
455      tree.setLoaded(evdnode,false);
456     
457      % Refresh node
458      tree.reloadNode(evdnode);
459     
460      h=warndlg(['Cannot access "' value '"'],'Access error','modal');
461      set(H.fh,'pointer','arrow')
462      return
463    else
464      directories = {ch([ch(:).isdir]).name};
465          if isunix
466                % Hide hidden directories in Linux
467                ind = regexp(directories,'\.*');
468                directories = {directories{cellfun('isempty',ind)}};
469          end
470    end
471   
472    for ii=1:length(directories)
473      if (any(strcmp(directories{ii}, {'.', '..', ''})) == 0)
474        count = count + 1;
475        iconpath = [H.iconpath,'Folder.gif'];
476       
477        %leafing=l_CheckLeafing([value, directories{ii}, filesep]);
478        leafing=false;
479        nodes(count) = uitreenode([value, directories{ii}, filesep], ...
480                                  directories{ii}, iconpath, leafing);
481      end
482    end
483   
484    if count==0
485      % Set loaded state for the node back to false
486      tree.setLoaded(evdnode,false);
487     
488      % Refresh node
489      tree.reloadNode(evdnode);
490     
491      set(H.fh,'pointer','arrow')
492      return
493    end
494  end
495 
496  %% Add child nodes
497  matlab_ver = version;
498  if length(nodes)==1 && strcmp(matlab_ver(1:3),'7.1')
499    % Make nodes a JavaArray
500    tmpnodes = nodes;
501    nodes=javaArray('com.mathworks.hg.peer.UITreeNode',1);
502    nodes(1) = java(tmpnodes);
503  end
504 
505  tree.add(evdnode,nodes);
506  tree.setLoaded(evdnode,true);
507  tree.reloadNode(evdnode);
508end
509set(H.fh,'pointer','arrow')
510
511
512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513% Check leafing for directories
514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515function leafing=l_CheckLeafing(path)
516
517s=dir(path);
518if isempty(s)
519  leafing = true;
520  return
521end
522
523if length(s([s(:).isdir]))>2
524  leafing = false;
525else
526  leafing = true;
527end
528
529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530% Execute when directory node is selected
531%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532function l_DirNodeSelected(h,evd,tree,filetree)
533
534H=getappdata(findall(0,'tag','an2_juigetfiles_main_fig'),'H');
535set(H.fh,'pointer','watch')
536drawnow
537
538% Clear file tree view
539filetree.setRoot([]);
540fileroot = uitreenode('Loading','Loading...','',false);
541filetree.setRoot(fileroot);
542filetree.reloadNode(fileroot);
543
544
545% Get Root
546%fileroot = handle(filetree.getRoot);
547
548% Remove all children
549%filetree.removeAllChildren(fileroot);
550%filetree.reloadNode(fileroot);
551
552
553% Get selected node
554currentnode=tree.getSelectedNodes;
555currentnode=handle(currentnode(1));
556
557% Get path
558path = currentnode.getValue;
559if strcmp(path,'My Computer')
560  %selfiletree.add(selfileroot,new_nodes);
561  %selfiletree.setLoaded(selfileroot,true);
562  %selfiletree.expand(selfileroot);
563  %selfileroot.setName([num2str(length(new_nodes)) ' Files Selected'])
564  fileroot.setValue('My Computer');
565  fileroot.setName('My Computer');
566  filetree.reloadNode(fileroot)
567  set(H.fh,'pointer','arrow')
568  return
569end
570
571
572% Try to get file information from the path
573ch=dir(path);
574if isempty(ch)
575  %rootnode = uitreenode(['Cannot access ' path],['Cannot access 'path],'',false);
576  %filetree.setRoot(rootnode);
577  %filetree.reloadNode(rootnode);
578  fileroot.setName(['Cannot access ' path]);
579  fileroot.setValue(['Cannot access ' path]);
580  filetree.reloadNode(fileroot)
581  set(H.pathedit,'string',path)
582  h=warndlg(['Cannot access "' path '"'],'Access error','modal');
583  set(H.fh,'pointer','arrow')
584  return
585end
586
587if H.updateHistory
588  % Add path to history
589  if H.historyind<length(H.history)
590    H.history(H.historyind+1:end)=[];
591  end
592  if isempty(H.history) || ~strcmp(H.history{end},path)
593    H.history{end+1} = path;
594  end
595  H.historyind = length(H.history);
596  set(H.forward_btn,'enable','off')
597  if length(H.history)>1
598    set(H.back_btn,'enable','on')
599  end
600 
601  % Keep maximum of 50 folders in history
602  if length(H.history)>50
603    H.history(1)=[];
604  end
605  setappdata(H.fh,'H',H)
606end
607
608% Get Name
609name = currentnode.getName;
610
611% Set up file tree root
612%rootnode = uitreenode(path,path,'',false);
613%filetree.setRoot(rootnode);
614fileroot.setName(path);
615fileroot.setValue(path);
616
617
618% Filter filenames
619filenames={ch(~[ch(:).isdir]).name};
620filter_val=get(H.filefilter_popup,'value');
621regexp_str = get(H.filefilter_popup,'userdata');
622regexp_str = regexp_str{filter_val};
623if ~strcmp(regexp_str,'\.*')
624  [s_ind,e_ind]=regexpi(filenames,['(' regexp_str ')$']);
625  if ~isempty(s_ind)
626    ind = (double(char(s_ind{:}))-double(char(e_ind{:})))~=0;
627    filenames = {filenames{logical(ind)}};
628  else
629    filenames = {};
630  end
631end
632directories={ch([ch(:).isdir]).name};
633if isunix
634  % Hide hidden directories in Linux
635  ind = regexp(directories,'\.*');
636  directories = {directories{cellfun('isempty',ind)}};
637end
638
639% Create child nodes
640count=0;
641for ii=1:length(directories)
642  if ~any(strcmp(directories{ii},{'.','..'}))
643    count=count+1;
644    nodes(count)=uitreenode([path,directories{ii},filesep],directories{ii},[H.iconpath,'Folder.gif'],false);
645  end
646end
647for ii=1:length(filenames)
648  count=count+1;
649  nodes(count)=uitreenode([path,filenames{ii}],filenames{ii},[H.iconpath,'new.gif'],true);
650end
651
652set(H.pathedit,'string',path)
653if count==0;
654  filetree.reloadNode(fileroot);
655  set(H.fh,'pointer','arrow')
656  return
657end
658
659%% Add child nodes
660matlab_ver = version;
661if length(nodes)==1 && any(strcmp(matlab_ver(1:3),{'7.1','7.2'}))
662  % Make nodes a JavaArray
663  tmpnodes = nodes;
664  nodes=javaArray('com.mathworks.hg.peer.UITreeNode',1);
665  nodes(1) = java(tmpnodes);
666end
667
668filetree.add(fileroot,nodes);
669filetree.setLoaded(fileroot,true);
670filetree.expand(fileroot);
671filetree.reloadNode(fileroot)
672set(H.fh,'pointer','arrow')
673%filetree.add(rootnode,nodes);
674%filetree.setLoaded(rootnode,true);
675%filetree.expand(rootnode);
676
677
678%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679% Files selected
680%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681function l_FileTreeNodeSelected(h,evd,filetree)
682%disp('l_FileTreeNodeSelected')
683
684nodes=filetree.getSelectedNodes;
685if isempty(nodes)
686  return
687end
688
689% Don't allow selection of directories
690count=0;
691for ii=1:length(nodes)
692  if ~nodes(ii).isRoot
693    count=count+1;
694    selnodes(count)=nodes(ii);
695  end
696end
697
698if count==0
699  filetree.setSelectedNodes([]);
700else
701  % Reselect only files
702  filetree.setSelectedNodes(selnodes);
703end
704
705
706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707% Filetree collapsed
708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709function l_FileTreeNodeCollapsed(h,evd,filetree)
710
711% Don't let the filetree root node to collapse
712filetree.expand(filetree.getRoot);
713
714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715% Node Dropped to selected files tree
716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717function l_SelFileTreeNodeDropped(h,evd,selfiletree)
718
719tmp=handle(evd);
720SourceNode=tmp.getSourceNode;
721TargetNode=tmp.getTargetNode;
722
723target_value = TargetNode.getValue;
724source_value = SourceNode.getValue;
725if SourceNode.isRoot || TargetNode.isRoot
726  return
727end
728if strcmp(target_value,source_value)
729  return
730end
731
732
733% Reorder nodes
734selfileroot = handle(selfiletree.getRoot);
735childcount=selfileroot.ChildCount;
736
737% Construct a new javaArray for nodes
738nodes=javaArray('com.mathworks.hg.peer.UITreeNode',childcount);
739
740
741count=0;
742fromUp = false;
743if childcount ~= 0
744  for ii=0:childcount-1
745    tmp=selfileroot.getChildAt(ii);
746    if strcmp(tmp.getValue,target_value)
747      if fromUp
748        count=count+1;
749        nodes(count) = TargetNode;
750        count=count+1;
751        nodes(count) = SourceNode;
752      else
753        count=count+1;
754        nodes(count) = SourceNode;
755        count=count+1;
756        nodes(count) = TargetNode;
757      end
758    elseif ~strcmp(tmp.getValue,source_value)
759      count=count+1;
760      nodes(count) = tmp;
761    else
762      fromUp = true;
763    end
764  end
765end
766
767%selfiletree.setRoot([]);
768%selfileroot = uitreenode('Selected Files','Selected Files','',false);
769%selfiletree.setRoot(selfileroot);
770selfiletree.removeAllChildren(selfileroot);
771
772% Add new nodes to the tree
773selfiletree.add(selfileroot,nodes);
774selfiletree.expand(selfileroot);
775selfiletree.reloadNode(selfileroot);
776
777
778%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779% ADD/REMOVE Files
780%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781function l_AddRemFiles(h,evd,opt,filetree,selfiletree)
782
783% Get handles
784H=getappdata(findall(0,'tag','an2_juigetfiles_main_fig'),'H');
785
786switch opt
787 case 'add'
788 
789  % Look for a quick return
790  if isempty(filetree.getSelectedNodes)
791    return
792  end
793 
794  % Get Rootnode
795  selfileroot = handle(selfiletree.getRoot);
796 
797   
798   if get(H.showpathcb,'value')
799     ShowPaths = true;
800   else
801     ShowPaths = false;
802   end
803   
804   % Get selected nodes
805   selnodes=filetree.getSelectedNodes;
806   
807   % Get existing nodes in the selfiletree
808   childcount=selfileroot.ChildCount;
809   count=0;
810   if childcount ~= 0
811     for ii=0:childcount-1
812       count=count+1;
813       selfilenodes(count) = selfileroot.getChildAt(ii);
814     end
815   end
816   
817   % Add existing nodes to new_nodes
818   selfilenodevalues={};
819   for ii=1:childcount
820     if ShowPaths
821       name_str = selfilenodes(ii).getValue;
822     else
823       [fpath,fname,fext] = fileparts(selfilenodes(ii).getValue);
824       name_str = [fname,fext];
825     end
826     new_nodes(ii) = uitreenode(selfilenodes(ii).getValue,...
827                                name_str,...
828                                [H.iconpath,'new.gif'],...
829                                true);
830     selfilenodevalues{ii}=selfilenodes(ii).getValue;
831   end
832   
833   % Add selected nodes to new_nodes
834   count=childcount;
835   count2=0;
836   for ii=childcount+1:childcount+length(selnodes)
837     count2=count2+1;
838     % Check if node already exists
839     tmp=selnodes(count2).getValue;
840     if ~any(strcmp(selfilenodevalues,selnodes(count2).getValue)) && ...
841         ~strcmp(tmp(end),'\') && ~selnodes(count2).isRoot
842       if ShowPaths
843         name_str = selnodes(count2).getValue;
844       else
845         [fpath,fname,fext] = fileparts(selnodes(count2).getValue);
846         name_str = [fname,fext];
847       end
848       count=count+1;
849       new_nodes(count) = uitreenode(selnodes(count2).getValue,...
850                                     name_str,...
851                                     [H.iconpath,'new.gif'],...
852                                     true);
853     end
854   end
855   if count==0
856     return
857   end
858   
859   % Enable Open button
860   set(H.openbtn,'enable','on')
861   
862   
863   matlab_ver = version;
864   if length(new_nodes)==1 && strcmp(matlab_ver(1:3),'7.1')
865     % Make nodes a JavaArray
866     tmpnodes = new_nodes;
867     new_nodes=javaArray('com.mathworks.hg.peer.UITreeNode',1);
868     new_nodes(1) = java(tmpnodes);
869   end
870   
871   % Remove old nodes from the tree
872   %selfiletree.setRoot([]);
873   %selfileroot = uitreenode('Selected Files','Selected Files','',false);
874   %selfiletree.setRoot(selfileroot);
875   selfileroot.removeAllChildren;
876   selfiletree.reloadNode(selfileroot);
877   %selfiletree.repaint;
878   %selfiletree.removeAllChildren(selfileroot);
879   
880   % Add new nodes to the tree
881   selfiletree.add(selfileroot,new_nodes);
882   %selfiletree.setLoaded(selfileroot,true);
883   selfiletree.expand(selfileroot);
884   selfileroot.setName([num2str(length(new_nodes)) ' Files Selected'])
885   selfiletree.reloadNode(selfileroot)
886   
887 case 'remove'
888  %keyboard
889  selnodes = selfiletree.getSelectedNodes;
890 
891  % Look for a quick return
892  if isempty(selnodes)
893    return
894  end
895 
896  % Get values to the selected nodes
897  selnodevalues = cell(1,length(selnodes));
898  for ii=1:length(selnodes)
899    tmp=handle(selnodes(ii));
900    selnodevalues{ii}=tmp.getValue;
901  end
902 
903  % Get Root
904  selfileroot=selfiletree.getRoot;
905 
906  % Get number of files in the tree
907  childcount = selfileroot.getChildCount;
908 
909  count = 0;
910  for ii=0:childcount-1
911    tmp=selfileroot.getChildAt(ii);
912    if ~any(strcmp(tmp.getValue,selnodevalues))
913      count=count+1;
914      new_nodes(count) = tmp;
915    end
916  end
917 
918  %selfiletree.setRoot([]);
919  %selfileroot = uitreenode('Selected Files','Selected Files','',false);
920  %selfiletree.setRoot(selfileroot);
921  selfileroot.removeAllChildren;
922  selfiletree.reloadNode(selfileroot);
923  %selfiletree.removeAllChildren(selfileroot);
924   
925  % Add new nodes to the tree
926  if count~=0
927    selfiletree.add(selfileroot,new_nodes);
928    selfileroot.setName([num2str(length(new_nodes)) ' Files Selected'])
929    selfiletree.reloadNode(selfileroot)
930  else
931    % Unenable Open button
932    set(H.openbtn,'enable','off')
933   
934    selfileroot.setName('0 Files Selected')
935    selfiletree.reloadNode(selfileroot)
936  end
937  %selfiletree.setLoaded(selfileroot,true);
938  selfiletree.expand(selfileroot);
939end
940
941%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942% Open directory
943%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944function l_OpenDirectory(h,evd,tree,directory)
945
946% Get handles
947H=getappdata(findall(0,'tag','an2_juigetfiles_main_fig'),'H');
948set(H.fh,'pointer','watch')
949drawnow
950
951if isempty(directory)
952  directory = get(h,'string');
953elseif isnumeric(directory)
954  tmp=evd.getCurrentNode;
955  if tmp.isRoot
956    set(H.fh,'pointer','arrow')
957    return
958  end
959  directory=tmp.getValue;
960end
961% Check that directory exists
962if ~isdir(directory)
963  h=errordlg({['Cannot open folder "' directory '".'],['Make sure the path is ' ...
964                      'correct.']},'Cannot open folder','modal');
965  set(H.fh,'pointer','arrow')
966  return
967end
968if ~strcmp(directory(end),filesep)
969  directory = [directory,filesep];
970end
971
972% See if we are in the specified directory already
973%keyboard
974tmp=tree.getSelectedNodes;
975if ~isempty(tmp)
976  tmp=handle(tmp(1));
977  if strcmp(tmp.getValue,directory)
978    set(H.fh,'pointer','arrow')
979    return
980  end
981end
982
983if H.updateHistory
984  % Add path to history
985  if H.historyind<length(H.history)
986    H.history(H.historyind+1:end)=[];
987  end
988  if isempty(H.history) || ~strcmp(H.history{end},directory)
989    H.history{end+1} = directory;
990  end
991  H.historyind = length(H.history);
992  set(H.forward_btn,'enable','off')
993  if length(H.history)>1
994    set(H.back_btn,'enable','on')
995  end
996 
997  % Keep maximum of 50 folders in history
998  if length(H.history)>50
999    H.history(1)=[];
1000  end
1001  setappdata(H.fh,'H',H)
1002end
1003
1004% Get root
1005rootnode = tree.getRoot;
1006
1007
1008ind=find(directory==filesep);
1009
1010%keyboard
1011drawnow
1012drawnow
1013start_ind=0;
1014currentnode = rootnode;
1015for ii=1:length(ind)
1016  if ii==1
1017    currentdir=directory(start_ind+1:ind(ii));
1018  else
1019    currentdir=directory(start_ind+1:ind(ii)-1);
1020  end
1021  childcount=currentnode.getChildCount;
1022  for kk=1:childcount
1023    tmp=currentnode.getChildAt(kk-1);
1024    name=tmp.getName;
1025    if strcmp(name,currentdir)
1026      %tree.expand(tmp);
1027      %tree.setLoaded(tmp,true)
1028      %tree.reloadNode(tmp);
1029      currentnode=tmp;
1030      if ii~=length(ind)
1031        tree.expand(currentnode)
1032      end
1033      %tree.setLoaded(tmp,true)
1034      %tree.setSelectedNode(currentnode);
1035      %tree.reloadNode(currentnode);
1036      %pause(12)
1037      drawnow
1038      break;
1039    end
1040  end
1041  start_ind = ind(ii);
1042end
1043
1044%tree.expand(currentnode)
1045%tree.setLoaded(currentnode,true)
1046tree.setSelectedNode(currentnode);
1047tree.reloadNode(currentnode);
1048set(H.fh,'pointer','arrow')
1049%tree.setSelectedNode(currentnode)
1050
1051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1052% SHOW/HIDE path names from selected files
1053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054function l_ShowHidePathNames(h,evd,selfiletree)
1055
1056if get(h,'value')==1
1057  ShowPaths = true;
1058else
1059  ShowPaths = false;
1060end
1061
1062% Redraw all nodes in the tree
1063selfileroot = handle(selfiletree.getRoot);
1064childcount=selfileroot.ChildCount;
1065if childcount==0
1066  return
1067end
1068
1069count=0;
1070if childcount ~= 0
1071  for ii=0:childcount-1
1072    count=count+1;
1073    childnode=selfileroot.getChildAt(ii);
1074    if ShowPaths
1075      childnode.setName(childnode.getValue);
1076    else
1077      [fpath,fname,fext] = fileparts(childnode.getValue);
1078      childnode.setName([fname,fext])
1079    end
1080  end
1081end
1082
1083% Reload root node
1084selfiletree.reloadNode(selfileroot);
1085
1086
1087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1088% BACK/FORWARD functions
1089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090function l_BackForward(h,evd,opt,tree)
1091
1092% Get handles
1093H=getappdata(findall(0,'tag','an2_juigetfiles_main_fig'),'H');
1094%set(H.back_btn,'enable','inactive')
1095%set(H.forward_btn,'enable','inactive')
1096
1097% Put up a flag that prevents the history to be updated
1098H.updateHistory = false;
1099setappdata(H.fh,'H',H)
1100
1101switch opt
1102 case 'back'
1103  %keyboard
1104  if length(H.history)>1
1105    H.historyind = H.historyind-1;
1106    drawnow % Java seems to be slow to update...
1107    l_OpenDirectory([],[],tree,H.history{H.historyind})
1108    drawnow
1109    set(H.forward_btn,'enable','on')
1110    if H.historyind==1
1111      set(H.back_btn,'enable','off')
1112    end
1113  end
1114 
1115  % Put history update flag down
1116  H.updateHistory = true;
1117  setappdata(H.fh,'H',H)
1118  %H.history
1119  %H.historyind
1120 case 'forward'
1121  if length(H.history)==H.historyind
1122    return
1123  end
1124 
1125  if H.historyind<length(H.history)
1126    H.historyind = H.historyind+1;
1127    drawnow
1128    l_OpenDirectory([],[],tree,H.history{H.historyind})
1129    drawnow
1130    set(H.back_btn,'enable','on')
1131    if H.historyind==length(H.history)
1132      set(H.forward_btn,'enable','off')
1133    end
1134  end
1135 
1136  % Put history update flag down
1137  H.updateHistory = true;
1138  setappdata(H.fh,'H',H)
1139  %H.history
1140  %H.historyind
1141end
1142
1143
1144%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1145% Resize Function
1146%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1147function l_Resize(h,evd)
1148
1149% To be written...
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008