[5164] | 1 | /**
|
---|
| 2 | *#########################################################################
|
---|
| 3 | *
|
---|
| 4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
| 5 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 6 | * University of Waikato, New Zealand.
|
---|
| 7 | *
|
---|
| 8 | * <BR><BR>
|
---|
| 9 | *
|
---|
| 10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 11 | *
|
---|
| 12 | * <BR><BR>
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 15 | *
|
---|
| 16 | * <BR><BR>
|
---|
| 17 | *
|
---|
| 18 | * This program is free software; you can redistribute it and/or modify
|
---|
| 19 | * it under the terms of the GNU General Public License as published by
|
---|
| 20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 21 | * (at your option) any later version.
|
---|
| 22 | *
|
---|
| 23 | * <BR><BR>
|
---|
| 24 | *
|
---|
| 25 | * This program is distributed in the hope that it will be useful,
|
---|
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 28 | * GNU General Public License for more details.
|
---|
| 29 | *
|
---|
| 30 | * <BR><BR>
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with this program; if not, write to the Free Software
|
---|
| 34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 35 | *########################################################################
|
---|
| 36 | */
|
---|
[5536] | 37 | package org.greenstone.gatherer.gui;
|
---|
| 38 |
|
---|
[5164] | 39 | import java.awt.*;
|
---|
| 40 | import java.awt.event.*;
|
---|
| 41 | import java.beans.*;
|
---|
| 42 | import java.io.File;
|
---|
| 43 | import java.io.IOException;
|
---|
| 44 | import java.text.*;
|
---|
| 45 | import java.util.*;
|
---|
| 46 | import javax.swing.*;
|
---|
| 47 | import javax.swing.filechooser.*;
|
---|
| 48 | import org.greenstone.gatherer.Dictionary;
|
---|
[6051] | 49 | import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
|
---|
[5164] | 50 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
[10011] | 51 | import org.greenstone.gatherer.util.JarTools;
|
---|
[6051] | 52 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
[5164] | 53 | import org.greenstone.gatherer.util.Utility;
|
---|
| 54 |
|
---|
[6051] | 55 | public class OpenCollectionDialog
|
---|
[5164] | 56 | extends JFileChooser {
|
---|
[10011] | 57 | static final private ImageIcon LOCKED_COLLECTION_ICON = JarTools.getImage("lcolicn.gif");
|
---|
| 58 | static final private ImageIcon NORMAL_COLLECTION_ICON = JarTools.getImage("ncolicn.gif");
|
---|
[5536] | 59 |
|
---|
[5164] | 60 | /** Constructor */
|
---|
| 61 | public OpenCollectionDialog(File file) {
|
---|
| 62 | super(file);
|
---|
| 63 | ///ystem.err.println("Size = " + getSize());
|
---|
| 64 | // Other initialization
|
---|
| 65 | setAcceptAllFileFilterUsed(false);
|
---|
[5593] | 66 | setDialogTitle(Dictionary.get("OpenCollectionDialog.Title"));
|
---|
[5164] | 67 | setFileFilter(new GathererFilter());
|
---|
| 68 | setFileSystemView(new GathererFileSystemView());
|
---|
| 69 | setFileView(new GathererFileView());
|
---|
| 70 | setSize(400,300);
|
---|
| 71 | // Stop the annoying renaming
|
---|
[12065] | 72 | GUIUtils.disableRename(this);
|
---|
[5164] | 73 | // Description accessory
|
---|
| 74 | DescriptionPreview accessory = new DescriptionPreview(this);
|
---|
| 75 | setAccessory(accessory);
|
---|
| 76 | // Move accessory
|
---|
| 77 | JPanel accessory_pane = (JPanel) accessory.getParent();
|
---|
| 78 | JPanel center_pane = (JPanel) accessory_pane.getParent();
|
---|
| 79 | center_pane.add(accessory_pane, BorderLayout.SOUTH);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[6051] | 82 | public void destroy() {
|
---|
[5302] | 83 | }
|
---|
| 84 |
|
---|
[5164] | 85 | public String getFileName() {
|
---|
| 86 | // Continually loop until the use chooses an appropriate file or cancels.
|
---|
| 87 | while(true) {
|
---|
| 88 | int return_val = showOpenDialog(null);
|
---|
| 89 | if(return_val == JFileChooser.APPROVE_OPTION) {
|
---|
| 90 | File file = getSelectedFile();
|
---|
| 91 | if(file != null && file.getName().endsWith(".col")) {
|
---|
| 92 | return file.getAbsolutePath();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | else {
|
---|
| 96 | return null;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | /* The DescriptionPreview accessory is adapted from the ImagePreview.java (an example used by FileChooserDemo2.java). */
|
---|
[6051] | 104 | private class DescriptionPreview
|
---|
[5164] | 105 | extends JPanel
|
---|
| 106 | implements PropertyChangeListener {
|
---|
| 107 | private File file = null;
|
---|
| 108 | private JTextArea text;
|
---|
[5536] | 109 |
|
---|
[5164] | 110 | public DescriptionPreview(JFileChooser fc) {
|
---|
| 111 | setName("Description Preview");
|
---|
| 112 | setPreferredSize(new Dimension(100, 75)); // About three rows.
|
---|
| 113 |
|
---|
[12119] | 114 | text = new JTextArea(Dictionary.get("OpenCollectionDialog.No_Description"));
|
---|
| 115 |
|
---|
[5164] | 116 | setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
| 117 | setLayout(new BorderLayout());
|
---|
| 118 | add(new JScrollPane(text), BorderLayout.CENTER);
|
---|
| 119 |
|
---|
| 120 | fc.addPropertyChangeListener(this);
|
---|
| 121 | }
|
---|
[5536] | 122 |
|
---|
[5164] | 123 | /** Whenever a new file or folder is selected we determine what description should be displayed. */
|
---|
| 124 | public void propertyChange(PropertyChangeEvent e) {
|
---|
| 125 | boolean update = false;
|
---|
| 126 | String prop = e.getPropertyName();
|
---|
[5536] | 127 | // If the directory changed, don't show an image.
|
---|
[5164] | 128 | if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
|
---|
| 129 | file = null;
|
---|
[6051] | 130 | update = true;
|
---|
[5536] | 131 | }
|
---|
| 132 | // If a file became selected, find out which one.
|
---|
| 133 | else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
|
---|
[5164] | 134 | file = (File) e.getNewValue();
|
---|
| 135 | update = true;
|
---|
| 136 | }
|
---|
[5536] | 137 |
|
---|
| 138 | // Update the description accordingly.
|
---|
[5164] | 139 | if (update) {
|
---|
| 140 | if(file == null) {
|
---|
[12119] | 141 | text.setText(Dictionary.get("OpenCollectionDialog.No_Description"));
|
---|
[5164] | 142 | }
|
---|
| 143 | else {
|
---|
| 144 | // Build a wrapper around the collection configuration file.
|
---|
[7275] | 145 | File config_file = new File(file.getParentFile(), Utility.CONFIG_FILE);
|
---|
[5164] | 146 | if(config_file.exists()) {
|
---|
[6051] | 147 | BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(config_file);
|
---|
| 148 | text.setText(StaticStrings.SPEECH_CHARACTER + collect_cfg.getName() + StaticStrings.SPEECH_CHARACTER + StaticStrings.NEW_LINE_CHAR);
|
---|
[5164] | 149 | text.setText(collect_cfg.getDescription());
|
---|
| 150 | text.setCaretPosition(0);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[5536] | 154 | }
|
---|
[5164] | 155 | }
|
---|
| 156 |
|
---|
| 157 | /** ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */
|
---|
[6051] | 158 | private class GathererFilter
|
---|
[5164] | 159 | extends FileFilter {
|
---|
[5536] | 160 |
|
---|
| 161 | // Accept all directories and all .col files
|
---|
[5164] | 162 | public boolean accept(File f) {
|
---|
| 163 | return (f.isDirectory() || f.getName().toLowerCase().endsWith(".col"));
|
---|
| 164 | }
|
---|
[5536] | 165 |
|
---|
| 166 | // The description of this filter
|
---|
[5164] | 167 | public String getDescription() {
|
---|
[5593] | 168 | return Dictionary.get("OpenCollectionDialog.Collection");
|
---|
[5164] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | private class GathererFileSystemView
|
---|
| 173 | extends FileSystemView {
|
---|
[6051] | 174 |
|
---|
[5164] | 175 | private FileSystemView default_system_view = FileSystemView.getFileSystemView();
|
---|
[6051] | 176 |
|
---|
[5164] | 177 | /** Creates a new folder with a default folder name. */
|
---|
[6051] | 178 | public File createNewFolder(File containingDir)
|
---|
[5164] | 179 | throws IOException {
|
---|
| 180 | return default_system_view.createNewFolder(containingDir);
|
---|
[6051] | 181 | }
|
---|
[5164] | 182 |
|
---|
| 183 | /** Gets the list of shown (i.e. */
|
---|
| 184 | public File[] getFiles(File dir, boolean useFileHiding) {
|
---|
[5536] | 185 | // Retrieve the files usually returned by the platform specific file system view.
|
---|
[5164] | 186 | File[] files = default_system_view.getFiles(dir, useFileHiding);
|
---|
[5536] | 187 | // Determine if the current dir actually contains a valid greenstone collection. To do this we check for the presence of the file <dir>/etc/collect.cfg and a directory named import.
|
---|
[7275] | 188 | File collect_cfg_file = new File(dir, Utility.CONFIG_FILE);
|
---|
[11625] | 189 | File import_dir_file = new File(dir, "import");
|
---|
[5164] | 190 | if(collect_cfg_file.exists() && import_dir_file.exists()) {
|
---|
| 191 | // Create a new dummy collection file.
|
---|
| 192 | String name = dir.getName();
|
---|
| 193 | File collection_file = new File(dir, name + ".col");
|
---|
| 194 | // If this file doesn't already exist.
|
---|
| 195 | if(!collection_file.exists()) {
|
---|
| 196 | File[] temp = new File[files.length + 1];
|
---|
| 197 | System.arraycopy(files, 0, temp, 0, files.length);
|
---|
| 198 | temp[files.length] = collection_file;
|
---|
| 199 | files = temp;
|
---|
| 200 | temp = null;
|
---|
| 201 | }
|
---|
| 202 | collection_file = null;
|
---|
| 203 | name = null;
|
---|
| 204 | }
|
---|
| 205 | import_dir_file = null;
|
---|
| 206 | collect_cfg_file = null;
|
---|
| 207 | return files;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** A FileView which returns differing icons depending on whether a lock file is present within the collection folder. */
|
---|
| 212 | private class GathererFileView
|
---|
| 213 | extends FileView {
|
---|
[5536] | 214 |
|
---|
[5164] | 215 | public String getDescription(File file) {
|
---|
| 216 | String description = null;
|
---|
[5536] | 217 | if (file.getName().endsWith(".col")) {
|
---|
| 218 | if (!lockExists(file)) {
|
---|
[5593] | 219 | description = Dictionary.get("OpenCollectionDialog.Normal_Collection");
|
---|
[5164] | 220 | }
|
---|
| 221 | else {
|
---|
[5593] | 222 | description = Dictionary.get("OpenCollectionDialog.Locked_Collection");
|
---|
[5164] | 223 | }
|
---|
| 224 | }
|
---|
| 225 | return description;
|
---|
| 226 | }
|
---|
[5536] | 227 |
|
---|
[5164] | 228 | public Icon getIcon(File file) {
|
---|
| 229 | Icon icon = null;
|
---|
[5536] | 230 | if (file.getName().endsWith(".col")) {
|
---|
| 231 | if (!lockExists(file)) {
|
---|
[5164] | 232 | icon = NORMAL_COLLECTION_ICON;
|
---|
| 233 | }
|
---|
| 234 | else {
|
---|
| 235 | icon = LOCKED_COLLECTION_ICON;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | return icon;
|
---|
| 239 | }
|
---|
[5536] | 240 |
|
---|
[5164] | 241 | /** Determine if a lock file exists. */
|
---|
| 242 | private boolean lockExists(File file) {
|
---|
| 243 | File lock_file = new File(file.getParentFile(), CollectionManager.LOCK_FILE);
|
---|
| 244 | return lock_file.exists();
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | }
|
---|