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

Last change on this file since 18412 was 18412, checked in by kjdon, 15 years ago

more modifications for RTL GLI, thanks to Amin Hedjazi

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