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

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

updated for new class name

  • Property svn:keywords set to Author Date Id Revision
File size: 39.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.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
55 extends DOMProxyListModel
56 implements BuildTypeManager.BuildTypeListener {
57
58 /** A reference to ourselves so our inner methods have access. */
59 private DOMProxyListModel index_model = null;
60 /** The default index. */
61 private Index default_index = null;
62
63 private Control controls = null;
64 private String build_type = null;
65
66 static final private Dimension FIELD_SIZE = new Dimension(200,30);
67 static final private String ALLFIELDS = "allfields";
68
69 static final private Dimension PROMPT_SIZE = new Dimension(400,400);
70 public SearchIndexManager(Element indexes, String current_build_type) {
71
72 super(indexes, StaticStrings.INDEX_ELEMENT, (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)?new MGIndex():new Index()));
73 DebugStream.println("SearchIndexManager: " + getSize() + " indexes parsed.");
74 index_model = this;
75
76 // Parse and retrieve the default index
77 NodeList default_index_elements = CollectionConfiguration.getElementsByTagName(StaticStrings.INDEX_DEFAULT_ELEMENT);
78 if(default_index_elements.getLength() > 0) {
79 if (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
80 default_index = new MGIndex((Element)default_index_elements.item(0));
81 } else {
82 default_index = new Index((Element)default_index_elements.item(0));
83 }
84 }
85 build_type = current_build_type;
86 }
87
88 /** Method to add a new index.
89 * @param index The <strong>Index</strong> to add.
90 * @see org.greenstone.gatherer.Gatherer
91 * @see org.greenstone.gatherer.collection.CollectionManager
92 */
93 private void addIndex(Index index, CollectionMeta metadatum) {
94 ///ystem.err.println("Adding an index: " + index.toString());
95 if(!contains(index)) {
96 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
97 // Retrieve the currently last index
98 if(getSize() > 0) {
99 Index last_index = (Index)getElementAt(getSize() - 1);
100 addAfter(index, last_index);
101
102 }
103 else {
104 add(index);
105 // Also set this index as the default one,
106 setDefault(index);
107 }
108 }
109 else {
110 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
111 }
112 }
113
114 public void buildTypeChanged(String new_build_type) {
115 if (build_type.equals(new_build_type)) {
116 return;
117 }
118 // we don;t care about this if old or new is not MG as MGPP and
119 // Lucene have the same index specification
120 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
121 return;
122 }
123 boolean mg_to_mgpp = true;
124 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
125 mg_to_mgpp = false;
126 }
127 if (mg_to_mgpp) {
128 changeToMGPPIndexes();
129 } else {
130 changeToMGIndexes();
131 }
132 build_type = new_build_type;
133 // its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
134 Index first_index = (Index) getElementAt(0);
135 setDefault(first_index);
136 first_index = null;
137 }
138
139 private void changeToMGIndexes() {
140 this.setClassType(new MGIndex());
141 Element mgpp_element = root;
142 // Retrieve and assign MG element and default index element
143 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
144 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
145 NodeList indexes = mg_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
146 // Replace mgpp element with mg element
147 setRoot(mg_element);
148
149 if(indexes.getLength() == 0) {
150 // we need to create some based on the mgpp indexes
151
152 // If the current mgpp index includes a text one, then generate text indexes for each of the registered levels.
153 Index index = getIndex(StaticStrings.TEXT_STR);
154 if(index != null) {
155 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
156 int level_size = levels.size();
157 for(int i = 0; i < level_size; i++) {
158 IndexOption level = (IndexOption) levels.get(i);
159 Index new_index = new MGIndex(level.getName(), index.getSources());
160 // Try to retrieve existing metadatum
161 String source_str = new_index.getID();
162 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
163 // If no metadata was found, add new pseudo metadata using the id
164 if(metadatum == null) {
165 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
166 metadatum.setAssigned(true);
167 metadatum.setValue(source_str);
168 }
169 // If it was found, ensure it is assigned
170 else {
171 metadatum.setAssigned(true);
172 }
173 source_str = null;
174 addIndex(new_index, metadatum);
175 new_index = null;
176 level = null;
177 }
178 }
179 }
180
181 // Unassign mgpp element
182 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
183 mgpp_element = null;
184
185 }
186
187 private void changeToMGPPIndexes() {
188 this.setClassType(new Index());
189 Element mg_element = root;
190 // Retrieve and assign the MGPP indexes element.
191 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
192 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
193 NodeList indexes = mgpp_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
194 if(indexes.getLength() != 0) {
195 // we just reinstate the indexes we had before the change
196 setRoot(mgpp_element);
197 } else {
198 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
199
200 ArrayList sources_list = new ArrayList();
201
202 // We first use details from the default index if any
203 if(default_index != null) {
204 ArrayList sources = default_index.getSources();
205 sources_list.addAll(sources);
206 }
207 int size = getSize();
208 for(int i = 0; i < size; i++) {
209 Index index = (Index) getElementAt(i);
210 ArrayList sources = index.getSources();
211 sources.removeAll(sources_list);
212 sources_list.addAll(sources);
213 index = null;
214 }
215 // Replace mg element with mgpp element
216 setRoot(mgpp_element);
217
218 // We now have a list of sources, so create new indexes based on these
219 int sources_list_size = sources_list.size();
220 for(int j = 0; j < sources_list_size; j++) {
221 Object source_object = sources_list.get(j);
222 String source_str = null;
223 if(source_object instanceof MetadataElement) {
224 source_str = ((MetadataElement) source_object).getFullName();
225 }
226 else {
227 source_str = source_object.toString();
228 }
229 ArrayList new_sources = new ArrayList();
230 new_sources.add(source_object);
231 source_object = null;
232 Index new_index = new Index(new_sources);
233 // Try to retrieve existing metadatum
234 source_str = new_index.getID();
235 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
236 // If no metadata was found, add new pseudo metadata using the id
237 if(metadatum == null) {
238 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
239 metadatum.setAssigned(true);
240 metadatum.setValue(source_str);
241 }
242 // If it was found, ensure it is assigned
243 else {
244 metadatum.setAssigned(true);
245 }
246 source_str = null;
247 addIndex(new_index, metadatum);
248 metadatum = null;
249 new_index = null;
250 new_sources = null;
251 source_str = null;
252 }
253
254 }
255
256 // Unassign MG element
257 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
258 mg_element = null;
259
260 }
261
262 public Control getControls() {
263 if (controls == null) {
264 controls = new IndexControl();
265 }
266 return controls;
267 }
268
269 /** Method to retrieve a certain index, as referenced by an index number.
270 * @param index An <i>int</i> which indicates the position of the desired index.
271 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
272 */
273 public Index getIndex(int index) {
274 if(0 <= index && index < getSize()) {
275 return (Index)getElementAt(index);
276 }
277 return null;
278 }
279
280 /** Method to retrieve a certain index, given its id.
281 * @param id the id of the index as a String
282 * @return the Index that matches id, or null if no such index exists
283 */
284 public Index getIndex(String id) {
285 int size = getSize();
286 for(int i = 0; i < size; i++) {
287 Index index = (Index) getElementAt(i);
288 if(index.getID().equals(id)) {
289 return index;
290 }
291 }
292 return null;
293 }
294
295 public ArrayList getIndexes() {
296 return children();
297 }
298
299
300
301 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
302 * @param mode the new mode as an int
303 */
304 public void modeChanged(int mode) {
305
306 }
307
308 private void moveIndex(Index index, boolean move_up)
309 {
310 // Determine the current position of the index
311 int position = indexOf(index);
312 // Determine if it can be moved, ie if its not already at the top trying to move up, or at the bottom trying to move down.
313 if(position == -1) {
314 return;
315 }
316 if(position == 0 && move_up) {
317 return;
318 }
319 if(position == (getSize()) - 1 && !move_up) {
320 return;
321 }
322
323 // Ok, move up
324 if (move_up) {
325 position--;
326 remove(index);
327 add(position, index);
328 }
329
330 // Or move down
331 else {
332 position++;
333 remove(index);
334 add(position, index);
335 }
336 }
337
338
339
340 /** Method to remove a certain index.
341 * @param index the Index to remove.
342 * @see org.greenstone.gatherer.Gatherer
343 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
344 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
345 * @see org.greenstone.gatherer.collection.CollectionManager
346 */
347 private void removeIndex(Index index) {
348 if(index != null) {
349 // Remove any current metadata from this index
350 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + index.getID());
351 // Remove the index
352 remove(index);
353 // Check if the index removed happens to be the default index
354 if(default_index != null && default_index.equals(index)) {
355 // If so our first solution is to set the first index to be default
356 if(getSize() > 0) {
357 Index another_index = (Index) getElementAt(0);
358 setDefault(another_index);
359 another_index = null;
360 }
361 else {
362 default_index.setAssigned(false);
363 }
364 }
365 }
366 }
367
368
369 /* replace an index in the list. new index may have the same sources but a
370 different name, or may be a new index altogether */
371 private void replaceIndex(Index old_index, Index new_index,
372 CollectionMeta coll_meta) {
373 if (old_index == null || new_index == null || coll_meta == null) {
374 return;
375 }
376 if (!old_index.getID().equals(new_index.getID()) && contains(new_index)) {
377 // shoudl we output an error??
378 return;
379 }
380 // Remove the old index coll meta
381 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + old_index.getID());
382 // Add the new coll meta
383 CollectionDesignManager.collectionmeta_manager.addMetadatum(coll_meta);
384
385 // get the position of the old one
386 int position = indexOf(old_index);
387 // remove it
388 remove(old_index);
389 // add the new one at that position
390 add(position, new_index);
391 }
392
393
394 /** Method to set the default index.
395 * @param index the new default Index
396 * @see org.greenstone.gatherer.Gatherer
397 * @see org.greenstone.gatherer.collection.CollectionManager
398 */
399 public void setDefault(Index index) {
400 if(index != null) {
401 if(default_index == null) {
402 // Create the default index element, and place immediately after indexes element.
403 Element default_index_element = root.getOwnerDocument().createElement(StaticStrings.INDEX_DEFAULT_ELEMENT);
404 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
405 default_index = new MGIndex(default_index_element);
406 } else {
407 default_index = new Index(default_index_element);
408 }
409 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
410 if(target_node != null) {
411 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
412 }
413 else {
414 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
415 }
416 } else {
417 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG) &&! (default_index instanceof org.greenstone.gatherer.cdm.MGIndex)) {
418 default_index = new MGIndex(default_index.getElement());
419 } else if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && default_index instanceof org.greenstone.gatherer.cdm.MGIndex) {
420 default_index = new Index(default_index.getElement());
421 }
422 }
423 default_index.setAssigned(true);
424 default_index.setSources(index.getSources());
425 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
426 ((MGIndex)default_index).setLevel(((MGIndex)index).getLevel());
427 }
428
429 }
430 else {
431 if(default_index != null) {
432 default_index.setAssigned(false);
433 }
434 }
435 }
436
437
438 private class IndexControl
439 extends JPanel
440 implements Control {
441
442 private JList index_list;
443 private JButton move_down_button;
444 private JButton move_up_button;
445 private JButton set_default_button;
446
447 private JButton new_button;
448 private JButton edit_button;
449 private JButton remove_button;
450
451 public IndexControl() {
452 super();
453 this.setComponentOrientation(Dictionary.getOrientation());
454 // Creation
455 JPanel assigned_indexes_pane = new JPanel();
456 assigned_indexes_pane.setComponentOrientation(Dictionary.getOrientation());
457
458 JLabel index_label = new JLabel(Dictionary.get("CDM.IndexManager.Indexes"));
459 index_label.setComponentOrientation(Dictionary.getOrientation());
460
461 index_list = new JList(index_model);
462 index_list.setCellRenderer(new IndexListRenderer());
463 index_list.setVisibleRowCount(6);
464 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
465 index_list.addMouseListener(new ClickListener());
466 index_list.setComponentOrientation(Dictionary.getOrientation());
467
468 JPanel movement_pane = new JPanel();
469 movement_pane.setComponentOrientation(Dictionary.getOrientation());
470
471 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
472 move_up_button.setEnabled(false);
473
474 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
475 move_down_button.setEnabled(false);
476
477 set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
478 set_default_button.setEnabled(false);
479
480 JPanel button_pane = new JPanel();
481 button_pane.setComponentOrientation(Dictionary.getOrientation());
482
483 new_button = new GLIButton(Dictionary.get("CDM.IndexManager.New_Index")+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get("CDM.IndexManager.New_Index_Tooltip"));
484 new_button.setEnabled(true);
485
486 edit_button = new GLIButton(Dictionary.get("CDM.IndexManager.Edit_Index")+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get("CDM.IndexManager.Edit_Index_Tooltip"));
487 edit_button.setEnabled(false);
488
489 remove_button = new GLIButton(Dictionary.get("CDM.IndexManager.Remove_Index"), Dictionary.get("CDM.IndexManager.Remove_Index_Tooltip"));
490 remove_button.setEnabled(false);
491
492 // Listeners
493 new_button.addActionListener(new NewIndexListener());
494 edit_button.addActionListener(new EditIndexListener());
495 remove_button.addActionListener(new RemoveIndexListener());
496 remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
497
498 index_list.addListSelectionListener(new IndexListListener());
499
500 move_down_button.addActionListener(new MoveListener(false));
501 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
502 move_up_button.addActionListener(new MoveListener(true));
503 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
504
505 set_default_button.addActionListener(new SetDefaultListener());
506 // Layout
507 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
508 movement_pane.setLayout(new GridLayout(3,1));
509 movement_pane.add(move_up_button);
510 movement_pane.add(move_down_button);
511 movement_pane.add(set_default_button);
512
513 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
514 assigned_indexes_pane.setLayout(new BorderLayout());
515 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
516 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
517 assigned_indexes_pane.add(movement_pane, BorderLayout.LINE_END);
518
519 button_pane.setLayout(new GridLayout(1,3,5,0));
520 button_pane.add(new_button);
521 button_pane.add(edit_button);
522 button_pane.add(remove_button);
523
524 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
525 setLayout(new BorderLayout());
526 add(assigned_indexes_pane, BorderLayout.CENTER);
527 add(button_pane, BorderLayout.SOUTH);
528
529 }
530 public void loseFocus() {}
531 public void gainFocus() {}
532 public void destroy() {}
533
534 /** Listens for double clicks apon the list and react as if the configure button was pushed. */
535 private class ClickListener
536 extends MouseAdapter {
537 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
538 * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
539 */
540 public void mouseClicked(MouseEvent event) {
541 if(event.getClickCount() == 2 ) {
542 if(!index_list.isSelectionEmpty()) {
543 Index index = (Index) index_list.getSelectedValue();
544 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
545 nip.destroy();
546 }
547 }
548 }
549 }
550
551
552 private class IndexListListener
553 implements ListSelectionListener {
554
555 /** This method is called whenever the source list selection changes. When it does we need to fill in the various parts of the list description panel
556 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
557 */
558 public void valueChanged(ListSelectionEvent event)
559 {
560 if (event.getValueIsAdjusting()) {
561 return;
562 }
563
564 set_default_button.setEnabled(true);
565 Object value = index_list.getSelectedValue();
566 if (value == null) {
567 move_down_button.setEnabled(false);
568 move_up_button.setEnabled(false);
569 remove_button.setEnabled(false);
570 edit_button.setEnabled(false);
571 set_default_button.setEnabled(false);
572 return;
573 }
574
575 // Enable the buttons appropriately
576 remove_button.setEnabled(true);
577 edit_button.setEnabled(true);
578 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
579 int i = index_list.getSelectedIndex();
580 int size = index_list.getModel().getSize();
581 move_up_button.setEnabled((i>0));
582 move_down_button.setEnabled((i<size-1));
583 }
584 }
585
586 private class IndexListRenderer
587 extends DefaultListCellRenderer {
588
589 /** Return a component that has been configured to display the specified value. */
590 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
591 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
592 if(default_index != null && default_index.equals(value)) {
593 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
594 }
595 return component;
596 }
597
598 }
599
600 private class NewIndexListener
601 implements ActionListener {
602
603 public void actionPerformed(ActionEvent event) {
604 NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
605 nip.destroy();
606 }
607 }
608
609 private class EditIndexListener
610 implements ActionListener {
611
612 public void actionPerformed(ActionEvent event) {
613 Index index = (Index) index_list.getSelectedValue();
614 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
615 nip.destroy();
616 }
617 }
618
619 private class RemoveIndexListener
620 implements ActionListener {
621
622 public void actionPerformed(ActionEvent event) {
623
624 int i = index_list.getSelectedIndex();
625 if (i != -1) {
626 removeIndex((Index) index_list.getSelectedValue());
627 }
628 int size = index_list.getModel().getSize();
629 if (i == size) {
630 i--;
631 }
632 index_list.setSelectedIndex(i);
633 // This will produce an event on the list, updating the other buttons
634 }
635 }
636
637 private class MoveListener
638 implements ActionListener {
639
640 private boolean move_up;
641
642 public MoveListener(boolean move_up) {
643 this.move_up = move_up;
644 }
645
646 public void actionPerformed(ActionEvent event) {
647 // Retrieve the selected index
648 Index index = (Index) index_list.getSelectedValue();
649 if(index != null) {
650 moveIndex(index, move_up);
651 // Ensure the index that moved is still selected
652 index_list.setSelectedValue(index, true);
653 index = null;
654 }
655 }
656 }
657
658 private class SetDefaultListener
659 implements ActionListener {
660
661 public void actionPerformed(ActionEvent event) {
662 Index index = (Index) index_list.getSelectedValue();
663 if(index != null) {
664 setDefault(index);
665 // This should cause a repaint of just the desired row
666 index_list.setSelectedValue(index, true);
667 }
668 set_default_button.setEnabled(false);
669 }
670 }
671
672
673
674 private class NewIndexPrompt
675 extends ModalDialog {
676
677 NewIndexPrompt new_index_prompt = null;
678
679 private JCheckBox text_checkbox;
680 private CheckList source_list;
681 // mg uses a level box
682 private JComboBox level_combobox;
683 // mgpp has a allfields selector
684 private JCheckBox allfields_box;
685
686 private JButton add_or_replace_button;
687 private JButton select_all_button;
688 private JButton select_none_button;
689 private JButton add_all_button;
690 //private JButton select_all_button;
691 //private JButton replace_button;
692 private JButton cancel_button;
693
694 private boolean mgpp_enabled = false;
695 private boolean editing = false;
696
697 public NewIndexPrompt(String build_type, Index existing_index) {
698 super(Gatherer.g_man, true);
699 this.setComponentOrientation(Dictionary.getOrientation());
700
701 new_index_prompt = this;
702
703 setModal(true);
704 setSize(PROMPT_SIZE);
705 if (existing_index != null) {
706 setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
707 editing = true;
708 } else {
709 setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
710 }
711
712 setJMenuBar(new SimpleMenuBar("searchindexes"));
713 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
714 mgpp_enabled = true;
715 }
716 JPanel content_pane = (JPanel)this.getContentPane();
717 content_pane.setComponentOrientation(Dictionary.getOrientation());
718 ArrayList new_data = new ArrayList();
719 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
720
721 JPanel details_pane = new JPanel();
722 details_pane.setComponentOrientation(Dictionary.getOrientation());
723
724 text_checkbox = new JCheckBox(Dictionary.get("CDM.IndexManager.Text_Source"));
725 text_checkbox.setToolTipText(Dictionary.get("CDM.IndexManager.Text_Source_Tooltip"));
726 text_checkbox.setComponentOrientation(Dictionary.getOrientation());
727 text_checkbox.addActionListener(new ActionListener() {
728 public void actionPerformed(ActionEvent event) {
729 validateAddOrReplaceButton();
730 }
731 });
732 JLabel source_label = new JLabel(Dictionary.get("CDM.IndexManager.Source"));
733 source_label.setComponentOrientation(Dictionary.getOrientation());
734 source_list = new CheckList(false);
735 source_list.setListData(new_data);
736 source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
737 source_list.addListSelectionListener(new SourceListListener());
738
739 JPanel button_pane = new JPanel();
740 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
741 button_pane.setComponentOrientation(Dictionary.getOrientation());
742
743 if (existing_index == null && mgpp_enabled) {
744 button_pane.setLayout(new GridLayout(2,3,5,0));
745 JPanel tmp = new JPanel();
746 tmp.setComponentOrientation(Dictionary.getOrientation());
747 button_pane.add(tmp);
748 } else {
749 button_pane.setLayout(new GridLayout(2,2,5,0));
750 }
751
752 select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
753 select_all_button.addActionListener(new ActionListener() {
754
755 public void actionPerformed(ActionEvent event) {
756 text_checkbox.setSelected(true);
757 source_list.setAllTicked();
758 validateAddOrReplaceButton();
759 }
760 });
761
762 select_none_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_None"), Dictionary.get("CDM.IndexManager.Select_None_Tooltip"));
763 select_none_button.addActionListener(new ActionListener() {
764
765 public void actionPerformed(ActionEvent event) {
766 text_checkbox.setSelected(false);
767 source_list.clearTicked();
768 validateAddOrReplaceButton();
769 }
770 });
771
772
773 button_pane.add(select_all_button);
774 button_pane.add(select_none_button);
775
776 if (existing_index != null) {
777 add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Replace_Index"), Dictionary.get("CDM.IndexManager.Replace_Index_Tooltip"));
778 add_or_replace_button.addActionListener(new ReplaceIndexListener());
779 } else {
780 add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_Index"), Dictionary.get("CDM.IndexManager.Add_Index_Tooltip"));
781 add_or_replace_button.addActionListener(new AddIndexListener());
782 }
783
784 add_or_replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
785 add_or_replace_button.setEnabled(false);
786 button_pane.add(add_or_replace_button);
787
788 if (existing_index == null && mgpp_enabled) {
789 add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
790 add_all_button.setEnabled(true);
791 add_all_button.addActionListener(new AddAllIndexActionListener());
792 button_pane.add(add_all_button);
793 }
794
795 // always have a cancel button
796 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
797 cancel_button.setEnabled(true);
798 cancel_button.addActionListener(new ActionListener() {
799 public void actionPerformed(ActionEvent event) {
800 new_index_prompt.dispose();
801 }
802 });
803 button_pane.add(cancel_button);
804
805 //Layout
806 details_pane.setLayout(new BorderLayout(10,10));
807 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
808 details_pane.add(text_checkbox, BorderLayout.NORTH);
809 details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
810
811
812 // do type specific stuff
813 if (mgpp_enabled) {
814 // allfields
815 allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
816 allfields_box.addItemListener(new AllFieldsBoxListener());
817 allfields_box.setComponentOrientation(Dictionary.getOrientation());
818 //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
819 details_pane.add(allfields_box, BorderLayout.SOUTH);
820
821
822 } else {
823 // index level
824 JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
825 level_label.setComponentOrientation(Dictionary.getOrientation());
826
827 level_combobox = new JComboBox();
828 level_combobox.setOpaque(false);
829 level_combobox.setPreferredSize(FIELD_SIZE);
830 // Note the order of these must be the same as the
831 // level order in Index
832 level_combobox.addItem(StaticStrings.DOCUMENT_STR);//Dictionary.get("CDM.LevelManager.Document"));
833 level_combobox.addItem(StaticStrings.SECTION_STR);//Dictionary.get("CDM.LevelManager.Section"));
834 level_combobox.addItem(StaticStrings.PARAGRAPH_STR);//Dictionary.get("CDM.LevelManager.Paragraph"));
835 level_combobox.setEditable(false);
836 level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
837 level_combobox.setComponentOrientation(Dictionary.getOrientation());
838 level_combobox.addActionListener(new ActionListener() {
839 public void actionPerformed(ActionEvent event) {
840 validateAddOrReplaceButton();
841 }
842 });
843 JPanel level_pane = new JPanel();
844 level_pane.setComponentOrientation(Dictionary.getOrientation());
845 level_pane.setLayout(new BorderLayout());
846 level_pane.add(level_label, BorderLayout.LINE_START);
847 level_pane.add(level_combobox, BorderLayout.CENTER);
848 details_pane.add(level_pane, BorderLayout.SOUTH);
849
850 }
851 // if we are editing, fill in the controls
852 if (existing_index !=null) {
853 ArrayList sources = existing_index.getSources();
854 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
855 allfields_box.setSelected(true);
856 source_list.setEnabled(false);
857 } else {
858 source_list.setTickedObjects(sources.toArray());
859 source_list.setEnabled(true);
860 if (sources.contains(StaticStrings.TEXT_STR)) {
861 text_checkbox.setSelected(true);
862 }
863 }
864 if (!mgpp_enabled && existing_index instanceof MGIndex) {
865 level_combobox.setSelectedIndex(((MGIndex)existing_index).getLevel());
866 }
867
868 }
869 content_pane.setLayout(new BorderLayout());
870 content_pane.add(source_label, BorderLayout.NORTH);
871 content_pane.add(details_pane, BorderLayout.CENTER);
872 content_pane.add(button_pane, BorderLayout.SOUTH);
873
874 // Display on screen.
875 Dimension screen_size = Configuration.screen_size;
876 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
877 screen_size = null;
878 setVisible(true);
879
880 }
881
882 /** Method which actually forces the dialog to be shown on screen.
883 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
884 */
885 public boolean display() {
886 setVisible(true);
887 return true;
888 // return success;
889 }
890
891 public void destroy() {
892 }
893
894 // Checks that specified index not already in the collection
895 private void validateAddOrReplaceButton() {
896 Index index;
897 ArrayList sources;
898 if (mgpp_enabled && allfields_box.isSelected()) {
899 sources = new ArrayList();
900 sources.add(ALLFIELDS);
901 index = new Index(sources);
902
903 } else if (text_checkbox.isSelected() ||
904 !source_list.isNothingTicked()) {
905 sources = source_list.getTicked();
906 if (text_checkbox.isSelected()) {
907 sources.add(0, StaticStrings.TEXT_STR);
908 }
909 if (mgpp_enabled) {
910 index = new Index(sources);
911 } else {
912 index = new MGIndex(level_combobox.getSelectedIndex(), sources);
913 }
914 } else {
915 // nothing selected
916 add_or_replace_button.setEnabled(false);
917 return;
918 }
919
920 sources = null;
921 if (index_model.contains(index)) {
922 add_or_replace_button.setEnabled(false);
923 }
924 else {
925 add_or_replace_button.setEnabled(true);
926 }
927
928 }
929
930 private Index generateNewIndex() {
931 Index index = null;
932 ArrayList sources;
933 if (mgpp_enabled && allfields_box.isSelected()) {
934 sources = new ArrayList();
935 sources.add(ALLFIELDS);
936 index = new Index(sources);
937 }
938 else if (text_checkbox.isSelected() || !source_list.isNothingTicked()) {
939 sources = source_list.getTicked();
940 if (text_checkbox.isSelected()) {
941 sources.add(0, StaticStrings.TEXT_STR);
942 }
943 if(mgpp_enabled) {
944 index = new Index(sources);
945 } else {
946 index = new MGIndex(level_combobox.getSelectedIndex(), sources);
947 }
948 }
949 return index;
950
951
952 }
953
954 private CollectionMeta generateCollectionMeta(Index index) {
955 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
956 if (use_macro_as_display_name(index.getID())) {
957 metadatum.setValue(get_macro_name(index.getID()));
958 } else {
959 metadatum.setValue(index.getID());
960 }
961 return metadatum;
962 }
963
964 private class AddIndexListener
965 implements ActionListener
966 {
967 public void actionPerformed(ActionEvent event)
968 {
969 Index index = generateNewIndex();
970 if (index != null) {
971
972 CollectionMeta metadatum = generateCollectionMeta(index);
973 // Add the index
974 addIndex(index, metadatum);
975 index_list.setSelectedValue(index, true);
976 }
977 new_index_prompt.dispose();
978 }
979 }
980
981 /** add all sources as separate indexes (fields). */
982 private class AddAllIndexActionListener
983 implements ActionListener {
984
985 public void actionPerformed(ActionEvent event) {
986 ArrayList all_sources = source_list.getAll();
987 all_sources.add(0, StaticStrings.TEXT_STR);
988 ArrayList new_sources = new ArrayList();
989 for(int i = 0; i < all_sources.size(); i++) {
990 Object source = all_sources.get(i);
991
992 // Create new index
993 new_sources.clear();
994 new_sources.add(source);
995 Index index = new Index(new_sources);
996 if(!index_model.contains(index)) {
997 // Determine the metadatum value
998 String name = source.toString();
999 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1000 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1001 }
1002 // Create new metadatum
1003 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
1004 metadatum.setValue(name);
1005 name = null;
1006 // Assign new index
1007 addIndex(index, metadatum);
1008 }
1009 source = null;
1010 index = null;
1011 }
1012 new_sources = null;
1013 new_index_prompt.dispose();
1014
1015 }
1016 }
1017
1018 private class AllFieldsBoxListener
1019 implements ItemListener {
1020
1021 public void itemStateChanged(ItemEvent event) {
1022 if (event.getStateChange() == ItemEvent.SELECTED) {
1023 source_list.setEnabled(false);
1024 } else if (event.getStateChange() == ItemEvent.DESELECTED) {
1025 source_list.setEnabled(true);
1026 }
1027 validateAddOrReplaceButton();
1028 }
1029
1030 }
1031
1032 private class SourceListListener
1033 implements ListSelectionListener {
1034
1035 public void valueChanged(ListSelectionEvent event) {
1036 if (event.getValueIsAdjusting()) {
1037 return;
1038 }
1039 validateAddOrReplaceButton();
1040 }
1041 }
1042
1043
1044 private class ReplaceIndexListener
1045 implements ActionListener {
1046
1047 public void actionPerformed(ActionEvent event)
1048 {
1049 Index index = generateNewIndex();
1050 if (index != null) {
1051 CollectionMeta metadatum = generateCollectionMeta(index);
1052 // replace the index
1053 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
1054 index_list.setSelectedValue(index, true);
1055 }
1056 new_index_prompt.dispose();
1057 }
1058 }
1059
1060 /**
1061 * If the index is built with DC metadata, use macro variable as the display text,
1062 * so that translations of DC metadata can be displayed in the search field drop-down list
1063 * when interface language changes.
1064 *
1065 * @param index_id Current index id.
1066 * @return Whether macro variable should be used.
1067 */
1068 private boolean use_macro_as_display_name(String index_id) {
1069 //String metaname = index_id;
1070 if (index_id.indexOf(":") != -1) {
1071 index_id = index_id.substring(index_id.indexOf(":") + 1);
1072 }
1073
1074 String field = null;
1075 String[] fields = index_id.split(",");
1076 for (int i = 0; i < fields.length; i++) {
1077 String s = fields[i];
1078 if (s.indexOf(".") != -1) {
1079 s = s.substring(s.indexOf(".") + 1);
1080 }
1081 if (field == null) {
1082 field = s;
1083 }
1084 else if (!field.equals(s)) {
1085 return false;
1086 }
1087 }
1088
1089 field = field.toLowerCase();
1090 if (field.equals("text") || field.equals("title") || field.equals("creator") || field.equals("subject") ||
1091 field.equals("description") || field.equals("publisher") || field.equals("contributor") || field.equals("date") ||
1092 field.equals("type") || field.equals("format") || field.equals("identifier") || field.equals("source") ||
1093 field.equals("language") || field.equals("relation") || field.equals("coverage") || field.equals("rights")) {
1094 return true;
1095 }
1096
1097 return false;
1098 }
1099
1100 /**
1101 * Get the corresponding macro variable name, eg. _labelTitle_, _labelCreator_.
1102 *
1103 * @param index_id Current index id.
1104 * @return Name of the macro variable.
1105 */
1106 private String get_macro_name(String index_id) {
1107 if (index_id.indexOf(":") != -1) {
1108 index_id = index_id.substring(index_id.indexOf(":") + 1);
1109 }
1110 if (index_id.indexOf(",") != -1) {
1111 index_id = index_id.substring(0, index_id.indexOf(","));
1112 }
1113 if (index_id.indexOf(".") != -1) {
1114 index_id = index_id.substring(index_id.indexOf(".") + 1);
1115 }
1116
1117 return "_label" + index_id + "_";
1118 }
1119 }
1120 }
1121}
Note: See TracBrowser for help on using the repository browser.