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