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

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

Removed all occurrences of classes explicitly importing other classes in the same package.

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