source: trunk/gli/src/org/greenstone/gatherer/cdm/SuperCollectionManager.java@ 7275

Last change on this file since 7275 was 7275, checked in by kjdon, 20 years ago

renamed Utility.CONFIG_DIR to Utility.CONFIG_FILE cos it refers to the file, not the directory. Also getConfigDir renamed to getConfigFile

  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 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 Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 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.cdm;
28
29import java.awt.*;
30import java.io.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.cdm.DOMProxyListModel;
37import org.greenstone.gatherer.checklist.CheckList;
38import org.greenstone.gatherer.checklist.Entry;
39import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
40import org.greenstone.gatherer.util.StaticStrings;
41import org.greenstone.gatherer.util.Utility;
42import org.w3c.dom.*;
43
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 {
47
48 static final public String SUPERCOLLECTION_COMMAND = "supercollection";
49 static final public String CCS_COMMAND = "ccs";
50 private ArrayList collection_checklist_model = null; // Model used to actually populate list
51 private Control controls = null;
52 private DOMProxyListModel model = null;
53 private String current_coll_name = null;
54
55
56 public SuperCollectionManager(Element supercollections_element) {
57 super(supercollections_element, StaticStrings.COLLECTION_ELEMENT, new SuperCollection());
58 Gatherer.println("SuperCollectionManager: " + getSize() + " supercollection members parsed.");
59 this.model = this;
60 }
61
62 public void destroy() {
63 if(controls != null) {
64 controls.destroy();
65 controls = null;
66 }
67 if(collection_checklist_model != null) {
68 collection_checklist_model.clear();
69 collection_checklist_model = null;
70 }
71 }
72
73 public void addSuperCollection(SuperCollection supercollection) {
74 if(!contains(supercollection)) {
75 add(getSize(), supercollection);
76 }
77 }
78
79 /** Method to retrieve the control for this manager.
80 * @return the Control for editing supercollection settings
81 */
82 public Control getControls() {
83 if(controls == null) {
84 // Build controls
85 this.controls = new SuperCollectionControl();
86 }
87 return controls;
88 }
89
90 public SuperCollection getSuperCollection(String collection_name) {
91 SuperCollection result = null;
92 int size = getSize();
93 for(int i = 0; result == null && i < size; i++) {
94 SuperCollection supercollection = (SuperCollection) getElementAt(i);
95 if(supercollection.getName().equals(collection_name)) {
96 result = supercollection;
97 }
98 supercollection = null;
99 }
100 return result;
101 }
102
103 public void removeSuperCollection(SuperCollection supercollection) {
104 remove(supercollection);
105 }
106
107 /** Provides controls for altering the SuperCollection settings. */
108 private class SuperCollectionControl
109 extends JPanel
110 implements Control {
111
112 private boolean init = true;
113 private CheckList collection_checklist = null;
114
115 SuperCollectionControl() {
116 super();
117
118 buildModel();
119 // Creation
120 JPanel header_panel = new JPanel();
121
122 JLabel title_label = new JLabel();
123 title_label.setHorizontalAlignment(JLabel.CENTER);
124 title_label.setOpaque(true);
125 Dictionary.registerText(title_label, "CDM.SuperCollectionManager.Title");
126
127 JTextArea instructions = new JTextArea();
128 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
129 instructions.setCaretPosition(0);
130 instructions.setEditable(false);
131 instructions.setLineWrap(true);
132 instructions.setRows(6);
133 instructions.setWrapStyleWord(true);
134 Dictionary.registerText(instructions, "CDM.SuperCollectionManager.Instructions");
135
136 collection_checklist = new CheckList(collection_checklist_model, false);
137
138 // Layout
139 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
140 header_panel.setLayout(new BorderLayout());
141 header_panel.add(title_label, BorderLayout.NORTH);
142 header_panel.add(new JScrollPane(instructions), BorderLayout.CENTER);
143
144 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
145 setLayout(new BorderLayout());
146 add(header_panel, BorderLayout.NORTH);
147 add(new JScrollPane(collection_checklist), BorderLayout.CENTER);
148 }
149
150 public void destroy() {
151 }
152
153 public void gainFocus() {
154 }
155
156 public void loseFocus() {
157 int super_collections_count = 0;
158 // Retrieve the current supercollections
159 ArrayList supercollections = children();
160 // 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
161 int size = collection_checklist_model.size();
162 for(int i = 0; i < size; i++) {
163 Entry entry = (Entry) collection_checklist_model.get(i);
164 if(entry.isSelected()) {
165 String collection_name = (String) entry.getProperty();
166 SuperCollection supercollection = getSuperCollection(collection_name);
167 // Create the supercollection element if necessary
168 if(supercollection == null) {
169 Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
170 supercollection = new SuperCollection(supercollection_element);
171 supercollection.setName(collection_name);
172 addSuperCollection(supercollection);
173 }
174 else {
175 supercollections.remove(supercollection);
176 }
177 supercollection.setAssigned(true);
178 super_collections_count++;
179 }
180 }
181 // Any collections left in the temporary list have been unselected, so delete them
182 for(int j = supercollections.size(); j > 0; j--) {
183 SuperCollection supercollection = (SuperCollection) supercollections.get(j - 1);
184 removeSuperCollection(supercollection);
185 }
186 // Now we only assign supercollections if more than one is selected
187 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (super_collections_count > 1 ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
188 }
189
190 private void buildModel() {
191 // We start by building a model of the installed collections.
192 collection_checklist_model = new ArrayList();
193 File gsdl_collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl_path));
194 current_coll_name = Gatherer.c_man.getCollection().getName();
195 File[] possible_collections = gsdl_collection_directory.listFiles();
196 for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
197 // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon.
198 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_FILE);
199 if(collect_cfg_file.exists()) {
200 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
201 StringBuffer title_buffer = new StringBuffer(collect_cfg.getName());
202 title_buffer.append(StaticStrings.SPACE_CHARACTER);
203 title_buffer.append(StaticStrings.OPEN_PARENTHESIS_CHARACTER);
204 title_buffer.append(possible_collections[i].getName());
205 title_buffer.append(StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
206 String collection_title = title_buffer.toString();
207 title_buffer = null;
208 String collection_name = possible_collections[i].getName();
209 // We do have to block the model collection.
210 if(!collect_cfg.getName().equals("**title**")) {
211 Entry entry = new Entry(collection_title);
212 entry.setProperty(collection_name);
213 // Check if a collection with this name exists. The current collection is always selected.
214 entry.setSelected(getSuperCollection(collection_name) != null || collection_name.equals(current_coll_name));
215 entry.setFixed(collection_name.equals(current_coll_name));
216 collection_checklist_model.add(entry);
217 entry = null;
218
219 }
220 collection_name = null;
221 collection_title = null;
222 collect_cfg = null;
223 }
224 collect_cfg_file = null;
225 }
226 possible_collections = null;
227 gsdl_collection_directory = null;
228 // Sort the result.
229 Collections.sort(collection_checklist_model);
230 }
231 }
232}
Note: See TracBrowser for help on using the repository browser.