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.Gatherer;
|
---|
50 | import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
|
---|
51 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
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 = Utility.getImage("lcolicn.gif");
|
---|
58 | static final private ImageIcon NORMAL_COLLECTION_ICON = Utility.getImage("ncolicn.gif");
|
---|
59 | /** The name of the mouse listener that initiates editing on a double click. */
|
---|
60 | static final private String SINGLE_CLICK_LISTENER = "SingleClickListener";
|
---|
61 |
|
---|
62 | /** Constructor */
|
---|
63 | public OpenCollectionDialog(File file) {
|
---|
64 | super(file);
|
---|
65 | ///ystem.err.println("Size = " + getSize());
|
---|
66 | // Other initialization
|
---|
67 | setAcceptAllFileFilterUsed(false);
|
---|
68 | setDialogTitle(Dictionary.get("OpenCollectionDialog.Title"));
|
---|
69 | setFileFilter(new GathererFilter());
|
---|
70 | setFileSystemView(new GathererFileSystemView());
|
---|
71 | setFileView(new GathererFileView());
|
---|
72 | setSize(400,300);
|
---|
73 | // Stop the annoying renaming
|
---|
74 | disableRename(this);
|
---|
75 | // Description accessory
|
---|
76 | DescriptionPreview accessory = new DescriptionPreview(this);
|
---|
77 | setAccessory(accessory);
|
---|
78 | // Move accessory
|
---|
79 | JPanel accessory_pane = (JPanel) accessory.getParent();
|
---|
80 | JPanel center_pane = (JPanel) accessory_pane.getParent();
|
---|
81 | center_pane.add(accessory_pane, BorderLayout.SOUTH);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void destroy() {
|
---|
85 | }
|
---|
86 |
|
---|
87 | public String getFileName() {
|
---|
88 | // Continually loop until the use chooses an appropriate file or cancels.
|
---|
89 | while(true) {
|
---|
90 | int return_val = showOpenDialog(null);
|
---|
91 | if(return_val == JFileChooser.APPROVE_OPTION) {
|
---|
92 | File file = getSelectedFile();
|
---|
93 | if(file != null && file.getName().endsWith(".col")) {
|
---|
94 | return file.getAbsolutePath();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | else {
|
---|
98 | return null;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Neat method to disable file renaming in filechooser.
|
---|
104 | * Thanks to: vladi21 from www.experts-exchange.com
|
---|
105 | */
|
---|
106 | static public void disableRename(Component c) {
|
---|
107 | if (c instanceof JList){
|
---|
108 | EventListener[] listeners=c.getListeners(MouseListener.class);
|
---|
109 | for(int i=0; listeners != null && i < listeners.length; i++) {
|
---|
110 | if (listeners[i].toString().indexOf(SINGLE_CLICK_LISTENER) != -1) {
|
---|
111 | c.removeMouseListener((MouseListener)listeners[i]);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return;
|
---|
115 | }
|
---|
116 | if (c instanceof Container) {
|
---|
117 | Component[] children = null;
|
---|
118 | children = ((Container)c).getComponents();
|
---|
119 | if (children != null) {
|
---|
120 | for(int i = 0; children != null && i < children.length; i++) {
|
---|
121 | disableRename(children[i]);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /* The DescriptionPreview accessory is adapted from the ImagePreview.java (an example used by FileChooserDemo2.java). */
|
---|
129 | private class DescriptionPreview
|
---|
130 | extends JPanel
|
---|
131 | implements PropertyChangeListener {
|
---|
132 | private File file = null;
|
---|
133 | private JTextArea text;
|
---|
134 |
|
---|
135 | public DescriptionPreview(JFileChooser fc) {
|
---|
136 | setName("Description Preview");
|
---|
137 | setPreferredSize(new Dimension(100, 75)); // About three rows.
|
---|
138 |
|
---|
139 | text = new JTextArea();
|
---|
140 | Dictionary.setText(text, "OpenCollectionDialog.No_Description");
|
---|
141 |
|
---|
142 | setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
143 | setLayout(new BorderLayout());
|
---|
144 | add(new JScrollPane(text), BorderLayout.CENTER);
|
---|
145 |
|
---|
146 | fc.addPropertyChangeListener(this);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Whenever a new file or folder is selected we determine what description should be displayed. */
|
---|
150 | public void propertyChange(PropertyChangeEvent e) {
|
---|
151 | boolean update = false;
|
---|
152 | String prop = e.getPropertyName();
|
---|
153 | // If the directory changed, don't show an image.
|
---|
154 | if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
|
---|
155 | file = null;
|
---|
156 | update = true;
|
---|
157 | }
|
---|
158 | // If a file became selected, find out which one.
|
---|
159 | else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
|
---|
160 | file = (File) e.getNewValue();
|
---|
161 | update = true;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // Update the description accordingly.
|
---|
165 | if (update) {
|
---|
166 | if(file == null) {
|
---|
167 | Dictionary.setText(text, "OpenCollectionDialog.No_Description");
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | // Build a wrapper around the collection configuration file.
|
---|
171 | File config_file = new File(file.getParentFile(), Utility.CONFIG_DIR);
|
---|
172 | if(config_file.exists()) {
|
---|
173 | BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(config_file);
|
---|
174 | text.setText(StaticStrings.SPEECH_CHARACTER + collect_cfg.getName() + StaticStrings.SPEECH_CHARACTER + StaticStrings.NEW_LINE_CHAR);
|
---|
175 | text.setText(collect_cfg.getDescription());
|
---|
176 | text.setCaretPosition(0);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */
|
---|
184 | private class GathererFilter
|
---|
185 | extends FileFilter {
|
---|
186 |
|
---|
187 | // Accept all directories and all .col files
|
---|
188 | public boolean accept(File f) {
|
---|
189 | return (f.isDirectory() || f.getName().toLowerCase().endsWith(".col"));
|
---|
190 | }
|
---|
191 |
|
---|
192 | // The description of this filter
|
---|
193 | public String getDescription() {
|
---|
194 | return Dictionary.get("OpenCollectionDialog.Collection");
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | private class GathererFileSystemView
|
---|
199 | extends FileSystemView {
|
---|
200 |
|
---|
201 | private FileSystemView default_system_view = FileSystemView.getFileSystemView();
|
---|
202 |
|
---|
203 | /** Creates a new folder with a default folder name. */
|
---|
204 | public File createNewFolder(File containingDir)
|
---|
205 | throws IOException {
|
---|
206 | return default_system_view.createNewFolder(containingDir);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Gets the list of shown (i.e. */
|
---|
210 | public File[] getFiles(File dir, boolean useFileHiding) {
|
---|
211 | // Retrieve the files usually returned by the platform specific file system view.
|
---|
212 | File[] files = default_system_view.getFiles(dir, useFileHiding);
|
---|
213 | // 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.
|
---|
214 | File collect_cfg_file = new File(dir, Utility.CONFIG_DIR);
|
---|
215 | File import_dir_file = new File(dir, Utility.IMPORT_DIR);
|
---|
216 | if(collect_cfg_file.exists() && import_dir_file.exists()) {
|
---|
217 | // Create a new dummy collection file.
|
---|
218 | String name = dir.getName();
|
---|
219 | File collection_file = new File(dir, name + ".col");
|
---|
220 | // If this file doesn't already exist.
|
---|
221 | if(!collection_file.exists()) {
|
---|
222 | File[] temp = new File[files.length + 1];
|
---|
223 | System.arraycopy(files, 0, temp, 0, files.length);
|
---|
224 | temp[files.length] = collection_file;
|
---|
225 | files = temp;
|
---|
226 | temp = null;
|
---|
227 | }
|
---|
228 | collection_file = null;
|
---|
229 | name = null;
|
---|
230 | }
|
---|
231 | import_dir_file = null;
|
---|
232 | collect_cfg_file = null;
|
---|
233 | return files;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** A FileView which returns differing icons depending on whether a lock file is present within the collection folder. */
|
---|
238 | private class GathererFileView
|
---|
239 | extends FileView {
|
---|
240 |
|
---|
241 | public String getDescription(File file) {
|
---|
242 | String description = null;
|
---|
243 | if (file.getName().endsWith(".col")) {
|
---|
244 | if (!lockExists(file)) {
|
---|
245 | description = Dictionary.get("OpenCollectionDialog.Normal_Collection");
|
---|
246 | }
|
---|
247 | else {
|
---|
248 | description = Dictionary.get("OpenCollectionDialog.Locked_Collection");
|
---|
249 | }
|
---|
250 | }
|
---|
251 | return description;
|
---|
252 | }
|
---|
253 |
|
---|
254 | public Icon getIcon(File file) {
|
---|
255 | Icon icon = null;
|
---|
256 | if (file.getName().endsWith(".col")) {
|
---|
257 | if (!lockExists(file)) {
|
---|
258 | icon = NORMAL_COLLECTION_ICON;
|
---|
259 | }
|
---|
260 | else {
|
---|
261 | icon = LOCKED_COLLECTION_ICON;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | return icon;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /** Determine if a lock file exists. */
|
---|
268 | private boolean lockExists(File file) {
|
---|
269 | File lock_file = new File(file.getParentFile(), CollectionManager.LOCK_FILE);
|
---|
270 | return lock_file.exists();
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|