source: an2_juigetfiles.m @ 73

Last change on this file since 73 was 51, checked in by tjniskan, 11 years ago
  • Added an option for rotating 3D and 4D data.
  • Fixed a minor bug that affected nonlinear map calculations.
  • Added a plugin for Nick and Riikka for calculating CBF maps from ASL

data using T1-maps.

  • Fixed a few minor regexp and questdlg bugs here and there.

M an2_export_gui.m
M an2_rot3d.m
M an2_revision.m
M an2_fitmaps.m
M aedes.m
A plugins/calc_asl_cbf.m
M an2_rotateflip.m
M an2_juigetfiles.m

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

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