Ignore:
Timestamp:
2003-07-15T13:55:22+12:00 (21 years ago)
Author:
jmt12
Message:

Major CDM rewrite so it uses DOM.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/cdm/SuperCollectionManager.java

    r4675 r4932  
    2828
    2929import java.awt.*;
    30 import java.awt.event.*;
    3130import java.io.*;
    3231import java.util.*;
    3332import javax.swing.*;
     33import javax.swing.event.*;
    3434import org.greenstone.gatherer.Configuration;
    3535import org.greenstone.gatherer.Dictionary;
    3636import org.greenstone.gatherer.Gatherer;
     37import org.greenstone.gatherer.cdm.DOMProxyListModel;
    3738import org.greenstone.gatherer.checklist.CheckList;
    3839import org.greenstone.gatherer.checklist.Entry;
    3940import org.greenstone.gatherer.collection.CollectionConfiguration;
     41import org.greenstone.gatherer.util.StaticStrings;
    4042import org.greenstone.gatherer.util.Utility;
    41 
    42 public class SuperCollectionManager {
     43import org.w3c.dom.*;
     44/** 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. */
     45public class SuperCollectionManager
     46    extends DOMProxyListModel {
    4347
    4448    static final public String SUPERCOLLECTION_COMMAND = "supercollection";
     
    4650
    4751    private String current_coll_name = null;
    48     private Control controls;
    49     private ArrayList collection_checklist_model;
    50 
    51     SuperCollectionManager() {
    52     // We start by building a model of the installed collections.
    53     collection_checklist_model = new ArrayList();
    54     File gsdl_collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
    55     current_coll_name = Gatherer.c_man.getCollection().getName();
    56     File[] possible_collections = gsdl_collection_directory.listFiles();
    57     for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
    58         // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon.
    59         File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_DIR);
    60         if(collect_cfg_file.exists()) {
    61         CollectionConfiguration collect_cfg = new CollectionConfiguration(collect_cfg_file);
    62         String collection_title = collect_cfg.getName();
    63         String collection_name = possible_collections[i].getName();
    64         // We do have to block the model collection.
    65         if(!collection_title.equals("**title**")) {
    66             Entry entry = new Entry(collection_title);
    67             entry.setProperty(collection_name);
    68             if (collection_name.equals(current_coll_name)) {
    69             // the current collection is always selected
    70             entry.setFixed(true);
    71             entry.setSelected(true);
    72             }
    73             collection_checklist_model.add(entry);
    74             entry = null;
    75 
    76         }
    77         collection_name = null;
    78         collection_title = null;
    79         collect_cfg = null;
     52    private Control controls = null;
     53    private ArrayList collection_checklist_model = null; // Model used to actually populate list
     54
     55    public SuperCollectionManager(Element supercollections_element) {
     56    super(supercollections_element, StaticStrings.COLLECTION_ELEMENT, new SuperCollection());
     57    Gatherer.println("SuperCollectionManager: " + getSize() + " supercollection members parsed.");
     58    }
     59
     60    public void destroy() {
     61    if(controls != null) {
     62        controls.destroy();
     63        controls = null;
     64    }
     65    if(collection_checklist_model != null) {
     66        collection_checklist_model.clear();
     67        collection_checklist_model = null;
     68    }
     69    }
     70
     71    public void addSuperCollection(SuperCollection supercollection) {
     72    if(!contains(supercollection)) {
     73        add(getSize(), supercollection);
     74    }
     75    }
     76
     77    /** Method to retrieve the control for this manager.
     78     * @return the Control for editing supercollection settings
     79     */
     80    public Control getControls() {
     81    if(controls == null) {
     82        // Build controls
     83        this.controls = new SuperCollectionControl();
     84    }
     85    return controls;
     86    }
     87
     88    public SuperCollection getSuperCollection(String collection_name) {
     89    SuperCollection result = null;
     90    int size = getSize();
     91    for(int i = 0; result == null && i < size; i++) {
     92        SuperCollection supercollection = (SuperCollection) getElementAt(i);
     93        if(supercollection.getName().equals(collection_name)) {
     94        result = supercollection;
    8095        }
    81         collect_cfg_file = null;
    82     }
    83     possible_collections = null;
    84     gsdl_collection_directory = null;
    85     // Sort the result.
    86     Collections.sort(collection_checklist_model);
    87     }
    88 
    89     public boolean parse(String command) {
    90     String command_lc = command.toLowerCase();
    91     if(command_lc.startsWith(SUPERCOLLECTION_COMMAND) || command_lc.startsWith(CCS_COMMAND)) {
    92         StringTokenizer tokenizer = new StringTokenizer(command);
    93         tokenizer.nextToken();
    94         // Read in each of the collection names.
    95         ArrayList collection_names = new ArrayList();
    96         while(tokenizer.hasMoreTokens()) {
    97         collection_names.add(tokenizer.nextToken());
    98         }
    99         // Now that we have the collection names, select any entries in the checklist that match.
    100         for(int i = 0; i < collection_checklist_model.size(); i++) {
    101         Entry entry = (Entry) collection_checklist_model.get(i);
    102         String collection_name = entry.getProperty();
    103         if(collection_names.contains(collection_name)) {
    104             entry.setSelected(true);
    105             collection_names.remove(collection_name); // Makes successive runs faster.
    106         }
    107         collection_name = null;
    108         entry = null;
    109         }
    110         // Done.
    111         return true;
    112     }
    113     return false;
    114     }
    115 
    116     /** Method to retrieve the control for this manager.
    117      * @return A JPanel containing the controls.
    118      */
    119     public JPanel getControls() {
    120     if(controls == null) {
    121         controls = new Control();
    122     }
    123     return controls;
    124     }
    125 
    126     public void invalidateControls() {
    127     controls = null;
    128     }
    129 
    130     /** Return the contents of this manager as a command or block of commands ready to be written to the collection configuration file.
    131      * @return A String containing a configuration command or block of commands, terminated with a new line. This value will be null if there was no supercollection command set. */
    132     public String toString() {
    133     StringBuffer command = new StringBuffer(SUPERCOLLECTION_COMMAND);
    134     // Now for each entry selected output its collection name (property setting)
    135     boolean found_entry = false;
    136     for(int i = 0; i < collection_checklist_model.size(); i++) {
    137         Entry entry = (Entry) collection_checklist_model.get(i);
    138         if(entry.isSelected()) {
    139         if (!entry.getProperty().equals(current_coll_name)){
    140             found_entry = true;
    141         }
    142         command.append(" ");
    143         command.append(entry.getProperty());
    144         }
    145         entry = null;
    146     }
    147     command.append("\n");
    148     command.append("\n");
    149    
    150     // It is possible that there was only one entry - the current collection -  in which case we return null.
    151     if(found_entry) {
    152         return command.toString();
    153     }
    154     else {
    155         return null;
    156     }
     96        supercollection = null;
     97    }
     98    return result;
     99    }
     100
     101    public void removeSuperCollection(SuperCollection supercollection) {
     102    remove(supercollection);
    157103    }
    158104
     
    169115
    170116    /** Provides controls for altering the SuperCollection settings. */
    171     private class Control
    172     extends JPanel {
     117    private class SuperCollectionControl
     118    extends JPanel
     119    implements Control {
    173120
    174121    private boolean init = true;
     122    private CheckList collection_checklist = null;
    175123    //private JSplitPane center_pane;
    176124
    177     Control() {
     125    SuperCollectionControl() {
    178126        super();
     127
     128        buildModel();
    179129        // Creation
    180130        JPanel header_panel = new JPanel();
     
    194144        instructions.setWrapStyleWord(true);
    195145
    196         CheckList collection_checklist = new CheckList(collection_checklist_model);
     146        collection_checklist = new CheckList(collection_checklist_model);
     147
    197148        // Layout
    198149        //center_pane.setTopComponent(new JScrollPane(instructions));
     
    209160    }
    210161
    211     public void paint(Graphics g) {
    212         super.paint(g);
    213         // If this is the first time we've painted this control, then set the divider 1/3:2/3
    214         //if(init) {
    215         //  init = false;
    216         //  center_pane.setDividerLocation(0.3);
    217         //  super.paint(g);
    218         //}
     162    public void destroy() {
     163    }
     164
     165    public void gainFocus() {
     166    }
     167
     168    public void loseFocus() {
     169        // Retrieve the current supercollections
     170        ArrayList supercollections = children();
     171        // 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
     172        int size = collection_checklist_model.size();
     173        for(int i = 0; i < size; i++) {
     174        Entry entry = (Entry) collection_checklist_model.get(i);
     175        if(entry.isSelected()) {
     176            String collection_name = (String) entry.getProperty();
     177            SuperCollection supercollection = getSuperCollection(collection_name);
     178            // Create the supercollection element if necessary
     179            if(supercollection == null) {
     180            Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
     181            supercollection = new SuperCollection(supercollection_element);
     182            supercollection.setName(collection_name);
     183            addSuperCollection(supercollection);
     184            }
     185            else {
     186            supercollections.remove(supercollection);
     187            }
     188            supercollection.setAssigned(true);
     189        }
     190        }
     191        // Any collections left in the temporary list have been unselected, so delete them
     192        for(int j = supercollections.size(); j > 0; j--) {
     193        SuperCollection supercollection = (SuperCollection) supercollections.get(j - 1);
     194        removeSuperCollection(supercollection);
     195        }
     196    }
     197
     198    private void buildModel() {
     199        // We start by building a model of the installed collections.
     200        collection_checklist_model = new ArrayList();
     201        File gsdl_collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
     202        current_coll_name = Gatherer.c_man.getCollection().getName();
     203        File[] possible_collections = gsdl_collection_directory.listFiles();
     204        for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
     205        // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon.
     206        File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_DIR);
     207        if(collect_cfg_file.exists()) {
     208            CollectionConfiguration collect_cfg = new CollectionConfiguration(collect_cfg_file);
     209            String collection_title = collect_cfg.getName();
     210            String collection_name = possible_collections[i].getName();
     211            // We do have to block the model collection.
     212            if(!collection_title.equals("**title**")) {
     213            Entry entry = new Entry(collection_title);
     214            entry.setProperty(collection_name);
     215            // Check if a collection with this name exists. The current collection is always selected.
     216            entry.setSelected(getSuperCollection(collection_name) != null || collection_name.equals(current_coll_name));
     217            entry.setFixed(collection_name.equals(current_coll_name));
     218            collection_checklist_model.add(entry);
     219            entry = null;
     220           
     221            }
     222            collection_name = null;
     223            collection_title = null;
     224            collect_cfg = null;
     225        }
     226        collect_cfg_file = null;
     227        }
     228        possible_collections = null;
     229        gsdl_collection_directory = null;
     230        // Sort the result.
     231        Collections.sort(collection_checklist_model);
    219232    }
    220233    }
Note: See TracChangeset for help on using the changeset viewer.