source: trunk/gli/src/org/greenstone/gatherer/gui/OpenCollectionDialog.java@ 8257

Last change on this file since 8257 was 8240, checked in by mdewsnip, 20 years ago

Removed unnecessary imports of org.greenstone.gatherer.Gatherer.

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