source: main/trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java@ 24367

Last change on this file since 24367 was 24367, checked in by ak19, 13 years ago

Changes to GLI to do with embedded metadata: the plugin and changes to the processing of ex.metadata to deal with ex.something.metadata

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