source: aedes_juigetfiles.m @ 134

Last change on this file since 134 was 134, checked in by tjniskan, 9 years ago
  • Fixed an "uitreenode" warning that gets thrown with Matlab R2008a

and newer

M aedes_revision.m
M aedes_juigetfiles.m

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

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