source: trunk/gli/src/org/greenstone/gatherer/cdm/CollectionDesignManager.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: 13.5 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.awt.event.*;
31import java.io.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.apache.xerces.parsers.*;
35import org.greenstone.gatherer.Configuration;
36import org.greenstone.gatherer.DebugStream;
37import org.greenstone.gatherer.Dictionary;
38import org.greenstone.gatherer.Gatherer;
39import org.greenstone.gatherer.LocalLibraryServer;
40import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
41import org.greenstone.gatherer.util.Utility;
42import org.greenstone.gatherer.util.ZipTools;
43import org.w3c.dom.*;
44import org.xml.sax.*;
45
46/** This manager provides access to submanagers, which in turn provide tools for the designing of Greenstone collections via the information stored in etc/collect.cfg. This class acts as a hub for the managers that handle specific parts of the configuration such as classifiers, format strings and language settings.
47 * @author John Thompson, Greenstone Digital Library, University of Waikato
48 * @version 2.3d
49 */
50public class CollectionDesignManager {
51 /** This listener listens for any event on any of the components in any of the sub-views, and marks the collection as needing saving if any change occurs. */
52 static public CDMChangeListener change_listener;
53 /** These listeners listen to changes in the Design mode so as to allow incremental building */
54 static public DesignChangeListener all_change_listener;
55 static public DesignChangeListener buildcol_change_listener;
56 static public DesignChangeListener collect_cfg_change_listener;
57 /** A list of classifiers to use at build time. */
58 static public ClassifierManager classifier_manager;
59 /** The CollectionConfiguration object on which this CDM will be based. */
60 static public CollectionConfiguration collect_config;
61 /** A manager of collection level metadata. */
62 static public CollectionMetaManager collectionmeta_manager;
63 /** A list of formating strings to use at build time. */
64 static public FormatManager format_manager;
65 /** The manager in charge of displaying this manager and the controls for other managers. */
66 static public GeneralManager general_manager;
67 /** List of indexes to be built, and the default index. */
68 static public IndexManager index_manager;
69 /** Contains instructions dealing with the collection language. */
70 static public LanguageManager language_manager;
71 /** A simple manager for the visual review of metadata sets. */
72 static public MetadataSetView metadataset_view;
73 /** A list of plugins to use at build time. */
74 static public PluginManager plugin_manager;
75 /** The manager in charge of all aspects of searchtypes. We also ask this manager whether we are MG or MGPP enabled. */
76 static public SearchTypeManager searchtype_manager;
77 /** Contains: A list of subcollections, (defined on metadatadata), a list of which subcollection indexes to build and the default subcollection index. */
78 static public SubcollectionManager subcollection_manager;
79
80 static public SubcollectionIndexManager subcollectionindex_manager;
81 /** A supercollection command allows a single search to be conducted across several collections. It is a very basic command and so avoids all the crazy model stuff that exists in most of the design managers. */
82 static public SuperCollectionManager supercollection_manager; // Just cause I could ;p
83 /** The text translation manager. */
84 static public TranslationView translation_view;
85 /** These mark what needs to happen when building a collection where ONLY design options have been changed.
86 The build requirements of the higher numbers must include doing everything from the lower numbers. */
87 static final public int ALL = 3;
88 static final public int BUILDCOL = 2;
89 static final public int UPDATE_COLLECT_CFG = 1;
90 static final public int NOTHING = 0;
91 static private int rebuildTypeRequired = NOTHING; //Rebuild type required if only design options have changed
92 static public boolean update_collect_cfg_required = false;
93
94 /** Constructor. Loads a certain collection configuration file, which is parsed into a DOM. This model is then registered with the command information managers, each of whom knows how to, and provides controls to, alter certain commands.
95 * @param collect_config_file the File representing a collection configuration file either in its text (G2) or xml (G3) form
96 */
97 public CollectionDesignManager(File collect_config_file) {
98 DebugStream.println("Initializaing CollectionDesignModule.");
99 change_listener = new CDMChangeListener();
100 all_change_listener = new DesignChangeListener(ALL);
101 buildcol_change_listener = new DesignChangeListener(BUILDCOL);
102 collect_cfg_change_listener = new DesignChangeListener(UPDATE_COLLECT_CFG);
103 // Parse the collection configuration
104 collect_config = new CollectionConfiguration(collect_config_file);
105 if (DebugStream.isDebuggingEnabled()) {
106 collect_config.display();
107 }
108 loadDesignDetails();
109 DebugStream.println("CollectionDesignModule loaded.");
110 }
111
112 /** Reloads the various managers to ensure they have built themselves from the latest details available in the collection configuration class
113 * @see org.greenstone.gatherer.cdm.ClassifierManager
114 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
115 * @see org.greenstone.gatherer.cdm.FormatManager
116 * @see org.greenstone.gatherer.cdm.GeneralManager
117 * @see org.greenstone.gatherer.cdm.IndexManager
118 * @see org.greenstone.gatherer.cdm.LanguageManager
119 * @see org.greenstone.gatherer.cdm.MetadataSetView
120 * @see org.greenstone.gatherer.cdm.PluginManager
121 * @see org.greenstone.gatherer.cdm.SearchTypeManager
122 * @see org.greenstone.gatherer.cdm.SubcollectionIndexManager
123 * @see org.greenstone.gatherer.cdm.SubcollectionManager
124 * @see org.greenstone.gatherer.cdm.SuperCollectionManager
125 * @see org.greenstone.gatherer.cdm.TranslationView
126 */
127 private void loadDesignDetails() {
128 // Create the command information managers, registering the config file with each as necessary
129 language_manager = new LanguageManager(collect_config.getLanguages());
130 collectionmeta_manager = new CollectionMetaManager();
131 classifier_manager = new ClassifierManager();
132 general_manager = new GeneralManager();
133
134 searchtype_manager = new SearchTypeManager(collect_config.getSearchType());
135 if(searchtype_manager.isSearchTypeEnabled()) {
136 // note this this could be lucene now, the format is the same as for mgpp
137 index_manager = new IndexManager(collect_config.getMGPPIndexes());
138 }
139 else {
140 index_manager = new IndexManager(collect_config.getMGIndexes());
141 }
142
143 metadataset_view = new MetadataSetView();
144 plugin_manager = new PluginManager();
145 plugin_manager.placeSeparator();
146 subcollection_manager = new SubcollectionManager();
147 subcollectionindex_manager = new SubcollectionIndexManager(collect_config.getSubIndexes());
148 supercollection_manager = new SuperCollectionManager(collect_config.getSuperCollection());
149 translation_view = new TranslationView();
150 format_manager = new FormatManager(); // Parse formats at the very end, given that they depend upon several other managers to appear properly.
151 }
152
153 /** This method deconstructs each of the managers, causing them to dispose of their controls.
154 */
155 public void destroy() {
156 // Remove visual the component from its parent.
157 if(general_manager.getParent() != null) {
158 general_manager.getParent().remove(general_manager);
159 }
160 // Remove references from persistant listeners.
161 classifier_manager.destroy();
162 classifier_manager = null;
163 format_manager.destroy();
164 format_manager = null;
165 general_manager.destroy();
166 general_manager = null;
167 index_manager.destroy();
168 index_manager = null;
169 language_manager.destroy();
170 language_manager = null;
171 metadataset_view.destroy();
172 metadataset_view = null;
173 plugin_manager.destroy();
174 plugin_manager = null;
175 subcollection_manager.destroy();
176 subcollection_manager = null;
177 supercollection_manager.destroy();
178 supercollection_manager = null;
179 translation_view.destroy();
180 translation_view = null;
181 }
182
183 /** Display the GUI interface for the CollectionDesignManager in the centre of the indicated panel.
184 * @param target the JPanel you wish to display the gui on
185 */
186 public void display(JPanel target) {
187 target.add(general_manager, BorderLayout.CENTER);
188 }
189 /** When the tab on the JTabbedPane that contains the GUI is selected, this method is called to ensure that the controls are all up to date, in terms of references to metadata etc.
190 */
191 public void gainFocus() {
192 general_manager.gainFocus();
193 }
194
195 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
196 * @param mode the new mode as an int
197 */
198 public void modeChanged(int mode) {
199 plugin_manager.modeChanged(mode);
200 subcollection_manager.modeChanged(mode);
201 }
202
203 /** The cdm is considered to be ready if the collect.cfg file was found and parsed and the collection title is not error.
204 * @return true if the collection is ready, false otherwise
205 */
206 public boolean ready() {
207 return collect_config.ready();
208 }
209
210 /** Ensure that saving will actually work. This can return false if there is a collection filename clash for instance. */
211 public boolean canSave() {
212 return (general_manager == null || general_manager.canSave());
213 }
214
215
216 public void save() {
217 save(false);
218 }
219 /** Cause the current collection configuration to be written out to disk.
220 */
221 public void save(boolean force_save)
222 {
223
224 general_manager.loseFocus();
225 if (!update_collect_cfg_required && !force_save) {
226 DebugStream.println("Asked to save the config file, but don't need to");
227 return;
228 }
229
230 DebugStream.println("Saving the collection config file");
231
232 // Release collection as necessary
233 String collection_name = Gatherer.c_man.getCollection().getName();
234 boolean collection_released = false;
235
236 if (Gatherer.c_man.built() && LocalLibraryServer.isRunning() == true) {
237 // Release the collection
238 LocalLibraryServer.releaseCollection(collection_name);
239 collection_released = true;
240 }
241
242 collect_config.save();
243
244 // Read collection
245 if (collection_released) {
246 // Now re-add collection to server to force format commands to be processed
247 LocalLibraryServer.addCollection(collection_name);
248 }
249
250 // Unset formats changed
251 update_collect_cfg_required = false;
252
253 }
254
255
256 static public Document XMLStringToDOM(StringBuffer xml, String form)
257 {
258 Document document = null;
259
260 // If something has gone horribly wrong then xml will be empty.
261 if(xml.length() > 0) {
262 try {
263 // Then read the xml from the piped input stream.
264 StringReader xml_sr = new StringReader(xml.toString());
265 InputSource source = new InputSource(xml_sr);
266 DOMParser parser = new DOMParser();
267 parser.parse(source);
268 document = parser.getDocument();
269 }
270 catch (Exception error) {
271 System.err.println("Failed when trying to parse XML stream ");
272 error.printStackTrace();
273 }
274 }
275 else {
276 //DebugStream.println("Zero length argument xml detected for: " + form);
277 String[] margs = new String[1];
278 margs[0] = form;
279 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", margs), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); // !! TO DO: This is bad -- this function is used for more than just plugins
280 }
281
282 return document;
283 }
284
285 public static int getRebuildTypeRequired() {
286 return rebuildTypeRequired;
287 }
288 public static void resetRebuildTypeRequired() {
289 setRebuildTypeRequired(NOTHING);
290 }
291 public static void setRebuildTypeRequired(int number) {
292 rebuildTypeRequired = number;
293 }
294
295 /**
296 * What exactly does this do?
297 */
298 private class CDMChangeListener
299 implements ActionListener, DocumentListener {
300
301 /** Gives notification that an event has happened */
302 public void actionPerformed(ActionEvent event) {
303 Gatherer.c_man.getCollection().setSaved(false);
304 }
305
306 /** Gives notification that an attribute or set of attributes changed. */
307 public void changedUpdate(DocumentEvent e) {
308 Gatherer.c_man.getCollection().setSaved(false);
309 }
310
311 /** Gives notification that there was an insert into the document. */
312 public void insertUpdate(DocumentEvent e) {
313 Gatherer.c_man.getCollection().setSaved(false);
314 }
315
316 /** Gives notification that a portion of the document has been removed. */
317 public void removeUpdate(DocumentEvent e) {
318 Gatherer.c_man.getCollection().setSaved(false);
319 }
320 }
321}
Note: See TracBrowser for help on using the repository browser.