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

Last change on this file since 12131 was 12127, checked in by kjdon, 18 years ago

changed the layout of the NewIndexPrompt. also removed allfields box stuff. this needss to be a button, but I am not sure where to put it yet

  • Property svn:keywords set to Author Date Id Revision
File size: 31.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Dictionary;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.gui.DesignPaneHeader;
39import org.greenstone.gatherer.gui.GComboBox;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.gui.ModalDialog;
42import org.greenstone.gatherer.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
644 private JButton add_button;
645 private JButton add_all_button;
646 private JButton select_all_button;
647 private JButton replace_button;
648 private JButton cancel_button;
649
650 // some panels that we need to manipulate later on
651 private JPanel boxes_pane;
652 private JPanel labels_pane;
653 private JLabel level_label;
654
655 private boolean mgpp_enabled = false;
656 private boolean editing = false;
657
658 public NewIndexPrompt(String build_type, Index existing_index) {
659 super(Gatherer.g_man, true);
660 new_index_prompt = this;
661
662 setModal(true);
663 setSize(PROMPT_SIZE);
664 if (existing_index != null) {
665 setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
666 editing = true;
667 } else {
668 setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
669 }
670
671 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
672 mgpp_enabled = true;
673 }
674 JPanel content_pane = (JPanel)this.getContentPane();
675 ArrayList new_data = new ArrayList();
676 new_data.add(CollectionConfiguration.TEXT_STR);
677 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
678
679 JPanel details_pane = new JPanel();
680 JPanel main_index_pane = new JPanel();
681
682
683 JLabel source_label = new JLabel(Dictionary.get("CDM.IndexManager.Source"));
684 source_list = new CheckList(false);
685 source_list.setListData(new_data);
686 source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
687 source_list.addListSelectionListener(new SourceListListener());
688
689 JPanel button_pane = new JPanel();
690 // three buttons for new index, or replace mg index, two buttons otherwise
691 if (existing_index == null || !mgpp_enabled) {
692 button_pane.setLayout(new GridLayout(1,3,5,0));
693 } else {
694 button_pane.setLayout(new GridLayout(1,2,5,0));
695 }
696
697 if (existing_index != null) {
698 replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Replace_Index"), Dictionary.get("CDM.IndexManager.Replace_Index_Tooltip"));
699 replace_button.setEnabled(false);
700 replace_button.addActionListener(new ReplaceIndexListener());
701 button_pane.add(replace_button);
702 } else {
703 add_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_Index"), Dictionary.get("CDM.IndexManager.Add_Index_Tooltip"));
704 add_button.setEnabled(false);
705 add_button.addActionListener(new AddIndexListener());
706 button_pane.add(add_button);
707 }
708
709 if (!mgpp_enabled) {
710 select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
711 select_all_button.setEnabled(true);
712 select_all_button.addActionListener(new SelectAllActionListener());
713 button_pane.add(select_all_button);
714 } else if (existing_index == null) {
715 // mgpp, and new index
716 add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
717 add_all_button.setEnabled(true);
718 add_all_button.addActionListener(new AddAllIndexActionListener());
719 button_pane.add(add_all_button);
720
721 }
722 // always have a cancel button
723 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
724 cancel_button.setEnabled(true);
725 cancel_button.addActionListener(new ActionListener() {
726 public void actionPerformed(ActionEvent event) {
727 new_index_prompt.dispose();
728 }
729 });
730 button_pane.add(cancel_button);
731
732 //Layout
733 details_pane.setLayout(new BorderLayout(10,10));
734 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
735 details_pane.add(source_label, BorderLayout.NORTH);
736 details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
737
738
739 // do type specific stuff
740 if (!mgpp_enabled) {
741
742 level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
743 level_combobox = new JComboBox();
744 level_combobox.setPreferredSize(FIELD_SIZE);
745 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
746 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
747 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
748 level_combobox.setEditable(false);
749 level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
750 JPanel level_pane = new JPanel();
751 level_pane.setLayout(new BorderLayout());
752 level_pane.add(level_label, BorderLayout.WEST);
753 level_pane.add(level_combobox, BorderLayout.CENTER);
754 details_pane.add(level_pane, BorderLayout.SOUTH);
755
756 }
757 // if we are editing, fill in the controls
758 if (existing_index !=null) {
759 ArrayList sources = existing_index.getSources();
760 source_list.setTickedObjects(sources.toArray());
761 source_list.setEnabled(true);
762 if (!mgpp_enabled) {
763 level_combobox.setSelectedIndex(existing_index.getLevel());
764 }
765
766 }
767 content_pane.setLayout(new BorderLayout());
768 content_pane.add(details_pane, BorderLayout.CENTER);
769 content_pane.add(button_pane, BorderLayout.SOUTH);
770
771 // Display on screen.
772 Dimension screen_size = Configuration.screen_size;
773 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
774 screen_size = null;
775 setVisible(true);
776
777 }
778
779 /** Method which actually forces the dialog to be shown on screen.
780 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
781 */
782 public boolean display() {
783 setVisible(true);
784 return true;
785 // return success;
786 }
787
788 public void destroy() {
789 }
790
791 private class AddIndexListener
792 implements ActionListener
793 {
794 public void actionPerformed(ActionEvent event)
795 {
796 Index index;
797 if (!source_list.isNothingTicked()) {
798 ArrayList sources = source_list.getTicked();
799 if(mgpp_enabled) {
800 index = new Index(sources);
801 } else {
802 index = new Index(level_combobox.getSelectedIndex(), sources);
803 }
804 } else {
805 return;
806 }
807
808 // Before we add the index to the model, we have to add a default collection metadata for this
809 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
810 metadatum.setValue(index.getID());
811
812 // Finally, add the index
813 addIndex(index, metadatum);
814 index_list.setSelectedValue(index, true);
815 new_index_prompt.dispose();
816 }
817 }
818
819 /** add all sources as separate indexes (fields). */
820 private class AddAllIndexActionListener
821 implements ActionListener {
822
823 public void actionPerformed(ActionEvent event) {
824 ArrayList all_sources = source_list.getAll();
825 ArrayList new_sources = new ArrayList();
826 for(int i = 0; i < all_sources.size(); i++) {
827 Object source = all_sources.get(i);
828
829 // Create new index
830 new_sources.clear();
831 new_sources.add(source);
832 Index index = new Index(new_sources);
833 if(!index_model.contains(index)) {
834 // Determine the metadatum value
835 String name = source.toString();
836 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
837 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
838 }
839 // Create new metadatum
840 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
841 metadatum.setValue(name);
842 name = null;
843 // Assign new index
844 addIndex(index, metadatum);
845 }
846 source = null;
847 index = null;
848 }
849 new_sources = null;
850 new_index_prompt.dispose();
851
852 }
853 }
854
855 private class SelectAllActionListener
856 implements ActionListener {
857
858 public void actionPerformed(ActionEvent event) {
859 source_list.setAllTicked();
860 if (editing) {
861 replace_button.setEnabled(true);
862 } else {
863 add_button.setEnabled(true);
864 }
865 }
866 }
867
868 private class SourceListListener
869 implements ListSelectionListener {
870
871 public void valueChanged(ListSelectionEvent event) {
872 if (event.getValueIsAdjusting()) {
873 return;
874 }
875 boolean enabled = !source_list.isNothingTicked();
876 if (editing) {
877 replace_button.setEnabled(enabled);
878 } else {
879 add_button.setEnabled(enabled);
880 }
881
882 }
883 }
884
885
886 private class ReplaceIndexListener
887 implements ActionListener {
888
889 public void actionPerformed(ActionEvent event) {
890
891 ArrayList sources;
892 Index index = null;
893 if (!source_list.isNothingTicked()) {
894 sources = source_list.getTicked();
895 if (mgpp_enabled) {
896 index = new Index(sources);
897 } else {
898 index = new Index(level_combobox.getSelectedIndex(), sources);
899 }
900 }
901 if (index != null) {
902 // Create the new collection meta
903 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
904 metadatum.setValue(index.getID());
905
906 // replace the index
907 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
908 index_list.setSelectedValue(index, true);
909
910 }
911 new_index_prompt.dispose();
912
913 }
914 }
915 }
916 }
917}
Note: See TracBrowser for help on using the repository browser.