/**
*#########################################################################
*
* 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