Changeset 10245


Ignore:
Timestamp:
2005-07-13T16:18:25+12:00 (19 years ago)
Author:
mdewsnip
Message:

Tidied up FileManager.java in preparation for updating collections on the server immediately when building collections remotely.

Location:
trunk/gli/src/org/greenstone/gatherer/file
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/file/FileManager.java

    r9114 r10245  
    88 * <BR><BR>
    99 *
    10  * Author: John Thompson, Greenstone Digital Library, University of Waikato
    11  *
    12  * <BR><BR>
    13  *
    14  * Copyright (C) 1999 New Zealand Digital Library Project
     10 * Author: John Thompson, NZDL Project, University of Waikato
     11 *
     12 * <BR><BR>
     13 *
     14 * Copyright (C) 2005 New Zealand Digital Library Project
    1515 *
    1616 * <BR><BR>
     
    4848import org.greenstone.gatherer.util.Utility;
    4949
     50
    5051/** Manages the moving of files within a separate thread.
    51  * @author John Thompson, Greenstone Digital Library, University of Waikato
    52  * @version 2.3
     52 * @author John Thompson, NZDL Project, University of Waikato
    5353 */
    54 public class FileManager {
     54public class FileManager
     55{
     56    /** Not only the queue of files to be moved, but also the object that moves them. */
     57    static private FileQueue file_queue = null;
    5558
    5659    public static int FILE_TYPE = 0;
    5760    public static int FOLDER_TYPE = 1;
    5861
    59     static public int countFolderDepth(File file) {
    60     int count = 0;
    61     while(file != null) {
    62         count++;
    63         file = file.getParentFile();
    64     }
    65     return count;
    66     }
    67 
    68     /** Not only the queue of files to be moved, but also the object that moves them. */
    69     private FileQueue queue = null;
     62
    7063    /** Constructor.
    7164     * @see org.greenstone.gatherer.file.FileQueue
    7265     */
    73     public FileManager() {
    74     queue = new FileQueue();
    75     queue.start();
    76     }
    77 
    78     /** Given the arguments, determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
    79     public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node) {
    80     byte type = 0;
    81     // If source and target are the same we are moving
    82     if(source == target) {
    83         type = FileJob.MOVE;
    84     }
    85     // If source and target are different
    86     else {
    87         // If target is the RecycleBin, we're deleting
    88         if (target instanceof RecycleBin) {
    89         // The workspace tree is read only.
    90         // ... except for files from the "Downloaded Files" folder
    91         boolean from_downloaded_files_folder = false;
    92         if (source_nodes != null) {
    93             for (int i = 0; i < source_nodes.length; i++) {
    94             if (source_nodes[i].getFile() != null) {
    95                 if (source_nodes[i].getFile().getAbsolutePath().startsWith(Utility.getCacheDir().getAbsolutePath())) {
    96                 from_downloaded_files_folder = true;
    97                 }
    98             }
     66    public FileManager()
     67    {
     68    file_queue = new FileQueue();
     69    file_queue.start();
     70    }
     71
     72
     73    /** Determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
     74    public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node)
     75    {
     76    long id = System.currentTimeMillis();
     77
     78    // If source and target are the same we're moving
     79    if (source == target) {
     80        // Start a new move FileTask and we're done
     81        (new FileTask(id, source, source_nodes, target, target_node, FileJob.MOVE)).start();
     82        return;
     83    }
     84
     85    // If target isn't the RecycleBin, we're copying
     86    if (!(target instanceof RecycleBin)) {
     87        // Start a new copy FileTask and we're done
     88        (new FileTask(id, source, source_nodes, target, target_node, FileJob.COPY)).start();
     89        return;
     90    }
     91
     92    // We're deleting... but first make sure source isn't read-only
     93    boolean read_only_source = false;
     94
     95    // The workspace tree is read-only...
     96    if (source.toString().equals("Workspace")) {
     97        read_only_source = true;
     98
     99        // ...except for files from the "Downloaded Files" folder
     100        String downloaded_files_folder_path = Utility.getCacheDir().getAbsolutePath();
     101        for (int i = 0; i < source_nodes.length; i++) {
     102        // Is this the "Downloaded Files" folder?
     103        if (source_nodes[i].getFile().getAbsolutePath().startsWith(downloaded_files_folder_path)) {
     104            // Can only delete from the "Downloaded Files" folder if a collection is open
     105            if (Gatherer.c_man.ready() == false) {
     106            return;
    99107            }
    100         }
    101 
    102         // Can only delete from the "Downloaded Files" folder if a collection is open
    103         if (from_downloaded_files_folder && Gatherer.c_man.ready() == false) {
    104             return;
    105         }
    106 
    107         if (source.toString().equals("Workspace") && !from_downloaded_files_folder) {
    108             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    109             return;
    110         }
    111         // Normal delete. Go ahead.
    112         else {
    113             type = FileJob.DELETE;
    114         }
    115         }
    116         // Otherwise we are copying
    117         else {
    118         type = FileJob.COPY;
    119         }
    120     }
    121     Task task = new Task(System.currentTimeMillis(), source, source_nodes, target, target_node, type);
    122     task.start();
    123     }
     108            read_only_source = false;
     109        }
     110        }
     111    }
     112
     113    // The source is read-only, so tell the user and abort
     114    if (read_only_source) {
     115        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     116        return;
     117    }
     118
     119    // Start a new delete FileTask and we're done
     120    (new FileTask(id, source, source_nodes, target, target_node, FileJob.DELETE)).start();
     121    }
     122
    124123
    125124    /** Retrieves the file queue object. */
    126     public FileQueue getQueue() {
    127     return queue;
    128     }
     125    public FileQueue getQueue()
     126    {
     127    return file_queue;
     128    }
     129
     130
     131    private class FileTask
     132    extends Thread
     133    {
     134    private long id;
     135    private DragComponent source;
     136    private FileNode[] source_nodes;
     137    private DragComponent target;
     138    private FileNode target_node;
     139    private byte type;
     140
     141
     142    public FileTask(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type)
     143    {
     144        this.id = id;
     145        this.source = source;
     146        this.source_nodes = source_nodes;
     147        this.target = target;
     148        this.target_node = target_node;
     149        this.type = type;
     150    }
     151
     152
     153    public void run()
     154    {
     155        // Check there is something to do
     156        if (source_nodes == null) {
     157        return;
     158        }
     159
     160        // Reset the progress bar and set it to indeterminate while calculating its size
     161        LongProgressBar progress_bar = file_queue.getProgressBar();
     162        progress_bar.reset();
     163        progress_bar.setIndeterminate(true);
     164
     165        // Calculate the progress bar size
     166        boolean cancelled = file_queue.calculateSize(source_nodes);
     167        if (!cancelled) {
     168        // Queue the job(s) (this may fail if we are asked to delete a read only file)
     169        for (int i = 0; i < source_nodes.length; i++) {
     170            file_queue.addJob(id, source, source_nodes[i], target, target_node, type, true);
     171        }
     172        }
     173
     174        progress_bar.setIndeterminate(false);
     175        progress_bar.clear();
     176    }
     177    }
     178
    129179
    130180    public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
     
    132182    }
    133183
     184
    134185    public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
    135186    newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
    136187    }
     188
     189
    137190    protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
    138191    // Ask the user for the directories name.
     
    184237    name = null;
    185238    }
    186 
    187     private class Task
    188     extends Thread {
    189     private byte type;
    190     private DragComponent source;
    191     private DragComponent target;
    192     private FileNode target_node;
    193     private FileNode[] source_nodes;
    194     private long id;
    195 
    196     public Task(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type) {
    197         this.id = id;
    198         this.source = source;
    199         this.source_nodes = source_nodes;
    200         this.target = target;
    201         this.target_node = target_node;
    202         this.type = type;
    203     }
    204 
    205     public void run()
    206     {
    207         // Check there is something to do
    208         if (source_nodes == null) {
    209         return;
    210         }
    211 
    212         // Reset the progress bar and set it to indeterminate while calculating its size
    213         LongProgressBar progress_bar = queue.getProgressBar();
    214         progress_bar.reset();
    215         progress_bar.setIndeterminate(true);
    216 
    217         // Calculate the progress bar size
    218         boolean cancelled = queue.calculateSize(source_nodes);
    219         if (!cancelled) {
    220         // Queue the job(s) (this may fail if we are asked to delete a read only file)
    221         for (int i = 0; i < source_nodes.length; i++) {
    222             queue.addJob(id, source, source_nodes[i], target, target_node, type, true);
    223         }
    224         }
    225 
    226         progress_bar.setIndeterminate(false);
    227         progress_bar.clear();
    228     }
    229     }
    230239}
  • trunk/gli/src/org/greenstone/gatherer/file/FileQueue.java

    r10007 r10245  
    158158
    159159
     160    private int countFolderDepth(File file)
     161    {
     162    int depth = 0;
     163    while (file != null) {
     164        depth++;
     165        file = file.getParentFile();
     166    }
     167    return depth;
     168    }
     169
     170
    160171    /** Format the given filename path string so that it is no longer than the given width. If it is wider replace starting directories with ...
    161172     * @param key The key <strong>String</Strong> used to retrieve a phrase from the dictionary for this item.
     
    241252            int max_folder_depth = Configuration.getInt("general.max_folder_depth", Configuration.COLLECTION_SPECIFIC);
    242253            boolean continue_over_depth = false;
    243             if(FileManager.countFolderDepth(source_file) > max_folder_depth) {
     254            if (countFolderDepth(source_file) > max_folder_depth) {
    244255                Object[] options = { Dictionary.get("General.Yes"), Dictionary.get("General.No"), Dictionary.get("FileActions.Increase_Depth") };
    245256                String args[] = { String.valueOf(max_folder_depth), source_file.getAbsolutePath() };
Note: See TracChangeset for help on using the changeset viewer.