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

Last change on this file since 36252 was 36252, checked in by kjdon, 23 months ago

adding in add all button string keys

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