- Timestamp:
- 2010-08-11T21:28:34+12:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java
r18947 r22605 34 34 import javax.swing.event.*; 35 35 import javax.swing.text.*; 36 import javax.swing.filechooser.FileFilter; 37 import javax.swing.filechooser.FileView; 36 38 import org.greenstone.gatherer.Configuration; 37 39 import org.greenstone.gatherer.Dictionary; … … 39 41 import org.greenstone.gatherer.collection.BasicCollectionConfiguration; 40 42 import org.greenstone.gatherer.remote.RemoteGreenstoneServer; 43 import org.greenstone.gatherer.util.JarTools; 41 44 import org.greenstone.gatherer.util.StaticStrings; 42 45 import org.greenstone.gatherer.util.Utility; … … 45 48 extends ModalDialog { 46 49 47 48 49 static public boolean titleClashes(String title, File current_config_file) { 50 51 static public boolean titleClashes(String collectDir, String title, File current_config_file) { 50 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. 51 53 if(title == null || title.length() == 0) { 52 54 return false; 53 55 } 54 File collect_directory = new File( Gatherer.getCollectDirectoryPath());56 File collect_directory = new File(collectDir); 55 57 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE; 56 58 File children[] = collect_directory.listFiles(); … … 72 74 73 75 private boolean cancelled; 76 private boolean collectDirChanged; 74 77 private File base_final; 78 private JButton chdir_button; 75 79 private JButton create_button; 76 80 private JComboBox base_collection; … … 79 83 private JTextArea description; 80 84 private JTextField title; 85 private String collectBasePath; 86 private String newCollectPath; 81 87 private String description_final; 82 88 private String title_final=""; 89 83 90 static private Dimension COMPONENT_SIZE = new Dimension(230, 25); 84 91 /** The size of this new collection dialog box. */ … … 94 101 this.setComponentOrientation(Dictionary.getOrientation()); 95 102 this.self = this; 103 newCollectPath = Gatherer.getCollectDirectoryPath(); 104 collectBasePath = null; 105 collectDirChanged = false; 106 96 107 // Setup 97 108 setJMenuBar(new SimpleMenuBar("creatingacollection")); … … 101 112 // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories. 102 113 Vector base_collection_model = new Vector(); 103 // need to modify this to base a coll on any collection from any site 104 if (Gatherer.GS3 && !Gatherer.isGsdlRemote) { 105 File sites_dir = new File(Gatherer.getSitesDirectoryPath()); 106 File [] sites = sites_dir.listFiles(); 107 for (int i=0; i<sites.length; i++) { 108 File collect_directory = new File(sites_dir + File.separator + sites[i].getName() + File.separator + "collect"); 109 if (collect_directory.exists()) { 110 addCollectionsToModel(base_collection_model, collect_directory, sites[i].getName()); 111 } 112 } 113 } else { 114 File collect_directory = new File(Gatherer.getCollectDirectoryPath()); 115 addCollectionsToModel(base_collection_model, collect_directory, null); 116 } 117 118 // Sort the result. 119 Collections.sort(base_collection_model); 120 base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection"))); 114 setupBaseCollections(base_collection_model, null); // if no collect directory passed in: use default collect directory 115 121 116 122 117 // Creation … … 190 185 button_pane.setComponentOrientation(Dictionary.getOrientation()); 191 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")); 192 188 JButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip")); 193 189 194 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 } 195 197 cancel_button.addActionListener(new CancelListener()); 196 198 create_button.addActionListener(new CreateListener()); … … 238 240 239 241 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); 240 button_pane.setLayout(new GridLayout(1,2)); 242 button_pane.setLayout(new GridLayout(1,3)); 243 button_pane.add(chdir_button); 241 244 button_pane.add(create_button); 242 245 button_pane.add(cancel_button); … … 327 330 public String getTitle() { 328 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 } 329 368 } 330 369 … … 363 402 } 364 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 365 510 private class CancelListener 366 511 implements ActionListener { … … 387 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 388 533 else { 389 if(titleClashes(title_final, null)) { 534 String collectDir = collectDirChanged ? newCollectPath : Gatherer.getCollectDirectoryPath(); 535 if(titleClashes(collectDir, title_final, null)) { 390 536 JOptionPane jOptionPane=new JOptionPane(); 391 537 jOptionPane.setComponentOrientation(Dictionary.getOrientation()); … … 395 541 } 396 542 } 543 } 544 545 // going through with collection creation 546 if(collectDirChanged) { 547 Gatherer.collectDirectoryHasChanged(Gatherer.getCollectDirectoryPath(), newCollectPath); 548 // will tell the server that the collect directory has changed and that 549 // the workspace needs to be refreshed (Documents in Greenstone Collections) 397 550 } 398 551 … … 435 588 } 436 589 public int compareTo(Object other) { 437 return toString().toLowerCase().compareTo(other.toString().toLowerCase()); 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 } 438 602 } 439 603 public boolean equals(Object other) { … … 450 614 } 451 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 } 452 724 } 453 725
Note:
See TracChangeset
for help on using the changeset viewer.