package org.greenstone.gatherer.file; /** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * *

* * Author: John Thompson, Greenstone Digital Library, University of Waikato * *

* * Copyright (C) 1999 New Zealand Digital Library Project * *

* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * *

* * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * *

* * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.file.FileJob; import org.greenstone.gatherer.file.FileNode; import org.greenstone.gatherer.gui.LongProgressBar; import org.greenstone.gatherer.gui.SmudgyLabel; import org.greenstone.gatherer.gui.tree.DragTree; import org.greenstone.gatherer.msm.GDMManager; import org.greenstone.gatherer.undo.UndoManager; import org.greenstone.gatherer.util.ArrayTools; import org.greenstone.gatherer.util.DragComponent; import org.greenstone.gatherer.util.SynchronizedTreeModelTools; import org.greenstone.gatherer.util.Utility; /** A threaded object which processes a queue of file actions such as copying and movement. It also handles updating the various trees involved so they are an accurate representation of the file system they are meant to match. * @author John Thompson, Greenstone Digital Library, University of Waikato * @version 2.3 */ public class FileQueue extends Thread implements TreeSelectionListener { /** When someone requests the movement queue to be dumped this cancel flag is set to true. */ private boolean cancel_action = false; /** A temporary mapping from currently existing FileNode folder to their equivelent FileNode folder within the undo managers tree. */ private HashMap completed_folder_mappings = new HashMap(); /** true to cause this file queue to return from run() as soon as there are no jobs left on the queue. Useful for undo jobs which must occur before a specific action. */ private boolean return_immediately = false; /** We are only allowed to wait under specific circumstances. */ private boolean wait_allowed = true; /** true if the user has selected yes to all from a file 'clash' dialog. */ private boolean yes_to_all = false; /** A temporary mapping from currently existing FileNodes to the potential FileNode folder within the undo managers tree. */ private HashMap recycle_folder_mappings = new HashMap(); /** A label explaining the current moving files status. */ private JLabel file_status = null; // was a SmudgyLabel /** A label explaining the status of this job. */ //private JLabel job_status = null; // no longer used /** A list containing a queue of waiting movement jobs. */ //private LinkedList queue; private ArrayList queue; /** A progress bar which shows how many bytes, out of the total size of bytes, has been moved. */ private LongProgressBar progress = null; /** The last piece of text shown on the file status label, just incase we are displaying a very temporary message. */ private String previous = null; /** Constructor. * @param return_immediately true to cause this file queue to return from run() as soon as there are no jobs left on the queue. * @see org.greenstone.gatherer.Configuration * @see org.greenstone.gatherer.gui.Coloring * @see org.greenstone.gatherer.gui.LongProgressBar */ public FileQueue(boolean return_immediately) { this.return_immediately = return_immediately; this.queue = new ArrayList();//LinkedList(); String args[] = new String[2]; args[0] = "0"; args[1] = "0"; file_status = new JLabel(get("Selected", args)); //new SmudgyLabel(get("Selected", args)); //job_status = new JLabel(get("No_Activity")); progress = new LongProgressBar(); progress.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); progress.setForeground(Gatherer.config.getColor("coloring.collection_tree_foreground", false)); progress.setString(get("No_Activity")); progress.setStringPainted(true); args = null; } /** Requeue an existing job into the queue. * @param job A previously created FileJob. */ synchronized public void addJob(FileJob job, int position) { job.done = true; // Ensure that the requeued job is marked as done. queue.add(position, job); notify(); } /** Add a new job to the queue, specifiying as many arguments as is necessary to complete this type of job (ie delete needs no target information). * @param id A long id unique to all jobs created by a single action. * @param source The DragComponent source of this file, most likely a DragTree. * @param child The FileNode you wish to mode. * @param target The DragComponent to move the file to, again most likely a DragTree. * @param parent The files new FileNode parent within the target. * @param type The type of this movement as an int, either COPY or DELETE. * @param undo true if this job should generate undo jobs, false for redo ones. * @param undoable true if this job can generate undo or redo jobs at all, false otherwise. */ public void addJob(long id, DragComponent source, FileNode child, DragComponent target, FileNode parent, byte type, boolean undo, boolean undoable, boolean folder_level) { addJob(id, source, child, target, parent, type, undo, undoable, folder_level, -1); } synchronized public void addJob(long id, DragComponent source, FileNode child, DragComponent target, FileNode parent, byte type, boolean undo, boolean undoable, boolean folder_level, int position) { FileJob job = new FileJob(id, source, child, target, parent, type, undo, undoable); job.folder_level = folder_level; ///ystem.err.println("Adding job: " + job); if(position != -1 && position <= queue.size() + 1) { queue.add(position, job); } else { queue.add(job); } notify(); } public void calculateSize(FileNode[] files) { progress.reset(); progress.setString(get("FileActions.Calculating_Size")); progress.setIndeterminate(true); Vector remaining = new Vector(); for(int i = 0; i < files.length; i++) { remaining.add(files[i]); } while(remaining.size() > 0) { FileNode node = (FileNode)remaining.remove(0); if(node.isLeaf()) { progress.addMaximum(node.getFile().length()); } else { for(int i = 0; i < node.getChildCount(); i++) { remaining.add(node.getChildAt(i)); } } } progress.setString(get("No_Activity")); progress.setIndeterminate(false); } /** This method is called to cancel the job queue at the next available moment. */ public void cancelAction() { cancel_action = true; } /** Access to the file state label. */ public JLabel getFileStatus() { return file_status; } /** Access to the job state label. */ // public JLabel getJobStatus() { // return job_status; // } /** Access to the progress bar. */ public LongProgressBar getProgress() { return progress; } /** Prevent the progress bar updating momentarily, while the progress bar size is re-adjusted. */ public void pause() { progress.setIndeterminate(true); } /** The run method exists in every thread, and here it is used to work its way through the queue of Jobs. If no jobs are waiting and it cans, it waits until a job arrives. If a job is present then it is either COPIED or DELETED, with the records being copied or removed as necessary, and directories being recursed through. Finally the user can press cancel to cause the loop to prematurely dump the job queue then wait. * @see org.greenstone.gatherer.Gatherer * @see org.greenstone.gatherer.collection.CollectionManager * @see org.greenstone.gatherer.file.FileJob * @see org.greenstone.gatherer.file.FileNode * @see org.greenstone.gatherer.gui.LongProgressBar * @see org.greenstone.gatherer.msm.MetadataSetManager * @see org.greenstone.gatherer.undo.UndoManager * @see org.greenstone.gatherer.util.Utility */ public void run() { super.setName("FileQueue"); while(!Gatherer.self.exit) { try { // Retrieve the next job int position = queue.size() - 1; FileJob job = removeJob(position); if(job != null) { ///ystem.err.println("Found job: " + job); // The user can cancel this individual action at several places, so keep track if the state is 'ready' for the next step. boolean ready = true; FileNode origin_node = job.getOrigin(); FileNode destination_node = job.getDestination(); FileSystemModel source_model = (FileSystemModel)job.source.getTreeModel(); FileSystemModel target_model = (FileSystemModel)job.target.getTreeModel(); if(destination_node == null) { // Retrieve the root node of the target model instead. A delete, or course, has no target file so all deleted files are added to the root of the Recycle Bin model. destination_node = (FileNode) target_model.getRoot(); } // Extract common job details. File source_file = origin_node.getFile(); File target_file = null; // Determine the target file for a copy or move. if(job.type == FileJob.COPY || job.type == FileJob.MOVE) { //target_file = new File(destination_node.getFile(), source_file.getName()); // use the name of the filenode instead of the name of the file - these should be the same except for the collection directories where we want the collection name to be used, not 'import' which is the underlying name target_file = new File(destination_node.getFile(), origin_node.toString()); } // To copy a file, copy it then add any metadata found at the source. If this file was already in our collection then we must ensure the lastest version of its metadata.xml has been saved to disk. To copy a directory simply create the directory at the destination, then add all of its children files as new jobs. if((job.type == FileJob.COPY || job.type == FileJob.MOVE) && !job.done) { ///ystem.err.println("Copy/Move: " + origin_node); FileNode new_node = null; // Check if file exists, and action as necessary. Be aware the user can choose to cancel the action all together (where upon ready becomes false). if(target_file.exists()) { // We've previously been told if(yes_to_all) { // Remove the old file and tree entry. target_file.delete(); ready = true; } else { ///atherer.println("Opps! This filename already exists. Give the user some options."); Object[] options = { get("General.Yes"), get("Yes_To_All"), get("General.No"), get("General.Cancel") }; int result = JOptionPane.showOptionDialog(Gatherer.g_man, get("File_Exists", target_file.getName()), get("General.Warning"), JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); switch(result) { case 1: // Yes To All yes_to_all = true; case 0: // Yes // Remove the old file and tree entry. if(destination_node != null) { TreePath destination_path = new TreePath(destination_node.getPath()); FileNode temp_target_node = new FileNode(target_file, target_model, true); TreePath target_path = destination_path.pathByAddingChild(temp_target_node); SynchronizedTreeModelTools.removeNodeFromParent(target_model, target_model.getNode(target_path)); target_path = null; temp_target_node = null; destination_path = null; } target_file.delete(); ready = true; break; case 3: // No To All cancel_action = true; case 2: // No default: ready = false; } } } // We proceed with the copy/move if the ready flag is still set. If it is that means there is no longer any existing file of the same name. if(ready) { // update status area String args[] = new String[1]; args[0] = "" + (queue.size() + 1) + ""; //job_status.setText(get("Jobs", args)); if(job.type == FileJob.COPY) { args[0] = Utility.formatPath("FileActions.Copying", source_file.getAbsolutePath(), file_status.getSize().width); file_status.setText(get("Copying", args)); } else { args[0] = Utility.formatPath("FileActions.Moving", source_file.getAbsolutePath(), file_status.getSize().width); file_status.setText(get("Moving", args)); } args = null; //file_status.setToolTipText(Utility.formatHTMLWidth(file_status.getText(), 80)); // If source is a file if(source_file.isFile()) { // copy the file. If anything goes wrong the copy file should throw the appropriate exception. No matter what exception is thrown (bar an IOException) we display some message, perhaps take some action, then cancel the remainder of the pending file jobs. No point in being told your out of hard drive space for each one of six thousand files eh? try { copyFile(source_file, target_file, progress); } // If we can't find the source file, then the most likely reason is that the file system has changed since the last time it was mapped. Warn the user that the requested file can't be found, then force a refresh of the source folder involved. catch(FileNotFoundException fnf_exception) { Gatherer.printStackTrace(fnf_exception); cancel_action = true; // Show warning. JOptionPane.showMessageDialog(Gatherer.g_man, get("File_Not_Found_Message", source_file.getName()), get("File_Not_Found_Title"), JOptionPane.ERROR_MESSAGE); // Force refresh of source folder. source_model.refresh(new TreePath(((FileNode)origin_node.getParent()).getPath())); } catch(FileAlreadyExistsException fae_exception) { Gatherer.printStackTrace(fae_exception); cancel_action = true; // Show warning. JOptionPane.showMessageDialog(Gatherer.g_man, get("File_Already_Exists_Message", target_file.getName()), get("File_Already_Exists_Title"), JOptionPane.ERROR_MESSAGE); // Nothing else can be done by the Gatherer. } catch(InsufficientSpaceException is_exception) { Gatherer.printStackTrace(is_exception); cancel_action = true; // Show warning. The message body of the expection explains how much more space is required for this file copy. JOptionPane.showMessageDialog(Gatherer.g_man, get("Insufficient_Space_Message", is_exception.getMessage()), get("Insufficient_Space_Title"), JOptionPane.ERROR_MESSAGE); // Nothing else can be done by the Gatherer. In fact if we are really out of space I'm not even sure we can quit safely. } catch(UnknownFileErrorException ufe_exception) { Gatherer.printStackTrace(ufe_exception); cancel_action = true; // Show warning JOptionPane.showMessageDialog(Gatherer.g_man, get("Unknown_File_Error_Message"), get("Unknown_File_Error_Title"), JOptionPane.ERROR_MESSAGE); // Nothing else we can do. } catch(IOException exception) { // Can't really do much about this. Gatherer.printStackTrace(exception); } // If not cancelled if(!cancel_action) { // Step one is to create a dummy FileNode. Its important it has the correct structure so getPath works. FileNode new_record = new FileNode(target_file); SynchronizedTreeModelTools.insertNodeInto(target_model, destination_node, new_record); new_node = new_record; // create undo job if(job.undoable) { job.undoable = false; if(job.type == FileJob.COPY) { // A copy is undone with a delete, so it doesn't really matter where the file originally came from (we're not moving it back there, but into the recycle bin). You may also notice we don't make use of the target parent record. This is because no undo action needs this information, and even if it did it could simply ask for records parent! Gatherer.c_man.undo.addUndo(job.ID(), UndoManager.FILE_COPY, null, null, job.target, new_record, job.undo); } else { // Movements however do need a source and source parent so the file can be moved back to the correct place. Gatherer.c_man.undo.addUndo(job.ID(), UndoManager.FILE_MOVE, job.source, (FileNode)origin_node.getParent(), job.target, new_record, job.undo); } } new_record = null; } } // Else else if(source_file.isDirectory()) { // create new record FileNode directory_record = new FileNode(target_file); ///ystem.err.println("Directory record = " + directory_record + " (" + target_file.getAbsolutePath() + ")"); SynchronizedTreeModelTools.insertNodeInto(target_model, destination_node, directory_record); // Why is this not happening eh? directory_record.setParent(destination_node); if(!target_file.exists()) { // make the directory target_file.mkdirs(); new_node = directory_record; // create undo job if(job.undoable) { job.undoable = false; if(job.type == FileJob.COPY) { // A copy is undone with a delete, so it doesn't really matter where the file originally came from (we're not moving it back there, but into the recycle bin). You may also notice we don't make use of the target parent record. This is because no undo action needs this information, and even if it did it could simply ask for records parent! Gatherer.c_man.undo.addUndo(job.ID(), UndoManager.FILE_COPY, null, null, job.target, directory_record, job.undo); } else { // Movements however do need a source and source parent so the file can be moved back to the correct place. Gatherer.c_man.undo.addUndo(job.ID(), UndoManager.FILE_MOVE, job.source, (FileNode)origin_node.getParent(), job.target, directory_record, job.undo); } } } // Else inform the users that a directory already exists and files will be copied into it //else { // JOptionPane.showMessageDialog(null, get("Directory_Exists", target_file.toString()), get("General.Warning"), JOptionPane.WARNING_MESSAGE); //} // Queue non-filtered child files for copying. If this directory already existed, the child records will have to generate the undo jobs, as we don't want to entirely delete this directory if it already existed. FileNode child_record = null; // In order to have a sane copy proceedure (rather than always copying last file first as it used to) we always add the child node at the position the parent was removed from. Consider the file job 'a' at the end of the queue which generates three new jobs 'b', 'c' and 'd'. The resulting flow should look like this. // -- Starting queue ...[a] // remove(position) = 'a' ... // add(position, 'b') ...[b] // add(position, 'c') ...[c][b] // add(position, 'd') ...[d][c][b] // Next loop // remove(position) = 'b' ...[d][c] for(int i = 0; i < origin_node.getChildCount(); i++) { child_record = (FileNode) origin_node.getChildAt(i); addJob(job.ID(), job.source, child_record, job.target, directory_record, job.type, job.undo, false, false, position); } child_record = null; directory_record = null; } // The file wasn't found! else { cancel_action = true; // Show warning. JOptionPane.showMessageDialog(Gatherer.g_man, get("File_Not_Found_Message", source_file.getName()), get("File_Not_Found_Title"), JOptionPane.ERROR_MESSAGE); // Force refresh of source folder. source_model.refresh(new TreePath(((FileNode)origin_node.getParent()).getPath())); } // We can't have been cancelled, and we must have created a new FileNode during the above phase, before we can handle metadata. if(!cancel_action && new_node != null) { /* Time to handle any existing metadata. */ // If the directory came from inside our collection... if (job.source.toString().equals("Collection")) { ///ystem.err.println("Move within collection..."); GDMManager gdm = Gatherer.c_man.getCollection().gdm; // we just retrieve the metadata attached to the origin node... ArrayList existing_metadata = gdm.getMetadata(source_file); ///ystem.err.println("Existing metadata for " + origin_node + ": " + gdm.toString(existing_metadata)); // then assign this remainder to the new folder. ///ystem.err.println("New metadata: " + gdm.toString(existing_metadata)); gdm.addMetadata(new_node, existing_metadata); existing_metadata = null; gdm = null; } // If it came from the recycle bin retrieve the metadata from there, once again remembering to account for inherited metadata else if (job.source.toString().equals("Undo")) { GDMManager gdm = Gatherer.c_man.getCollection().gdm; // Retrieve metadata from the recycle bin ArrayList existing_metadata = Gatherer.c_man.undo.getMetadata(source_file); // then assign this remainder to the new folder. gdm.addMetadata(new_node, existing_metadata); existing_metadata = null; gdm = null; } // Otherwise if it came from the workspace use the MSMs parsers to search for folder level metadata (such as metadata.xml or marc records). else if (job.source.toString().equals("Workspace")) { cancel_action = Gatherer.c_man.getCollection().msm.searchForMetadata(new_node, origin_node, job.folder_level); } } new_node = null; } } // If we haven't been cancelled, and we've been asked to delete a directory/file, or perhaps as part of a move, we delete the file. This involves removing any existing metadata and then copying the file to the recycled bin (for a delete only), then deleting the file. When deleting a directory record from the tree (or from the filesystem for that matter) we must ensure that all of the descendant records have already been removed. If we fail to do this the delete will fail, or you will be bombarded with hundreds of 'Parent node of null not allowed' error messages. Also be aware that if the user has cancelled just this action, because of say a name clash, then we shouldn't do any deleting of any sort dammit. if(!cancel_action && ready && (job.type == FileJob.DELETE || job.type == FileJob.MOVE)) { ///ystem.err.print("Delete/Move: " + origin_node + " -> "); // If the source is an empty directory or a file. Don't do anything to the root node of a tree. File[] child_list = source_file.listFiles(); if(source_file.isFile() || (child_list != null && (child_list.length == 0 || (child_list.length == 1 && child_list[0].getName().equals(Utility.METADATA_XML))) && origin_node.getParent() != null)) { ///ystem.err.println("File or empty directory."); // Delete any metadata.xml still in the directory. if(child_list != null && child_list.length == 1) { child_list[0].delete(); } ///ystem.err.println("Origin is file or is directory and is empty."); // update status area String args[] = new String[1]; args[0] = "" + (queue.size() + 1) + ""; //job_status.setText(get("Jobs", args)); args[0] = Utility.formatPath("FileActions.Deleting", source_file.getAbsolutePath(), file_status.getSize().width); file_status.setText(get("Deleting", args)); args = null; //file_status.setToolTipText(Utility.formatHTMLWidth(file_status.getText(), 80)); // Remove its metadata ArrayList metadatum = null; if(job.source == Gatherer.c_man.undo) { Gatherer.c_man.undo.addMetadata(target_file, metadatum); } else { metadatum= Gatherer.c_man.getCollection().gdm.removeMetadata(origin_node.getFile()); } // determine it parent node FileNode parent_record = (FileNode)origin_node.getParent(); // Remove from model SynchronizedTreeModelTools.removeNodeFromParent(source_model, origin_node); // If we are deleting File recycled_file = null; FileNode recycled_parent = null; if(job.type == FileJob.DELETE) { // See if this record already has a previous recycle folder noted in the recycle folder mappings. recycled_parent = (FileNode) recycle_folder_mappings.get(origin_node); if(recycled_parent != null) { recycle_folder_mappings.remove(origin_node); } else { recycled_parent = (FileNode) Gatherer.c_man.undo.getTreeModel().getRoot(); } // This may be a directory which we have already added previously. if(completed_folder_mappings.containsKey(origin_node)) { FileNode recycled_record = (FileNode) completed_folder_mappings.get(origin_node); // Replace the temporary directory record in the undo tree with this one. SynchronizedTreeModelTools.replaceNode(Gatherer.c_man.undo.getTreeModel(), recycled_record, origin_node); origin_node.setFile(recycled_record.getFile()); } else { // copy the file to the recycle bin recycled_file = new File(recycled_parent.getFile(), origin_node.toString()); // If the file already exists, delete it. if(recycled_file.exists()) { recycled_file.delete(); } recycled_file.deleteOnExit(); copyFile(source_file, recycled_file, progress); origin_node.setFile(recycled_file); // Add the node to the appropriate place in the UndoManagers tree model. Unfortunately this one does have the possibility of altering the GUI (if the removeNodeFromParent above hasn't occured yet), so we must put it on the queue. SynchronizedTreeModelTools.insertNodeInto(Gatherer.c_man.undo.getTreeModel(), recycled_parent, origin_node); } Gatherer.c_man.undo.addMetadata(source_file, metadatum); } // delete the source file Utility.delete(source_file); // create undo job as necessary. File move would have been handled above. if(job.undoable) { job.undoable = false; // The target is null, indicating that we moved this file to the recycle bin. The UndoManager will replace null with 'this'. Gatherer.c_man.undo.addUndo(job.ID(), UndoManager.FILE_DELETE, job.source, parent_record, null, origin_node, job.undo); } recycled_parent = null; recycled_file = null; //metadatum = null; } // Else the source is a directory and it has children remaining else if(child_list != null && child_list.length > 0) { ///ystem.err.print("Nonempty directory -> "); ///ystem.err.println("Directory is non-empty. Remove children first."); FileNode recycle_folder_record = null; // Don't worry about all this for true file move actions. if(job.type == FileJob.DELETE) { // See if this record already has a previous recycle folder noted in the recycle folder mappings. FileNode parent = (FileNode) recycle_folder_mappings.get(origin_node); if(parent != null) { recycle_folder_mappings.remove(origin_node); } else { parent = (FileNode) Gatherer.c_man.undo.getTreeModel().getRoot(); } // We must add the folder node to our undo manager tree now, so we'll have something to add children to. We use a copy of the directory file record. File recycle_folder = new File(parent.getFile(), origin_node.toString()); recycle_folder.deleteOnExit(); recycle_folder.mkdirs(); recycle_folder_record = new FileNode(recycle_folder); // Add this node to the undo tree model. SynchronizedTreeModelTools.insertNodeInto(Gatherer.c_man.undo.getTreeModel(), parent, recycle_folder_record); // We add an entry to the complete mappings to ensure this directory isn't added again completed_folder_mappings.put(origin_node, recycle_folder_record); ///ystem.err.println("Added completed directories mapping " + origin_node); // queue all of its children, (both filtered and non-filtered), but for deleting only. Don't queue jobs for a current move event, as they would be queued as part of copying. I have no idea way, per sec, however the children within the origin node are always invalid during deletion (there are several copies of some nodes?!?). I'll check that each child is only added once. ///ystem.err.println("Directory has " + origin_node.getChildCount() + " children."); ///ystem.err.println("Directory actually has " + child_list.length + " children."); origin_node.unmap(); origin_node.map(); ///ystem.err.println("Directory has " + origin_node.getChildCount() + " children."); ///ystem.err.println("Directory actually has " + child_list.length + " children."); for(int i = 0; i < origin_node.getChildCount(); i++) { FileNode child_record = (FileNode) origin_node.getChildAt(i); addJob(job.ID(), job.source, child_record, job.target, destination_node, FileJob.DELETE, job.undo, false, false, position); if(recycle_folder_record != null) { recycle_folder_mappings.put(child_record, recycle_folder_record); } } } // Requeue a delete job -after- the children have been dealt with. Remember I've reversed the direction of the queue so sooner is later. Te-he. Also have to remember that we have have followed this path to get here for a move job: Copy Directory -> Queue Child Files -> Delete Directory (must occur after child files) -> Queue Directory. // One special case. Do not requeue root nodes. Don't requeue jobs marked as done. if(origin_node.getParent() != null && !job.done) { ///ystem.err.println("Requeue"); job.type = FileJob.DELETE; // You only requeue jobs that are deletes, as directories must be inspected before children, but deleted after. addJob(job, position); } else { ///ystem.err.println("I've already done this job twice. I refuse to requeue it again!!!"); } } } job = null; source_file = null; target_file = null; origin_node = null; // We can only break out of the while loop if we are out of files, or if the action was cancelled. if(cancel_action) { // Empty queue clearJobs(); cancel_action = false; } // Debugging pause. ///ystem.err.println("Job complete."); } else { synchronized(this) { ///ystem.err.println("Queue size = " + queue.size()); // Force the trees to refresh (but only if there is something on screen!) if(Gatherer.g_man != null) { Gatherer.g_man.refreshTrees(); } // Reset status area //job_status.setText(get("No_Selection")); file_status.setText(get("No_Activity")); progress.reset(); progress.setString(get("No_Activity")); yes_to_all = false; completed_folder_mappings.clear(); recycle_folder_mappings.clear(); // Now wait if applicable. if(return_immediately) { return; } ///ystem.err.println("Waiting"); wait(); } } } catch (Exception error) { Gatherer.printStackTrace(error); } } } /** A second lock is necessary to prevent the thread waiting, because its notify occured momentarily before it waited. If the flag is set then the thread can't wait, but loops around. This may chew processor time so should only be used if you are certain files are about to be placed on the queue. * @param wait_allowed The new state of the wait_allowed flag, as a boolean. */ public void setWaitAllowed(boolean wait_allowed) { this.wait_allowed = wait_allowed; } /** Restore the progress bar so that it updates normally. * @see org.greenstone.gatherer.gui.LongProgressBar */ synchronized public void unpause() { progress.setIndeterminate(false); if(previous != null) { progress.setString(previous); previous = null; } } /** Called when the user makes some selection in one of the trees we are listening to. From this we update the status details. */ public void valueChanged(TreeSelectionEvent event) { JTree tree = (JTree) event.getSource(); if(tree.getSelectionCount() > 0) { TreePath selection[] = tree.getSelectionPaths(); int file_count = 0; int dir_count = 0; for(int i = 0; i < selection.length; i++) { TreeNode record = (TreeNode) selection[i].getLastPathComponent(); if(record.isLeaf()) { file_count++; } else { dir_count++; } record = null; } selection = null; String args[] = new String[2]; args[0] = "" + file_count + ""; args[1] = "" + dir_count + ""; //job_status.setText(get("Selected", args)); args = null; } tree = null; } synchronized private void clearJobs() { queue.clear(); } /** Copy a file from the source location to the destination location. * @param source The source File. * @param destination The destination File. * @see org.greenstone.gatherer.Gatherer */ public void copyFile(File source, File destination, LongProgressBar progress) throws FileAlreadyExistsException, FileNotFoundException, InsufficientSpaceException, IOException, UnknownFileErrorException { if(source.isDirectory()) { destination.mkdirs(); } else { // Check if the origin file exists. if(!source.exists()) { throw(new FileNotFoundException()); } // Check if the destination file does not exist. if(destination.exists()) { throw(new FileAlreadyExistsException()); } File dirs = destination.getParentFile(); dirs.mkdirs(); // Copy the file. FileInputStream f_in = new FileInputStream(source); FileOutputStream f_out = new FileOutputStream(destination); byte data[] = new byte[Utility.BUFFER_SIZE]; int data_size = 0; while((data_size = f_in.read(data, 0, Utility.BUFFER_SIZE)) != -1 && !cancel_action) { long destination_size = destination.length(); try { f_out.write(data, 0, data_size); } // If an IO exception occurs, we can do some maths to determine if the number of bytes written to the file was less than expected. If so we assume a InsufficientSpace exception. If not we just throw the exception again. catch (IOException io_exception) { if(destination_size + (long) data_size > destination.length()) { // Determine the difference (which I guess is in bytes). long difference = (destination_size + (long) data_size) - destination.length(); // Transform that into a human readable string. String message = Utility.formatFileLength(difference); throw(new InsufficientSpaceException(message)); } else { throw(io_exception); } } if(progress != null) { progress.addValue(data_size); } } // Flush and close the streams to ensure all bytes are written. f_in.close(); f_out.close(); // We have now, in theory, produced an exact copy of the source file. Check this by comparing sizes. if(!cancel_action && source.length() != destination.length()) { throw(new UnknownFileErrorException()); } // If we were cancelled, ensure that none of the destination file exists. if(cancel_action) { destination.delete(); } } } /** Retrieve a phrase from the dictionary based on the key. * @param key The key index as a String. * @return The desired phrase also as a String. */ private String get(String key) { return get(key, (String[])null); } /** Retrieve a phrase from the dictionary based on the key, and taking into account the given argument. * @param key The key index as a String. * @param args A String which is the argument to be added to the phrase. * @return The desired phrase also as a String. */ private String get(String key, String arg) { String args[] = new String[1]; args[0] = arg; return get(key, args); } /** Retrieve a phrase from the dictionary based on the key, and taking into account the given arguments. * @param key The key index as a String. * @param args A String[] of arguments to be added to the phrase. * @return The desired phrase also as a String. * @see org.greenstone.gatherer.Dictionary * @see org.greenstone.gatherer.Gatherer */ private String get(String key, String args[]) { if(key.indexOf('.') == -1) { key = "FileActions." + key; } return Gatherer.dictionary.get(key, args); } private FileJob removeJob(int position) { FileJob job = null; if(queue.size() > 0) { job = (FileJob) queue.remove(position); } return job; } }