source: main/trunk/gli/src/org/greenstone/gatherer/cdm/SearchIndexManager.java@ 36164

Last change on this file since 36164 was 36164, checked in by kjdon, 2 years ago

added default_indicator_key, changed the order of the buttons

  • Property svn:keywords set to Author Date Id Revision
File size: 21.0 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.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.gui.DesignPaneHeader;
39import org.greenstone.gatherer.gui.GComboBox;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.gui.ModalDialog;
42import org.greenstone.gatherer.gui.SimpleMenuBar;
43import org.greenstone.gatherer.metadata.MetadataElement;
44import org.greenstone.gatherer.metadata.MetadataSetManager;
45import org.greenstone.gatherer.util.CheckList;
46import org.greenstone.gatherer.util.JarTools;
47import org.greenstone.gatherer.util.XMLTools;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.w3c.dom.*;
50/** This class is resposible for storing the indexes which have been assigned to this collection and the default index, and providing methods for interacting with both these data pools. It also knows how to turn itself into a String as it would be displayed in the collection configuration file.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class SearchIndexManager extends BaseIndexManager {
55
56 static final private Dimension FIELD_SIZE = new Dimension(200,30);
57 static final private String ALLFIELDS = "allfields";
58
59 public SearchIndexManager(Element indexes, String current_build_type) {
60
61 super(indexes, current_build_type, StaticStrings.INDEX_ELEMENT, StaticStrings.INDEX_DEFAULT_ELEMENT, (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)?new MGIndex():new Index()));
62
63 this.controls_title_key = "CDM.IndexManager.Indexes";
64 this.new_button_tooltip_key = "CDM.IndexManager.New_Button_Tooltip";
65 this.edit_button_tooltip_key = "CDM.IndexManager.Edit_Button_Tooltip";
66 this.remove_button_tooltip_key = "CDM.IndexManager.Remove_Button_Tooltip";
67 this.default_indicator_key = "CDM.IndexManager.Default_Index_Indicator";
68 this.nip_new_index_key = "CDM.IndexManager.New_Index";
69 this.nip_edit_index_key = "CDM.IndexManager.Edit_Index";
70 this.nip_source_label_key = "CDM.IndexManager.Source";
71 this.nip_add_index_button_key = "CDM.IndexManager.Add_Index";
72 this.nip_add_index_tooltip_key = "CDM.IndexManager.Add_Index_Tooltip";
73 this.nip_replace_index_button_key = "CDM.IndexManager.Replace_Index";
74 this.nip_replace_index_tooltip_key = "CDM.IndexManager.Replace_Index_Tooltip";
75
76 }
77
78
79 public void buildTypeChanged(String new_build_type) {
80 if (build_type.equals(new_build_type)) {
81 return;
82 }
83 // we don;t care about this if old or new is not MG as MGPP and
84 // Lucene have the same index specification
85 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
86 return;
87 }
88 boolean mg_to_mgpp = true;
89 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
90 mg_to_mgpp = false;
91 }
92 if (mg_to_mgpp) {
93 changeToMGPPIndexes();
94 } else {
95 changeToMGIndexes();
96 }
97 build_type = new_build_type;
98 // its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
99 Index first_index = (Index) getElementAt(0);
100 setDefault(first_index);
101 first_index = null;
102 }
103
104 private void changeToMGIndexes() {
105 this.setClassType(new MGIndex());
106 Element mgpp_element = root;
107 // Retrieve and assign MG element and default index element
108 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
109 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
110 NodeList indexes = mg_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
111 // Replace mgpp element with mg element
112 setRoot(mg_element);
113
114 if(indexes.getLength() == 0) {
115 // we need to create some based on the mgpp indexes
116
117 // If the current mgpp index includes a text one, then generate text indexes for each of the registered levels.
118 Index index = getIndex(StaticStrings.TEXT_STR);
119 if(index != null) {
120 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
121 int level_size = levels.size();
122 for(int i = 0; i < level_size; i++) {
123 IndexOption level = (IndexOption) levels.get(i);
124 Index new_index = new MGIndex(level.getName(), index.getSources());
125 // Try to retrieve existing metadatum
126 String source_str = new_index.getID();
127 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
128 // If no metadata was found, add new pseudo metadata using the id
129 if(metadatum == null) {
130 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
131 metadatum.setAssigned(true);
132 metadatum.setValue(source_str);
133 }
134 // If it was found, ensure it is assigned
135 else {
136 metadatum.setAssigned(true);
137 }
138 source_str = null;
139 addIndex(new_index, metadatum);
140 new_index = null;
141 level = null;
142 }
143 }
144 }
145
146 // Unassign mgpp element
147 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
148 mgpp_element = null;
149
150 }
151
152 private void changeToMGPPIndexes() {
153 this.setClassType(new Index());
154 Element mg_element = root;
155 // Retrieve and assign the MGPP indexes element.
156 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
157 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
158 NodeList indexes = mgpp_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
159 if(indexes.getLength() != 0) {
160 // we just reinstate the indexes we had before the change
161 setRoot(mgpp_element);
162 } else {
163 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
164
165 ArrayList sources_list = new ArrayList();
166
167 // We first use details from the default index if any
168 if(default_index != null) {
169 ArrayList sources = default_index.getSources();
170 sources_list.addAll(sources);
171 }
172 int size = getSize();
173 for(int i = 0; i < size; i++) {
174 Index index = (Index) getElementAt(i);
175 ArrayList sources = index.getSources();
176 sources.removeAll(sources_list);
177 sources_list.addAll(sources);
178 index = null;
179 }
180 // Replace mg element with mgpp element
181 setRoot(mgpp_element);
182
183 // We now have a list of sources, so create new indexes based on these
184 int sources_list_size = sources_list.size();
185 for(int j = 0; j < sources_list_size; j++) {
186 Object source_object = sources_list.get(j);
187 String source_str = null;
188 if(source_object instanceof MetadataElement) {
189 source_str = ((MetadataElement) source_object).getFullName();
190 }
191 else {
192 source_str = source_object.toString();
193 }
194 ArrayList new_sources = new ArrayList();
195 new_sources.add(source_object);
196 source_object = null;
197 Index new_index = new Index(new_sources);
198 // Try to retrieve existing metadatum
199 source_str = new_index.getID();
200 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
201 // If no metadata was found, add new pseudo metadata using the id
202 if(metadatum == null) {
203 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
204 metadatum.setAssigned(true);
205 metadatum.setValue(source_str);
206 }
207 // If it was found, ensure it is assigned
208 else {
209 metadatum.setAssigned(true);
210 }
211 source_str = null;
212 addIndex(new_index, metadatum);
213 metadatum = null;
214 new_index = null;
215 new_sources = null;
216 source_str = null;
217 }
218
219 }
220
221 // Unassign MG element
222 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
223 mg_element = null;
224
225 }
226
227
228
229 public Control getControls() {
230 if (controls == null) {
231 controls = new SearchIndexControl();
232 }
233 return controls;
234 }
235
236
237
238
239 /** Method to set the default index.
240 * @param index the new default Index
241 * @see org.greenstone.gatherer.Gatherer
242 * @see org.greenstone.gatherer.collection.CollectionManager
243 */
244 public void setDefault(Index index) {
245 if(index != null) {
246 if(default_index == null) {
247 // Create the default index element, and place immediately after indexes element.
248 Element default_index_element = root.getOwnerDocument().createElement(StaticStrings.INDEX_DEFAULT_ELEMENT);
249 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
250 default_index = new MGIndex(default_index_element);
251 } else {
252 default_index = new Index(default_index_element);
253 }
254 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
255 if(target_node != null) {
256 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
257 }
258 else {
259 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
260 }
261 } else {
262 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG) &&! (default_index instanceof org.greenstone.gatherer.cdm.MGIndex)) {
263 default_index = new MGIndex(default_index.getElement());
264 } else if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && default_index instanceof org.greenstone.gatherer.cdm.MGIndex) {
265 default_index = new Index(default_index.getElement());
266 }
267 }
268 default_index.setAssigned(true);
269 default_index.setSources(index.getSources());
270 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
271 ((MGIndex)default_index).setLevel(((MGIndex)index).getLevel());
272 }
273
274 }
275 else {
276 if(default_index != null) {
277 default_index.setAssigned(false);
278 }
279 }
280 }
281
282
283 private class SearchIndexControl
284 extends IndexControl {
285
286 public SearchIndexControl() {
287 super();
288 }
289
290
291 /** we want our own custom new index prompt for searhc indexes */
292 protected NewIndexPrompt createNewIndexPrompt(String build_type, Index index) {
293 return new NewSearchIndexPrompt(build_type, index);
294
295 }
296
297 /** we customise this to add text box, allfields, and add all, select all buttons etc */
298 protected class NewSearchIndexPrompt
299 extends NewIndexPrompt {
300
301 private JCheckBox text_checkbox;
302 // mg uses a level box
303 private JComboBox level_combobox;
304 // mgpp has a allfields selector
305 private JCheckBox allfields_box;
306
307 private JButton select_all_button;
308 private JButton select_none_button;
309 private JButton add_all_button;
310
311 private boolean mgpp_enabled = false;
312 private boolean editing = false;
313
314 public NewSearchIndexPrompt(String build_type, Index existing_index) {
315 super(build_type, existing_index);
316 }
317
318 /** inside here is where we customise our controls */
319 protected void generateContents(String build_type, Index existing_index) {
320 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
321 mgpp_enabled = false;
322 } else {
323 mgpp_enabled = true;
324 }
325 super.generateContents(build_type, existing_index);
326 text_checkbox = new JCheckBox(Dictionary.get("CDM.IndexManager.Text_Source"));
327 text_checkbox.setToolTipText(Dictionary.get("CDM.IndexManager.Text_Source_Tooltip"));
328 text_checkbox.setComponentOrientation(Dictionary.getOrientation());
329 text_checkbox.addActionListener(new ActionListener() {
330 public void actionPerformed(ActionEvent event) {
331 validateAddOrReplaceButton();
332 }
333 });
334 if (existing_index == null && mgpp_enabled) {
335 button_pane.setLayout(new GridLayout(2,3,5,0));
336 JPanel tmp = new JPanel();
337 tmp.setComponentOrientation(Dictionary.getOrientation());
338 button_pane.add(tmp);
339 } else {
340 button_pane.setLayout(new GridLayout(2,2,5,0));
341 }
342
343 select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
344 select_all_button.addActionListener(new ActionListener() {
345
346 public void actionPerformed(ActionEvent event) {
347 text_checkbox.setSelected(true);
348 source_list.setAllTicked();
349 validateAddOrReplaceButton();
350 }
351 });
352
353 select_none_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_None"), Dictionary.get("CDM.IndexManager.Select_None_Tooltip"));
354 select_none_button.addActionListener(new ActionListener() {
355
356 public void actionPerformed(ActionEvent event) {
357 text_checkbox.setSelected(false);
358 source_list.clearTicked();
359 validateAddOrReplaceButton();
360 }
361 });
362
363
364 button_pane.add(select_all_button);
365 button_pane.add(select_none_button);
366
367 if (existing_index == null && mgpp_enabled) {
368 add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
369 add_all_button.setEnabled(true);
370 add_all_button.addActionListener(new AddAllIndexActionListener());
371 button_pane.add(add_all_button);
372 }
373
374 button_pane.add(add_or_replace_button);
375 button_pane.add(cancel_button);
376 details_pane.add(text_checkbox, BorderLayout.NORTH);
377 // do type specific stuff
378 if (mgpp_enabled) {
379 // allfields
380 allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
381 allfields_box.addItemListener(new AllFieldsBoxListener());
382 allfields_box.setComponentOrientation(Dictionary.getOrientation());
383 //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
384 details_pane.add(allfields_box, BorderLayout.SOUTH);
385
386
387 } else {
388 // index level
389 JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
390 level_label.setComponentOrientation(Dictionary.getOrientation());
391
392 level_combobox = new JComboBox();
393 level_combobox.setOpaque(false);
394 level_combobox.setPreferredSize(FIELD_SIZE);
395 // Note the order of these must be the same as the
396 // level order in Index
397 level_combobox.addItem(StaticStrings.DOCUMENT_STR);//Dictionary.get("CDM.LevelManager.Document"));
398 level_combobox.addItem(StaticStrings.SECTION_STR);//Dictionary.get("CDM.LevelManager.Section"));
399 level_combobox.addItem(StaticStrings.PARAGRAPH_STR);//Dictionary.get("CDM.LevelManager.Paragraph"));
400 level_combobox.setEditable(false);
401 level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
402 level_combobox.setComponentOrientation(Dictionary.getOrientation());
403 level_combobox.addActionListener(new ActionListener() {
404 public void actionPerformed(ActionEvent event) {
405 validateAddOrReplaceButton();
406 }
407 });
408 JPanel level_pane = new JPanel();
409 level_pane.setComponentOrientation(Dictionary.getOrientation());
410 level_pane.setLayout(new BorderLayout());
411 level_pane.add(level_label, BorderLayout.LINE_START);
412 level_pane.add(level_combobox, BorderLayout.CENTER);
413 details_pane.add(level_pane, BorderLayout.SOUTH);
414
415 }
416 // if we are editing, fill in the controls
417 if (existing_index !=null) {
418 ArrayList sources = existing_index.getSources();
419 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
420 allfields_box.setSelected(true);
421 source_list.setEnabled(false);
422 } else {
423 source_list.setTickedObjects(sources.toArray());
424 source_list.setEnabled(true);
425 if (sources.contains(StaticStrings.TEXT_STR)) {
426 text_checkbox.setSelected(true);
427 }
428 }
429 if (!mgpp_enabled && existing_index instanceof MGIndex) {
430 level_combobox.setSelectedIndex(((MGIndex)existing_index).getLevel());
431 }
432
433 }
434
435 }
436 // Checks that specified index not already in the collection
437 protected void validateAddOrReplaceButton() {
438 Index index;
439 ArrayList sources;
440 if (mgpp_enabled && allfields_box.isSelected()) {
441 sources = new ArrayList();
442 sources.add(ALLFIELDS);
443 index = new Index(sources);
444
445 } else if (text_checkbox.isSelected() ||
446 !source_list.isNothingTicked()) {
447 sources = source_list.getTicked();
448 if (text_checkbox.isSelected()) {
449 sources.add(0, StaticStrings.TEXT_STR);
450 }
451 if (mgpp_enabled) {
452 index = new Index(sources);
453 } else {
454 index = new MGIndex(level_combobox.getSelectedIndex(), sources);
455 }
456 } else {
457 // nothing selected
458 add_or_replace_button.setEnabled(false);
459 return;
460 }
461
462 sources = null;
463 if (index_model.contains(index)) {
464 add_or_replace_button.setEnabled(false);
465 }
466 else {
467 add_or_replace_button.setEnabled(true);
468 }
469
470 }
471
472 protected Index generateNewIndex() {
473 Index index = null;
474 ArrayList sources;
475 if (mgpp_enabled && allfields_box.isSelected()) {
476 sources = new ArrayList();
477 sources.add(ALLFIELDS);
478 index = new Index(sources);
479 }
480 else if (text_checkbox.isSelected() || !source_list.isNothingTicked()) {
481 sources = source_list.getTicked();
482 if (text_checkbox.isSelected()) {
483 sources.add(0, StaticStrings.TEXT_STR);
484 }
485 if(mgpp_enabled) {
486 index = new Index(sources);
487 } else {
488 index = new MGIndex(level_combobox.getSelectedIndex(), sources);
489 }
490 }
491 return index;
492 }
493
494
495 /** add all sources as separate indexes (fields). */
496 private class AddAllIndexActionListener
497 implements ActionListener {
498
499 public void actionPerformed(ActionEvent event) {
500 ArrayList all_sources = source_list.getAll();
501 all_sources.add(0, StaticStrings.TEXT_STR);
502 ArrayList new_sources = new ArrayList();
503 for(int i = 0; i < all_sources.size(); i++) {
504 Object source = all_sources.get(i);
505
506 // Create new index
507 new_sources.clear();
508 new_sources.add(source);
509 Index index = new Index(new_sources);
510 if(!index_model.contains(index)) {
511 // Determine the metadatum value
512 String name = source.toString();
513 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
514 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
515 }
516 // Create new metadatum
517 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
518 metadatum.setValue(name);
519 name = null;
520 // Assign new index
521 addIndex(index, metadatum);
522 }
523 source = null;
524 index = null;
525 }
526 new_sources = null;
527 new_index_prompt.dispose();
528
529 }
530 }
531
532 private class AllFieldsBoxListener
533 implements ItemListener {
534
535 public void itemStateChanged(ItemEvent event) {
536 if (event.getStateChange() == ItemEvent.SELECTED) {
537 source_list.setEnabled(false);
538 text_checkbox.setEnabled(false);
539 } else if (event.getStateChange() == ItemEvent.DESELECTED) {
540 source_list.setEnabled(true);
541 text_checkbox.setEnabled(true);
542 }
543 validateAddOrReplaceButton();
544 }
545
546 }
547
548
549 } // NewSearchIndexPrompt
550 }// SearchIndexControl
551} // SearchIndexManager
552
Note: See TracBrowser for help on using the repository browser.