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

Last change on this file since 12749 was 12729, checked in by kjdon, 18 years ago

added a menu bar for New index prompt

  • Property svn:keywords set to Author Date Id Revision
File size: 34.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Dictionary;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.gui.DesignPaneHeader;
39import org.greenstone.gatherer.gui.GComboBox;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.gui.ModalDialog;
42import org.greenstone.gatherer.gui.SimpleMenuBar;
43import org.greenstone.gatherer.metadata.MetadataElement;
44import org.greenstone.gatherer.metadata.MetadataSetManager;
45import org.greenstone.gatherer.util.CheckList;
46import org.greenstone.gatherer.util.JarTools;
47import org.greenstone.gatherer.util.XMLTools;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.w3c.dom.*;
50/** This class is resposible for storing the indexes which have been assigned to this collection and the default index, and providing methods for interacting with both these data pools. It also knows how to turn itself into a String as it would be displayed in the collection configuration file.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class 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 = CollectionDesignManager.collect_config.getDocumentElement().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 Gatherer.c_man.configurationChanged();
107 }
108 else {
109 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
110 }
111 }
112
113 public void buildTypeChanged(String new_build_type) {
114 if (build_type.equals(new_build_type)) {
115 return;
116 }
117 // we don;t care about this if old or new is not MG as MGPP and
118 // Lucene have the same index specification
119 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
120 return;
121 }
122 boolean mg_to_mgpp = true;
123 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
124 mg_to_mgpp = false;
125 }
126 build_type = new_build_type;
127 if (mg_to_mgpp) {
128 changeToMGPPIndexes();
129 } else {
130 changeToMGIndexes();
131 }
132 // its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
133 Index first_index = (Index) getElementAt(0);
134 setDefault(first_index);
135 first_index = null;
136 }
137
138 private void changeToMGIndexes() {
139 Element mgpp_element = root;
140 // Retrieve and assign MG element and default index element
141 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
142 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
143 NodeList indexes = mg_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
144 if(indexes.getLength() != 0) {
145 //just reinstate what we had previously
146 setRoot(mg_element);
147 } else {
148 // If mg element has no indexes, and the current mgpp index includes a text one, then generate text indexes for each of the registered levels.
149 Index index = getIndex(StaticStrings.TEXT_STR);
150 if(index != null) {
151 // Replace mgpp element with mg element
152 setRoot(mg_element);
153 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
154 int level_size = levels.size();
155 for(int i = 0; i < level_size; i++) {
156 Level level = (Level) levels.get(i);
157 Index new_index = new Index(level.getLevel(), index.getSources());
158 // Try to retrieve existing metadatum
159 String source_str = new_index.getID();
160 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
161 // If no metadata was found, add new pseudo metadata using the id
162 if(metadatum == null) {
163 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
164 metadatum.setAssigned(true);
165 metadatum.setValue(source_str);
166 }
167 // If it was found, ensure it is assigned
168 else {
169 metadatum.setAssigned(true);
170 }
171 source_str = null;
172 addIndex(new_index, metadatum);
173 new_index = null;
174 level = null;
175 }
176 }
177 }
178
179 // Unassign mgpp element
180 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
181 mgpp_element = null;
182
183 }
184
185 private void changeToMGPPIndexes() {
186 Element mg_element = root;
187 // Retrieve and assign the MGPP indexes element.
188 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
189 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
190 NodeList indexes = mgpp_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
191 if(indexes.getLength() != 0) {
192 // we just reinstate the indexes we had before the change
193 setRoot(mgpp_element);
194 } else {
195 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
196
197 ArrayList sources_list = new ArrayList();
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 // Schedule the collection for saving
334 Gatherer.c_man.configurationChanged();
335 }
336
337
338
339 /** Method to remove a certain index.
340 * @param index the Index to remove.
341 * @see org.greenstone.gatherer.Gatherer
342 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
343 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
344 * @see org.greenstone.gatherer.collection.CollectionManager
345 */
346 private void removeIndex(Index index) {
347 if(index != null) {
348 // Remove any current metadata from this index
349 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + index.getID());
350 // Remove the index
351 remove(index);
352 // Check if the index removed happens to be the default index
353 if(default_index != null && default_index.equals(index)) {
354 // If so our first solution is to set the first index to be default
355 if(getSize() > 0) {
356 Index another_index = (Index) getElementAt(0);
357 setDefault(another_index);
358 another_index = null;
359 }
360 else {
361 default_index.setAssigned(false);
362 }
363 }
364 Gatherer.c_man.configurationChanged();
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 // Schedule the collection for saving
393 Gatherer.c_man.configurationChanged();
394 }
395
396
397 /** Method to set the default index.
398 * @param index the new default Index
399 * @see org.greenstone.gatherer.Gatherer
400 * @see org.greenstone.gatherer.collection.CollectionManager
401 */
402 public void setDefault(Index index) {
403 if(index != null) {
404 if(default_index == null) {
405 // Create the default index element, and place immediately after indexes element.
406 Element default_index_element = root.getOwnerDocument().createElement(StaticStrings.INDEX_DEFAULT_ELEMENT);
407 default_index = new Index(default_index_element);
408 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
409 if(target_node != null) {
410 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
411 }
412 else {
413 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
414 }
415 }
416 default_index.setAssigned(true);
417 default_index.setLevel(index.getLevel());
418 default_index.setSources(index.getSources());
419
420 }
421 else {
422 if(default_index != null) {
423 default_index.setAssigned(false);
424 }
425 }
426 Gatherer.c_man.configurationChanged();
427
428 }
429
430
431 private class IndexControl
432 extends JPanel
433 implements Control {
434
435 private JList index_list;
436 private JButton move_down_button;
437 private JButton move_up_button;
438 private JButton set_default_button;
439
440 private JButton new_button;
441 private JButton edit_button;
442 private JButton remove_button;
443
444 public IndexControl() {
445 super();
446
447 // Creation
448 JPanel assigned_indexes_pane = new JPanel();
449
450 JLabel index_label = new JLabel(Dictionary.get("CDM.IndexManager.Indexes"));
451 index_list = new JList(index_model);
452 index_list.setCellRenderer(new IndexListRenderer());
453 index_list.setVisibleRowCount(6);
454 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
455
456 JPanel movement_pane = new JPanel();
457 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
458 move_up_button.setEnabled(false);
459
460 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
461 move_down_button.setEnabled(false);
462
463 set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
464 set_default_button.setEnabled(false);
465
466 JPanel button_pane = new JPanel();
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
481 index_list.addListSelectionListener(new IndexListListener());
482
483 move_down_button.addActionListener(new MoveListener(false));
484 move_up_button.addActionListener(new MoveListener(true));
485
486 set_default_button.addActionListener(new SetDefaultListener());
487 // Layout
488 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
489 movement_pane.setLayout(new GridLayout(3,1));
490 movement_pane.add(move_up_button);
491 movement_pane.add(move_down_button);
492 movement_pane.add(set_default_button);
493
494 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
495 assigned_indexes_pane.setLayout(new BorderLayout());
496 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
497 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
498 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
499
500 button_pane.setLayout(new GridLayout(1,3,5,0));
501 button_pane.add(new_button);
502 button_pane.add(edit_button);
503 button_pane.add(remove_button);
504
505 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
506 setLayout(new BorderLayout());
507 add(assigned_indexes_pane, BorderLayout.CENTER);
508 add(button_pane, BorderLayout.SOUTH);
509
510 }
511 public void loseFocus() {}
512 public void gainFocus() {}
513 public void destroy() {}
514
515 private class IndexListListener
516 implements ListSelectionListener {
517
518 /** 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
519 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
520 */
521 public void valueChanged(ListSelectionEvent event)
522 {
523 if (event.getValueIsAdjusting()) {
524 return;
525 }
526
527 set_default_button.setEnabled(true);
528 Object value = index_list.getSelectedValue();
529 if (value == null) {
530 move_down_button.setEnabled(false);
531 move_up_button.setEnabled(false);
532 remove_button.setEnabled(false);
533 edit_button.setEnabled(false);
534 set_default_button.setEnabled(false);
535 return;
536 }
537
538 // Enable the buttons appropriately
539 remove_button.setEnabled(true);
540 edit_button.setEnabled(true);
541 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
542 int i = index_list.getSelectedIndex();
543 int size = index_list.getModel().getSize();
544 move_up_button.setEnabled((i>0));
545 move_down_button.setEnabled((i<size-1));
546 }
547 }
548
549 private class IndexListRenderer
550 extends DefaultListCellRenderer {
551
552 /** Return a component that has been configured to display the specified value. */
553 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
554 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
555 if(default_index != null && default_index.equals(value)) {
556 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
557 }
558 return component;
559 }
560
561 }
562
563 private class NewIndexListener
564 implements ActionListener {
565
566 public void actionPerformed(ActionEvent event) {
567 NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
568 nip.destroy();
569 }
570 }
571
572 private class EditIndexListener
573 implements ActionListener {
574
575 public void actionPerformed(ActionEvent event) {
576 Index index = (Index) index_list.getSelectedValue();
577 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
578 nip.destroy();
579 }
580 }
581
582 private class RemoveIndexListener
583 implements ActionListener {
584
585 public void actionPerformed(ActionEvent event) {
586
587 int i = index_list.getSelectedIndex();
588 if (i != -1) {
589 removeIndex((Index) index_list.getSelectedValue());
590 }
591 int size = index_list.getModel().getSize();
592 if (i == size) {
593 i--;
594 }
595 index_list.setSelectedIndex(i);
596 // This will produce an event on the list, updating the other buttons
597 }
598 }
599
600 private class MoveListener
601 implements ActionListener {
602
603 private boolean move_up;
604
605 public MoveListener(boolean move_up) {
606 this.move_up = move_up;
607 }
608
609 public void actionPerformed(ActionEvent event) {
610 // Retrieve the selected index
611 Index index = (Index) index_list.getSelectedValue();
612 if(index != null) {
613 moveIndex(index, move_up);
614 // Ensure the index that moved is still selected
615 index_list.setSelectedValue(index, true);
616 index = null;
617 }
618 }
619 }
620
621 private class SetDefaultListener
622 implements ActionListener {
623
624 public void actionPerformed(ActionEvent event) {
625 Index index = (Index) index_list.getSelectedValue();
626 if(index != null) {
627 setDefault(index);
628 // This should cause a repaint of just the desired row
629 index_list.setSelectedValue(index, true);
630 }
631 set_default_button.setEnabled(false);
632 }
633 }
634
635
636
637 private class NewIndexPrompt
638 extends ModalDialog {
639
640 NewIndexPrompt new_index_prompt = null;
641
642 private JCheckBox text_checkbox;
643 private CheckList source_list;
644 // mg uses a level box
645 private JComboBox level_combobox;
646 // mgpp has a allfields selector
647 private JCheckBox allfields_box;
648
649 private JButton add_or_replace_button;
650 private JButton select_all_button;
651 private JButton select_none_button;
652 private JButton add_all_button;
653 //private JButton select_all_button;
654 //private JButton replace_button;
655 private JButton cancel_button;
656
657 private boolean mgpp_enabled = false;
658 private boolean editing = false;
659
660 public NewIndexPrompt(String build_type, Index existing_index) {
661 super(Gatherer.g_man, true);
662 new_index_prompt = this;
663
664 setModal(true);
665 setSize(PROMPT_SIZE);
666 if (existing_index != null) {
667 setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
668 editing = true;
669 } else {
670 setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
671 }
672
673 setJMenuBar(new SimpleMenuBar("searchindexes"));
674 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
675 mgpp_enabled = true;
676 }
677 JPanel content_pane = (JPanel)this.getContentPane();
678 ArrayList new_data = new ArrayList();
679 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
680
681 JPanel details_pane = new JPanel();
682
683 text_checkbox = new JCheckBox(Dictionary.get("CDM.IndexManager.Text_Source"));
684 text_checkbox.setToolTipText(Dictionary.get("CDM.IndexManager.Text_Source_Tooltip"));
685 text_checkbox.addActionListener(new ActionListener() {
686 public void actionPerformed(ActionEvent event) {
687 validateAddOrReplaceButton();
688 }
689 });
690 JLabel source_label = new JLabel(Dictionary.get("CDM.IndexManager.Source"));
691 source_list = new CheckList(false);
692 source_list.setListData(new_data);
693 source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
694 source_list.addListSelectionListener(new SourceListListener());
695
696 JPanel button_pane = new JPanel();
697 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
698
699 if (existing_index == null && mgpp_enabled) {
700 button_pane.setLayout(new GridLayout(2,3,5,0));
701 button_pane.add(new JPanel());
702 } else {
703 button_pane.setLayout(new GridLayout(2,2,5,0));
704 }
705
706 select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
707 select_all_button.addActionListener(new ActionListener() {
708
709 public void actionPerformed(ActionEvent event) {
710 text_checkbox.setSelected(true);
711 source_list.setAllTicked();
712 validateAddOrReplaceButton();
713 }
714 });
715
716 select_none_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_None"), Dictionary.get("CDM.IndexManager.Select_None_Tooltip"));
717 select_none_button.addActionListener(new ActionListener() {
718
719 public void actionPerformed(ActionEvent event) {
720 text_checkbox.setSelected(false);
721 source_list.clearTicked();
722 validateAddOrReplaceButton();
723 }
724 });
725
726
727 button_pane.add(select_all_button);
728 button_pane.add(select_none_button);
729
730 if (existing_index != null) {
731 add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Replace_Index"), Dictionary.get("CDM.IndexManager.Replace_Index_Tooltip"));
732 add_or_replace_button.addActionListener(new ReplaceIndexListener());
733 } else {
734 add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_Index"), Dictionary.get("CDM.IndexManager.Add_Index_Tooltip"));
735 add_or_replace_button.addActionListener(new AddIndexListener());
736 }
737
738 add_or_replace_button.setEnabled(false);
739 button_pane.add(add_or_replace_button);
740
741 if (existing_index == null && mgpp_enabled) {
742 add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
743 add_all_button.setEnabled(true);
744 add_all_button.addActionListener(new AddAllIndexActionListener());
745 button_pane.add(add_all_button);
746 }
747
748 // always have a cancel button
749 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
750 cancel_button.setEnabled(true);
751 cancel_button.addActionListener(new ActionListener() {
752 public void actionPerformed(ActionEvent event) {
753 new_index_prompt.dispose();
754 }
755 });
756 button_pane.add(cancel_button);
757
758 //Layout
759 details_pane.setLayout(new BorderLayout(10,10));
760 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
761 details_pane.add(text_checkbox, BorderLayout.NORTH);
762 details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
763
764
765 // do type specific stuff
766 if (mgpp_enabled) {
767 // allfields
768 allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
769 allfields_box.addItemListener(new AllFieldsBoxListener());
770 //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
771 details_pane.add(allfields_box, BorderLayout.SOUTH);
772
773
774 } else {
775 // index level
776 JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
777 level_combobox = new JComboBox();
778 level_combobox.setPreferredSize(FIELD_SIZE);
779 // Note the order of these must be the same as the
780 // level order in Index
781 level_combobox.addItem(StaticStrings.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
782 level_combobox.addItem(StaticStrings.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
783 level_combobox.addItem(StaticStrings.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
784 level_combobox.setEditable(false);
785 level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
786 level_combobox.addActionListener(new ActionListener() {
787 public void actionPerformed(ActionEvent event) {
788 validateAddOrReplaceButton();
789 }
790 });
791 JPanel level_pane = new JPanel();
792 level_pane.setLayout(new BorderLayout());
793 level_pane.add(level_label, BorderLayout.WEST);
794 level_pane.add(level_combobox, BorderLayout.CENTER);
795 details_pane.add(level_pane, BorderLayout.SOUTH);
796
797 }
798 // if we are editing, fill in the controls
799 if (existing_index !=null) {
800 ArrayList sources = existing_index.getSources();
801 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
802 allfields_box.setSelected(true);
803 source_list.setEnabled(false);
804 } else {
805 source_list.setTickedObjects(sources.toArray());
806 source_list.setEnabled(true);
807 if (sources.contains(StaticStrings.TEXT_STR)) {
808 text_checkbox.setSelected(true);
809 }
810 }
811 if (!mgpp_enabled) {
812 level_combobox.setSelectedIndex(existing_index.getLevel());
813 }
814
815 }
816 content_pane.setLayout(new BorderLayout());
817 content_pane.add(source_label, BorderLayout.NORTH);
818 content_pane.add(details_pane, BorderLayout.CENTER);
819 content_pane.add(button_pane, BorderLayout.SOUTH);
820
821 // Display on screen.
822 Dimension screen_size = Configuration.screen_size;
823 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
824 screen_size = null;
825 setVisible(true);
826
827 }
828
829 /** Method which actually forces the dialog to be shown on screen.
830 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
831 */
832 public boolean display() {
833 setVisible(true);
834 return true;
835 // return success;
836 }
837
838 public void destroy() {
839 }
840
841 // Checks that specified index not already in the collection
842 private void validateAddOrReplaceButton() {
843 Index index;
844 ArrayList sources;
845 if (mgpp_enabled && allfields_box.isSelected()) {
846 sources = new ArrayList();
847 sources.add(ALLFIELDS);
848 index = new Index(sources);
849
850 } else if (text_checkbox.isSelected() ||
851 !source_list.isNothingTicked()) {
852 sources = source_list.getTicked();
853 if (text_checkbox.isSelected()) {
854 sources.add(0, StaticStrings.TEXT_STR);
855 }
856 if (mgpp_enabled) {
857 index = new Index(sources);
858 } else {
859 index = new Index(level_combobox.getSelectedIndex(), sources);
860 }
861 } else {
862 // nothing selected
863 add_or_replace_button.setEnabled(false);
864 return;
865 }
866
867 sources = null;
868 if (index_model.contains(index)) {
869 add_or_replace_button.setEnabled(false);
870 }
871 else {
872 add_or_replace_button.setEnabled(true);
873 }
874
875 }
876 private class AddIndexListener
877 implements ActionListener
878 {
879 public void actionPerformed(ActionEvent event)
880 {
881 Index index;
882 if (mgpp_enabled && allfields_box.isSelected()) {
883 ArrayList sources = new ArrayList();
884 sources.add(ALLFIELDS);
885 index = new Index(sources);
886 }
887 else if (text_checkbox.isSelected() ||
888 !source_list.isNothingTicked()) {
889 ArrayList 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 return;
900 }
901
902 // Before we add the index to the model, we have to add a default collection metadata for this
903 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
904 metadatum.setValue(index.getID());
905
906 // Finally, add the index
907 addIndex(index, metadatum);
908 index_list.setSelectedValue(index, true);
909 new_index_prompt.dispose();
910 }
911 }
912
913 /** add all sources as separate indexes (fields). */
914 private class AddAllIndexActionListener
915 implements ActionListener {
916
917 public void actionPerformed(ActionEvent event) {
918 ArrayList all_sources = source_list.getAll();
919 all_sources.add(0, StaticStrings.TEXT_STR);
920 ArrayList new_sources = new ArrayList();
921 for(int i = 0; i < all_sources.size(); i++) {
922 Object source = all_sources.get(i);
923
924 // Create new index
925 new_sources.clear();
926 new_sources.add(source);
927 Index index = new Index(new_sources);
928 if(!index_model.contains(index)) {
929 // Determine the metadatum value
930 String name = source.toString();
931 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
932 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
933 }
934 // Create new metadatum
935 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
936 metadatum.setValue(name);
937 name = null;
938 // Assign new index
939 addIndex(index, metadatum);
940 }
941 source = null;
942 index = null;
943 }
944 new_sources = null;
945 new_index_prompt.dispose();
946
947 }
948 }
949
950 private class AllFieldsBoxListener
951 implements ItemListener {
952
953 public void itemStateChanged(ItemEvent event) {
954 if (event.getStateChange() == ItemEvent.SELECTED) {
955 source_list.setEnabled(false);
956 } else if (event.getStateChange() == ItemEvent.DESELECTED) {
957 source_list.setEnabled(true);
958 }
959 validateAddOrReplaceButton();
960 }
961
962 }
963
964 private class SourceListListener
965 implements ListSelectionListener {
966
967 public void valueChanged(ListSelectionEvent event) {
968 if (event.getValueIsAdjusting()) {
969 return;
970 }
971 validateAddOrReplaceButton();
972 }
973 }
974
975
976 private class ReplaceIndexListener
977 implements ActionListener {
978
979 public void actionPerformed(ActionEvent event) {
980
981 ArrayList sources;
982 Index index = null;
983 if (text_checkbox.isSelected() || !source_list.isNothingTicked()) {
984 sources = source_list.getTicked();
985 if (text_checkbox.isSelected()) {
986 sources.add(0, StaticStrings.TEXT_STR);
987 }
988 if (mgpp_enabled) {
989 index = new Index(sources);
990 } else {
991 index = new Index(level_combobox.getSelectedIndex(), sources);
992 }
993 }
994 if (index != null) {
995 // Create the new collection meta
996 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
997 metadatum.setValue(index.getID());
998
999 // replace the index
1000 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
1001 index_list.setSelectedValue(index, true);
1002
1003 }
1004 new_index_prompt.dispose();
1005
1006 }
1007 }
1008 }
1009 }
1010}
Note: See TracBrowser for help on using the repository browser.