source: main/trunk/gli/src/org/greenstone/gatherer/cdm/BaseIndexManager.java@ 36150

Last change on this file since 36150 was 36150, checked in by kjdon, 2 years ago

took the basic functions from IndexMnaager and put them into BaseINdexManager. SortFieldManager and FacetManager inherit from this, as they basically have all the same functionality, just different element names and gui strings

File size: 27.8 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 is a base class, inherited by indexes, sortfields, facets, which all pretty much do the same thing just with different element names. It is responsible for storing the indexes/sorts/facets which have been assigned to this collection and the default index (if there is one), 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 */
52public abstract class BaseIndexManager
53 extends DOMProxyListModel
54 implements BuildTypeManager.BuildTypeListener {
55
56 /** A reference to ourselves so our inner methods have access. */
57 protected DOMProxyListModel index_model = null;
58 /** The default index. */
59 protected Index default_index = null;
60
61 protected Control controls = null;
62 protected String build_type = null;
63
64 // XML element names
65 protected String index_element_name = null;
66 protected String index_default_element_name = null;
67
68 // Disctionary key strings for the interface - set these in subclass
69 protected String controls_title_key = null;
70 protected String new_button_key = "CDM.IndexManager.New";
71 protected String new_button_tooltip_key = null;
72 protected String edit_button_key = "CDM.IndexManager.Edit";
73 protected String edit_button_tooltip_key = null;
74 protected String remove_button_key = "CDM.IndexManager.Remove";
75 protected String remove_button_tooltip_key = null;
76
77 protected String nip_new_index_key = null;
78 protected String nip_edit_index_key = null;
79 protected String nip_source_label_key = null; //"CDM.IndexManager.Source"
80
81 protected String nip_add_index_button_key = null;//"CDM.IndexManager.Add_Index"
82 protected String nip_add_index_tooltip_key = null; //"CDM.IndexManager.Add_Index_Tooltip"
83 protected String nip_replace_index_button_key = null; //"CDM.IndexManager.Replace_Index"
84 protected String nip_replace_index_tooltip_key = null; //"CDM.IndexManager.Replace_Index_Tooltip"
85
86 static final protected Dimension PROMPT_SIZE = new Dimension(400,400);
87
88 public BaseIndexManager(Element indexes, String current_build_type, String index_element_name, String index_default_element_name, Index class_type) {
89
90 super(indexes, index_element_name, class_type);
91 DebugStream.println(this.getClass().getSimpleName()+": " + getSize() + " items parsed.");
92 this.index_element_name = index_element_name;
93 this.index_default_element_name = index_default_element_name;
94
95 index_model = this;
96
97 // Parse and retrieve the default index - if we should have one
98 if (this.index_default_element_name != null) {
99 NodeList default_index_elements = CollectionConfiguration.getElementsByTagName(this.index_default_element_name);
100 if(default_index_elements.getLength() > 0) {
101 default_index = createIndex((Element)default_index_elements.item(0));
102 }
103 }
104 build_type = current_build_type;
105 }
106
107 private Index createIndex(Element elem) {
108 return (Index)getClassType().create(elem);
109 }
110
111 protected Index createIndex(ArrayList sources) {
112 return (Index)((Index)getClassType()).create(sources);
113 }
114
115 /** Method to add a new index.
116 * @param index The <strong>Index</strong> to add.
117 * @see org.greenstone.gatherer.Gatherer
118 * @see org.greenstone.gatherer.collection.CollectionManager
119 */
120 private void addIndex(Index index, CollectionMeta metadatum) {
121 ///ystem.err.println("Adding an index: " + index.toString());
122 if(!contains(index)) {
123 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
124 // Retrieve the currently last index
125 if(getSize() > 0) {
126 Index last_index = (Index)getElementAt(getSize() - 1);
127 addAfter(index, last_index);
128
129 }
130 else {
131 add(index);
132 // Also set this index as the default one,
133 setDefault(index);
134 }
135 }
136 else {
137 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
138 }
139 }
140
141 /** change based on indexing type */
142 public void buildTypeChanged(String new_build_type) {
143
144 }
145
146 public Control getControls() {
147 if (controls == null) {
148 controls = new IndexControl();
149 }
150 return controls;
151 }
152
153 /** Method to retrieve a certain index, as referenced by an index number.
154 * @param index An <i>int</i> which indicates the position of the desired index.
155 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
156 */
157 public Index getIndex(int index) {
158 if(0 <= index && index < getSize()) {
159 return (Index)getElementAt(index);
160 }
161 return null;
162 }
163
164 /** Method to retrieve a certain index, given its id.
165 * @param id the id of the index as a String
166 * @return the Index that matches id, or null if no such index exists
167 */
168 public Index getIndex(String id) {
169 int size = getSize();
170 for(int i = 0; i < size; i++) {
171 Index index = (Index) getElementAt(i);
172 if(index.getID().equals(id)) {
173 return index;
174 }
175 }
176 return null;
177 }
178
179 public ArrayList getIndexes() {
180 return children();
181 }
182
183
184
185 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
186 * @param mode the new mode as an int
187 */
188 public void modeChanged(int mode) {
189
190 }
191
192 private void moveIndex(Index index, boolean move_up)
193 {
194 // Determine the current position of the index
195 int position = indexOf(index);
196 // 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.
197 if(position == -1) {
198 return;
199 }
200 if(position == 0 && move_up) {
201 return;
202 }
203 if(position == (getSize()) - 1 && !move_up) {
204 return;
205 }
206
207 // Ok, move up
208 if (move_up) {
209 position--;
210 remove(index);
211 add(position, index);
212 }
213
214 // Or move down
215 else {
216 position++;
217 remove(index);
218 add(position, index);
219 }
220 }
221
222
223
224 /** Method to remove a certain index.
225 * @param index the Index to remove.
226 * @see org.greenstone.gatherer.Gatherer
227 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
228 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
229 * @see org.greenstone.gatherer.collection.CollectionManager
230 */
231 private void removeIndex(Index index) {
232 if(index != null) {
233 // Remove any current metadata from this index
234 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + index.getID());
235 // Remove the index
236 remove(index);
237 // Check if the index removed happens to be the default index
238 if(default_index != null && default_index.equals(index)) {
239 // If so our first solution is to set the first index to be default
240 if(getSize() > 0) {
241 Index another_index = (Index) getElementAt(0);
242 setDefault(another_index);
243 another_index = null;
244 }
245 else {
246 default_index.setAssigned(false);
247 }
248 }
249 }
250 }
251
252
253 /* replace an index in the list. new index may have the same sources but a
254 different name, or may be a new index altogether */
255 private void replaceIndex(Index old_index, Index new_index,
256 CollectionMeta coll_meta) {
257 if (old_index == null || new_index == null || coll_meta == null) {
258 return;
259 }
260 if (!old_index.getID().equals(new_index.getID()) && contains(new_index)) {
261 // shoudl we output an error??
262 return;
263 }
264 // Remove the old index coll meta
265 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + old_index.getID());
266 // Add the new coll meta
267 CollectionDesignManager.collectionmeta_manager.addMetadatum(coll_meta);
268
269 // get the position of the old one
270 int position = indexOf(old_index);
271 // remove it
272 remove(old_index);
273 // add the new one at that position
274 add(position, new_index);
275 }
276
277
278 /** Method to set the default index.
279 * @param index the new default Index
280 * @see org.greenstone.gatherer.Gatherer
281 * @see org.greenstone.gatherer.collection.CollectionManager
282 */
283 public void setDefault(Index index) {
284 if (this.index_default_element_name == null) {
285 return; // no default needed
286 }
287 if(index != null) {
288 if(default_index == null ) {
289 // Create the default index element, and place immediately after indexes element.
290 Element default_index_element = root.getOwnerDocument().createElement(this.index_default_element_name);
291 default_index = createIndex(default_index_element);
292
293 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
294 if(target_node != null) {
295 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
296 }
297 else {
298 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
299 }
300 }
301 default_index.setAssigned(true);
302 default_index.setSources(index.getSources());
303
304 }
305 else {
306 if(default_index != null) {
307 default_index.setAssigned(false);
308 }
309 }
310 }
311
312
313 private class IndexControl
314 extends JPanel
315 implements Control {
316
317 private JList index_list;
318 private JButton move_down_button;
319 private JButton move_up_button;
320 private JButton set_default_button;
321
322 private JButton new_button;
323 private JButton edit_button;
324 private JButton remove_button;
325
326 private boolean has_default = false;
327 public IndexControl() {
328 super();
329 this.setComponentOrientation(Dictionary.getOrientation());
330 if (index_default_element_name != null) {
331 has_default = true;
332 }
333 // Creation
334 JPanel assigned_indexes_pane = new JPanel();
335 assigned_indexes_pane.setComponentOrientation(Dictionary.getOrientation());
336
337 JLabel index_label = new JLabel(Dictionary.get(controls_title_key));
338 index_label.setComponentOrientation(Dictionary.getOrientation());
339
340 index_list = new JList(index_model);
341 index_list.setCellRenderer(new IndexListRenderer());
342 index_list.setVisibleRowCount(6);
343 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
344 index_list.addMouseListener(new ClickListener());
345 index_list.setComponentOrientation(Dictionary.getOrientation());
346
347 JPanel movement_pane = new JPanel();
348 movement_pane.setComponentOrientation(Dictionary.getOrientation());
349
350 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
351 move_up_button.setEnabled(false);
352
353 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
354 move_down_button.setEnabled(false);
355
356 set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
357 set_default_button.setEnabled(false);
358
359 JPanel button_pane = new JPanel();
360 button_pane.setComponentOrientation(Dictionary.getOrientation());
361
362 new_button = new GLIButton(Dictionary.get(new_button_key)+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get(new_button_tooltip_key));
363 new_button.setEnabled(true);
364
365 edit_button = new GLIButton(Dictionary.get(edit_button_key)+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get(edit_button_tooltip_key));
366 edit_button.setEnabled(false);
367
368 remove_button = new GLIButton(Dictionary.get(remove_button_key), Dictionary.get(remove_button_tooltip_key));
369 remove_button.setEnabled(false);
370
371 // Listeners
372 new_button.addActionListener(new NewIndexListener());
373 edit_button.addActionListener(new EditIndexListener());
374 remove_button.addActionListener(new RemoveIndexListener());
375 remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
376
377 index_list.addListSelectionListener(new IndexListListener());
378
379 move_down_button.addActionListener(new MoveListener(false));
380 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
381 move_up_button.addActionListener(new MoveListener(true));
382 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
383
384
385 // Layout
386 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
387 movement_pane.setLayout(new GridLayout(3,1));
388 movement_pane.add(move_up_button);
389 movement_pane.add(move_down_button);
390 if (has_default) {
391 set_default_button.addActionListener(new SetDefaultListener());
392 movement_pane.add(set_default_button);
393 }
394 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
395 assigned_indexes_pane.setLayout(new BorderLayout());
396 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
397 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
398 assigned_indexes_pane.add(movement_pane, BorderLayout.LINE_END);
399
400 button_pane.setLayout(new GridLayout(1,3,5,0));
401 button_pane.add(new_button);
402 button_pane.add(edit_button);
403 button_pane.add(remove_button);
404
405 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
406 setLayout(new BorderLayout());
407 add(assigned_indexes_pane, BorderLayout.CENTER);
408 add(button_pane, BorderLayout.SOUTH);
409
410 }
411 public void loseFocus() {}
412 public void gainFocus() {}
413 public void destroy() {}
414
415 /** Listens for double clicks apon the list and react as if the configure button was pushed. */
416 private class ClickListener
417 extends MouseAdapter {
418 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
419 * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
420 */
421 public void mouseClicked(MouseEvent event) {
422 if(event.getClickCount() == 2 ) {
423 if(!index_list.isSelectionEmpty()) {
424 Index index = (Index) index_list.getSelectedValue();
425 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
426 nip.destroy();
427 }
428 }
429 }
430 }
431
432
433 private class IndexListListener
434 implements ListSelectionListener {
435
436 /** 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
437 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
438 */
439 public void valueChanged(ListSelectionEvent event)
440 {
441 if (event.getValueIsAdjusting()) {
442 return;
443 }
444
445 set_default_button.setEnabled(true);
446 Object value = index_list.getSelectedValue();
447 if (value == null) {
448 move_down_button.setEnabled(false);
449 move_up_button.setEnabled(false);
450 remove_button.setEnabled(false);
451 edit_button.setEnabled(false);
452 set_default_button.setEnabled(false);
453 return;
454 }
455
456 // Enable the buttons appropriately
457 remove_button.setEnabled(true);
458 edit_button.setEnabled(true);
459 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
460 int i = index_list.getSelectedIndex();
461 int size = index_list.getModel().getSize();
462 move_up_button.setEnabled((i>0));
463 move_down_button.setEnabled((i<size-1));
464 }
465 }
466
467 private class IndexListRenderer
468 extends DefaultListCellRenderer {
469
470 /** Return a component that has been configured to display the specified value. */
471 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
472 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
473 if(default_index != null && default_index.equals(value)) {
474 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
475 }
476 return component;
477 }
478
479 }
480
481 private class NewIndexListener
482 implements ActionListener {
483
484 public void actionPerformed(ActionEvent event) {
485 NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
486 nip.destroy();
487 }
488 }
489
490 private class EditIndexListener
491 implements ActionListener {
492
493 public void actionPerformed(ActionEvent event) {
494 Index index = (Index) index_list.getSelectedValue();
495 NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
496 nip.destroy();
497 }
498 }
499
500 private class RemoveIndexListener
501 implements ActionListener {
502
503 public void actionPerformed(ActionEvent event) {
504
505 int i = index_list.getSelectedIndex();
506 if (i != -1) {
507 removeIndex((Index) index_list.getSelectedValue());
508 }
509 int size = index_list.getModel().getSize();
510 if (i == size) {
511 i--;
512 }
513 index_list.setSelectedIndex(i);
514 // This will produce an event on the list, updating the other buttons
515 }
516 }
517
518 private class MoveListener
519 implements ActionListener {
520
521 private boolean move_up;
522
523 public MoveListener(boolean move_up) {
524 this.move_up = move_up;
525 }
526
527 public void actionPerformed(ActionEvent event) {
528 // Retrieve the selected index
529 Index index = (Index) index_list.getSelectedValue();
530 if(index != null) {
531 moveIndex(index, move_up);
532 // Ensure the index that moved is still selected
533 index_list.setSelectedValue(index, true);
534 index = null;
535 }
536 }
537 }
538
539 private class SetDefaultListener
540 implements ActionListener {
541
542 public void actionPerformed(ActionEvent event) {
543 Index index = (Index) index_list.getSelectedValue();
544 if(index != null) {
545 setDefault(index);
546 // This should cause a repaint of just the desired row
547 index_list.setSelectedValue(index, true);
548 }
549 set_default_button.setEnabled(false);
550 }
551 }
552
553
554
555 private class NewIndexPrompt
556 extends ModalDialog {
557
558 NewIndexPrompt new_index_prompt = null;
559
560 private CheckList source_list;
561 private JButton add_or_replace_button;
562
563 //private JButton select_none_button;
564 //private JButton add_all_button;
565 //private JButton select_all_button;
566 //private JButton replace_button;
567 private JButton cancel_button;
568
569 private boolean editing = false;
570
571
572 public NewIndexPrompt(String build_type, Index existing_index) {
573 super(Gatherer.g_man, true);
574 this.setComponentOrientation(Dictionary.getOrientation());
575
576 new_index_prompt = this;
577
578 setModal(true);
579 setSize(PROMPT_SIZE);
580 if (existing_index != null) {
581 setTitle (Dictionary.get(nip_edit_index_key));
582 editing = true;
583 } else {
584 setTitle(Dictionary.get(nip_new_index_key));
585 }
586 // simple help menu, set to searchindexes section
587 setJMenuBar(new SimpleMenuBar("searchindexes"));
588
589 JPanel content_pane = (JPanel)this.getContentPane();
590 content_pane.setComponentOrientation(Dictionary.getOrientation());
591 ArrayList new_data = new ArrayList();
592 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
593
594 JPanel details_pane = new JPanel();
595 details_pane.setComponentOrientation(Dictionary.getOrientation());
596
597 JLabel source_label = new JLabel(Dictionary.get(nip_source_label_key));
598 source_label.setComponentOrientation(Dictionary.getOrientation());
599 source_list = new CheckList(false);
600 source_list.setListData(new_data);
601 source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
602 source_list.addListSelectionListener(new SourceListListener());
603
604 JPanel button_pane = new JPanel();
605 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
606 button_pane.setComponentOrientation(Dictionary.getOrientation());
607
608 button_pane.setLayout(new GridLayout(0,2,5,0));
609
610 if (existing_index != null) {
611 add_or_replace_button = new GLIButton(Dictionary.get(nip_add_index_button_key), Dictionary.get(nip_replace_index_tooltip_key));
612 add_or_replace_button.addActionListener(new ReplaceIndexListener());
613 } else {
614 add_or_replace_button = new GLIButton(Dictionary.get(nip_add_index_button_key), Dictionary.get(nip_add_index_tooltip_key));
615 add_or_replace_button.addActionListener(new AddIndexListener());
616 }
617
618 add_or_replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
619 add_or_replace_button.setEnabled(false);
620 button_pane.add(add_or_replace_button);
621
622
623 // always have a cancel button
624 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
625 cancel_button.setEnabled(true);
626 cancel_button.addActionListener(new ActionListener() {
627 public void actionPerformed(ActionEvent event) {
628 new_index_prompt.dispose();
629 }
630 });
631 button_pane.add(cancel_button);
632
633 //Layout
634 details_pane.setLayout(new BorderLayout(10,10));
635 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
636 details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
637
638 // if we are editing, fill in the controls
639 if (existing_index !=null) {
640 ArrayList sources = existing_index.getSources();
641 source_list.setTickedObjects(sources.toArray());
642 source_list.setEnabled(true);
643 }
644 content_pane.setLayout(new BorderLayout());
645 content_pane.add(source_label, BorderLayout.NORTH);
646 content_pane.add(details_pane, BorderLayout.CENTER);
647 content_pane.add(button_pane, BorderLayout.SOUTH);
648
649 // Display on screen.
650 Dimension screen_size = Configuration.screen_size;
651 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
652 screen_size = null;
653 setVisible(true);
654
655 }
656
657 /** Method which actually forces the dialog to be shown on screen.
658 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
659 */
660 public boolean display() {
661 setVisible(true);
662 return true;
663 // return success;
664 }
665
666 public void destroy() {
667 }
668
669 // Checks that specified index not already in the collection
670 private void validateAddOrReplaceButton() {
671 Index index;
672 ArrayList sources;
673 if (!source_list.isNothingTicked()) {
674 sources = source_list.getTicked();
675 index = createIndex(sources);
676 } else {
677 // nothing selected
678 add_or_replace_button.setEnabled(false);
679 return;
680 }
681
682 sources = null;
683 if (index_model.contains(index)) {
684 add_or_replace_button.setEnabled(false);
685 }
686 else {
687 add_or_replace_button.setEnabled(true);
688 }
689
690 }
691
692 private Index generateNewIndex() {
693 Index index = null;
694 ArrayList sources;
695
696 if (!source_list.isNothingTicked()) {
697 sources = source_list.getTicked();
698 index = createIndex(sources);
699 }
700 return index;
701 }
702
703 private CollectionMeta generateCollectionMeta(Index index) {
704 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
705 if (use_macro_as_display_name(index.getID())) {
706 metadatum.setValue(get_macro_name(index.getID()));
707 } else {
708 metadatum.setValue(index.getID());
709 }
710 return metadatum;
711 }
712
713 private class AddIndexListener
714 implements ActionListener
715 {
716 public void actionPerformed(ActionEvent event)
717 {
718 Index index = generateNewIndex();
719 if (index != null) {
720
721 CollectionMeta metadatum = generateCollectionMeta(index);
722 // Add the index
723 addIndex(index, metadatum);
724 index_list.setSelectedValue(index, true);
725 }
726 new_index_prompt.dispose();
727 }
728 }
729
730
731 private class SourceListListener
732 implements ListSelectionListener {
733
734 public void valueChanged(ListSelectionEvent event) {
735 if (event.getValueIsAdjusting()) {
736 return;
737 }
738 validateAddOrReplaceButton();
739 }
740 }
741
742
743 private class ReplaceIndexListener
744 implements ActionListener {
745
746 public void actionPerformed(ActionEvent event)
747 {
748 Index index = generateNewIndex();
749 if (index != null) {
750 CollectionMeta metadatum = generateCollectionMeta(index);
751 // replace the index
752 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
753 index_list.setSelectedValue(index, true);
754 }
755 new_index_prompt.dispose();
756 }
757 }
758
759 /**
760 * If the index is built with DC metadata, use macro variable as the display text,
761 * so that translations of DC metadata can be displayed in the search field drop-down list
762 * when interface language changes.
763 *
764 * @param index_id Current index id.
765 * @return Whether macro variable should be used.
766 */
767 private boolean use_macro_as_display_name(String index_id) {
768 //String metaname = index_id;
769 if (index_id.indexOf(":") != -1) {
770 index_id = index_id.substring(index_id.indexOf(":") + 1);
771 }
772
773 String field = null;
774 String[] fields = index_id.split(",");
775 for (int i = 0; i < fields.length; i++) {
776 String s = fields[i];
777 if (s.indexOf(".") != -1) {
778 s = s.substring(s.indexOf(".") + 1);
779 }
780 if (field == null) {
781 field = s;
782 }
783 else if (!field.equals(s)) {
784 return false;
785 }
786 }
787
788 field = field.toLowerCase();
789 if (field.equals("text") || field.equals("title") || field.equals("creator") || field.equals("subject") ||
790 field.equals("description") || field.equals("publisher") || field.equals("contributor") || field.equals("date") ||
791 field.equals("type") || field.equals("format") || field.equals("identifier") || field.equals("source") ||
792 field.equals("language") || field.equals("relation") || field.equals("coverage") || field.equals("rights")) {
793 return true;
794 }
795
796 return false;
797 }
798
799 /**
800 * Get the corresponding macro variable name, eg. _labelTitle_, _labelCreator_.
801 *
802 * @param index_id Current index id.
803 * @return Name of the macro variable.
804 */
805 private String get_macro_name(String index_id) {
806 if (index_id.indexOf(":") != -1) {
807 index_id = index_id.substring(index_id.indexOf(":") + 1);
808 }
809 if (index_id.indexOf(",") != -1) {
810 index_id = index_id.substring(0, index_id.indexOf(","));
811 }
812 if (index_id.indexOf(".") != -1) {
813 index_id = index_id.substring(index_id.indexOf(".") + 1);
814 }
815
816 return "_label" + index_id + "_";
817 }
818 }
819 }
820}
Note: See TracBrowser for help on using the repository browser.