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

Last change on this file since 4932 was 4932, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

  • 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.Configuration;
35import org.greenstone.gatherer.Dictionary;
36import org.greenstone.gatherer.Gatherer;
37import org.greenstone.gatherer.cdm.DOMProxyListModel;
38import org.greenstone.gatherer.checklist.CheckList;
39import org.greenstone.gatherer.checklist.Entry;
40import org.greenstone.gatherer.collection.CollectionConfiguration;
41import org.greenstone.gatherer.util.StaticStrings;
42import org.greenstone.gatherer.util.Utility;
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 {
47
48 static final public String SUPERCOLLECTION_COMMAND = "supercollection";
49 static final public String CCS_COMMAND = "ccs";
50
51 private String current_coll_name = 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;
95 }
96 supercollection = null;
97 }
98 return result;
99 }
100
101 public void removeSuperCollection(SuperCollection supercollection) {
102 remove(supercollection);
103 }
104
105 /** Call dictionary get with just a key.
106 * @param key A String which is mapped to a initial String within the ResourceBundle.
107 * @return A String which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
108 */
109 private String get(String key) {
110 if(key.indexOf(".") == -1) {
111 key = "CDM.SuperCollectionManager." + key;
112 }
113 return Gatherer.dictionary.get(key);
114 }
115
116 /** Provides controls for altering the SuperCollection settings. */
117 private class SuperCollectionControl
118 extends JPanel
119 implements Control {
120
121 private boolean init = true;
122 private CheckList collection_checklist = null;
123 //private JSplitPane center_pane;
124
125 SuperCollectionControl() {
126 super();
127
128 buildModel();
129 // Creation
130 JPanel header_panel = new JPanel();
131
132 JLabel title_label = new JLabel(get("Title"));
133 title_label.setHorizontalAlignment(JLabel.CENTER);
134 title_label.setOpaque(true);
135
136 //center_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
137
138 JTextArea instructions = new JTextArea(get("Instructions"));
139 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
140 instructions.setCaretPosition(0);
141 instructions.setEditable(false);
142 instructions.setLineWrap(true);
143 instructions.setRows(8);
144 instructions.setWrapStyleWord(true);
145
146 collection_checklist = new CheckList(collection_checklist_model);
147
148 // Layout
149 //center_pane.setTopComponent(new JScrollPane(instructions));
150 //center_pane.setBottomComponent(new JScrollPane(collection_checklist));
151 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
152 header_panel.setLayout(new BorderLayout());
153 header_panel.add(title_label, BorderLayout.NORTH);
154 header_panel.add(new JScrollPane(instructions), BorderLayout.CENTER);
155
156 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
157 setLayout(new BorderLayout());
158 add(header_panel, BorderLayout.NORTH);
159 add(new JScrollPane(collection_checklist), BorderLayout.CENTER);
160 }
161
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);
232 }
233 }
234}
Note: See TracBrowser for help on using the repository browser.