source: aedes_juigetfiles.m @ 140

Last change on this file since 140 was 140, checked in by tjniskan, 9 years ago
  • Fixed: M-files beginning with a dot are now ignored in the plugins

folder (thanks Mikko)

  • Escape key now closes the multiple file dialog (aedes_juigetfiles)

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

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

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