source: an2_juigetfiles.m @ 48

Last change on this file since 48 was 47, checked in by tjniskan, 11 years ago
  • Fixed a bug in an2_fitmaps which prevented saving multislice maps
  • Fixed a bug in the handling of hidden folder under Linux in an2_juigetfiles

M an2_revision.m
M an2_fitmaps.m
M an2_juigetfiles.m

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

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