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

Last change on this file since 12108 was 12087, checked in by kjdon, 18 years ago

indented the file properly

  • Property svn:keywords set to Author Date Id Revision
File size: 32.3 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.metadata.MetadataElement;
43import org.greenstone.gatherer.metadata.MetadataSetManager;
44import org.greenstone.gatherer.util.CheckList;
45import org.greenstone.gatherer.util.JarTools;
46import org.greenstone.gatherer.util.StaticStrings;
47import org.w3c.dom.*;
48/** 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.
49 * @author John Thompson, Greenstone Digital Library, University of Waikato
50 * @version 2.3
51 */
52public class IndexManager
53 extends DOMProxyListModel
54 implements BuildTypeManager.BuildTypeListener {
55
56 /** A reference to ourselves so our inner methods have access. */
57 private DOMProxyListModel index_model = null;
58 /** The default index. */
59 private Index default_index = null;
60
61 private Control controls = null;
62 private String build_type = null;
63
64 static final private Dimension FIELD_SIZE = new Dimension(200,30);
65 static final private String ALLFIELDS = "allfields";
66
67 static final private Dimension PROMPT_SIZE = new Dimension(400,400);
68 public IndexManager(Element indexes, String current_build_type) {
69
70 super(indexes, CollectionConfiguration.INDEX_ELEMENT, new Index());
71 DebugStream.println("IndexManager: " + getSize() + " indexes parsed.");
72 index_model = this;
73
74 // Parse and retrieve the default index
75 NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
76 if(default_index_elements.getLength() > 0) {
77 default_index = new Index((Element)default_index_elements.item(0));
78 }
79 build_type = current_build_type;
80 }
81
82 /** Method to add a new index.
83 * @param index The <strong>Index</strong> to add.
84 * @see org.greenstone.gatherer.Gatherer
85 * @see org.greenstone.gatherer.collection.CollectionManager
86 */
87 private void addIndex(Index index, CollectionMeta metadatum) {
88 ///ystem.err.println("Adding an index: " + index.toString());
89 if(!contains(index)) {
90 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
91 // Retrieve the currently last index
92 if(getSize() > 0) {
93 Index last_index = (Index)getElementAt(getSize() - 1);
94 addAfter(index, last_index);
95
96 }
97 else {
98 add(index);
99 // Also set this index as the default one,
100 setDefault(index);
101 }
102 Gatherer.c_man.configurationChanged();
103 }
104 else {
105 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
106 }
107 }
108
109 public void buildTypeChanged(String new_build_type) {
110 if (build_type.equals(new_build_type)) {
111 return;
112 }
113 // we don;t care about this if old or new is not MG as MGPP and
114 // Lucene have the same index specification
115 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
116 return;
117 }
118 boolean mg_to_mgpp = true;
119 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
120 mg_to_mgpp = false;
121 }
122 build_type = new_build_type;
123 if (mg_to_mgpp) {
124 changeToMGPPIndexes();
125 } else {
126 changeToMGIndexes();
127 }
128 // its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
129 Index first_index = (Index) getElementAt(0);
130 setDefault(first_index);
131 first_index = null;
132 }
133
134 private void changeToMGIndexes() {
135 Element mgpp_element = root;
136 // Retrieve and assign MG element and default index element
137 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
138 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
139 NodeList indexes = mg_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
140 if(indexes.getLength() != 0) {
141 //just reinstate what we had previously
142 setRoot(mg_element);
143 } else {
144 // 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.
145 Index index = getIndex(CollectionConfiguration.TEXT_STR);
146 if(index != null) {
147 // Replace mgpp element with mg element
148 setRoot(mg_element);
149 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
150 int level_size = levels.size();
151 for(int i = 0; i < level_size; i++) {
152 Level level = (Level) levels.get(i);
153 Index new_index = new Index(level.getLevel(), index.getSources());
154 // Try to retrieve existing metadatum
155 String source_str = new_index.getID();
156 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
157 // If no metadata was found, add new pseudo metadata using the id
158 if(metadatum == null) {
159 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
160 metadatum.setAssigned(true);
161 metadatum.setValue(source_str);
162 }
163 // If it was found, ensure it is assigned
164 else {
165 metadatum.setAssigned(true);
166 }
167 source_str = null;
168 addIndex(new_index, metadatum);
169 new_index = null;
170 level = null;
171 }
172 }
173 }
174
175 // Unassign mgpp element
176 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
177 mgpp_element = null;
178
179 }
180
181 private void changeToMGPPIndexes() {
182 Element mg_element = root;
183 // Retrieve and assign the MGPP indexes element.
184 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
185 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
186 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
187 if(indexes.getLength() != 0) {
188 // we just reinstate the indexes we had before the change
189 setRoot(mgpp_element);
190 } else {
191 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
192
193 ArrayList sources_list = new ArrayList();
194 // We first use details from the default index if any
195 if(default_index != null) {
196 ArrayList sources = default_index.getSources();
197 sources_list.addAll(sources);
198 }
199 int size = getSize();
200 for(int i = 0; i < size; i++) {
201 Index index = (Index) getElementAt(i);
202 ArrayList sources = index.getSources();
203 sources.removeAll(sources_list);
204 sources_list.addAll(sources);
205 index = null;
206 }
207 // Replace mg element with mgpp element
208 setRoot(mgpp_element);
209
210 // We now have a list of sources, so create new indexes based on these
211 int sources_list_size = sources_list.size();
212 for(int j = 0; j < sources_list_size; j++) {
213 Object source_object = sources_list.get(j);
214 String source_str = null;
215 if(source_object instanceof MetadataElement) {
216 source_str = ((MetadataElement) source_object).getFullName();
217 }
218 else {
219 source_str = source_object.toString();
220 }
221 ArrayList new_sources = new ArrayList();
222 new_sources.add(source_object);
223 source_object = null;
224 Index new_index = new Index(new_sources);
225 // Try to retrieve existing metadatum
226 source_str = new_index.getID();
227 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
228 // If no metadata was found, add new pseudo metadata using the id
229 if(metadatum == null) {
230 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
231 metadatum.setAssigned(true);
232 metadatum.setValue(source_str);
233 }
234 // If it was found, ensure it is assigned
235 else {
236 metadatum.setAssigned(true);
237 }
238 source_str = null;
239 addIndex(new_index, metadatum);
240 metadatum = null;
241 new_index = null;
242 new_sources = null;
243 source_str = null;
244 }
245
246 }
247
248 // Unassign MG element
249 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
250 mg_element = null;
251
252 }
253
254 public Control getControls() {
255 if (controls == null) {
256 controls = new IndexControl();
257 }
258 return controls;
259 }
260
261 /** Method to retrieve a certain index, as referenced by an index number.
262 * @param index An <i>int</i> which indicates the position of the desired index.
263 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
264 */
265 public Index getIndex(int index) {
266 if(0 <= index && index < getSize()) {
267 return (Index)getElementAt(index);
268 }
269 return null;
270 }
271
272 /** Method to retrieve a certain index, given its id.
273 * @param id the id of the index as a String
274 * @return the Index that matches id, or null if no such index exists
275 */
276 public Index getIndex(String id) {
277 int size = getSize();
278 for(int i = 0; i < size; i++) {
279 Index index = (Index) getElementAt(i);
280 if(index.getID().equals(id)) {
281 return index;
282 }
283 }
284 return null;
285 }
286
287 public ArrayList getIndexes() {
288 return children();
289 }
290
291
292
293 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
294 * @param mode the new mode as an int
295 */
296 public void modeChanged(int mode) {
297
298 }
299
300 private void moveIndex(Index index, boolean move_up)
301 {
302 // Determine the current position of the index
303 int position = indexOf(index);
304 // 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.
305 if(position == -1) {
306 return;
307 }
308 if(position == 0 && move_up) {
309 return;
310 }
311 if(position == (getSize()) - 1 && !move_up) {
312 return;
313 }
314
315 // Ok, move up
316 if (move_up) {
317 position--;
318 remove(index);
319 add(position, index);
320 }
321
322 // Or move down
323 else {
324 position++;
325 remove(index);
326 add(position, index);
327 }
328
329 // Schedule the collection for saving
330 Gatherer.c_man.configurationChanged();
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(CollectionConfiguration.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 Gatherer.c_man.configurationChanged();
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(CollectionConfiguration.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 // Schedule the collection for saving
389 Gatherer.c_man.configurationChanged();
390 }
391
392
393 /** Method to set the default index.
394 * @param index the new default Index
395 * @see org.greenstone.gatherer.Gatherer
396 * @see org.greenstone.gatherer.collection.CollectionManager
397 */
398 public void setDefault(Index index) {
399 if(index != null) {
400 if(default_index == null) {
401 // Create the default index element, and place immediately after indexes element.
402 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
403 default_index = new Index(default_index_element);
404 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
405 if(target_node != null) {
406 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
407 }
408 else {
409 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
410 }
411 }
412 default_index.setAssigned(true);
413 default_index.setLevel(index.getLevel());
414 default_index.setSources(index.getSources());
415
416 }
417 else {
418 if(default_index != null) {
419 default_index.setAssigned(false);
420 }
421 }
422 Gatherer.c_man.configurationChanged();
423 }
424
425
426 private class IndexControl
427 extends JPanel
428 implements Control {
429
430 private JList index_list;
431 private JButton move_down_button;
432 private JButton move_up_button;
433 private JButton set_default_button;
434
435 private JButton new_button;
436 private JButton edit_button;
437 private JButton remove_button;
438
439 public IndexControl() {
440 super();
441
442 // Creation
443 JPanel assigned_indexes_pane = new JPanel();
444
445 JLabel index_label = new JLabel();
446 Dictionary.registerText(index_label, "CDM.IndexManager.Indexes");
447 index_list = new JList(index_model);
448 index_list.setCellRenderer(new IndexListRenderer());
449 index_list.setVisibleRowCount(6);
450 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
451
452 JPanel movement_pane = new JPanel();
453 move_up_button = new GLIButton("", JarTools.getImage("arrow-up.gif"));
454 move_up_button.setEnabled(false);
455 move_up_button.setMnemonic(KeyEvent.VK_U);
456 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
457
458 move_down_button = new GLIButton("", JarTools.getImage("arrow-down.gif"));
459 move_down_button.setEnabled(false);
460 move_down_button.setMnemonic(KeyEvent.VK_D);
461 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
462
463 set_default_button = new GLIButton();
464 set_default_button.setEnabled(false);
465 set_default_button.setMnemonic(KeyEvent.VK_S);
466 Dictionary.registerBoth(set_default_button, "CDM.IndexManager.Set_Default", "CDM.IndexManager.Set_Default_Tooltip");
467
468 JPanel button_pane = new JPanel();
469 new_button = new GLIButton();
470 new_button.setEnabled(true);
471 new_button.setMnemonic(KeyEvent.VK_A);
472 Dictionary.registerBoth(new_button, "CDM.IndexManager.New_Index", "CDM.IndexManager.New_Index_Tooltip");
473 edit_button = new GLIButton();
474 edit_button.setEnabled(false);
475 edit_button.setMnemonic(KeyEvent.VK_A);
476 Dictionary.registerBoth(edit_button, "CDM.IndexManager.Edit_Index", "CDM.IndexManager.Edit_Index_Tooltip");
477 remove_button = new GLIButton();
478 remove_button.setEnabled(false);
479 remove_button.setMnemonic(KeyEvent.VK_R);
480 Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
481
482 // Listeners
483 new_button.addActionListener(new NewIndexListener());
484 edit_button.addActionListener(new EditIndexListener());
485 remove_button.addActionListener(new RemoveIndexListener());
486
487 index_list.addListSelectionListener(new IndexListListener());
488
489 move_down_button.addActionListener(new MoveListener(false));
490 move_up_button.addActionListener(new MoveListener(true));
491
492 set_default_button.addActionListener(new SetDefaultListener());
493 // Layout
494 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
495 movement_pane.setLayout(new GridLayout(3,1));
496 movement_pane.add(move_up_button);
497 movement_pane.add(move_down_button);
498 movement_pane.add(set_default_button);
499
500 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
501 assigned_indexes_pane.setLayout(new BorderLayout());
502 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
503 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
504 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
505
506 button_pane.setLayout(new GridLayout(1,3,5,0));
507 button_pane.add(new_button);
508 button_pane.add(edit_button);
509 button_pane.add(remove_button);
510
511 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
512 setLayout(new BorderLayout());
513 add(assigned_indexes_pane, BorderLayout.CENTER);
514 add(button_pane, BorderLayout.SOUTH);
515
516 }
517 public void loseFocus() {}
518 public void gainFocus() {}
519 public void destroy() {}
520
521 private class IndexListListener
522 implements ListSelectionListener {
523
524 /** 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
525 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
526 */
527 public void valueChanged(ListSelectionEvent event)
528 {
529 if (event.getValueIsAdjusting()) {
530 return;
531 }
532
533 Object value = index_list.getSelectedValue();
534 if (value == null) {
535 move_down_button.setEnabled(false);
536 move_up_button.setEnabled(false);
537 remove_button.setEnabled(false);
538 edit_button.setEnabled(false);
539 set_default_button.setEnabled(false);
540 return;
541 }
542
543 // Enable the buttons appropriately
544 remove_button.setEnabled(true);
545 edit_button.setEnabled(true);
546 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
547 int i = index_list.getSelectedIndex();
548 int size = index_list.getModel().getSize();
549 move_up_button.setEnabled((i>0));
550 move_down_button.setEnabled((i<size-1));
551 }
552 }
553
554 private class IndexListRenderer
555 extends DefaultListCellRenderer {
556
557 /** Return a component that has been configured to display the specified value. */
558 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
559 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
560 if(default_index != null && default_index.equals(value)) {
561 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
562 }
563 return component;
564 }
565
566 }
567
568 private class NewIndexListener
569 implements ActionListener {
570
571 public void actionPerformed(ActionEvent event) {
572 NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
573 nip.destroy();
574 }
575 }
576
577 private class EditIndexListener
578 implements ActionListener {
579
580 public void actionPerformed(ActionEvent event) {
581 Index index = (Index) index_list.getSelectedValue();
582 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
583 nip.destroy();
584 }
585 }
586
587 private class RemoveIndexListener
588 implements ActionListener {
589
590 public void actionPerformed(ActionEvent event) {
591
592 int i = index_list.getSelectedIndex();
593 if (i != -1) {
594 removeIndex((Index) index_list.getSelectedValue());
595 }
596 int size = index_list.getModel().getSize();
597 if (i == size) {
598 i--;
599 }
600 index_list.setSelectedIndex(i);
601 // This will produce an event on the list, updating the other buttons
602 if (size == 0) {
603 // We have removed the last index, should be able to add what's filled in currently, if valid
604 // validateAddButton();
605 }
606 }
607 }
608
609 private class MoveListener
610 implements ActionListener {
611
612 private boolean move_up;
613
614 public MoveListener(boolean move_up) {
615 this.move_up = move_up;
616 }
617
618 public void actionPerformed(ActionEvent event) {
619 // Retrieve the selected index
620 Index index = (Index) index_list.getSelectedValue();
621 if(index != null) {
622 moveIndex(index, move_up);
623 // Ensure the index that moved is still selected
624 index_list.setSelectedValue(index, true);
625 index = null;
626 }
627 }
628 }
629
630 private class SetDefaultListener
631 implements ActionListener {
632
633 public void actionPerformed(ActionEvent event) {
634 Index index = (Index) index_list.getSelectedValue();
635 if(index != null) {
636 setDefault(index);
637 // This should cause a repaint of just the desired row
638 index_list.setSelectedValue(index, true);
639 }
640 set_default_button.setEnabled(false);
641 }
642 }
643
644
645
646 private class NewIndexPrompt
647 extends ModalDialog {
648
649 NewIndexPrompt new_index_prompt = null;
650
651 private CheckList source_list;
652 // mg uses a level box
653 private JComboBox level_combobox;
654 // mgpp has a allfields selector
655 private JCheckBox allfields_box;
656
657 private JButton add_button;
658 private JButton replace_button;
659 private JButton add_all_button;
660 private JButton cancel_button;
661
662 // some panels that we need to manipulate later on
663 private JPanel boxes_pane;
664 private JPanel labels_pane;
665 private JLabel level_label;
666 private JPanel allfields_pane;
667
668 private boolean mgpp_enabled = false;
669 private boolean editing = false;
670
671 public NewIndexPrompt(String build_type, Index existing_index) {
672 super(Gatherer.g_man, true);
673 new_index_prompt = this;
674
675 setModal(true);
676 setSize(PROMPT_SIZE);
677 if (existing_index != null) {
678 setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
679 editing = true;
680 } else {
681 setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
682 }
683
684 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
685 mgpp_enabled = true;
686 }
687 JPanel content_pane = (JPanel)this.getContentPane();
688 ArrayList new_data = new ArrayList();
689 new_data.add(CollectionConfiguration.TEXT_STR);
690 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
691
692 JPanel details_pane = new JPanel();
693 labels_pane = new JPanel();
694 boxes_pane = new JPanel();
695
696 JPanel main_index_pane = new JPanel();
697
698 JLabel source_label = new JLabel();
699 Dictionary.registerText(source_label, "CDM.IndexManager.Source");
700 source_list = new CheckList(false);
701 source_list.setListData(new_data);
702 Dictionary.registerTooltip(source_list, "CDM.IndexManager.Source_Tooltip");
703 source_list.addListSelectionListener(new SourceListListener());
704
705 JPanel button_pane = new JPanel();
706 if (existing_index != null) {
707 button_pane.setLayout(new GridLayout(1,2,5,0));
708 replace_button = new GLIButton();
709 replace_button.setEnabled(false);
710 Dictionary.registerBoth(replace_button, "CDM.IndexManager.Replace_Index", "CDM.IndexManager.Replace_Index_Tooltip");
711 button_pane.add(replace_button);
712 replace_button.addActionListener(new ReplaceIndexListener());
713 } else {
714 button_pane.setLayout(new GridLayout(1,3,5,0));
715 add_button = new GLIButton();
716 add_button.setEnabled(false);
717 add_button.setMnemonic(KeyEvent.VK_A);
718 Dictionary.registerBoth(add_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
719 add_button.addActionListener(new AddIndexListener());
720
721
722 add_all_button = new GLIButton();
723 add_all_button.setEnabled(true);
724 add_all_button.setMnemonic(KeyEvent.VK_L);
725 Dictionary.registerBoth(add_all_button, "CDM.IndexManager.Add_All", "CDM.IndexManager.Add_All_Tooltip");
726 add_all_button.addActionListener(new AddAllIndexActionListener());
727 button_pane.add(add_button);
728 button_pane.add(add_all_button);
729
730 }
731 cancel_button = new GLIButton();
732 cancel_button.setEnabled(true);
733 Dictionary.registerBoth(cancel_button, "General.Cancel", "General.Cancel");
734 cancel_button.addActionListener(new ActionListener() {
735 public void actionPerformed(ActionEvent event) {
736 new_index_prompt.dispose();
737 }
738 });
739 button_pane.add(cancel_button);
740
741 // Layout
742 labels_pane.setLayout(new BorderLayout());
743 labels_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5));
744 labels_pane.add(source_label, BorderLayout.CENTER);
745
746 boxes_pane.setLayout(new BorderLayout());
747 boxes_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
748
749 details_pane.setLayout(new BorderLayout());
750 details_pane.add(labels_pane, BorderLayout.WEST);
751 details_pane.add(boxes_pane, BorderLayout.CENTER);
752
753
754 // do type specific stuff
755 if (!mgpp_enabled) {
756
757 level_label = new JLabel();
758 Dictionary.registerText(level_label, "CDM.IndexManager.Level");
759 level_combobox = new JComboBox();
760 level_combobox.setPreferredSize(FIELD_SIZE);
761 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
762 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
763 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
764 level_combobox.setEditable(false);
765 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
766 labels_pane.add(level_label, BorderLayout.SOUTH);
767 boxes_pane.add(level_combobox, BorderLayout.SOUTH);
768
769 } else {
770 allfields_pane = new JPanel();
771 allfields_box = new JCheckBox();
772 JLabel allfields_label = new JLabel();
773 Dictionary.registerText(allfields_label, "CDM.IndexManager.Allfields_Index");
774 allfields_pane.setLayout(new BorderLayout());
775 allfields_pane.add(allfields_box, BorderLayout.WEST);
776 allfields_pane.add(allfields_label, BorderLayout.CENTER);
777
778
779 }
780 // if we are editing, fill in the controls
781 if (existing_index !=null) {
782 ArrayList sources = existing_index.getSources();
783 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
784 source_list.setEnabled(false);
785 allfields_box.setSelected(true);
786 } else {
787 source_list.setTickedObjects(sources.toArray());
788 source_list.setEnabled(true);
789 }
790 if (!mgpp_enabled) {
791 level_combobox.setSelectedIndex(existing_index.getLevel());
792 }
793
794 }
795 content_pane.setLayout(new BorderLayout());
796 content_pane.add(details_pane, BorderLayout.CENTER);
797 content_pane.add(button_pane, BorderLayout.SOUTH);
798
799 // Display on screen.
800 Dimension screen_size = Configuration.screen_size;
801 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
802 screen_size = null;
803 setVisible(true);
804
805 }
806
807 /** Method which actually forces the dialog to be shown on screen.
808 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
809 */
810 public boolean display() {
811 setVisible(true);
812 return true;
813 // return success;
814 }
815
816 public void destroy() {
817 }
818
819 private class AddIndexListener
820 implements ActionListener
821 {
822 public void actionPerformed(ActionEvent event)
823 {
824 Index index;
825 if (mgpp_enabled && allfields_box.isSelected()) {
826 ArrayList sources = new ArrayList();
827 sources.add(ALLFIELDS);
828 index = new Index(sources);
829 }
830 else if (!source_list.isNothingTicked()) {
831 ArrayList sources = source_list.getTicked();
832 if(mgpp_enabled) {
833 index = new Index(sources);
834 } else {
835 index = new Index(level_combobox.getSelectedIndex(), sources);
836 }
837 } else {
838 return;
839 }
840
841 // Before we add the index to the model, we have to add a default collection metadata for this
842 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
843 metadatum.setValue(index.getID());
844
845 // Finally, add the index
846 addIndex(index, metadatum);
847 index_list.setSelectedValue(index, true);
848 new_index_prompt.dispose();
849 }
850 }
851
852 /** add all sources as indexes. for MG, this adds them in one combined
853 index, for MGPP this adds them as individual indexes (fields) */
854 private class AddAllIndexActionListener
855 implements ActionListener {
856
857 public void actionPerformed(ActionEvent event) {
858 ArrayList all_sources = source_list.getAll();
859 if (!mgpp_enabled) {
860 Index index = new Index(level_combobox.getSelectedIndex(), all_sources);
861 if (!index_model.contains(index)) {
862 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
863 metadatum.setValue("all fields"); // need a good name
864 addIndex(index, metadatum);
865 }
866 all_sources = null;
867 index = null;
868 return;
869 }
870
871 ArrayList new_sources = new ArrayList();
872 for(int i = 0; i < all_sources.size(); i++) {
873 Object source = all_sources.get(i);
874
875 // Create new index
876 new_sources.clear();
877 new_sources.add(source);
878 Index index = new Index(new_sources);
879 if(!index_model.contains(index)) {
880 // Determine the metadatum value
881 String name = source.toString();
882 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
883 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
884 }
885 // Create new metadatum
886 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
887 metadatum.setValue(name);
888 name = null;
889 // Assign new index
890 addIndex(index, metadatum);
891 }
892 source = null;
893 index = null;
894 }
895 new_sources = null;
896 new_index_prompt.dispose();
897
898 }
899 }
900
901 private class SourceListListener
902 implements ListSelectionListener {
903
904 public void valueChanged(ListSelectionEvent event) {
905 if (event.getValueIsAdjusting()) {
906 return;
907 }
908 boolean enabled = !source_list.isNothingTicked();
909 if (editing) {
910 replace_button.setEnabled(enabled);
911 } else {
912 add_button.setEnabled(enabled);
913 }
914
915 }
916 }
917
918
919 private class ReplaceIndexListener
920 implements ActionListener {
921
922 public void actionPerformed(ActionEvent event) {
923
924 ArrayList sources;
925 Index index = null;
926 if (mgpp_enabled && allfields_box.isSelected()) {
927 sources = new ArrayList();
928 sources.add(ALLFIELDS);
929 index = new Index(sources);
930 }
931 else if (!source_list.isNothingTicked()) {
932 sources = source_list.getTicked();
933 if (mgpp_enabled) {
934 index = new Index(sources);
935 } else {
936 index = new Index(level_combobox.getSelectedIndex(), sources);
937 }
938 }
939 if (index != null) {
940 // Create the new collection meta
941 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
942 metadatum.setValue(index.getID());
943
944 // replace the index
945 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
946 index_list.setSelectedValue(index, true);
947
948 }
949 new_index_prompt.dispose();
950
951 }
952 }
953 }
954 }
955}
Note: See TracBrowser for help on using the repository browser.