source: main/trunk/gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java@ 23238

Last change on this file since 23238 was 23238, checked in by ak19, 13 years ago

In the New Collection and Open Collection dialogs, the OK buttons now come first/leftmost and are followed by the Change_Dir buttons. Kathy explained that the default button (OK) being leftmost is more sensible.

  • Property svn:keywords set to Author Date Id Revision
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 }
353 } else {
354 if(collect_directory == null) { // if no collect directory passed in: use default collect directory
355 collect_directory = new File(Gatherer.getCollectDirectoryPath());
356 }
357 addCollectionsToModel(base_collection_model, collect_directory, null);
358 }
359
360 // Sort the result.
361 Collections.sort(base_collection_model);
362 base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection")));
363
364 // last item allows one to open up collections from other collect folders to base the new one on
365 if(!Gatherer.isGsdlRemote) { // Not yet implemented for client GLI
366 base_collection_model.add(new Item(null, Dictionary.get("NewCollectionPrompt.OtherCollections")));
367 }
368 }
369
370 private void addCollectionsToModel(Vector base_collection_model, File collect_directory, String site) {
371 File[] possible_collections = collect_directory.listFiles();
372 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
373 for (int i = 0; possible_collections != null && i < possible_collections.length; i++) {
374 // If the directory has a etc/collect.cfg file then it looks like a collection
375 File collect_cfg_file = new File(possible_collections[i], file_name);
376 if (collect_cfg_file.exists()) {
377 // Check for group coll: Check the collect.cfg for collectgroup=true
378 BasicCollectionConfiguration config = new BasicCollectionConfiguration(collect_cfg_file);
379 if (config.getCollectGroup().equals("true")) {
380 // try subdirs in here
381 addCollectionsToModel(base_collection_model, possible_collections[i], site);
382 }
383 else {
384
385 // If the directory has a metadata/ then the collection can be used as a base
386 File metadata_directory = new File(possible_collections[i], "metadata");
387 if (metadata_directory.exists()) {
388 // Add to model
389 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
390 if (Gatherer.GS3 && site != null) {
391 collect_cfg.setSite(site);
392 }
393 Item item = new Item(possible_collections[i], collect_cfg);
394 if (!base_collection_model.contains(item)) {
395 base_collection_model.add(item);
396 }
397 }
398 }
399 }
400 }
401
402 }
403
404 private class OtherCollectionsListener implements ActionListener {
405
406 // the JComboBox listener will receive an ActionEvent
407 // when a selection has been made
408
409 public void actionPerformed(ActionEvent event) {
410
411 // If the Other Collections item was selected (which is the last one in the list)
412 if(base_collection.getSelectedIndex() == base_collection.getItemCount()-1) {
413
414 // open a filechooser in whatever is the current collect directory
415 // within the NewCollectionDetailsPrompt's settings
416 if(collectBasePath == null) {
417 collectBasePath = newCollectPath;
418 }
419
420 JFileChooser chooser = new JFileChooser(collectBasePath);
421 chooser.setFileView(new CollectionFileView());
422 chooser.setDialogTitle(Dictionary.get("NewCollectionPrompt.ChooseACollection"));
423 chooser.setApproveButtonText(Dictionary.get("NewCollectionPrompt.Select"));
424
425 // to prevent the user from traversing into collection folders,
426 // need to:
427 // - NOT set the filechooser to Directories only, but files and directories
428 // (so that they can still select directories)
429 // - add a filefilter that accepts only directories (i.e. rejects files)
430 // - remove the accept all filter
431 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
432 //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
433 chooser.setFileFilter(new CollectionFileFilter());
434 chooser.setAcceptAllFileFilterUsed(false);
435
436 int returnVal = chooser.showOpenDialog(NewCollectionDetailsPrompt.this);
437 if(returnVal == JFileChooser.APPROVE_OPTION) {
438
439 File selectedFile = chooser.getSelectedFile();
440
441 // don't bother reloading if nothing has changed
442 if(collectBasePath.equals(selectedFile.getAbsolutePath() + File.separator)) {
443 // don't want Other Collections... selected
444 base_collection.setSelectedIndex(0);
445 } else {
446
447 // If the selected directory is a collection folder get its collect dir,
448 // make collectBasePath the collect dir, and select the collection itself
449 String cfg = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
450 boolean isCollection = false;
451 while(new File(selectedFile, cfg).exists()) { // while loop handles collectgroup
452 selectedFile = selectedFile.getParentFile();
453 isCollection = true;
454 } // else may be a collect dir, in which case leave it as it is again
455
456 // found collect directory for base collections
457 collectBasePath = selectedFile.getAbsolutePath()+File.separator;
458
459 // change the available collections to base this collection on
460 base_collection.removeAllItems();
461 Vector base_collection_model = new Vector();
462 setupBaseCollections(base_collection_model, collectBasePath);
463 for(int i = 0; i < base_collection_model.size(); i++) {
464 Item item = (Item)base_collection_model.get(i);
465 base_collection.addItem(item);
466
467 // select the originally chosen collection, if any, in the combobox
468 if(isCollection && chooser.getSelectedFile().equals(item.getFile())) {
469 base_collection.setSelectedIndex(i);
470 }
471 }
472
473 // But if the user selected a collect group, set combobox to New Collection
474 if(!isCollection) {
475 // if it was a collectgroup, nothing new would be selected, set to New Coll
476 if(base_collection.getSelectedIndex() == base_collection.getItemCount()-1) {
477 base_collection.setSelectedIndex(0);
478 }
479 }
480
481 }
482
483 } else { // cancelled out of dialog, so jump to New Collection
484 base_collection.setSelectedIndex(0);
485 }
486 }
487 }
488 }
489
490 private class ChangeDirListener implements ActionListener {
491 public void actionPerformed(ActionEvent event) {
492 String oldCollectPath = newCollectPath;
493 JFileChooser chooser = new JFileChooser(newCollectPath);
494 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
495 chooser.setDialogTitle(Dictionary.get("General.ChooseCollectDirectory"));
496
497 int returnVal = chooser.showOpenDialog(NewCollectionDetailsPrompt.this);
498 if(returnVal == JFileChooser.APPROVE_OPTION) {
499 newCollectPath = chooser.getSelectedFile().getAbsolutePath() + File.separator;
500
501 // Must NOT change the available collections to base this collection on here.
502 // It will change any collection the user previously chose to base the newcoll on.
503
504 // will be used to test for duplicate collection names in current collect dir
505 collectDirChanged = newCollectPath.equals(oldCollectPath) ? false : true;
506 }
507 }
508 }
509
510 private class CancelListener
511 implements ActionListener {
512 public void actionPerformed(ActionEvent event) {
513 cancelled = true;
514 self.dispose();
515 }
516 }
517
518
519 private class CreateListener
520 implements ActionListener {
521
522 public void actionPerformed(ActionEvent event) {
523 // Validate.
524 title_final = title.getText();
525 if(title_final.length() == 0) {
526 JOptionPane jOptionPane=new JOptionPane();
527 jOptionPane.setComponentOrientation(Dictionary.getOrientation());
528 jOptionPane.setOpaque(!Utility.isMac());
529 jOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE);
530 return;
531 }
532 // 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
533 else {
534 String collectDir = collectDirChanged ? newCollectPath : Gatherer.getCollectDirectoryPath();
535 if(titleClashes(collectDir, title_final, null)) {
536 JOptionPane jOptionPane=new JOptionPane();
537 jOptionPane.setComponentOrientation(Dictionary.getOrientation());
538 jOptionPane.setOpaque(!Utility.isMac());
539 if (jOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Clash"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
540 return;
541 }
542 }
543 }
544 description_final = description.getText();
545
546 // If we got this far there are no errors.
547 Item item_final = (Item) base_collection.getSelectedItem();
548 base_final = item_final.getFile();
549
550 cancelled = false;
551
552 self.dispose();
553
554 // going through with collection creation
555 if(collectDirChanged) {
556 Gatherer.collectDirectoryHasChanged(Gatherer.getCollectDirectoryPath(),
557 newCollectPath, Gatherer.g_man.getContentPane());
558 // will tell the server that the collect directory has changed and that
559 // the workspace needs to be refreshed (Documents in Greenstone Collections)
560 }
561 }
562 }
563
564 private class DescriptionListener
565 extends KeyAdapter {
566 public void keyPressed(KeyEvent event) {
567 if(event.getKeyCode() == KeyEvent.VK_TAB) {
568 event.consume();
569 base_collection.grabFocus();
570 }
571 }
572 }
573
574 private class Item
575 implements Comparable {
576 private BasicCollectionConfiguration config;
577 private File file;
578 private String name;
579 public Item(File file, BasicCollectionConfiguration config) {
580 this.config = config;
581 this.file = file;
582 this.name = null;
583 }
584 public Item(File file, String name) {
585 this.config = null;
586 this.file = file;
587 this.name = name;
588 }
589 public int compareTo(Object other) {
590 if(this == null && other == null) {
591 return 0;
592 }
593 else if(other == null) {
594 return 1;
595 }
596 else if(this == null) {
597 return -1;
598 }
599 else {
600 return toString().toLowerCase().compareTo(other.toString().toLowerCase());
601 }
602 }
603 public boolean equals(Object other) {
604 return compareTo(other) == 0;
605 }
606 public File getFile() {
607 return file;
608 }
609 public String toString() {
610 if(name == null && config != null) {
611 name = config.toString();
612 }
613 return name;
614 }
615 }
616
617 /** FileFilter for the Filechooser to choose the collection to base the new one on:
618 * Only displays directories. */
619 private class CollectionFileFilter extends FileFilter {
620 public boolean accept(File f) {
621 return f.isDirectory();
622 }
623 public String getDescription() {
624 return "greenstone collection view"; //return "directories only";
625 }
626 }
627
628 /** Folders that can be used as a base collection have a special icon */
629 private class CollectionFileView extends FileView {
630 final String configfile_name;
631 final ImageIcon COLLECTION_ICON = JarTools.getImage("gsCollection.gif", true);
632
633 private File previous = null;
634 private boolean previousWasCollection = false;
635
636 public CollectionFileView() {
637 configfile_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
638 }
639
640 // A human readable description of the file.
641 public String getDescription(File f) {
642 if(!f.equals(previous)) {
643 previous = f;
644 // no idea if it's a collection or not, calculate this next time
645 previousWasCollection = false;
646 }
647 return null; // FilewView superclass will handle it
648 }
649
650 // The icon that represents this file in the JFileChooser.
651 public Icon getIcon(File f) {
652
653 if(f.equals(previous) && previousWasCollection) {
654 return COLLECTION_ICON;
655 }
656
657 if(isCollectionBase(f)) {
658 return COLLECTION_ICON;
659 }
660
661 return null; // FileView superclass will do the default
662 }
663
664 // The name of the file.
665 public String getName(File f) {
666 if(!f.equals(previous)) {
667 previous = f;
668 // no idea if it's a collection or not, calculate this next time
669 previousWasCollection = false;
670 }
671 return f.getName();
672 }
673
674 // A human readable description of the type of the file.
675 public String getTypeDescription(File f) {
676 if(f.equals(previous) && previousWasCollection) {
677 return "collection";
678 }
679 if(isCollectionBase(f)) {
680 return "collection";
681 }
682 // else
683 return "not a collection";
684 }
685
686 // Whether the directory is traversable or not.
687 public Boolean isTraversable(File f) {
688 if(f.equals(previous) && previousWasCollection) {
689 return Boolean.FALSE;
690 }
691
692 if(isCollectionBase(f)) {
693 return Boolean.FALSE;
694 }
695
696 if(f.isFile()) { // not traversable
697 return Boolean.FALSE;
698 }
699 // any other kind of directory is traversable
700 return Boolean.TRUE;
701 }
702
703
704 /** returns false for collect groups and collect dirs. */
705 private boolean isCollectionBase(File f) {
706 previous = f;
707 previousWasCollection = false;
708
709 if(!f.isFile()) {
710 // If the directory has a etc/collect.cfg file then it looks like a collection
711 File collect_cfg_file = new File(f, configfile_name);
712 if (collect_cfg_file.exists()) {
713 // Check for group coll: Check the collect.cfg for collectgroup=true
714 BasicCollectionConfiguration config = new BasicCollectionConfiguration(collect_cfg_file);
715 if (!config.getCollectGroup().equals("true")) {
716 previousWasCollection = true;
717 }
718 }
719 }
720 return previousWasCollection;
721 }
722
723 }
724}
725
726
727
Note: See TracBrowser for help on using the repository browser.