source: aedes_juigetfiles.m @ 143

Last change on this file since 143 was 143, checked in by tjniskan, 9 years ago
  • Fixed a bug in detecting Matlab version in aedes_resviewer.m, which

resulted in a crash in Matlab R2010a and newer.

  • Fixed a bunch of warnings that were generated in aedes_juigetfiles.m with newer Matlab

versions

M aedes_resviewer.m
M aedes_revision.m
M aedes_juigetfiles.m

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

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