source: gli/branches/3.03/src/org/greenstone/gatherer/cdm/IndexManager.java@ 14746

Last change on this file since 14746 was 14232, checked in by xiao, 17 years ago

add c ClickListener to listen for double click upon the index list and react as if the EditIndex button was clicked.

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