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

Last change on this file since 18886 was 18886, checked in by anna, 15 years ago

Changes for ticket #369. Display translations of search metadata fields if they meets certain criteria and translation of search levels.

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