Today I want to look at 2 new features of our Rocket FM application that we have been building. We are going to look at how to delete files and how to download them. We will start with the delete feature and move on to downloading because the download feature is a pretty cool one and I will get into why I think it is a good one.

You always have to be aware of what the user may do that could cause our application to fail. Let's say I created a click event for out delete button that called some logic to delete a file. Well what if a file(s) was not selected. We could use some logic to make sure a file is selected. For me though that is unnecessary code to write. I think a better solution is to just now allow the delete button to be clicked until a file is selected. Using data binding and the enabled property of the button we can check to make sure at least one file is selected.

To delete a file we need to update our file manager component first and a a delete method. There is really nothing special going on here, a simple cffile operation to delete the file will work. Now that we have the back end we need to add our deleteFiles method that is called when the delete button is clicked. First we need to figure out what files are selected. This will be at least 1 but could be any number of files. To do that we use the selectedIndicies property of the data grid. In each iteration of the loop we make a call to delete our file. Finally we need to update the list of files. To do that we will focus on the selectedItem property of the tree. I am checking to see if that variable is not null because if it is not we can use the path to go out and get the new list of files. If it is null it probably means that we are in the home directory. This is beccause when the application is launched we grab a list of files for the home directory but its not actually selected by the user.

I said earlier that I was excited about the download feature and here is why. What we are going to do is use some logic to determine how many files are selected when the click download. If one file is selected we will just present the user with a file download for that file. If the user selects multiple files I want to auto magically present them with a zip file containing all of the files selected. That is just another nice little feature that I think will help the user experience.

We already created the logic before to only enable the button when 1 or more files are selected. We also defined a click handler for the button to call a method downloadFiles(). Back in our file manager component we are going to create 3 methods. You may be asking yourself, is he mad? I assure you I am not so let's look at why we should do that. First off we need a download for a single file. I talked this over with a friend and while I agree that I rarely use a component to produce output I really just want of of my file operations to be in one place so I am sticking it in here.

Next we need to create to separate download zip methods. One is going to be for anyone using CF7 and one is going to be for CF8 users. This is because 8 introduced cfzip that makes zipping files a breeze. First we will look at the CF7 solution. To make this work you need to head over to RIAForge and download the zipcfc component written by Artur Kordowski. I will include this in svn unless I get asked to remove it but if your playing along at home you will want to drop it in the org/vega/ folder. All your doing here is looping through the list of files and adding them to the zip using the Zip component. Using CF8 is a little bit easier so anyone who can I would recommend taking advantage of the little code involved.

Finally we need to implement our downloadFiles method. If there is only 1 file selected we will make a call to our download file method else we are going to download a zip. To download the zip we are going to construct a list of full path names.

Everything is moving along nicely but I thought we would sneak one more feature in here. I want to add a Ctrl+A keyboard shortcut for the application. Anytime that shortcut is activated I want to select all the files in the data grid. First thing to do is add a application complete event listener and this is because we want to make sure the stage is available before we attach a keyboard event listener to it. To get everything to work we attach a key down event listener to the stage. Everytime a key is pressed our on key down method is called. A little logic will allow us to find out if ctrl+a is clicked. If it is we grab a list of all the files in the grid and select them by assigning our array to the selectedIndicies property.

I am really rambling on now but I hope your enjoying the series. Please feel free to ask me anything about the project and go over to the project home to grab the latest code from svn.