/** *######################################################################### * * 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 Project, NZDL, University of Waikato * * Copyright (C) 2003 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. *######################################################################## */ package org.greenstone.gatherer.gui; import java.awt.*; import java.awt.event.*; import java.io.File; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.collection.BasicCollectionConfiguration; import org.greenstone.gatherer.remote.RemoteGreenstoneServer; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; public class NewCollectionDetailsPrompt extends ModalDialog { static public boolean titleClashes(String title, File current_config_file) { // 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. if(title == null || title.length() == 0) { return false; } File collect_directory = new File(Gatherer.getCollectDirectoryPath()); String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE; File children[] = collect_directory.listFiles(); for(int i = 0; children != null && i < children.length; i++) { if(children[i].isDirectory()) { File config_file = new File(children[i], file_name); if(current_config_file == null || !config_file.equals(current_config_file)) { BasicCollectionConfiguration other_collection = new BasicCollectionConfiguration(config_file); if(other_collection.getName().equalsIgnoreCase(title)) { return true; } other_collection = null; } config_file = null; } } return false; } private boolean cancelled; private File base_final; private JButton create_button; private JComboBox base_collection; private JDialog self; private JRadioButton personal_collection_button = null; private JTextArea description; private JTextField title; private String description_final; private String title_final=""; static private Dimension COMPONENT_SIZE = new Dimension(230, 25); /** The size of this new collection dialog box. */ static private Dimension SIZE = new Dimension(600, 280); static private int FILENAME_SIZE = 8; /** Constructor. * @see org.greenstone.gatherer.util.Utility */ public NewCollectionDetailsPrompt() { super(Gatherer.g_man, true); this.cancelled = true; this.setComponentOrientation(Dictionary.getOrientation()); this.self = this; // Setup setJMenuBar(new SimpleMenuBar("creatingacollection")); setSize(SIZE); setTitle(Dictionary.get("NewCollectionPrompt.Title")); // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories. Vector base_collection_model = new Vector(); // need to modify this to base a coll on any collection from any site if (Gatherer.GS3 && !Gatherer.isGsdlRemote) { File sites_dir = new File(Gatherer.getSitesDirectoryPath()); File [] sites = sites_dir.listFiles(); for (int i=0; i 8) { new_name_buffer.replace(new_name_buffer.length() - suffix.length(), new_name_buffer.length(), suffix); } // Or just append it if that isn't necessary else { new_name_buffer.append(suffix); } } // All done return new_name_buffer.toString(); } private boolean filenameClashes(String filename) { File collect_directory = new File(Gatherer.getCollectDirectoryPath()); File children[] = collect_directory.listFiles(); for(int i = 0; children != null && i < children.length; i++) { if(children[i].getName().equals(filename)) { return true; } } return false; } public String getTitle() { return title_final; } private void addCollectionsToModel(Vector base_collection_model, File collect_directory, String site) { File[] possible_collections = collect_directory.listFiles(); String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE; for (int i = 0; possible_collections != null && i < possible_collections.length; i++) { // If the directory has a etc/collect.cfg file then it looks like a collection File collect_cfg_file = new File(possible_collections[i], file_name); if (collect_cfg_file.exists()) { // Check for group coll: Check the collect.cfg for collectgroup=true BasicCollectionConfiguration config = new BasicCollectionConfiguration(collect_cfg_file); if (config.getCollectGroup().equals("true")) { // try subdirs in here addCollectionsToModel(base_collection_model, possible_collections[i], site); } else { // If the directory has a metadata/ then the collection can be used as a base File metadata_directory = new File(possible_collections[i], "metadata"); if (metadata_directory.exists()) { // Add to model BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file); if (Gatherer.GS3 && site != null) { collect_cfg.setSite(site); } Item item = new Item(possible_collections[i], collect_cfg); if (!base_collection_model.contains(item)) { base_collection_model.add(item); } } } } } } private class CancelListener implements ActionListener { public void actionPerformed(ActionEvent event) { cancelled = true; self.dispose(); } } private class CreateListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Validate. title_final = title.getText(); if(title_final.length() == 0) { JOptionPane jOptionPane=new JOptionPane(); jOptionPane.setComponentOrientation(Dictionary.getOrientation()); jOptionPane.setOpaque(!Utility.isMac()); jOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE); return; } // 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 else { if(titleClashes(title_final, null)) { JOptionPane jOptionPane=new JOptionPane(); jOptionPane.setComponentOrientation(Dictionary.getOrientation()); jOptionPane.setOpaque(!Utility.isMac()); if (jOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Clash"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; } } } description_final = description.getText(); // If we got this far there are no errors. Item item_final = (Item) base_collection.getSelectedItem(); base_final = item_final.getFile(); cancelled = false; self.dispose(); } } private class DescriptionListener extends KeyAdapter { public void keyPressed(KeyEvent event) { if(event.getKeyCode() == KeyEvent.VK_TAB) { event.consume(); base_collection.grabFocus(); } } } private class Item implements Comparable { private BasicCollectionConfiguration config; private File file; private String name; public Item(File file, BasicCollectionConfiguration config) { this.config = config; this.file = file; this.name = null; } public Item(File file, String name) { this.config = null; this.file = file; this.name = name; } public int compareTo(Object other) { return toString().toLowerCase().compareTo(other.toString().toLowerCase()); } public boolean equals(Object other) { return compareTo(other) == 0; } public File getFile() { return file; } public String toString() { if(name == null && config != null) { name = config.toString(); } return name; } } }