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

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

made [Default Index] thingy customisable

File size: 28.4 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 protected String default_indicator_key = null; //"CDM.IndexManager.Default_Index_Indicator";
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 protected 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 protected 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 protected 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 protected 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 protected 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
314 protected class IndexControl
315 extends JPanel
316 implements Control {
317
318 protected JList index_list;
319 protected JButton move_down_button;
320 protected JButton move_up_button;
321 protected JButton set_default_button;
322
323 protected JButton new_button;
324 protected JButton edit_button;
325 protected JButton remove_button;
326
327 protected boolean has_default = false;
328 public IndexControl() {
329 super();
330 this.setComponentOrientation(Dictionary.getOrientation());
331 if (index_default_element_name != null) {
332 has_default = true;
333 }
334 // Creation
335 JPanel assigned_indexes_pane = new JPanel();
336 assigned_indexes_pane.setComponentOrientation(Dictionary.getOrientation());
337
338 JLabel index_label = new JLabel(Dictionary.get(controls_title_key));
339 index_label.setComponentOrientation(Dictionary.getOrientation());
340
341 index_list = new JList(index_model);
342 index_list.setCellRenderer(new IndexListRenderer());
343 index_list.setVisibleRowCount(6);
344 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
345 index_list.addMouseListener(new ClickListener());
346 index_list.setComponentOrientation(Dictionary.getOrientation());
347
348 JPanel movement_pane = new JPanel();
349 movement_pane.setComponentOrientation(Dictionary.getOrientation());
350
351 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
352 move_up_button.setEnabled(false);
353
354 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
355 move_down_button.setEnabled(false);
356
357 set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
358 set_default_button.setEnabled(false);
359
360 JPanel button_pane = new JPanel();
361 button_pane.setComponentOrientation(Dictionary.getOrientation());
362
363 new_button = new GLIButton(Dictionary.get(new_button_key)+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get(new_button_tooltip_key));
364 new_button.setEnabled(true);
365
366 edit_button = new GLIButton(Dictionary.get(edit_button_key)+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get(edit_button_tooltip_key));
367 edit_button.setEnabled(false);
368
369 remove_button = new GLIButton(Dictionary.get(remove_button_key), Dictionary.get(remove_button_tooltip_key));
370 remove_button.setEnabled(false);
371
372 // Listeners
373 new_button.addActionListener(new NewIndexListener());
374 edit_button.addActionListener(new EditIndexListener());
375 remove_button.addActionListener(new RemoveIndexListener());
376 remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
377
378 index_list.addListSelectionListener(new IndexListListener());
379
380 move_down_button.addActionListener(new MoveListener(false));
381 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
382 move_up_button.addActionListener(new MoveListener(true));
383 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
384
385
386 // Layout
387 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
388 movement_pane.setLayout(new GridLayout(3,1));
389 movement_pane.add(move_up_button);
390 movement_pane.add(move_down_button);
391 if (has_default) {
392 set_default_button.addActionListener(new SetDefaultListener());
393 movement_pane.add(set_default_button);
394 }
395 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
396 assigned_indexes_pane.setLayout(new BorderLayout());
397 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
398 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
399 assigned_indexes_pane.add(movement_pane, BorderLayout.LINE_END);
400
401 button_pane.setLayout(new GridLayout(1,3,5,0));
402 button_pane.add(new_button);
403 button_pane.add(edit_button);
404 button_pane.add(remove_button);
405
406 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
407 setLayout(new BorderLayout());
408 add(assigned_indexes_pane, BorderLayout.CENTER);
409 add(button_pane, BorderLayout.SOUTH);
410
411 }
412 public void loseFocus() {}
413 public void gainFocus() {}
414 public void destroy() {}
415
416 protected NewIndexPrompt createNewIndexPrompt(String build_type, Index index) {
417 return new NewIndexPrompt(build_type, index);
418
419 }
420 /** Listens for double clicks apon the list and react as if the configure button was pushed. */
421 protected class ClickListener
422 extends MouseAdapter {
423 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
424 * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
425 */
426 public void mouseClicked(MouseEvent event) {
427 if(event.getClickCount() == 2 ) {
428 if(!index_list.isSelectionEmpty()) {
429 Index index = (Index) index_list.getSelectedValue();
430 NewIndexPrompt nip = createNewIndexPrompt(build_type, index);
431 nip.destroy();
432 }
433 }
434 }
435 }
436
437
438 protected class IndexListListener
439 implements ListSelectionListener {
440
441 /** 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
442 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
443 */
444 public void valueChanged(ListSelectionEvent event)
445 {
446 if (event.getValueIsAdjusting()) {
447 return;
448 }
449
450 set_default_button.setEnabled(true);
451 Object value = index_list.getSelectedValue();
452 if (value == null) {
453 move_down_button.setEnabled(false);
454 move_up_button.setEnabled(false);
455 remove_button.setEnabled(false);
456 edit_button.setEnabled(false);
457 set_default_button.setEnabled(false);
458 return;
459 }
460
461 // Enable the buttons appropriately
462 remove_button.setEnabled(true);
463 edit_button.setEnabled(true);
464 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
465 int i = index_list.getSelectedIndex();
466 int size = index_list.getModel().getSize();
467 move_up_button.setEnabled((i>0));
468 move_down_button.setEnabled((i<size-1));
469 }
470 }
471
472 protected class IndexListRenderer
473 extends DefaultListCellRenderer {
474
475 /** Return a component that has been configured to display the specified value. */
476 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
477 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
478 if(default_index != null && default_index.equals(value)) {
479 component.setText(component.getText() + " " + Dictionary.get(default_indicator_key));
480 }
481 return component;
482 }
483
484 }
485
486 protected class NewIndexListener
487 implements ActionListener {
488
489 public void actionPerformed(ActionEvent event) {
490 NewIndexPrompt nip = createNewIndexPrompt(build_type, null);
491 nip.destroy();
492 }
493 }
494
495 protected class EditIndexListener
496 implements ActionListener {
497
498 public void actionPerformed(ActionEvent event) {
499 Index index = (Index) index_list.getSelectedValue();
500 NewIndexPrompt nip = createNewIndexPrompt(build_type, index);
501 nip.destroy();
502 }
503 }
504
505 protected class RemoveIndexListener
506 implements ActionListener {
507
508 public void actionPerformed(ActionEvent event) {
509
510 int i = index_list.getSelectedIndex();
511 if (i != -1) {
512 removeIndex((Index) index_list.getSelectedValue());
513 }
514 int size = index_list.getModel().getSize();
515 if (i == size) {
516 i--;
517 }
518 index_list.setSelectedIndex(i);
519 // This will produce an event on the list, updating the other buttons
520 }
521 }
522
523 protected class MoveListener
524 implements ActionListener {
525
526 protected boolean move_up;
527
528 public MoveListener(boolean move_up) {
529 this.move_up = move_up;
530 }
531
532 public void actionPerformed(ActionEvent event) {
533 // Retrieve the selected index
534 Index index = (Index) index_list.getSelectedValue();
535 if(index != null) {
536 moveIndex(index, move_up);
537 // Ensure the index that moved is still selected
538 index_list.setSelectedValue(index, true);
539 index = null;
540 }
541 }
542 }
543
544 protected class SetDefaultListener
545 implements ActionListener {
546
547 public void actionPerformed(ActionEvent event) {
548 Index index = (Index) index_list.getSelectedValue();
549 if(index != null) {
550 setDefault(index);
551 // This should cause a repaint of just the desired row
552 index_list.setSelectedValue(index, true);
553 }
554 set_default_button.setEnabled(false);
555 }
556 }
557
558
559
560 protected class NewIndexPrompt
561 extends ModalDialog {
562
563 NewIndexPrompt new_index_prompt = null;
564
565 protected CheckList source_list;
566 protected JButton add_or_replace_button;
567
568 //protected JButton select_none_button;
569 //protected JButton add_all_button;
570 //protected JButton select_all_button;
571 //protected JButton replace_button;
572 protected JButton cancel_button;
573
574 protected JPanel content_pane;
575 protected JPanel details_pane;
576 protected JPanel button_pane;
577 protected boolean editing = false;
578
579
580 public NewIndexPrompt(String build_type, Index existing_index) {
581 super(Gatherer.g_man, true);
582 this.setComponentOrientation(Dictionary.getOrientation());
583
584 new_index_prompt = this;
585
586 setModal(true);
587 setSize(PROMPT_SIZE);
588 if (existing_index != null) {
589 setTitle (Dictionary.get(nip_edit_index_key));
590 editing = true;
591 } else {
592 setTitle(Dictionary.get(nip_new_index_key));
593 }
594 // simple help menu, set to searchindexes section
595 setJMenuBar(new SimpleMenuBar("searchindexes"));
596
597 content_pane = (JPanel)this.getContentPane();
598 content_pane.setComponentOrientation(Dictionary.getOrientation());
599 generateContents(build_type, existing_index);
600
601 // Display on screen.
602 Dimension screen_size = Configuration.screen_size;
603 setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
604 screen_size = null;
605 setVisible(true);
606
607 }
608
609
610 /** Method which actually forces the dialog to be shown on screen.
611 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
612 */
613 public boolean display() {
614 setVisible(true);
615 return true;
616 // return success;
617 }
618
619 public void destroy() {
620 }
621
622 protected void generateContents(String build_type, Index existing_index) {
623 ArrayList new_data = new ArrayList();
624 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
625
626 details_pane = new JPanel();
627 details_pane.setComponentOrientation(Dictionary.getOrientation());
628
629 JLabel source_label = new JLabel(Dictionary.get(nip_source_label_key));
630 source_label.setComponentOrientation(Dictionary.getOrientation());
631 source_list = new CheckList(false);
632 source_list.setListData(new_data);
633 source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
634 source_list.addListSelectionListener(new SourceListListener());
635
636 button_pane = new JPanel();
637 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
638 button_pane.setComponentOrientation(Dictionary.getOrientation());
639
640 button_pane.setLayout(new GridLayout(0,2,5,0));
641
642 if (existing_index != null) {
643 add_or_replace_button = new GLIButton(Dictionary.get(nip_add_index_button_key), Dictionary.get(nip_replace_index_tooltip_key));
644 add_or_replace_button.addActionListener(new ReplaceIndexListener());
645 } else {
646 add_or_replace_button = new GLIButton(Dictionary.get(nip_add_index_button_key), Dictionary.get(nip_add_index_tooltip_key));
647 add_or_replace_button.addActionListener(new AddIndexListener());
648 }
649
650 add_or_replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
651 add_or_replace_button.setEnabled(false);
652 button_pane.add(add_or_replace_button);
653
654
655 // always have a cancel button
656 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
657 cancel_button.setEnabled(true);
658 cancel_button.addActionListener(new ActionListener() {
659 public void actionPerformed(ActionEvent event) {
660 new_index_prompt.dispose();
661 }
662 });
663 button_pane.add(cancel_button);
664
665 //Layout
666 details_pane.setLayout(new BorderLayout(10,10));
667 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
668 details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
669
670 // if we are editing, fill in the controls
671 if (existing_index !=null) {
672 ArrayList sources = existing_index.getSources();
673 source_list.setTickedObjects(sources.toArray());
674 source_list.setEnabled(true);
675 }
676 content_pane.setLayout(new BorderLayout());
677 content_pane.add(source_label, BorderLayout.NORTH);
678 content_pane.add(details_pane, BorderLayout.CENTER);
679 content_pane.add(button_pane, BorderLayout.SOUTH);
680
681
682 }
683 // Checks that specified index not already in the collection
684 protected void validateAddOrReplaceButton() {
685 Index index;
686 ArrayList sources;
687 if (!source_list.isNothingTicked()) {
688 sources = source_list.getTicked();
689 index = createIndex(sources);
690 } else {
691 // nothing selected
692 add_or_replace_button.setEnabled(false);
693 return;
694 }
695
696 sources = null;
697 if (index_model.contains(index)) {
698 add_or_replace_button.setEnabled(false);
699 }
700 else {
701 add_or_replace_button.setEnabled(true);
702 }
703
704 }
705
706 protected Index generateNewIndex() {
707 Index index = null;
708 ArrayList sources;
709
710 if (!source_list.isNothingTicked()) {
711 sources = source_list.getTicked();
712 index = createIndex(sources);
713 }
714 return index;
715 }
716
717 protected CollectionMeta generateCollectionMeta(Index index) {
718 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
719 if (use_macro_as_display_name(index.getID())) {
720 metadatum.setValue(get_macro_name(index.getID()));
721 } else {
722 metadatum.setValue(index.getID());
723 }
724 return metadatum;
725 }
726
727 protected class AddIndexListener
728 implements ActionListener
729 {
730 public void actionPerformed(ActionEvent event)
731 {
732 Index index = generateNewIndex();
733 if (index != null) {
734
735 CollectionMeta metadatum = generateCollectionMeta(index);
736 // Add the index
737 addIndex(index, metadatum);
738 index_list.setSelectedValue(index, true);
739 }
740 new_index_prompt.dispose();
741 }
742 }
743
744
745 protected class SourceListListener
746 implements ListSelectionListener {
747
748 public void valueChanged(ListSelectionEvent event) {
749 if (event.getValueIsAdjusting()) {
750 return;
751 }
752 validateAddOrReplaceButton();
753 }
754 }
755
756
757 protected class ReplaceIndexListener
758 implements ActionListener {
759
760 public void actionPerformed(ActionEvent event)
761 {
762 Index index = generateNewIndex();
763 if (index != null) {
764 CollectionMeta metadatum = generateCollectionMeta(index);
765 // replace the index
766 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
767 index_list.setSelectedValue(index, true);
768 }
769 new_index_prompt.dispose();
770 }
771 }
772
773 /**
774 * If the index is built with DC metadata, use macro variable as the display text,
775 * so that translations of DC metadata can be displayed in the search field drop-down list
776 * when interface language changes.
777 *
778 * @param index_id Current index id.
779 * @return Whether macro variable should be used.
780 */
781 protected boolean use_macro_as_display_name(String index_id) {
782 //String metaname = index_id;
783 if (index_id.indexOf(":") != -1) {
784 index_id = index_id.substring(index_id.indexOf(":") + 1);
785 }
786
787 String field = null;
788 String[] fields = index_id.split(",");
789 for (int i = 0; i < fields.length; i++) {
790 String s = fields[i];
791 if (s.indexOf(".") != -1) {
792 s = s.substring(s.indexOf(".") + 1);
793 }
794 if (field == null) {
795 field = s;
796 }
797 else if (!field.equals(s)) {
798 return false;
799 }
800 }
801
802 field = field.toLowerCase();
803 if (field.equals("text") || field.equals("title") || field.equals("creator") || field.equals("subject") ||
804 field.equals("description") || field.equals("publisher") || field.equals("contributor") || field.equals("date") ||
805 field.equals("type") || field.equals("format") || field.equals("identifier") || field.equals("source") ||
806 field.equals("language") || field.equals("relation") || field.equals("coverage") || field.equals("rights")) {
807 return true;
808 }
809
810 return false;
811 }
812
813 /**
814 * Get the corresponding macro variable name, eg. _labelTitle_, _labelCreator_.
815 *
816 * @param index_id Current index id.
817 * @return Name of the macro variable.
818 */
819 protected String get_macro_name(String index_id) {
820 if (index_id.indexOf(":") != -1) {
821 index_id = index_id.substring(index_id.indexOf(":") + 1);
822 }
823 if (index_id.indexOf(",") != -1) {
824 index_id = index_id.substring(0, index_id.indexOf(","));
825 }
826 if (index_id.indexOf(".") != -1) {
827 index_id = index_id.substring(index_id.indexOf(".") + 1);
828 }
829
830 return "_label" + index_id + "_";
831 }
832 }
833 }
834}
Note: See TracBrowser for help on using the repository browser.