source: aedes_juigetfiles.m @ 133

Last change on this file since 133 was 133, checked in by tjniskan, 9 years ago
  • Fixed the Matlab version detection bug
  • Added zoom function to mouse wheel
  • Fixed a bug in contrast auto-balancing

M aedes_getmatlabversion.m
M aedes_resviewer.m
M aedes.m
M aedes_res2table.m
M aedes_revision.m
M aedes_juigetfiles.m

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

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