Ignore:
Timestamp:
2010-08-11T21:28:34+12:00 (14 years ago)
Author:
ak19
Message:

Ticket #152: Allowing different paths to collect dir so that GLI can work with collect dirs on pen drives. NONE OF THESE CHANGES ARE FOR Client-GLI AS YET. 1. Preferences (Connection tab), Open and New Collection dialogs allow one to change the current collect directory containing the collections to select from. 2. New Collection dialog allows one to base a new collection on an existing collection in a collect dir other than the current collect dir. 3. Collections in the Documents in Greenstone Collections Workspace Tree Node now have two additional rightclick options: to move and copy these collections to another location.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java

    r18947 r22605  
    3434import javax.swing.event.*;
    3535import javax.swing.text.*;
     36import javax.swing.filechooser.FileFilter;
     37import javax.swing.filechooser.FileView;
    3638import org.greenstone.gatherer.Configuration;
    3739import org.greenstone.gatherer.Dictionary;
     
    3941import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
    4042import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
     43import org.greenstone.gatherer.util.JarTools;
    4144import org.greenstone.gatherer.util.StaticStrings;
    4245import org.greenstone.gatherer.util.Utility;
     
    4548    extends ModalDialog {
    4649
    47      
    48    
    49     static public boolean titleClashes(String title, File current_config_file) {
     50   
     51    static public boolean titleClashes(String collectDir, String title, File current_config_file) {
    5052    // An empty collection title never clashes with anything. It may look ugly in the final collection but there is nothing wrong with having no title for a particular language.
    5153    if(title == null || title.length() == 0) {
    5254        return false;
    5355    }
    54     File collect_directory = new File(Gatherer.getCollectDirectoryPath());
     56    File collect_directory = new File(collectDir);
    5557    String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
    5658    File children[] = collect_directory.listFiles();
     
    7274
    7375    private boolean cancelled;
     76    private boolean collectDirChanged;
    7477    private File base_final;
     78    private JButton chdir_button;
    7579    private JButton create_button;
    7680    private JComboBox base_collection;
     
    7983    private JTextArea description;
    8084    private JTextField title;
     85    private String collectBasePath;
     86    private String newCollectPath;
    8187    private String description_final;
    8288    private String title_final="";
     89
    8390    static private Dimension COMPONENT_SIZE = new Dimension(230, 25);
    8491    /** The size of this new collection dialog box. */
     
    94101        this.setComponentOrientation(Dictionary.getOrientation());
    95102    this.self = this;
     103    newCollectPath = Gatherer.getCollectDirectoryPath();
     104    collectBasePath = null;
     105    collectDirChanged = false;
     106
    96107    // Setup
    97108    setJMenuBar(new SimpleMenuBar("creatingacollection"));
     
    101112    // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories.
    102113    Vector base_collection_model = new Vector();
    103     // need to modify this to base a coll on any collection from any site
    104     if (Gatherer.GS3 && !Gatherer.isGsdlRemote) {
    105         File sites_dir = new File(Gatherer.getSitesDirectoryPath());
    106         File [] sites = sites_dir.listFiles();
    107         for (int i=0; i<sites.length; i++) {
    108         File collect_directory = new File(sites_dir + File.separator + sites[i].getName() + File.separator + "collect");
    109         if (collect_directory.exists()) {
    110             addCollectionsToModel(base_collection_model, collect_directory, sites[i].getName());
    111         }
    112         }
    113     } else {
    114        File collect_directory = new File(Gatherer.getCollectDirectoryPath());
    115        addCollectionsToModel(base_collection_model, collect_directory, null);
    116     }
    117 
    118     // Sort the result.
    119     Collections.sort(base_collection_model);
    120     base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection")));
     114    setupBaseCollections(base_collection_model, null); // if no collect directory passed in: use default collect directory
     115
    121116
    122117    // Creation
     
    190185        button_pane.setComponentOrientation(Dictionary.getOrientation());
    191186    create_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
     187    chdir_button = new GLIButton(Dictionary.get("General.CD"), Dictionary.get("General.CD_Tooltip"));
    192188    JButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
    193189
    194190    // Connection
     191    chdir_button.addActionListener(new ChangeDirListener());
     192    if(Gatherer.isGsdlRemote) { // for client GLI disable changing directories
     193        chdir_button.setEnabled(false);     
     194    } else { // can't base collections on any in OTHER collect dirs in client GLI
     195        base_collection.addActionListener(new OtherCollectionsListener());
     196    }
    195197    cancel_button.addActionListener(new CancelListener());
    196198    create_button.addActionListener(new CreateListener());
     
    238240
    239241    button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
    240     button_pane.setLayout(new GridLayout(1,2));
     242    button_pane.setLayout(new GridLayout(1,3));
     243    button_pane.add(chdir_button);
    241244    button_pane.add(create_button);
    242245    button_pane.add(cancel_button);
     
    327330    public String getTitle() {
    328331    return title_final;
     332    }
     333
     334    private void setupBaseCollections(Vector base_collection_model, String collectDirectory) {
     335
     336    File collect_directory = null;
     337    if(collectDirectory != null) {
     338        collect_directory = new File(collectDirectory);
     339    }
     340
     341    // need to modify this to base a coll on any collection from any site
     342    if (Gatherer.GS3 && !Gatherer.isGsdlRemote) {
     343        File sites_dir = new File(Gatherer.getSitesDirectoryPath());
     344        File [] sites = sites_dir.listFiles();
     345        for (int i=0; i<sites.length; i++) {       
     346        if(collect_directory == null) {
     347            collect_directory = new File(sites_dir + File.separator + sites[i].getName() + File.separator + "collect");
     348        }
     349        if (collect_directory.exists()) {
     350            addCollectionsToModel(base_collection_model, collect_directory, sites[i].getName());
     351        }
     352        }
     353    } else {
     354        if(collect_directory == null) { // if no collect directory passed in: use default collect directory
     355        collect_directory = new File(Gatherer.getCollectDirectoryPath());
     356        }
     357       addCollectionsToModel(base_collection_model, collect_directory, null);
     358    }
     359
     360    // Sort the result.
     361    Collections.sort(base_collection_model);
     362    base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection")));
     363   
     364    // last item allows one to open up collections from other collect folders to base the new one on
     365    if(!Gatherer.isGsdlRemote) { // Not yet implemented for client GLI
     366        base_collection_model.add(new Item(null, Dictionary.get("NewCollectionPrompt.OtherCollections")));
     367    }
    329368    }
    330369
     
    363402    }
    364403
     404    private class OtherCollectionsListener implements ActionListener {
     405
     406    // the JComboBox listener will receive an ActionEvent
     407    // when a selection has been made
     408
     409    public void actionPerformed(ActionEvent event) {
     410
     411        // If the Other Collections item was selected (which is the last one in the list)
     412        if(base_collection.getSelectedIndex() == base_collection.getItemCount()-1) {
     413
     414        // open a filechooser in whatever is the current collect directory
     415        // within the NewCollectionDetailsPrompt's settings
     416        if(collectBasePath == null) {
     417            collectBasePath = newCollectPath;
     418        }
     419
     420        JFileChooser chooser = new JFileChooser(collectBasePath);
     421        chooser.setFileView(new CollectionFileView());
     422        chooser.setDialogTitle(Dictionary.get("NewCollectionPrompt.ChooseACollection"));
     423        chooser.setApproveButtonText(Dictionary.get("NewCollectionPrompt.Select"));
     424       
     425        // to prevent the user from traversing into collection folders,
     426        // need to:
     427        // - NOT set the filechooser to Directories only, but files and directories
     428        // (so that they can still select directories)
     429        // - add a filefilter that accepts only directories (i.e. rejects files)
     430        // - remove the accept all filter
     431        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
     432        //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     433        chooser.setFileFilter(new CollectionFileFilter());
     434        chooser.setAcceptAllFileFilterUsed(false);
     435
     436        int returnVal = chooser.showOpenDialog(NewCollectionDetailsPrompt.this);
     437        if(returnVal == JFileChooser.APPROVE_OPTION) {
     438
     439            File selectedFile = chooser.getSelectedFile();
     440           
     441            // don't bother reloading if nothing has changed
     442            if(collectBasePath.equals(selectedFile.getAbsolutePath() + File.separator)) {
     443            // don't want Other Collections... selected
     444            base_collection.setSelectedIndex(0);
     445            } else {
     446
     447            // If the selected directory is a collection folder get its collect dir,
     448            // make collectBasePath the collect dir, and select the collection itself
     449            String cfg = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
     450            boolean isCollection = false;
     451            while(new File(selectedFile, cfg).exists()) { // while loop handles collectgroup
     452                selectedFile = selectedFile.getParentFile();
     453                isCollection = true;
     454            } // else may be a collect dir, in which case leave it as it is again
     455
     456            // found collect directory for base collections
     457            collectBasePath = selectedFile.getAbsolutePath()+File.separator;
     458
     459            // change the available collections to base this collection on
     460            base_collection.removeAllItems();
     461            Vector base_collection_model = new Vector();
     462            setupBaseCollections(base_collection_model, collectBasePath);
     463            for(int i = 0; i < base_collection_model.size(); i++) {
     464                Item item = (Item)base_collection_model.get(i);
     465                base_collection.addItem(item);
     466
     467                // select the originally chosen collection, if any, in the combobox
     468                if(isCollection && chooser.getSelectedFile().equals(item.getFile())) {
     469                base_collection.setSelectedIndex(i);
     470                }
     471            }
     472           
     473            // But if the user selected a collect group, set combobox to New Collection
     474            if(!isCollection) {
     475                // if it was a collectgroup, nothing new would be selected, set to New Coll             
     476                if(base_collection.getSelectedIndex() == base_collection.getItemCount()-1) {
     477                base_collection.setSelectedIndex(0);
     478                }
     479            }
     480
     481            }
     482
     483        } else { // cancelled out of dialog, so jump to New Collection
     484            base_collection.setSelectedIndex(0);
     485        }       
     486        }
     487    }
     488    }
     489
     490    private class ChangeDirListener implements ActionListener {
     491    public void actionPerformed(ActionEvent event) {
     492        String oldCollectPath = newCollectPath;
     493        JFileChooser chooser = new JFileChooser(newCollectPath);
     494        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     495        chooser.setDialogTitle(Dictionary.get("General.ChooseCollectDirectory"));
     496
     497        int returnVal = chooser.showOpenDialog(NewCollectionDetailsPrompt.this);
     498        if(returnVal == JFileChooser.APPROVE_OPTION) {
     499        newCollectPath = chooser.getSelectedFile().getAbsolutePath() + File.separator;
     500
     501        // Must NOT change the available collections to base this collection on here.
     502        // It will change any collection the user previously chose to base the newcoll on.
     503
     504        // will be used to test for duplicate collection names in current collect dir
     505        collectDirChanged = newCollectPath.equals(oldCollectPath) ? false : true;
     506        }
     507    }
     508    }
     509
    365510    private class CancelListener
    366511    implements ActionListener {
     
    387532        // We must ensure that the collection title is unique. This is a pain in the nether regions as we are forced to load the collect.cfg of each other collection in turn looking for a conflicting title
    388533        else {
    389         if(titleClashes(title_final, null)) {
     534        String collectDir = collectDirChanged ? newCollectPath : Gatherer.getCollectDirectoryPath();
     535        if(titleClashes(collectDir, title_final, null)) {
    390536            JOptionPane jOptionPane=new JOptionPane();
    391537                    jOptionPane.setComponentOrientation(Dictionary.getOrientation());
     
    395541            }
    396542        }
     543        }
     544
     545        // going through with collection creation
     546        if(collectDirChanged) {     
     547        Gatherer.collectDirectoryHasChanged(Gatherer.getCollectDirectoryPath(), newCollectPath);
     548        // will tell the server that the collect directory has changed and that
     549        // the workspace needs to be refreshed (Documents in Greenstone Collections)
    397550        }
    398551
     
    435588    }
    436589    public int compareTo(Object other) {
    437         return toString().toLowerCase().compareTo(other.toString().toLowerCase());
     590        if(this == null && other == null) {
     591        return 0;
     592        }
     593        else if(other == null) {
     594        return 1;
     595        }
     596        else if(this == null) {
     597        return -1;
     598        }
     599        else {
     600        return toString().toLowerCase().compareTo(other.toString().toLowerCase());
     601        }
    438602    }
    439603    public boolean equals(Object other) {
     
    450614    }
    451615    }
     616
     617    /** FileFilter for the Filechooser to choose the collection to base the new one on:
     618     * Only displays directories. */
     619    private class CollectionFileFilter extends FileFilter {
     620    public boolean accept(File f) {
     621        return f.isDirectory();
     622    }
     623    public String getDescription() {
     624        return "greenstone collection view"; //return "directories only";
     625    }
     626    }
     627
     628    /** Folders that can be used as a base collection have a special icon */
     629    private class CollectionFileView extends FileView {
     630    final String configfile_name;   
     631    final ImageIcon COLLECTION_ICON = JarTools.getImage("gsCollection.gif", true);
     632   
     633    private File previous = null;
     634    private boolean previousWasCollection = false;
     635
     636    public CollectionFileView() {
     637        configfile_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
     638    }
     639
     640    // A human readable description of the file.
     641    public String getDescription(File f) {
     642        if(!f.equals(previous)) {
     643        previous = f;
     644        // no idea if it's a collection or not, calculate this next time
     645        previousWasCollection = false;
     646        }
     647        return null; // FilewView superclass will handle it
     648    }
     649       
     650    // The icon that represents this file in the JFileChooser.
     651    public Icon getIcon(File f) {
     652       
     653        if(f.equals(previous) && previousWasCollection) {
     654        return COLLECTION_ICON;
     655        }
     656       
     657        if(isCollectionBase(f)) {
     658        return COLLECTION_ICON;
     659        }       
     660       
     661        return null; // FileView superclass will do the default
     662    }
     663       
     664    // The name of the file.
     665    public String getName(File f) {
     666        if(!f.equals(previous)) {
     667        previous = f;
     668        // no idea if it's a collection or not, calculate this next time
     669        previousWasCollection = false;
     670        }
     671        return f.getName();
     672    }
     673
     674    // A human readable description of the type of the file.
     675    public String getTypeDescription(File f) {
     676        if(f.equals(previous) && previousWasCollection) {
     677        return "collection";
     678        }
     679        if(isCollectionBase(f)) {
     680        return "collection";
     681        }
     682        // else
     683        return "not a collection";
     684    }
     685
     686    // Whether the directory is traversable or not.
     687    public Boolean isTraversable(File f) {
     688        if(f.equals(previous) && previousWasCollection) {
     689        return Boolean.FALSE;
     690        }       
     691       
     692        if(isCollectionBase(f)) {
     693        return Boolean.FALSE;
     694        }
     695
     696        if(f.isFile()) { // not traversable
     697        return Boolean.FALSE;
     698        }
     699        // any other kind of directory is traversable
     700        return Boolean.TRUE;       
     701    }
     702
     703   
     704    /** returns false for collect groups and collect dirs. */
     705    private boolean isCollectionBase(File f) {     
     706        previous = f;
     707        previousWasCollection = false;
     708
     709        if(!f.isFile()) {
     710        // If the directory has a etc/collect.cfg file then it looks like a collection
     711        File collect_cfg_file = new File(f, configfile_name);
     712        if (collect_cfg_file.exists()) {
     713            // Check for group coll: Check the collect.cfg for collectgroup=true
     714            BasicCollectionConfiguration config = new BasicCollectionConfiguration(collect_cfg_file);
     715            if (!config.getCollectGroup().equals("true")) {
     716            previousWasCollection = true;
     717            }       
     718        }
     719        }
     720        return previousWasCollection;
     721    }
     722
     723    }
    452724}
    453725
Note: See TracChangeset for help on using the changeset viewer.