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

Last change on this file since 10556 was 10556, checked in by kjdon, 19 years ago

fixes to make the config file save only if it has changed

  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 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.collection.BasicCollectionConfiguration;
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.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 private boolean superCollectionChanged = false;
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 Gatherer.c_man.configurationChanged();
78 }
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
106 private void removeSuperCollection(SuperCollection supercollection) {
107 if(contains(supercollection)) {
108 remove(supercollection);
109 Gatherer.c_man.configurationChanged();
110 }
111 }
112
113 /** Provides controls for altering the SuperCollection settings. */
114 private class SuperCollectionControl
115 extends JPanel
116 implements Control {
117
118 private boolean init = true;
119 private CheckList collection_checklist = null;
120
121 SuperCollectionControl() {
122 super();
123
124 // Creation
125 JPanel header_panel = new JPanel();
126
127 JLabel title_label = new JLabel();
128 title_label.setHorizontalAlignment(JLabel.CENTER);
129 title_label.setOpaque(true);
130 Dictionary.registerText(title_label, "CDM.SuperCollectionManager.Title");
131
132 JTextArea instructions = new JTextArea();
133 instructions.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
134 instructions.setCaretPosition(0);
135 instructions.setEditable(false);
136 instructions.setLineWrap(true);
137 instructions.setRows(6);
138 instructions.setWrapStyleWord(true);
139 Dictionary.registerText(instructions, "CDM.SuperCollectionManager.Instructions");
140
141 collection_checklist = new CheckList(false);
142 buildModel();
143 collection_checklist.setListData(collection_checklist_model);
144
145 // Layout
146 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
147 header_panel.setLayout(new BorderLayout());
148 header_panel.add(title_label, BorderLayout.NORTH);
149 header_panel.add(new JScrollPane(instructions), BorderLayout.CENTER);
150
151 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
152 setLayout(new BorderLayout());
153 add(header_panel, BorderLayout.NORTH);
154 add(new JScrollPane(collection_checklist), BorderLayout.CENTER);
155 }
156
157 public void destroy() {
158 }
159
160 public void gainFocus() {
161 }
162
163 public void loseFocus() {
164 CollectionDesignManager.collect_cfg_change_listener.maybeSetRebuildRequired(); // !! TO DO: This is crap
165 int super_collections_count = 0;
166 // Retrieve the current supercollections
167 ArrayList supercollections = children();
168 // 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
169 // we ignore the current coll at the moment
170 int size = collection_checklist_model.size();
171 for(int i = 0; i < size; i++) {
172 CheckListEntry entry = (CheckListEntry) collection_checklist_model.get(i);
173 if(entry.isSelected()) {
174 String collection_name = (String) entry.getProperty();
175 if (!collection_name.equals(current_coll_name)) {
176 SuperCollection supercollection = getSuperCollection(collection_name);
177 // Create the supercollection element if necessary
178 if(supercollection == null) {
179 Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
180 supercollection = new SuperCollection(supercollection_element);
181 supercollection.setName(collection_name);
182 addSuperCollection(supercollection);
183 }
184 else {
185 supercollections.remove(supercollection);
186 }
187 supercollection.setAssigned(true);
188 super_collections_count++;
189 }
190 }
191 }
192 if (super_collections_count > 0) {
193 // we have some super colls, add in the current collection
194 SuperCollection supercollection = getSuperCollection(current_coll_name);
195 // Create the supercollection element if necessary
196 if(supercollection == null) {
197 Element supercollection_element = root.getOwnerDocument().createElement(StaticStrings.COLLECTION_ELEMENT);
198 supercollection = new SuperCollection(supercollection_element);
199 supercollection.setName(current_coll_name);
200 addSuperCollection(supercollection);
201 }
202 else {
203 supercollections.remove(supercollection);
204 }
205 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
206 } else {
207 // 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.
208 model.root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
209 }
210
211 // Any collections left in the temporary list have been unselected, so delete them
212 for(int j = supercollections.size(); j > 0; j--) {
213 SuperCollection supercollection = (SuperCollection) supercollections.get(j - 1);
214 removeSuperCollection(supercollection);
215 }
216 }
217
218
219 private void buildModel()
220 {
221 collection_checklist_model = new ArrayList();
222 current_coll_name = Gatherer.c_man.getCollection().getName();
223
224 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
225 File[] possible_collections = collect_directory.listFiles();
226 for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
227 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_FILE);
228 if (collect_cfg_file.exists()) {
229 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
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(possible_collections[i].getName());
234 title_buffer.append(StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
235 String collection_title = title_buffer.toString();
236 title_buffer = null;
237 String collection_name = possible_collections[i].getName();
238
239 // We have to block the model collection.
240 if (collect_cfg.getName().equals("**title**")) {
241 continue;
242 }
243
244 // The current collection is always selected.
245 CheckListEntry entry = new CheckListEntry(collection_title);
246 entry.setProperty(collection_name);
247 entry.setSelected(getSuperCollection(collection_name) != null || collection_name.equals(current_coll_name));
248 entry.setFixed(collection_name.equals(current_coll_name));
249 collection_checklist_model.add(entry);
250 }
251 }
252 }
253 }
254}
Note: See TracBrowser for help on using the repository browser.