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

Last change on this file since 36303 was 36303, checked in by kjdon, 21 months ago

need to use correct type for searchmeta, could be index/sort/facet

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