//file-browser from which you may open documents and search your scwork folder for documents
//by typing the start of the files' names.
// Thursday 5th October 2006

//   usage  =  FileFinder.new( posRight, posTop);

FileFinder{	
	
	classvar find, currentPath, fileArray, folderArray, pathetic, fileList, folderList, win;
	classvar again, sPath, sText, sBut, results, folders, stuff, backBut, mode = 0, temp;
	
	
	
	*new{ | left = 680, top = 800 | //where to draw the window
		
		fileArray = [];
		folderArray = [];
		
		
		
		find = { | path |
			mode = 0;
			fileArray = [];
			folderArray = [];
			pathetic = PathName.new(path);
			
			currentPath  = pathetic.pathOnly;
			
			pathetic.pathMatch.do{ |i|
				if(PathName(i).isFile, {fileArray=fileArray.add(PathName(i).fileName.asString)},
				   {folderArray=folderArray.add(PathName(i).fileName.asString)});
			};
			fileList.items = fileArray;
			folderList.items = folderArray;
			
			win.name_(pathetic.folderName);
		};
		
		
		win = SCWindow("scwork", Rect(left, top, 340, 280) ).alpha_(0.9).front;
		win.view.background = Color.grey;
		fileList = SCListView(win, Rect(190, 10, 140, 260));
		fileList.items = fileArray;
		fileList.background_(Color.white);
		fileList.action = {arg sbs; 
			sText.string="Search for Files/Folders";
			if(mode == 0, {
				Document.open(currentPath.asString++fileArray.at(sbs.value).asString)
			}, {
				if(PathName(results.at(sbs.value)).isFile, {
					Document.open(results.at(sbs.value).asString);
					currentPath = results.at(sbs.value);
				},{
					find.value(results.at(sbs.value).asString++"*")})
			});
		};
		
		
		folderList = SCListView(win, Rect(10, 10, 140, 240));
		folderList.items = folderArray;
		folderList.background_(Color.white);
		
		
		folderList.action = { arg sbs;
			sText.string="Search for Files/Folders";
			find.value(currentPath.asString++folderArray.at(sbs.value).asString++"/*");
			 
		};
			 
			 
			 backBut = SCButton(win, Rect(155, 10, 30, 260)).states=[["<--", Color.white, Color.black]];
			 backBut.action={ 
				 sText.string="Search for Files/Folders";
				 
				 find.value(currentPath.dirname.asString++"/*")
				  
				  
				  
			 };
				  
				  find.value("~/scwork/*");
				   
				   /////////////////////////
				   //file-search stuff
				   
				   
				   sText = SCStaticText(win, Rect(20, 250, 200, 30)).string="Search for Files/Folders";
				   
				   
				   sBut = SCButton(win, Rect(10, 255, 9, 20)).states=[["", Color.red, Color.red]];
				   
				   sBut.keyDownAction = {|a, b| sText.string=sText.string++ b.value;
					   mode = 1;
					   results = [];
					   
					   folders = ["~/scwork/"];
					   
					   5.do{  //levels of resursive folder-searching
		
						   folders.do{ | thisPath |
							   
							   stuff = PathName(thisPath++"*").pathMatch; //all things
							   stuff.do{|i| if(PathName(i).isFolder, { 
								   
								   folders=folders.reject({arg j; i==j});
								   
								   folders=folders.add(i);
								   
							   });
								   
							   };
							   
						   };
						   
					   };
					   
					   folders.do{ | thisPath |
						   results = results.addAll( PathName(thisPath++sText.string++"*").pathMatch );
					   };
					   
					   if(results.size>0, {
						   temp = [];
						   results.do{ |i| temp = temp.add(PathName(i).fileName.asString) };
						   fileList.items = temp;
					   }, {fileList.items = []});
				   };
				   sBut.action = { sText.string=""};
				   
	} //EO *new
				   
}
//bobby