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 | */
|
---|
37 | package org.greenstone.gatherer.gui;
|
---|
38 |
|
---|
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;
|
---|
49 | import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
|
---|
50 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
51 | import org.greenstone.gatherer.util.JarTools;
|
---|
52 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
53 | import org.greenstone.gatherer.util.Utility;
|
---|
54 |
|
---|
55 | public class OpenCollectionDialog
|
---|
56 | extends JFileChooser {
|
---|
57 | static final private ImageIcon LOCKED_COLLECTION_ICON = JarTools.getImage("lcolicn.gif");
|
---|
58 | static final private ImageIcon NORMAL_COLLECTION_ICON = JarTools.getImage("ncolicn.gif");
|
---|
59 |
|
---|
60 | /** Constructor */
|
---|
61 | public OpenCollectionDialog(File file) {
|
---|
62 | super(file);
|
---|
63 | ///ystem.err.println("Size = " + getSize());
|
---|
64 | // Other initialization
|
---|
65 | setAcceptAllFileFilterUsed(false);
|
---|
66 | setDialogTitle(Dictionary.get("OpenCollectionDialog.Title"));
|
---|
67 | setFileFilter(new GathererFilter());
|
---|
68 | setFileSystemView(new GathererFileSystemView());
|
---|
69 | setFileView(new GathererFileView());
|
---|
70 | setSize(400,300);
|
---|
71 | // Stop the annoying renaming
|
---|
72 | GUIUtils.disableRename(this);
|
---|
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 |
|
---|
82 | public void destroy() {
|
---|
83 | }
|
---|
84 |
|
---|
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). */
|
---|
104 | private class DescriptionPreview
|
---|
105 | extends JPanel
|
---|
106 | implements PropertyChangeListener {
|
---|
107 | private File file = null;
|
---|
108 | private JTextArea text;
|
---|
109 |
|
---|
110 | public DescriptionPreview(JFileChooser fc) {
|
---|
111 | setName("Description Preview");
|
---|
112 | setPreferredSize(new Dimension(100, 75)); // About three rows.
|
---|
113 |
|
---|
114 | text = new JTextArea(Dictionary.get("OpenCollectionDialog.No_Description"));
|
---|
115 |
|
---|
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 | }
|
---|
122 |
|
---|
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();
|
---|
127 | // If the directory changed, don't show an image.
|
---|
128 | if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
|
---|
129 | file = null;
|
---|
130 | update = true;
|
---|
131 | }
|
---|
132 | // If a file became selected, find out which one.
|
---|
133 | else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
|
---|
134 | file = (File) e.getNewValue();
|
---|
135 | update = true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Update the description accordingly.
|
---|
139 | if (update) {
|
---|
140 | if(file == null) {
|
---|
141 | text.setText(Dictionary.get("OpenCollectionDialog.No_Description"));
|
---|
142 | }
|
---|
143 | else {
|
---|
144 | // Build a wrapper around the collection configuration file.
|
---|
145 | File config_file = new File(file.getParentFile(), Utility.CONFIG_FILE);
|
---|
146 | if(config_file.exists()) {
|
---|
147 | BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(config_file);
|
---|
148 | text.setText(StaticStrings.SPEECH_CHARACTER + collect_cfg.getName() + StaticStrings.SPEECH_CHARACTER + StaticStrings.NEW_LINE_CHAR);
|
---|
149 | text.setText(collect_cfg.getDescription());
|
---|
150 | text.setCaretPosition(0);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */
|
---|
158 | private class GathererFilter
|
---|
159 | extends FileFilter {
|
---|
160 |
|
---|
161 | // Accept all directories and all .col files
|
---|
162 | public boolean accept(File f) {
|
---|
163 | return (f.isDirectory() || f.getName().toLowerCase().endsWith(".col"));
|
---|
164 | }
|
---|
165 |
|
---|
166 | // The description of this filter
|
---|
167 | public String getDescription() {
|
---|
168 | return Dictionary.get("OpenCollectionDialog.Collection");
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | private class GathererFileSystemView
|
---|
173 | extends FileSystemView {
|
---|
174 |
|
---|
175 | private FileSystemView default_system_view = FileSystemView.getFileSystemView();
|
---|
176 |
|
---|
177 | /** Creates a new folder with a default folder name. */
|
---|
178 | public File createNewFolder(File containingDir)
|
---|
179 | throws IOException {
|
---|
180 | return default_system_view.createNewFolder(containingDir);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Gets the list of shown (i.e. */
|
---|
184 | public File[] getFiles(File dir, boolean useFileHiding) {
|
---|
185 | // Retrieve the files usually returned by the platform specific file system view.
|
---|
186 | File[] files = default_system_view.getFiles(dir, useFileHiding);
|
---|
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.
|
---|
188 | File collect_cfg_file = new File(dir, Utility.CONFIG_FILE);
|
---|
189 | File import_dir_file = new File(dir, "import");
|
---|
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 {
|
---|
214 |
|
---|
215 | public String getDescription(File file) {
|
---|
216 | String description = null;
|
---|
217 | if (file.getName().endsWith(".col")) {
|
---|
218 | if (!lockExists(file)) {
|
---|
219 | description = Dictionary.get("OpenCollectionDialog.Normal_Collection");
|
---|
220 | }
|
---|
221 | else {
|
---|
222 | description = Dictionary.get("OpenCollectionDialog.Locked_Collection");
|
---|
223 | }
|
---|
224 | }
|
---|
225 | return description;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public Icon getIcon(File file) {
|
---|
229 | Icon icon = null;
|
---|
230 | if (file.getName().endsWith(".col")) {
|
---|
231 | if (!lockExists(file)) {
|
---|
232 | icon = NORMAL_COLLECTION_ICON;
|
---|
233 | }
|
---|
234 | else {
|
---|
235 | icon = LOCKED_COLLECTION_ICON;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | return icon;
|
---|
239 | }
|
---|
240 |
|
---|
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 | }
|
---|