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

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

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