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

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

updated to use new SearchMeta instead of collectionmeta

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