source: aedes_juigetfiles.m @ 96

Last change on this file since 96 was 96, checked in by tjniskan, 10 years ago
  • Added a possibility of loading ROIs in NIfTI file format.
  • Added custom filter feature to aedes_juigetfiles

M aedes_data_read.m
M aedes.m
M aedes_revision.m
M aedes_juigetfiles.m

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

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