/** *######################################################################### * * 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. *######################################################################## */ package org.greenstone.gatherer.gui; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.File; import java.io.IOException; import java.text.*; import java.util.*; import javax.swing.*; import javax.swing.filechooser.*; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.collection.BasicCollectionConfiguration; import org.greenstone.gatherer.collection.CollectionManager; import org.greenstone.gatherer.util.JarTools; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; public class OpenCollectionDialog extends JFileChooser { static final private ImageIcon LOCKED_COLLECTION_ICON = JarTools.getImage("lcolicn.gif"); static final private ImageIcon NORMAL_COLLECTION_ICON = JarTools.getImage("ncolicn.gif"); /** Constructor */ public OpenCollectionDialog(File file) { super(file); ///ystem.err.println("Size = " + getSize()); // Other initialization setAcceptAllFileFilterUsed(false); setDialogTitle(Dictionary.get("OpenCollectionDialog.Title")); setFileFilter(new GathererFilter()); setFileSystemView(new GathererFileSystemView()); setFileView(new GathererFileView()); setSize(400,300); // Stop the annoying renaming GUIUtils.disableRename(this); // Description accessory DescriptionPreview accessory = new DescriptionPreview(this); setAccessory(accessory); // Move accessory JPanel accessory_pane = (JPanel) accessory.getParent(); JPanel center_pane = (JPanel) accessory_pane.getParent(); center_pane.add(accessory_pane, BorderLayout.SOUTH); } public void destroy() { } public String getFileName() { // Continually loop until the use chooses an appropriate file or cancels. while(true) { int return_val = showOpenDialog(null); if(return_val == JFileChooser.APPROVE_OPTION) { File file = getSelectedFile(); if(file != null && file.getName().endsWith(".col")) { return file.getAbsolutePath(); } } else { return null; } } } /* The DescriptionPreview accessory is adapted from the ImagePreview.java (an example used by FileChooserDemo2.java). */ private class DescriptionPreview extends JPanel implements PropertyChangeListener { private File file = null; private JTextArea text; public DescriptionPreview(JFileChooser fc) { setName("Description Preview"); setPreferredSize(new Dimension(100, 75)); // About three rows. text = new JTextArea(Dictionary.get("OpenCollectionDialog.No_Description")); setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); setLayout(new BorderLayout()); add(new JScrollPane(text), BorderLayout.CENTER); fc.addPropertyChangeListener(this); } /** Whenever a new file or folder is selected we determine what description should be displayed. */ public void propertyChange(PropertyChangeEvent e) { boolean update = false; String prop = e.getPropertyName(); // If the directory changed, don't show an image. if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) { file = null; update = true; } // If a file became selected, find out which one. else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) { file = (File) e.getNewValue(); update = true; } // Update the description accordingly. if (update) { if(file == null) { text.setText(Dictionary.get("OpenCollectionDialog.No_Description")); } else { // Build a wrapper around the collection configuration file. File config_file = new File(file.getParentFile(), Utility.CONFIG_FILE); if(config_file.exists()) { BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(config_file); text.setText(StaticStrings.SPEECH_CHARACTER + collect_cfg.getName() + StaticStrings.SPEECH_CHARACTER + StaticStrings.NEW_LINE_CHAR); text.setText(collect_cfg.getDescription()); text.setCaretPosition(0); } } } } } /** ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */ private class GathererFilter extends FileFilter { // Accept all directories and all .col files public boolean accept(File f) { return (f.isDirectory() || f.getName().toLowerCase().endsWith(".col")); } // The description of this filter public String getDescription() { return Dictionary.get("OpenCollectionDialog.Collection"); } } private class GathererFileSystemView extends FileSystemView { private FileSystemView default_system_view = FileSystemView.getFileSystemView(); /** Creates a new folder with a default folder name. */ public File createNewFolder(File containingDir) throws IOException { return default_system_view.createNewFolder(containingDir); } /** Gets the list of shown (i.e. */ public File[] getFiles(File dir, boolean useFileHiding) { // Retrieve the files usually returned by the platform specific file system view. File[] files = default_system_view.getFiles(dir, useFileHiding); // Determine if the current dir actually contains a valid greenstone collection. To do this we check for the presence of the file /etc/collect.cfg and a directory named import. File collect_cfg_file = new File(dir, Utility.CONFIG_FILE); File import_dir_file = new File(dir, "import"); if(collect_cfg_file.exists() && import_dir_file.exists()) { // Create a new dummy collection file. String name = dir.getName(); File collection_file = new File(dir, name + ".col"); // If this file doesn't already exist. if(!collection_file.exists()) { File[] temp = new File[files.length + 1]; System.arraycopy(files, 0, temp, 0, files.length); temp[files.length] = collection_file; files = temp; temp = null; } collection_file = null; name = null; } import_dir_file = null; collect_cfg_file = null; return files; } } /** A FileView which returns differing icons depending on whether a lock file is present within the collection folder. */ private class GathererFileView extends FileView { public String getDescription(File file) { String description = null; if (file.getName().endsWith(".col")) { if (!lockExists(file)) { description = Dictionary.get("OpenCollectionDialog.Normal_Collection"); } else { description = Dictionary.get("OpenCollectionDialog.Locked_Collection"); } } return description; } public Icon getIcon(File file) { Icon icon = null; if (file.getName().endsWith(".col")) { if (!lockExists(file)) { icon = NORMAL_COLLECTION_ICON; } else { icon = LOCKED_COLLECTION_ICON; } } return icon; } /** Determine if a lock file exists. */ private boolean lockExists(File file) { File lock_file = new File(file.getParentFile(), CollectionManager.LOCK_FILE); return lock_file.exists(); } } }