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

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

setDefault tooltip now customisable; added a bevel border round the controls; set type for collectionmeta

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