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

Last change on this file since 19224 was 19224, checked in by kjdon, 15 years ago

added code for collect groups

  • Property svn:keywords set to Author Date Id Revision
File size: 10.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.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.greenstone.gatherer.Dictionary;
45import org.w3c.dom.*;
46
47/** 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. */
48public class SuperCollectionManager
49 extends DOMProxyListModel {
50
51 static final public String SUPERCOLLECTION_COMMAND = "supercollection";
52 static final public String CCS_COMMAND = "ccs";
53 private ArrayList collection_checklist_model = null; // Model used to actually populate list
54 private Control controls = null;
55 private DOMProxyListModel model = null;
56 private String current_coll_name = null;
57 private boolean superCollectionChanged = false;
58
59 public SuperCollectionManager(Element supercollections_element) {
60 super(supercollections_element, StaticStrings.COLLECTION_ELEMENT, new SuperCollection());
61 DebugStream.println("SuperCollectionManager: " + getSize() + " supercollection members parsed.");
62 this.model = this;
63 }
64
65 public void destroy() {
66 if(controls != null) {
67 controls.destroy();
68 controls = null;
69 }
70 if(collection_checklist_model != null) {
71 collection_checklist_model.clear();
72 collection_checklist_model = null;
73 }
74 }
75
76 private void addSuperCollection(SuperCollection supercollection) {
77 if(!contains(supercollection)) {
78 add(getSize(), supercollection);
79 }
80 }
81
82 /** Method to retrieve the control for this manager.
83 * @return the Control for editing supercollection settings
84 */
85 public Control getControls() {
86 if(controls == null) {
87 // Build controls
88 this.controls = new SuperCollectionControl();
89 }
90 return controls;
91 }
92
93 public SuperCollection getSuperCollection(String collection_name) {
94 SuperCollection result = null;
95 int size = getSize();
96 for(int i = 0; result == null && i < size; i++) {
97 SuperCollection supercollection = (SuperCollection) getElementAt(i);
98 if(supercollection.getName().equals(collection_name)) {
99 result = supercollection;
100 }
101 supercollection = null;
102 }
103 return result;
104 }
105 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
106 * @param mode the new mode as an int
107 */
108 public void modeChanged(int mode) {
109
110 }
111
112 private void removeSuperCollection(SuperCollection supercollection) {
113 if(contains(supercollection)) {
114 remove(supercollection);
115 }
116 }
117
118 /** Provides controls for altering the SuperCollection settings. */
119 private class SuperCollectionControl
120 extends JPanel
121 implements Control {
122
123 private boolean init = true;
124 private CheckList collection_checklist = null;
125
126 SuperCollectionControl() {
127 super();
128 this.setComponentOrientation(Dictionary.getOrientation());
129 // Creation
130 JPanel header_panel = new DesignPaneHeader("CDM.GUI.SuperCollection", "xcollectionsearching");
131
132 collection_checklist = new CheckList(false);
133 buildModel();
134 collection_checklist.setListData(collection_checklist_model);
135
136 JPanel collection_checklist_pane = new JPanel();
137 collection_checklist_pane.setComponentOrientation(Dictionary.getOrientation());
138 collection_checklist_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
139 collection_checklist_pane.setLayout(new BorderLayout());
140 collection_checklist_pane.add(new JScrollPane(collection_checklist), BorderLayout.CENTER);
141
142 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
143 setLayout(new BorderLayout());
144 add(header_panel, BorderLayout.NORTH);
145 add(collection_checklist_pane, BorderLayout.CENTER);
146 }
147
148 public void destroy() {
149 }
150
151 public void gainFocus() {
152 }
153
154 public void loseFocus() {
155 int super_collections_count = 0;
156 // Retrieve the current supercollections
157 ArrayList supercollections = children();
158 // 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
159 // we ignore the current coll at the moment
160 int size = collection_checklist_model.size();
161 for(int i = 0; i < size; i++) {
162 CheckListEntry entry = (CheckListEntry) collection_checklist_model.get(i);
163 if(entry.isSelected()) {
164 String collection_name = (String) entry.getProperty();
165 if (!collection_name.equals(current_coll_name)) {
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 }
182 if (super_collections_count > 0) {
183 // we have some super colls, add in the current collection
184 SuperCollection supercollection = getSuperCollection(current_coll_name);
185 // Create the supercollection element if necessary
186 if(supercollection == null) {
187 Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
188 supercollection = new SuperCollection(supercollection_element);
189 supercollection.setName(current_coll_name);
190 addSuperCollection(supercollection);
191 }
192 else {
193 supercollections.remove(supercollection);
194 }
195 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
196 } else {
197 // 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.
198 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
199 }
200
201 // Any collections left in the temporary list have been unselected, so delete them
202 for(int j = supercollections.size(); j > 0; j--) {
203 SuperCollection supercollection = (SuperCollection) supercollections.get(j - 1);
204 removeSuperCollection(supercollection);
205 }
206 }
207
208
209 private void buildModel()
210 {
211 collection_checklist_model = new ArrayList();
212 current_coll_name = CollectionManager.getLoadedGroupQualifiedCollectionName(true);
213 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
214 add_collections_to_model(collect_directory);
215 }
216
217 private void add_collections_to_model(File collect_directory) {
218 File[] possible_collections = collect_directory.listFiles();
219 for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
220 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
221 File collect_cfg_file = new File(possible_collections[i], file_name);
222 if (collect_cfg_file.exists()) {
223 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
224 // look to see if group set?
225 // => if it is, don't add, but recurse to look for child collections in collect-group
226 if (collect_cfg.getCollectGroup().equals("true")) {
227 add_collections_to_model(possible_collections[i]);
228 } else {
229 // its a collection
230 StringBuffer title_buffer = new StringBuffer(collect_cfg.getName());
231 title_buffer.append(StaticStrings.SPACE_CHARACTER);
232 title_buffer.append(StaticStrings.OPEN_PARENTHESIS_CHARACTER);
233 title_buffer.append(collect_cfg.getShortName());
234 title_buffer.append(StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
235 String collection_title = title_buffer.toString();
236 title_buffer = null;
237 String collection_name = collect_cfg.getShortName();
238 // We have to block the model collection.
239 if (collect_cfg.getName().equals("**title**")) {
240 continue;
241 }
242
243 // The current collection is always selected.
244 CheckListEntry entry = new CheckListEntry(collection_title);
245 entry.setProperty(collection_name);
246 entry.setSelected(getSuperCollection(collection_name) != null || collection_name.equals(current_coll_name));
247 entry.setFixed(collection_name.equals(current_coll_name));
248 collection_checklist_model.add(entry);
249 }
250 }
251
252 }
253 }
254 }
255}
Note: See TracBrowser for help on using the repository browser.