source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 27.4 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 * Author: John Thompson, Greenstone Project, NZDL, University of Waikato
9 *
10 * Copyright (C) 2003 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gui;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.File;
32import java.util.*;
33import javax.swing.*;
34import javax.swing.event.*;
35import javax.swing.text.*;
36import javax.swing.filechooser.FileFilter;
37import javax.swing.filechooser.FileView;
38import org.greenstone.gatherer.Configuration;
39import org.greenstone.gatherer.Dictionary;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
42import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
43import org.greenstone.gatherer.util.JarTools;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.Utility;
46
47public class NewCollectionDetailsPrompt
48 extends ModalDialog {
49
50
51 static public boolean titleClashes(String collectDir, String title, File current_config_file) {
52 // An empty collection title never clashes with anything. It may look ugly in the final collection but there is nothing wrong with having no title for a particular language.
53 if(title == null || title.length() == 0) {
54 return false;
55 }
56 File collect_directory = new File(collectDir);
57 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
58 File children[] = collect_directory.listFiles();
59 for(int i = 0; children != null && i < children.length; i++) {
60 if(children[i].isDirectory()) {
61 File config_file = new File(children[i], file_name);
62 if(current_config_file == null || !config_file.equals(current_config_file)) {
63 BasicCollectionConfiguration other_collection = new BasicCollectionConfiguration(config_file);
64 if(other_collection.getName().equalsIgnoreCase(title)) {
65 return true;
66 }
67 other_collection = null;
68 }
69 config_file = null;
70 }
71 }
72 return false;
73 }
74
75 private boolean cancelled;
76 private boolean collectDirChanged;
77 private File base_final;
78 private JButton chdir_button;
79 private JButton create_button;
80 private JComboBox base_collection;
81 private JDialog self;
82 private JRadioButton personal_collection_button = null;
83 private JTextArea description;
84 private JTextField title;
85 private String collectBasePath;
86 private String newCollectPath;
87 private String description_final;
88 private String title_final="";
89
90 static private Dimension COMPONENT_SIZE = new Dimension(230, 25);
91 /** The size of this new collection dialog box. */
92 static private Dimension SIZE = new Dimension(600, 280);
93 static private int FILENAME_SIZE = 8;
94
95 /** Constructor.
96 * @see org.greenstone.gatherer.util.Utility
97 */
98 public NewCollectionDetailsPrompt() {
99 super(Gatherer.g_man, true);
100 this.cancelled = true;
101 this.setComponentOrientation(Dictionary.getOrientation());
102 this.self = this;
103 newCollectPath = Gatherer.getCollectDirectoryPath();
104 collectBasePath = null;
105 collectDirChanged = false;
106
107 // Setup
108 setJMenuBar(new SimpleMenuBar("creatingacollection"));
109 setSize(SIZE);
110 setTitle(Dictionary.get("NewCollectionPrompt.Title"));
111
112 // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories.
113 Vector base_collection_model = new Vector();
114 setupBaseCollections(base_collection_model, null); // if no collect directory passed in: use default collect directory
115
116
117 // Creation
118 JPanel content_pane = (JPanel) getContentPane();
119 content_pane.setComponentOrientation(Dictionary.getOrientation());
120 content_pane.setOpaque(true);
121 JPanel upper_pane = new JPanel();
122 upper_pane.setComponentOrientation(Dictionary.getOrientation());
123 JLabel instructions_label = new JLabel(Dictionary.get("NewCollectionPrompt.Instructions"));
124 instructions_label.setComponentOrientation(Dictionary.getOrientation());
125
126 JPanel title_pane = new JPanel();
127 title_pane.setComponentOrientation(Dictionary.getOrientation());
128 JLabel title_label = new JLabel(Dictionary.get("CDM.General.Collection_Name"));
129 title_label.setComponentOrientation(Dictionary.getOrientation());
130 title = new JTextField();
131 title.setComponentOrientation(Dictionary.getOrientation());
132 title.setPreferredSize(COMPONENT_SIZE);
133 title.setToolTipText(Dictionary.get("CDM.General.Collection_Name_Tooltip"));
134 JLabel name_label = new JLabel(Dictionary.get("NewCollectionPrompt.Collection_Name"));
135 name_label.setComponentOrientation(Dictionary.getOrientation());
136
137 JPanel center_pane = new JPanel();
138 center_pane.setComponentOrientation(Dictionary.getOrientation());
139 JPanel description_pane = new JPanel();
140 description_pane.setComponentOrientation(Dictionary.getOrientation());
141 JLabel description_label = new JLabel(Dictionary.get("NewCollectionPrompt.Collection_Description"));
142 description_label.setComponentOrientation(Dictionary.getOrientation());
143 description = new JTextArea();
144 description.setComponentOrientation(Dictionary.getOrientation());
145 description.setBackground(Configuration.getColor("coloring.editable_background", false));
146 description.setForeground(Configuration.getColor("coloring.editable_foreground", false));
147 description.setRows(5);
148 description.setToolTipText(Dictionary.get("CDM.General.Collection_Extra_Tooltip"));
149
150 JPanel bottom_pane = new JPanel();
151 bottom_pane.setComponentOrientation(Dictionary.getOrientation());
152 // Base Collection
153 JPanel base_collection_pane = new JPanel();
154 base_collection_pane.setComponentOrientation(Dictionary.getOrientation());
155
156 JLabel base_collection_label = new JLabel(Dictionary.get("NewCollectionPrompt.Base_Collection"));
157 base_collection_label.setComponentOrientation(Dictionary.getOrientation());
158
159 base_collection = new JComboBox(base_collection_model);
160 base_collection.setComponentOrientation(Dictionary.getOrientation());
161 base_collection.setOpaque(false);
162 base_collection.setToolTipText(Dictionary.get("NewCollectionPrompt.Base_Collection_Tooltip"));
163
164 JPanel collection_scope_pane = new JPanel();
165 collection_scope_pane.setComponentOrientation(Dictionary.getOrientation());
166
167 personal_collection_button = new JRadioButton(Dictionary.get("NewCollectionPrompt.Collection_Scope_Personal"));
168 personal_collection_button.setToolTipText(Dictionary.get("NewCollectionPrompt.Collection_Scope_Personal_Tooltip"));
169 personal_collection_button.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
170 personal_collection_button.setOpaque(false);
171 personal_collection_button.setComponentOrientation(Dictionary.getOrientation());
172
173 JRadioButton shared_collection_button = new JRadioButton(Dictionary.get("NewCollectionPrompt.Collection_Scope_Shared"));
174 shared_collection_button.setToolTipText(Dictionary.get("NewCollectionPrompt.Collection_Scope_Shared_Tooltip"));
175 shared_collection_button.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
176 shared_collection_button.setOpaque(false);
177 shared_collection_button.setComponentOrientation(Dictionary.getOrientation());
178
179 ButtonGroup collection_scope_group = new ButtonGroup();
180 collection_scope_group.add(personal_collection_button);
181 collection_scope_group.add(shared_collection_button);
182 personal_collection_button.setSelected(true);
183
184 JPanel button_pane = new JPanel();
185 button_pane.setComponentOrientation(Dictionary.getOrientation());
186 create_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
187 chdir_button = new GLIButton(Dictionary.get("General.CD"), Dictionary.get("General.CD_Tooltip"));
188 JButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
189
190 // Connection
191 chdir_button.addActionListener(new ChangeDirListener());
192 if(Gatherer.isGsdlRemote) { // for client GLI disable changing directories
193 chdir_button.setEnabled(false);
194 } else { // can't base collections on any in OTHER collect dirs in client GLI
195 base_collection.addActionListener(new OtherCollectionsListener());
196 }
197 cancel_button.addActionListener(new CancelListener());
198 create_button.addActionListener(new CreateListener());
199 description.addKeyListener(new DescriptionListener());
200
201 // Layout
202 title_pane.setLayout(new BorderLayout(5,0));
203 title_pane.add(title_label, BorderLayout.LINE_START);
204 title_pane.add(title, BorderLayout.CENTER);
205
206 upper_pane.setLayout(new GridLayout(2,1));
207 upper_pane.add(instructions_label);
208 upper_pane.add(title_pane);
209
210 JScrollPane scrol_tmp;
211
212 description_pane.setLayout(new BorderLayout());
213 description_pane.add(description_label, BorderLayout.NORTH);
214 scrol_tmp=new JScrollPane(description);
215 scrol_tmp.setComponentOrientation(Dictionary.getOrientation());
216 description_pane.add(scrol_tmp, BorderLayout.CENTER);
217
218 base_collection_pane.setLayout(new BorderLayout(5,0));
219 base_collection_pane.add(base_collection_label, BorderLayout.LINE_START);
220 base_collection_pane.add(base_collection, BorderLayout.CENTER);
221
222 collection_scope_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
223 collection_scope_pane.setLayout(new GridLayout(1,2));
224 collection_scope_pane.add(personal_collection_button);
225 collection_scope_pane.add(shared_collection_button);
226
227 center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
228 center_pane.setLayout(new BorderLayout());
229 center_pane.add(description_pane, BorderLayout.CENTER);
230
231 bottom_pane.setLayout(new BorderLayout());
232 bottom_pane.add(base_collection_pane, BorderLayout.NORTH);
233 if (Gatherer.isGsdlRemote) {
234 bottom_pane.add(collection_scope_pane, BorderLayout.CENTER);
235 bottom_pane.add(button_pane, BorderLayout.SOUTH);
236 }
237 else {
238 bottom_pane.add(button_pane, BorderLayout.CENTER);
239 }
240
241 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
242 button_pane.setLayout(new GridLayout(1,3));
243 button_pane.add(create_button);
244 button_pane.add(chdir_button);
245 button_pane.add(cancel_button);
246
247 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
248 content_pane.setLayout(new BorderLayout());
249 content_pane.add(upper_pane, BorderLayout.NORTH);
250 content_pane.add(center_pane, BorderLayout.CENTER);
251 content_pane.add(bottom_pane, BorderLayout.SOUTH);
252 // Final dialog setup & positioning.
253 getRootPane().setDefaultButton(create_button);
254 Dimension screen_size = Configuration.screen_size;
255 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
256 setVisible(true);
257 }
258
259 public boolean isCancelled() {
260 return cancelled;
261 }
262
263 public File getBase() {
264 return base_final;
265 }
266
267 public String getDescription() {
268 return description_final;
269 }
270
271 /** Generates the collection short filename by taking the first eight characters of the title (spaces removed) and then adjusting by further truncating and then adding an unique suffix as necessary.
272 * @return the filename as a String
273 */
274 public String getName()
275 {
276 // Retrieve the first 8 non-whitespace ASCII characters of title_final.
277 StringBuffer name_buffer = new StringBuffer("");
278 for (int i = 0, u = 0; (i < title_final.length() && u < 8); i++) {
279 // To be safe we make sure the internal collection name (folder name) contains only ASCII characters
280 char c = title_final.charAt(i);
281 if ((int) c < 128 && Character.isLetterOrDigit(c)) {
282 name_buffer.append(Character.toLowerCase(c));
283 u++;
284 }
285 }
286
287 // Use "col" as the base collection name if we have nothing left
288 if (name_buffer.length() == 0) {
289 name_buffer = new StringBuffer("col");
290 }
291
292 // Remote collections that aren't shared have the user's username prefixed to the collection name
293 if (Gatherer.isGsdlRemote && personal_collection_button.isSelected()) {
294 name_buffer = new StringBuffer(Gatherer.remoteGreenstoneServer.getUsername() + "-" + name_buffer.toString());
295 }
296
297 // We need to ensure the filename is unique
298 int counter = 0;
299 StringBuffer new_name_buffer = new StringBuffer(name_buffer.toString());
300 while(filenameClashes(new_name_buffer.toString())) {
301 new_name_buffer = new StringBuffer(name_buffer.toString());
302 counter++;
303 String suffix = String.valueOf(counter);
304 // If we have to truncate the namestring so as to fit the suffix
305 if(suffix.length() + new_name_buffer.length() > 8) {
306 new_name_buffer.replace(new_name_buffer.length() - suffix.length(), new_name_buffer.length(), suffix);
307 }
308 // Or just append it if that isn't necessary
309 else {
310 new_name_buffer.append(suffix);
311 }
312 }
313
314 // All done
315 return new_name_buffer.toString();
316 }
317
318 private boolean filenameClashes(String filename)
319 {
320 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
321 File children[] = collect_directory.listFiles();
322 for(int i = 0; children != null && i < children.length; i++) {
323 if(children[i].getName().equals(filename)) {
324 return true;
325 }
326 }
327 return false;
328 }
329
330 public String getTitle() {
331 return title_final;
332 }
333
334 private void setupBaseCollections(Vector base_collection_model, String collectDirectory) {
335
336 File collect_directory = null;
337 if(collectDirectory != null) {
338 collect_directory = new File(collectDirectory);
339 }
340
341 // need to modify this to base a coll on any collection from any site
342 if (Gatherer.GS3 && !Gatherer.isGsdlRemote) {
343 File sites_dir = new File(Gatherer.getSitesDirectoryPath());
344 File [] sites = sites_dir.listFiles();
345 for (int i=0; i<sites.length; i++) {
346 if(collect_directory == null) {
347 collect_directory = new File(sites_dir + File.separator + sites[i].getName() + File.separator + "collect");
348 }
349 if (collect_directory.exists()) {
350 addCollectionsToModel(base_collection_model, collect_directory, sites[i].getName());
351 }
352 collect_directory = null;
353 }
354 } else {
355 if(collect_directory == null) { // if no collect directory passed in: use default collect directory
356 collect_directory = new File(Gatherer.getCollectDirectoryPath());
357 }
358 addCollectionsToModel(base_collection_model, collect_directory, null);
359 }
360
361 // Sort the result.
362 Collections.sort(base_collection_model);
363 base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection")));
364
365 // last item allows one to open up collections from other collect folders to base the new one on
366 if(!Gatherer.isGsdlRemote) { // Not yet implemented for client GLI
367 base_collection_model.add(new Item(null, Dictionary.get("NewCollectionPrompt.OtherCollections")));
368 }
369 }
370
371 private void addCollectionsToModel(Vector base_collection_model, File collect_directory, String site) {
372 File[] possible_collections = collect_directory.listFiles();
373 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
374 for (int i = 0; possible_collections != null && i < possible_collections.length; i++) {
375 // If the directory has a etc/collect.cfg file then it looks like a collection
376 File collect_cfg_file = new File(possible_collections[i], file_name);
377 if (collect_cfg_file.exists()) {
378 // Check for group coll: Check the collect.cfg for collectgroup=true
379 BasicCollectionConfiguration config = new BasicCollectionConfiguration(collect_cfg_file);
380 if (config.getCollectGroup().equals("true")) {
381 // try subdirs in here
382 addCollectionsToModel(base_collection_model, possible_collections[i], site);
383 }
384 else {
385
386 // If the directory has a metadata/ then the collection can be used as a base
387 File metadata_directory = new File(possible_collections[i], "metadata");
388 if (metadata_directory.exists()) {
389 // Add to model
390 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
391 if (Gatherer.GS3 && site != null) {
392 collect_cfg.setSite(site);
393 }
394 Item item = new Item(possible_collections[i], collect_cfg);
395 if (!base_collection_model.contains(item)) {
396 base_collection_model.add(item);
397 }
398 }
399 }
400 }
401 }
402
403 }
404
405 private class OtherCollectionsListener implements ActionListener {
406
407 // the JComboBox listener will receive an ActionEvent
408 // when a selection has been made
409
410 public void actionPerformed(ActionEvent event) {
411
412 // If the Other Collections item was selected (which is the last one in the list)
413 if(base_collection.getSelectedIndex() == base_collection.getItemCount()-1) {
414
415 // open a filechooser in whatever is the current collect directory
416 // within the NewCollectionDetailsPrompt's settings
417 if(collectBasePath == null) {
418 collectBasePath = newCollectPath;
419 }
420
421 JFileChooser chooser = new JFileChooser(collectBasePath);
422 chooser.setFileView(new CollectionFileView());
423 chooser.setDialogTitle(Dictionary.get("NewCollectionPrompt.ChooseACollection"));
424 chooser.setApproveButtonText(Dictionary.get("NewCollectionPrompt.Select"));
425
426 // to prevent the user from traversing into collection folders,
427 // need to:
428 // - NOT set the filechooser to Directories only, but files and directories
429 // (so that they can still select directories)
430 // - add a filefilter that accepts only directories (i.e. rejects files)
431 // - remove the accept all filter
432 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
433 //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
434 chooser.setFileFilter(new CollectionFileFilter());
435 chooser.setAcceptAllFileFilterUsed(false);
436
437 int returnVal = chooser.showOpenDialog(NewCollectionDetailsPrompt.this);
438 if(returnVal == JFileChooser.APPROVE_OPTION) {
439
440 File selectedFile = chooser.getSelectedFile();
441
442 // don't bother reloading if nothing has changed
443 if(collectBasePath.equals(selectedFile.getAbsolutePath() + File.separator)) {
444 // don't want Other Collections... selected
445 base_collection.setSelectedIndex(0);
446 } else {
447
448 // If the selected directory is a collection folder get its collect dir,
449 // make collectBasePath the collect dir, and select the collection itself
450 String cfg = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
451 boolean isCollection = false;
452 while(new File(selectedFile, cfg).exists()) { // while loop handles collectgroup
453 selectedFile = selectedFile.getParentFile();
454 isCollection = true;
455 } // else may be a collect dir, in which case leave it as it is again
456
457 // found collect directory for base collections
458 collectBasePath = selectedFile.getAbsolutePath()+File.separator;
459
460 // change the available collections to base this collection on
461 base_collection.removeAllItems();
462 Vector base_collection_model = new Vector();
463 setupBaseCollections(base_collection_model, collectBasePath);
464 for(int i = 0; i < base_collection_model.size(); i++) {
465 Item item = (Item)base_collection_model.get(i);
466 base_collection.addItem(item);
467
468 // select the originally chosen collection, if any, in the combobox
469 if(isCollection && chooser.getSelectedFile().equals(item.getFile())) {
470 base_collection.setSelectedIndex(i);
471 }
472 }
473
474 // But if the user selected a collect group, set combobox to New Collection
475 if(!isCollection) {
476 // if it was a collectgroup, nothing new would be selected, set to New Coll
477 if(base_collection.getSelectedIndex() == base_collection.getItemCount()-1) {
478 base_collection.setSelectedIndex(0);
479 }
480 }
481
482 }
483
484 } else { // cancelled out of dialog, so jump to New Collection
485 base_collection.setSelectedIndex(0);
486 }
487 }
488 }
489 }
490
491 private class ChangeDirListener implements ActionListener {
492 public void actionPerformed(ActionEvent event) {
493 String oldCollectPath = newCollectPath;
494 JFileChooser chooser = new JFileChooser(newCollectPath);
495 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
496 chooser.setDialogTitle(Dictionary.get("General.ChooseCollectDirectory"));
497
498 int returnVal = chooser.showOpenDialog(NewCollectionDetailsPrompt.this);
499 if(returnVal == JFileChooser.APPROVE_OPTION) {
500 newCollectPath = chooser.getSelectedFile().getAbsolutePath() + File.separator;
501
502 // Must NOT change the available collections to base this collection on here.
503 // It will change any collection the user previously chose to base the newcoll on.
504
505 // will be used to test for duplicate collection names in current collect dir
506 collectDirChanged = newCollectPath.equals(oldCollectPath) ? false : true;
507 }
508 }
509 }
510
511 private class CancelListener
512 implements ActionListener {
513 public void actionPerformed(ActionEvent event) {
514 cancelled = true;
515 self.dispose();
516 }
517 }
518
519
520 private class CreateListener
521 implements ActionListener {
522
523 public void actionPerformed(ActionEvent event) {
524 // Validate.
525 title_final = title.getText();
526 if(title_final.length() == 0) {
527 JOptionPane jOptionPane=new JOptionPane();
528 jOptionPane.setComponentOrientation(Dictionary.getOrientation());
529 jOptionPane.setOpaque(!Utility.isMac());
530 jOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE);
531 return;
532 }
533 // We must ensure that the collection title is unique. This is a pain in the nether regions as we are forced to load the collect.cfg of each other collection in turn looking for a conflicting title
534 else {
535 String collectDir = collectDirChanged ? newCollectPath : Gatherer.getCollectDirectoryPath();
536 if(titleClashes(collectDir, title_final, null)) {
537 JOptionPane jOptionPane=new JOptionPane();
538 jOptionPane.setComponentOrientation(Dictionary.getOrientation());
539 jOptionPane.setOpaque(!Utility.isMac());
540 if (jOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Clash"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
541 return;
542 }
543 }
544 }
545 description_final = description.getText();
546
547 // If we got this far there are no errors.
548 Item item_final = (Item) base_collection.getSelectedItem();
549 base_final = item_final.getFile();
550
551 cancelled = false;
552
553 self.dispose();
554
555 // going through with collection creation
556 if(collectDirChanged) {
557 Gatherer.collectDirectoryHasChanged(Gatherer.getCollectDirectoryPath(),
558 newCollectPath, Gatherer.g_man.getContentPane());
559 // will tell the server that the collect directory has changed and that
560 // the workspace needs to be refreshed (Documents in Greenstone Collections)
561 }
562 }
563 }
564
565 private class DescriptionListener
566 extends KeyAdapter {
567 public void keyPressed(KeyEvent event) {
568 if(event.getKeyCode() == KeyEvent.VK_TAB) {
569 event.consume();
570 base_collection.grabFocus();
571 }
572 }
573 }
574
575 private class Item
576 implements Comparable {
577 private BasicCollectionConfiguration config;
578 private File file;
579 private String name;
580 public Item(File file, BasicCollectionConfiguration config) {
581 this.config = config;
582 this.file = file;
583 this.name = null;
584 }
585 public Item(File file, String name) {
586 this.config = null;
587 this.file = file;
588 this.name = name;
589 }
590 public int compareTo(Object other) {
591 if(this == null && other == null) {
592 return 0;
593 }
594 else if(other == null) {
595 return 1;
596 }
597 else if(this == null) {
598 return -1;
599 }
600 else {
601 return toString().toLowerCase().compareTo(other.toString().toLowerCase());
602 }
603 }
604 public boolean equals(Object other) {
605 return compareTo(other) == 0;
606 }
607 public File getFile() {
608 return file;
609 }
610 public String toString() {
611 if(name == null && config != null) {
612 name = config.toString();
613 }
614 return name;
615 }
616 }
617
618 /** FileFilter for the Filechooser to choose the collection to base the new one on:
619 * Only displays directories. */
620 private class CollectionFileFilter extends FileFilter {
621 public boolean accept(File f) {
622 return f.isDirectory();
623 }
624 public String getDescription() {
625 return "greenstone collection view"; //return "directories only";
626 }
627 }
628
629 /** Folders that can be used as a base collection have a special icon */
630 private class CollectionFileView extends FileView {
631 final String configfile_name;
632 final ImageIcon COLLECTION_ICON = JarTools.getImage("gsCollection.gif", true);
633
634 private File previous = null;
635 private boolean previousWasCollection = false;
636
637 public CollectionFileView() {
638 configfile_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
639 }
640
641 // A human readable description of the file.
642 public String getDescription(File f) {
643 if(!f.equals(previous)) {
644 previous = f;
645 // no idea if it's a collection or not, calculate this next time
646 previousWasCollection = false;
647 }
648 return null; // FilewView superclass will handle it
649 }
650
651 // The icon that represents this file in the JFileChooser.
652 public Icon getIcon(File f) {
653
654 if(f.equals(previous) && previousWasCollection) {
655 return COLLECTION_ICON;
656 }
657
658 if(isCollectionBase(f)) {
659 return COLLECTION_ICON;
660 }
661
662 return null; // FileView superclass will do the default
663 }
664
665 // The name of the file.
666 public String getName(File f) {
667 if(!f.equals(previous)) {
668 previous = f;
669 // no idea if it's a collection or not, calculate this next time
670 previousWasCollection = false;
671 }
672 return f.getName();
673 }
674
675 // A human readable description of the type of the file.
676 public String getTypeDescription(File f) {
677 if(f.equals(previous) && previousWasCollection) {
678 return "collection";
679 }
680 if(isCollectionBase(f)) {
681 return "collection";
682 }
683 // else
684 return "not a collection";
685 }
686
687 // Whether the directory is traversable or not.
688 public Boolean isTraversable(File f) {
689 if(f.equals(previous) && previousWasCollection) {
690 return Boolean.FALSE;
691 }
692
693 if(isCollectionBase(f)) {
694 return Boolean.FALSE;
695 }
696
697 if(f.isFile()) { // not traversable
698 return Boolean.FALSE;
699 }
700 // any other kind of directory is traversable
701 return Boolean.TRUE;
702 }
703
704
705 /** returns false for collect groups and collect dirs. */
706 private boolean isCollectionBase(File f) {
707 previous = f;
708 previousWasCollection = false;
709
710 if(!f.isFile()) {
711 // If the directory has a etc/collect.cfg file then it looks like a collection
712 File collect_cfg_file = new File(f, configfile_name);
713 if (collect_cfg_file.exists()) {
714 // Check for group coll: Check the collect.cfg for collectgroup=true
715 BasicCollectionConfiguration config = new BasicCollectionConfiguration(collect_cfg_file);
716 if (!config.getCollectGroup().equals("true")) {
717 previousWasCollection = true;
718 }
719 }
720 }
721 return previousWasCollection;
722 }
723
724 }
725}
726
727
728
Note: See TracBrowser for help on using the repository browser.