/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.cdm; import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.cdm.DOMProxyListModel; import org.greenstone.gatherer.checklist.CheckList; import org.greenstone.gatherer.checklist.Entry; import org.greenstone.gatherer.collection.BasicCollectionConfiguration; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; import org.w3c.dom.*; /** This class contains the information about what supercollection has been specified (if any) and methods for changing this information. Note that there is a major difference between this manager and the others in that its DOM model is never used directly in any list component. It is only used to decide whether a certain entry in the actual checklist is checked. */ public class SuperCollectionManager extends DOMProxyListModel { static final public String SUPERCOLLECTION_COMMAND = "supercollection"; static final public String CCS_COMMAND = "ccs"; private ArrayList collection_checklist_model = null; // Model used to actually populate list private Control controls = null; private DOMProxyListModel model = null; private String current_coll_name = null; public SuperCollectionManager(Element supercollections_element) { super(supercollections_element, StaticStrings.COLLECTION_ELEMENT, new SuperCollection()); Gatherer.println("SuperCollectionManager: " + getSize() + " supercollection members parsed."); this.model = this; } public void destroy() { if(controls != null) { controls.destroy(); controls = null; } if(collection_checklist_model != null) { collection_checklist_model.clear(); collection_checklist_model = null; } } public void addSuperCollection(SuperCollection supercollection) { if(!contains(supercollection)) { add(getSize(), supercollection); } } /** Method to retrieve the control for this manager. * @return the Control for editing supercollection settings */ public Control getControls() { if(controls == null) { // Build controls this.controls = new SuperCollectionControl(); } return controls; } public SuperCollection getSuperCollection(String collection_name) { SuperCollection result = null; int size = getSize(); for(int i = 0; result == null && i < size; i++) { SuperCollection supercollection = (SuperCollection) getElementAt(i); if(supercollection.getName().equals(collection_name)) { result = supercollection; } supercollection = null; } return result; } public void removeSuperCollection(SuperCollection supercollection) { remove(supercollection); } /** Provides controls for altering the SuperCollection settings. */ private class SuperCollectionControl extends JPanel implements Control { private boolean init = true; private CheckList collection_checklist = null; SuperCollectionControl() { super(); buildModel(); // Creation JPanel header_panel = new JPanel(); JLabel title_label = new JLabel(); title_label.setHorizontalAlignment(JLabel.CENTER); title_label.setOpaque(true); Dictionary.registerText(title_label, "CDM.SuperCollectionManager.Title"); JTextArea instructions = new JTextArea(); instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); instructions.setCaretPosition(0); instructions.setEditable(false); instructions.setLineWrap(true); instructions.setRows(6); instructions.setWrapStyleWord(true); Dictionary.registerText(instructions, "CDM.SuperCollectionManager.Instructions"); collection_checklist = new CheckList(collection_checklist_model, false); // Layout header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0)); header_panel.setLayout(new BorderLayout()); header_panel.add(title_label, BorderLayout.NORTH); header_panel.add(new JScrollPane(instructions), BorderLayout.CENTER); setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); setLayout(new BorderLayout()); add(header_panel, BorderLayout.NORTH); add(new JScrollPane(collection_checklist), BorderLayout.CENTER); } public void destroy() { } public void gainFocus() { } public void loseFocus() { int super_collections_count = 0; // Retrieve the current supercollections ArrayList supercollections = children(); // Now iterate through the checklist, and for each checked box found ensure the Supercollection exists, and ensure its assigned. Remove any supercollections altered in this way from the temporary array list int size = collection_checklist_model.size(); for(int i = 0; i < size; i++) { Entry entry = (Entry) collection_checklist_model.get(i); if(entry.isSelected()) { String collection_name = (String) entry.getProperty(); SuperCollection supercollection = getSuperCollection(collection_name); // Create the supercollection element if necessary if(supercollection == null) { Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT); supercollection = new SuperCollection(supercollection_element); supercollection.setName(collection_name); addSuperCollection(supercollection); } else { supercollections.remove(supercollection); } supercollection.setAssigned(true); super_collections_count++; } } // Any collections left in the temporary list have been unselected, so delete them for(int j = supercollections.size(); j > 0; j--) { SuperCollection supercollection = (SuperCollection) supercollections.get(j - 1); removeSuperCollection(supercollection); } // Now we only assign supercollections if more than one is selected model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (super_collections_count > 1 ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); } private void buildModel() { // We start by building a model of the installed collections. collection_checklist_model = new ArrayList(); File gsdl_collection_directory; if (Gatherer.GS3) { gsdl_collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl3_path, Gatherer.config.site_name)); } else { gsdl_collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl_path)); } current_coll_name = Gatherer.c_man.getCollection().getName(); File[] possible_collections = gsdl_collection_directory.listFiles(); for(int i = 0; possible_collections != null && i < possible_collections.length; i++) { // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon. File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_FILE); if(collect_cfg_file.exists()) { BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file); StringBuffer title_buffer = new StringBuffer(collect_cfg.getName()); title_buffer.append(StaticStrings.SPACE_CHARACTER); title_buffer.append(StaticStrings.OPEN_PARENTHESIS_CHARACTER); title_buffer.append(possible_collections[i].getName()); title_buffer.append(StaticStrings.CLOSE_PARENTHESIS_CHARACTER); String collection_title = title_buffer.toString(); title_buffer = null; String collection_name = possible_collections[i].getName(); // We do have to block the model collection. if(!collect_cfg.getName().equals("**title**")) { Entry entry = new Entry(collection_title); entry.setProperty(collection_name); // Check if a collection with this name exists. The current collection is always selected. entry.setSelected(getSuperCollection(collection_name) != null || collection_name.equals(current_coll_name)); entry.setFixed(collection_name.equals(current_coll_name)); collection_checklist_model.add(entry); entry = null; } collection_name = null; collection_title = null; collect_cfg = null; } collect_cfg_file = null; } possible_collections = null; gsdl_collection_directory = null; // Sort the result. Collections.sort(collection_checklist_model); } } }