source: gli/branches/rtl-gli/src/org/greenstone/gatherer/cdm/SuperCollectionManager.java@ 18368

Last change on this file since 18368 was 14041, checked in by xiao, 17 years ago

Changes made to look for collectionConfig.xml in gs3 mode and collect.cfg in gs2 mode, rather than presumably only for the file collect.cfg.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 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.Gatherer;
37import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
38import org.greenstone.gatherer.collection.CollectionManager;
39import org.greenstone.gatherer.util.CheckList;
40import org.greenstone.gatherer.util.CheckListEntry;
41import org.greenstone.gatherer.util.StaticStrings;
42import org.greenstone.gatherer.util.Utility;
43import org.greenstone.gatherer.gui.DesignPaneHeader;
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 private boolean superCollectionChanged = false;
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 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
105 * @param mode the new mode as an int
106 */
107 public void modeChanged(int mode) {
108
109 }
110
111 private void removeSuperCollection(SuperCollection supercollection) {
112 if(contains(supercollection)) {
113 remove(supercollection);
114 }
115 }
116
117 /** Provides controls for altering the SuperCollection settings. */
118 private class SuperCollectionControl
119 extends JPanel
120 implements Control {
121
122 private boolean init = true;
123 private CheckList collection_checklist = null;
124
125 SuperCollectionControl() {
126 super();
127
128 // Creation
129 JPanel header_panel = new DesignPaneHeader("CDM.GUI.SuperCollection", "xcollectionsearching");
130
131 collection_checklist = new CheckList(false);
132 buildModel();
133 collection_checklist.setListData(collection_checklist_model);
134
135 JPanel collection_checklist_pane = new JPanel();
136 collection_checklist_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
137 collection_checklist_pane.setLayout(new BorderLayout());
138 collection_checklist_pane.add(new JScrollPane(collection_checklist), BorderLayout.CENTER);
139
140 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
141 setLayout(new BorderLayout());
142 add(header_panel, BorderLayout.NORTH);
143 add(collection_checklist_pane, BorderLayout.CENTER);
144 }
145
146 public void destroy() {
147 }
148
149 public void gainFocus() {
150 }
151
152 public void loseFocus() {
153 int super_collections_count = 0;
154 // Retrieve the current supercollections
155 ArrayList supercollections = children();
156 // 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
157 // we ignore the current coll at the moment
158 int size = collection_checklist_model.size();
159 for(int i = 0; i < size; i++) {
160 CheckListEntry entry = (CheckListEntry) collection_checklist_model.get(i);
161 if(entry.isSelected()) {
162 String collection_name = (String) entry.getProperty();
163 if (!collection_name.equals(current_coll_name)) {
164 SuperCollection supercollection = getSuperCollection(collection_name);
165 // Create the supercollection element if necessary
166 if(supercollection == null) {
167 Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
168 supercollection = new SuperCollection(supercollection_element);
169 supercollection.setName(collection_name);
170 addSuperCollection(supercollection);
171 }
172 else {
173 supercollections.remove(supercollection);
174 }
175 supercollection.setAssigned(true);
176 super_collections_count++;
177 }
178 }
179 }
180 if (super_collections_count > 0) {
181 // we have some super colls, add in the current collection
182 SuperCollection supercollection = getSuperCollection(current_coll_name);
183 // Create the supercollection element if necessary
184 if(supercollection == null) {
185 Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
186 supercollection = new SuperCollection(supercollection_element);
187 supercollection.setName(current_coll_name);
188 addSuperCollection(supercollection);
189 }
190 else {
191 supercollections.remove(supercollection);
192 }
193 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
194 } else {
195 // current collection is the only one - don't bother adding it, because assigned is false, and we don't want to update the config file for an unassigned item.
196 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
197 }
198
199 // Any collections left in the temporary list have been unselected, so delete them
200 for(int j = supercollections.size(); j > 0; j--) {
201 SuperCollection supercollection = (SuperCollection) supercollections.get(j - 1);
202 removeSuperCollection(supercollection);
203 }
204 }
205
206
207 private void buildModel()
208 {
209 collection_checklist_model = new ArrayList();
210 current_coll_name = CollectionManager.getLoadedCollectionName();
211
212 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
213 File[] possible_collections = collect_directory.listFiles();
214 for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
215 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
216 File collect_cfg_file = new File(possible_collections[i], file_name);
217 if (collect_cfg_file.exists()) {
218 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
219 StringBuffer title_buffer = new StringBuffer(collect_cfg.getName());
220 title_buffer.append(StaticStrings.SPACE_CHARACTER);
221 title_buffer.append(StaticStrings.OPEN_PARENTHESIS_CHARACTER);
222 title_buffer.append(possible_collections[i].getName());
223 title_buffer.append(StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
224 String collection_title = title_buffer.toString();
225 title_buffer = null;
226 String collection_name = possible_collections[i].getName();
227
228 // We have to block the model collection.
229 if (collect_cfg.getName().equals("**title**")) {
230 continue;
231 }
232
233 // The current collection is always selected.
234 CheckListEntry entry = new CheckListEntry(collection_title);
235 entry.setProperty(collection_name);
236 entry.setSelected(getSuperCollection(collection_name) != null || collection_name.equals(current_coll_name));
237 entry.setFixed(collection_name.equals(current_coll_name));
238 collection_checklist_model.add(entry);
239 }
240 }
241 }
242 }
243}
Note: See TracBrowser for help on using the repository browser.