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

Last change on this file since 12284 was 12167, checked in by kjdon, 18 years ago

add and replace buttons now one button called add_or_replace_button; replace all and select all buttons now one button called add_or_replace_all_button; added in allfields selector

  • Property svn:keywords set to Author Date Id Revision
File size: 32.9 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(Dictionary.get("CDM.IndexManager.Indexes"));
446 index_list = new JList(index_model);
447 index_list.setCellRenderer(new IndexListRenderer());
448 index_list.setVisibleRowCount(6);
449 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
450
451 JPanel movement_pane = new JPanel();
452 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
453 move_up_button.setEnabled(false);
454
455 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
456 move_down_button.setEnabled(false);
457
458 set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
459 set_default_button.setEnabled(false);
460
461 JPanel button_pane = new JPanel();
462 new_button = new GLIButton(Dictionary.get("CDM.IndexManager.New_Index"), Dictionary.get("CDM.IndexManager.New_Index_Tooltip"));
463 new_button.setEnabled(true);
464
465 edit_button = new GLIButton(Dictionary.get("CDM.IndexManager.Edit_Index"), Dictionary.get("CDM.IndexManager.Edit_Index_Tooltip"));
466 edit_button.setEnabled(false);
467
468 remove_button = new GLIButton(Dictionary.get("CDM.IndexManager.Remove_Index"), Dictionary.get("CDM.IndexManager.Remove_Index_Tooltip"));
469 remove_button.setEnabled(false);
470
471 // Listeners
472 new_button.addActionListener(new NewIndexListener());
473 edit_button.addActionListener(new EditIndexListener());
474 remove_button.addActionListener(new RemoveIndexListener());
475
476 index_list.addListSelectionListener(new IndexListListener());
477
478 move_down_button.addActionListener(new MoveListener(false));
479 move_up_button.addActionListener(new MoveListener(true));
480
481 set_default_button.addActionListener(new SetDefaultListener());
482 // Layout
483 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
484 movement_pane.setLayout(new GridLayout(3,1));
485 movement_pane.add(move_up_button);
486 movement_pane.add(move_down_button);
487 movement_pane.add(set_default_button);
488
489 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
490 assigned_indexes_pane.setLayout(new BorderLayout());
491 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
492 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
493 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
494
495 button_pane.setLayout(new GridLayout(1,3,5,0));
496 button_pane.add(new_button);
497 button_pane.add(edit_button);
498 button_pane.add(remove_button);
499
500 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
501 setLayout(new BorderLayout());
502 add(assigned_indexes_pane, BorderLayout.CENTER);
503 add(button_pane, BorderLayout.SOUTH);
504
505 }
506 public void loseFocus() {}
507 public void gainFocus() {}
508 public void destroy() {}
509
510 private class IndexListListener
511 implements ListSelectionListener {
512
513 /** 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
514 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
515 */
516 public void valueChanged(ListSelectionEvent event)
517 {
518 if (event.getValueIsAdjusting()) {
519 return;
520 }
521
522 Object value = index_list.getSelectedValue();
523 if (value == null) {
524 move_down_button.setEnabled(false);
525 move_up_button.setEnabled(false);
526 remove_button.setEnabled(false);
527 edit_button.setEnabled(false);
528 set_default_button.setEnabled(false);
529 return;
530 }
531
532 // Enable the buttons appropriately
533 remove_button.setEnabled(true);
534 edit_button.setEnabled(true);
535 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
536 int i = index_list.getSelectedIndex();
537 int size = index_list.getModel().getSize();
538 move_up_button.setEnabled((i>0));
539 move_down_button.setEnabled((i<size-1));
540 }
541 }
542
543 private class IndexListRenderer
544 extends DefaultListCellRenderer {
545
546 /** Return a component that has been configured to display the specified value. */
547 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
548 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
549 if(default_index != null && default_index.equals(value)) {
550 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
551 }
552 return component;
553 }
554
555 }
556
557 private class NewIndexListener
558 implements ActionListener {
559
560 public void actionPerformed(ActionEvent event) {
561 NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
562 nip.destroy();
563 }
564 }
565
566 private class EditIndexListener
567 implements ActionListener {
568
569 public void actionPerformed(ActionEvent event) {
570 Index index = (Index) index_list.getSelectedValue();
571 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
572 nip.destroy();
573 }
574 }
575
576 private class RemoveIndexListener
577 implements ActionListener {
578
579 public void actionPerformed(ActionEvent event) {
580
581 int i = index_list.getSelectedIndex();
582 if (i != -1) {
583 removeIndex((Index) index_list.getSelectedValue());
584 }
585 int size = index_list.getModel().getSize();
586 if (i == size) {
587 i--;
588 }
589 index_list.setSelectedIndex(i);
590 // This will produce an event on the list, updating the other buttons
591 if (size == 0) {
592 // We have removed the last index, should be able to add what's filled in currently, if valid
593 // validateAddButton();
594 }
595 }
596 }
597
598 private class MoveListener
599 implements ActionListener {
600
601 private boolean move_up;
602
603 public MoveListener(boolean move_up) {
604 this.move_up = move_up;
605 }
606
607 public void actionPerformed(ActionEvent event) {
608 // Retrieve the selected index
609 Index index = (Index) index_list.getSelectedValue();
610 if(index != null) {
611 moveIndex(index, move_up);
612 // Ensure the index that moved is still selected
613 index_list.setSelectedValue(index, true);
614 index = null;
615 }
616 }
617 }
618
619 private class SetDefaultListener
620 implements ActionListener {
621
622 public void actionPerformed(ActionEvent event) {
623 Index index = (Index) index_list.getSelectedValue();
624 if(index != null) {
625 setDefault(index);
626 // This should cause a repaint of just the desired row
627 index_list.setSelectedValue(index, true);
628 }
629 set_default_button.setEnabled(false);
630 }
631 }
632
633
634
635 private class NewIndexPrompt
636 extends ModalDialog {
637
638 NewIndexPrompt new_index_prompt = null;
639
640 private CheckList source_list;
641 // mg uses a level box
642 private JComboBox level_combobox;
643 // mgpp has a allfields selector
644 private JCheckBox allfields_box;
645
646 private JButton add_or_replace_button;
647 private JButton add_or_select_all_button;
648 //private JButton select_all_button;
649 //private JButton replace_button;
650 private JButton cancel_button;
651
652 private boolean mgpp_enabled = false;
653 private boolean editing = false;
654
655 public NewIndexPrompt(String build_type, Index existing_index) {
656 super(Gatherer.g_man, true);
657 new_index_prompt = this;
658
659 setModal(true);
660 setSize(PROMPT_SIZE);
661 if (existing_index != null) {
662 setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
663 editing = true;
664 } else {
665 setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
666 }
667
668 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
669 mgpp_enabled = true;
670 }
671 JPanel content_pane = (JPanel)this.getContentPane();
672 ArrayList new_data = new ArrayList();
673 new_data.add(CollectionConfiguration.TEXT_STR);
674 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
675
676 JPanel details_pane = new JPanel();
677 JPanel main_index_pane = new JPanel();
678
679
680 JLabel source_label = new JLabel(Dictionary.get("CDM.IndexManager.Source"));
681 source_list = new CheckList(false);
682 source_list.setListData(new_data);
683 source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
684 source_list.addListSelectionListener(new SourceListListener());
685
686 JPanel button_pane = new JPanel();
687 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
688 // three buttons for new index, or replace mg index, two buttons otherwise
689 if (existing_index == null || !mgpp_enabled) {
690 button_pane.setLayout(new GridLayout(1,3,5,0));
691 } else {
692 button_pane.setLayout(new GridLayout(1,2,5,0));
693 }
694
695 if (existing_index != null) {
696 add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Replace_Index"), Dictionary.get("CDM.IndexManager.Replace_Index_Tooltip"));
697 add_or_replace_button.addActionListener(new ReplaceIndexListener());
698 } else {
699 add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_Index"), Dictionary.get("CDM.IndexManager.Add_Index_Tooltip"));
700 add_or_replace_button.addActionListener(new AddIndexListener());
701 }
702
703 add_or_replace_button.setEnabled(false);
704 button_pane.add(add_or_replace_button);
705
706 if (!mgpp_enabled) {
707 // its select all
708 add_or_select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
709 add_or_select_all_button.setEnabled(true);
710 add_or_select_all_button.addActionListener(new SelectAllActionListener());
711 button_pane.add(add_or_select_all_button);
712 } else if (existing_index == null) {
713 // mgpp, and new index
714 add_or_select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
715 add_or_select_all_button.setEnabled(true);
716 add_or_select_all_button.addActionListener(new AddAllIndexActionListener());
717 button_pane.add(add_or_select_all_button);
718
719 }
720
721 // always have a cancel button
722 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
723 cancel_button.setEnabled(true);
724 cancel_button.addActionListener(new ActionListener() {
725 public void actionPerformed(ActionEvent event) {
726 new_index_prompt.dispose();
727 }
728 });
729 button_pane.add(cancel_button);
730
731 //Layout
732 details_pane.setLayout(new BorderLayout(10,10));
733 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
734 details_pane.add(source_label, BorderLayout.NORTH);
735 details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
736
737
738 // do type specific stuff
739 if (mgpp_enabled) {
740 // allfields
741 allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
742 allfields_box.addItemListener(new AllFieldsBoxListener());
743 //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
744 details_pane.add(allfields_box, BorderLayout.SOUTH);
745
746
747 } else {
748 // index level
749 JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
750 level_combobox = new JComboBox();
751 level_combobox.setPreferredSize(FIELD_SIZE);
752 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
753 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
754 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
755 level_combobox.setEditable(false);
756 level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
757 JPanel level_pane = new JPanel();
758 level_pane.setLayout(new BorderLayout());
759 level_pane.add(level_label, BorderLayout.WEST);
760 level_pane.add(level_combobox, BorderLayout.CENTER);
761 details_pane.add(level_pane, BorderLayout.SOUTH);
762
763 }
764 // if we are editing, fill in the controls
765 if (existing_index !=null) {
766 ArrayList sources = existing_index.getSources();
767 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
768 allfields_box.setSelected(true);
769 source_list.setEnabled(false);
770 } else {
771 source_list.setTickedObjects(sources.toArray());
772 source_list.setEnabled(true);
773 }
774 if (!mgpp_enabled) {
775 level_combobox.setSelectedIndex(existing_index.getLevel());
776 }
777
778 }
779 content_pane.setLayout(new BorderLayout());
780 content_pane.add(details_pane, BorderLayout.CENTER);
781 content_pane.add(button_pane, BorderLayout.SOUTH);
782
783 // Display on screen.
784 Dimension screen_size = Configuration.screen_size;
785 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
786 screen_size = null;
787 setVisible(true);
788
789 }
790
791 /** Method which actually forces the dialog to be shown on screen.
792 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
793 */
794 public boolean display() {
795 setVisible(true);
796 return true;
797 // return success;
798 }
799
800 public void destroy() {
801 }
802
803 // Checks that specified index not already in the collection
804 private void validateAddOrReplaceButton() {
805 Index index;
806 ArrayList sources;
807 if (mgpp_enabled && allfields_box.isSelected()) {
808 sources = new ArrayList();
809 sources.add(ALLFIELDS);
810 index = new Index(sources);
811
812 } else if (!source_list.isNothingTicked()) {
813 Object object[] = source_list.getTicked().toArray();
814 sources = new ArrayList();
815 for(int i = 0; i < object.length; i++) {
816 sources.add(object[i]);
817 }
818 object = null;
819
820 if (mgpp_enabled) {
821 index = new Index(sources);
822 } else {
823 index = new Index(level_combobox.getSelectedIndex(), sources);
824 }
825 } else {
826 // nothing selected
827 add_or_replace_button.setEnabled(false);
828 return;
829 }
830
831 sources = null;
832 if (index_model.contains(index)) {
833 add_or_replace_button.setEnabled(false);
834 }
835 else {
836 add_or_replace_button.setEnabled(true);
837 }
838
839 }
840 private class AddIndexListener
841 implements ActionListener
842 {
843 public void actionPerformed(ActionEvent event)
844 {
845 Index index;
846 if (mgpp_enabled && allfields_box.isSelected()) {
847 ArrayList sources = new ArrayList();
848 sources.add(ALLFIELDS);
849 index = new Index(sources);
850 }
851 else if (!source_list.isNothingTicked()) {
852 ArrayList sources = source_list.getTicked();
853 if(mgpp_enabled) {
854 index = new Index(sources);
855 } else {
856 index = new Index(level_combobox.getSelectedIndex(), sources);
857 }
858 } else {
859 return;
860 }
861
862 // Before we add the index to the model, we have to add a default collection metadata for this
863 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
864 metadatum.setValue(index.getID());
865
866 // Finally, add the index
867 addIndex(index, metadatum);
868 index_list.setSelectedValue(index, true);
869 new_index_prompt.dispose();
870 }
871 }
872
873 /** add all sources as separate indexes (fields). */
874 private class AddAllIndexActionListener
875 implements ActionListener {
876
877 public void actionPerformed(ActionEvent event) {
878 ArrayList all_sources = source_list.getAll();
879 ArrayList new_sources = new ArrayList();
880 for(int i = 0; i < all_sources.size(); i++) {
881 Object source = all_sources.get(i);
882
883 // Create new index
884 new_sources.clear();
885 new_sources.add(source);
886 Index index = new Index(new_sources);
887 if(!index_model.contains(index)) {
888 // Determine the metadatum value
889 String name = source.toString();
890 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
891 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
892 }
893 // Create new metadatum
894 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
895 metadatum.setValue(name);
896 name = null;
897 // Assign new index
898 addIndex(index, metadatum);
899 }
900 source = null;
901 index = null;
902 }
903 new_sources = null;
904 new_index_prompt.dispose();
905
906 }
907 }
908
909 private class AllFieldsBoxListener
910 implements ItemListener {
911
912 public void itemStateChanged(ItemEvent event) {
913 if (event.getStateChange() == ItemEvent.SELECTED) {
914 source_list.setEnabled(false);
915 } else if (event.getStateChange() == ItemEvent.DESELECTED) {
916 source_list.setEnabled(true);
917 }
918 validateAddOrReplaceButton();
919 }
920
921 }
922
923 private class SelectAllActionListener
924 implements ActionListener {
925
926 public void actionPerformed(ActionEvent event) {
927 source_list.setAllTicked();
928 validateAddOrReplaceButton();
929 }
930 }
931
932 private class SourceListListener
933 implements ListSelectionListener {
934
935 public void valueChanged(ListSelectionEvent event) {
936 if (event.getValueIsAdjusting()) {
937 return;
938 }
939 validateAddOrReplaceButton();
940 }
941 }
942
943
944 private class ReplaceIndexListener
945 implements ActionListener {
946
947 public void actionPerformed(ActionEvent event) {
948
949 ArrayList sources;
950 Index index = null;
951 if (!source_list.isNothingTicked()) {
952 sources = source_list.getTicked();
953 if (mgpp_enabled) {
954 index = new Index(sources);
955 } else {
956 index = new Index(level_combobox.getSelectedIndex(), sources);
957 }
958 }
959 if (index != null) {
960 // Create the new collection meta
961 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
962 metadatum.setValue(index.getID());
963
964 // replace the index
965 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
966 index_list.setSelectedValue(index, true);
967
968 }
969 new_index_prompt.dispose();
970
971 }
972 }
973 }
974 }
975}
Note: See TracBrowser for help on using the repository browser.