source: trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java@ 4580

Last change on this file since 4580 was 4540, checked in by kjdon, 21 years ago

have changed teh way indexes are written to and read from the config file: greenstone extracted metadata has the ex namespace internally in the gatherer, but not in the index specifications in the config file.

  • Property svn:keywords set to Author Date Id Revision
File size: 28.2 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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43package org.greenstone.gatherer.cdm;
44/**************************************************************************************
45 * Title: Gatherer
46 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
47 * Copyright: Copyright (c) 2001
48 * Company: The University of Waikato
49 * Written: 03/05/02
50 * Revised: 17/11/02 - Commented
51 **************************************************************************************/
52import java.awt.BorderLayout;
53import java.awt.Component;
54import java.awt.Dimension;
55import java.awt.GridLayout;
56import java.awt.event.ActionEvent;
57import java.awt.event.ActionListener;
58import java.awt.event.KeyAdapter;
59import java.awt.event.KeyEvent;
60import java.util.Collections;
61import java.util.StringTokenizer;
62import java.util.Vector;
63import javax.swing.BorderFactory;
64import javax.swing.DefaultListCellRenderer;
65import javax.swing.DefaultListModel;
66import javax.swing.JButton;
67import javax.swing.JComboBox;
68import javax.swing.JLabel;
69import javax.swing.JList;
70import javax.swing.JOptionPane;
71import javax.swing.JPanel;
72import javax.swing.JScrollPane;
73import javax.swing.JTextArea;
74import javax.swing.JTextField;
75import javax.swing.event.ListDataEvent;
76import javax.swing.event.ListDataListener;
77import javax.swing.event.ListSelectionEvent;
78import javax.swing.event.ListSelectionListener;
79import org.greenstone.gatherer.Gatherer;
80import org.greenstone.gatherer.cdm.CommandTokenizer;
81import org.greenstone.gatherer.cdm.Index;
82import org.greenstone.gatherer.msm.ElementWrapper;
83import org.greenstone.gatherer.msm.MSMUtils;
84import org.greenstone.gatherer.util.ExclusiveListSelectionListener;
85import org.w3c.dom.Element;
86/** This class is resposible for storing the indexes which have been assigned to this collection and the default index, 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.
87 * @author John Thompson, Greenstone Digital Library, University of Waikato
88 * @version 2.3
89 */
90public class IndexManager
91 extends DefaultListModel {
92 /** A reference to our creator, the collection design manager. */
93 private CollectionDesignManager manager = null;
94 /** The controls for editing the indexes. */
95 private Control controls = null;
96 /** A reference to ourselves so our inner methods have access. */
97 private DefaultListModel model = null;
98 /** A reference to the Gatherer, for access to the Dictionary and messaging purposes. */
99 private Gatherer gatherer = null;
100 /** The default index. */
101 private Index default_index = null;
102 /** Constructor.
103 * @param gatherer A reference to the <strong>Gatherer</strong>.
104 * @param manager A reference to the <strong>CollectionDesignManager</strong>.
105 */
106 public IndexManager(Gatherer gatherer, CollectionDesignManager manager) {
107 super();
108 this.gatherer = gatherer;
109 this.manager = manager;
110 this.model = this;
111 }
112 /** Method to add a new index.
113 * @param index The <strong>Index</strong> to add.
114 * @see org.greenstone.gatherer.Gatherer
115 * @see org.greenstone.gatherer.collection.CollectionManager
116 */
117 public void addIndex(Index index) {
118 if(!contains(index)) {
119 String index_str = index.toString(false).toLowerCase();
120 // Add alphabetically.
121 for(int i = 0; i < size(); i++) {
122 Index sibling = (Index) get(i);
123 String sibling_str = sibling.toString(false).toLowerCase();
124 int position = index_str.compareTo(sibling_str);
125 // Sibling is before index.
126 if(position > 0) {
127 // Carry on.
128 }
129 // Index is equal to, or before sibling. Insert it.
130 else if(position == 0 || position < 0) {
131 add(i, index);
132 gatherer.c_man.configurationChanged();
133 return;
134 }
135 }
136 // If we got this far, we haven't inserted index, and we are out of model so.
137 addElement(index);
138 gatherer.c_man.configurationChanged();
139 }
140 else {
141 JOptionPane.showMessageDialog(manager.gui, get("CDM.IndexManager.Index_Exists"), get("General.Warning"), JOptionPane.WARNING_MESSAGE);
142 }
143 }
144 /** Method to acquire the controls for editing the indexes.
145 * @return A <strong>JPanel</strong> containing the controls.
146 * @see org.greenstone.gatherer.cdm.IndexManager.Control
147 */
148 public JPanel getControls() {
149 if(controls == null) {
150 controls = new Control();
151 }
152 return controls;
153 }
154 /** Method to get the default index.
155 * @return The default <strong>Index</strong>.
156 */
157 public Index getDefault() {
158 return default_index;
159 }
160 /** Method to retrieve a certain index, as referenced by an index number.
161 * @param index An <i>int</i> which indicates the position of the desired index.
162 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
163 */
164 public Index getIndex(int index) {
165 if(0 <= index && index < size()) {
166 return (Index)get(index);
167 }
168 return null;
169 }
170 /** Method to retrieve a certain index, given its name.
171 * @param name The name of the index as a <Strong>String</strong>.
172 * @return The <strong>Index</strong> that matches name, or <i>null</i> if no such index exists.
173 */
174 public Index getIndex(String name) {
175 ///ystem.err.println("Searching for index " + name);
176 for(int i = 0; i < size(); i++) {
177 Index index = (Index) get(i);
178 if(index.toString(false).equals(name)) {
179 ///ystem.err.println("Found.");
180 return (Index)get(i);
181 }
182 }
183 ///ystem.err.println("No such index.");
184 return null;
185 }
186 /** A method to retrieve all of the indexes associated with this manager.
187 * @return A <strong>Vector</strong> of <strong>Index</strong>es.
188 */
189 public Vector getIndexes() {
190 Vector indexes = new Vector();
191 for(int i = 0; i < size(); i++) {
192 indexes.add(get(i));
193 }
194 Collections.sort(indexes);
195 return indexes;
196 }
197 /** Mark the current set of controls, if any, as obsolete and deallocate them. If further need of the controls will cause new controls to be created.
198 * @see org.greenstone.gatherer.cdm.IndexManager.Control
199 */
200 public void invalidateControls() {
201 if(controls != null) {
202 controls.destroy();
203 }
204 controls = null;
205 }
206 /** Method that attempts to parse an index related command from the given command. If such a command is parsed, it is immediately registered with this manager.
207 * @param command The <strong>String</strong> to parse.
208 * @return A <i>boolean</i> which is <i>true</i> if a command was parsed, <i>false</i> otherwise.
209 * @see org.greenstone.gatherer.cdm.CommandTokenizer
210 * @see org.greenstone.gatherer.cdm.Index
211 */
212 public boolean parse(String command) {
213 String temp = command.toLowerCase();
214 if(temp.startsWith("indexes")) {
215 CommandTokenizer ct = new CommandTokenizer(command);
216 ct.nextToken(); // Throw away indexes.
217 while(ct.hasMoreTokens()) {
218 String entry = ct.nextToken();
219 Index index = Index.parseIndexConfig(entry, manager);
220 if (index != null) {
221 addIndex(index);
222
223 } else { // return false if there is one error??
224 return false;
225 }
226 }
227
228 return true;
229 }
230 else if(temp.startsWith("defaultindex")) {
231 CommandTokenizer ct = new CommandTokenizer(command);
232 ct.nextToken();
233 String entry = ct.nextToken();
234 Index index = Index.parseIndexConfig(entry, manager);
235 if (index != null) {
236 setDefault(index);
237 return true;
238 }
239 return false;
240 }
241
242 return false;
243 }
244
245 public void removeAll() {
246 removeAllElements();
247 default_index = null;
248 gatherer.c_man.configurationChanged();
249 }
250
251 /** Method to remove a certain index.
252 * @param index The <Strong>Index</strong> to remove.
253 * @see org.greenstone.gatherer.Gatherer
254 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
255 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
256 * @see org.greenstone.gatherer.collection.CollectionManager
257 */
258 public void removeIndex(Index index) {
259 if(index != null) {
260 String name = index.getName();
261 if(name != null) {
262 Language default_language = manager.languages.getDefaultLanguage();
263 CollectionMeta metadata = new CollectionMeta(manager, index, default_language, name);
264 manager.collectionmetadatum.removeMetadata(metadata);
265 }
266 removeElement(index);
267 if(default_index != null && default_index.equals(index)) {
268 default_index = null;
269 }
270 gatherer.c_man.configurationChanged();
271 }
272 }
273 /** Method to set the default index.
274 * @param index The new default <strong>Index</strong>.
275 * @see org.greenstone.gatherer.Gatherer
276 * @see org.greenstone.gatherer.collection.CollectionManager
277 */
278 public void setDefault(Index index) {
279 default_index = index;
280 if(index != null && !contains(index)) {
281 addElement(index);
282 }
283 gatherer.c_man.configurationChanged();
284 }
285 /** Method to print out the contents of this class as a string.
286 * @return A <strong>String</strong> just as it would appear in the colleciton configuration file.
287 */
288 public String toString() {
289 String text = "";
290 // First the indexes.
291 if(size() > 0) {
292 text = "indexes ";
293 for(int i = 0; i < size(); i++) {
294 Index index = (Index) get(i);
295 text = text + index.toStringConfig();
296 if(i < size() - 1) {
297 text = text + " ";
298 }
299 }
300 // Now the default index if there is one, or just the first index
301 // if there isn't
302 if(default_index != null) {
303 text = text + "\ndefaultindex " + default_index.toStringConfig() + "\n";
304 }
305 text = text + "\n";
306 }
307 return text;
308 }
309
310
311
312
313
314 /** Overloaded to call get with both a key and an empty argument array.
315 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
316 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
317 */
318 private String get(String key) {
319 return get(key, null);
320 }
321 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
322 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.
323 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
324 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
325 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
326 * @see org.greenstone.gatherer.Gatherer
327 * @see org.greenstone.gatherer.Dictionary
328 */
329 private String get(String key, String args[]) {
330 if(key.indexOf('.') == -1) {
331 key = "CDM.IndexManager." + key;
332 }
333 return gatherer.dictionary.get(key, args);
334 }
335 /** This class creates a set of controls for editing the indexes. */
336 private class Control
337 extends JPanel {
338 /** The default size of a label on this control. */
339 private Dimension LABEL_SIZE = new Dimension(120,25);
340 /** The button used to add a new index. */
341 private JButton add = null;
342 /** The button used to clear the default index. */
343 private JButton clear_default = null;
344 /** The button used to remove an index. */
345 private JButton remove = null;
346 /** The button used to set the default index. */
347 private JButton set_default = null;
348 /** The combobox used to adjust what level the new index will built on. */
349 private JComboBox level = null;
350 /** The label denoting the default index to be built for this collection. */
351 private JLabel default_label = null;
352 /** The label denoting the list of assigned indexes. */
353 private JLabel index_label = null;
354 /** The label denoting the control for choosing index level. */
355 private JLabel level_label = null;
356 /** The label denoting the name of the index. */
357 private JLabel name_label = null;
358 /** The label denoting the list of possible sources for the index. */
359 private JLabel source_label = null;
360 /** The title shown at the top of the index controls. */
361 private JLabel title = null;
362 /** The list of assigned indexes. */
363 private JList index_list = null;
364 /** The list of possible sources on which the index could be built. */
365 private JList source_list = null;
366 /** The panel used to display the list of assigned indexes. */
367 private JPanel assigned_pane = null;
368 /** The panel which contains the buttons used to edit indexes. */
369 private JPanel button_pane = null;
370 /** The panel on which all other panels are displayed. */
371 private JPanel central_pane = null;
372 /** The control pane showing the editable controls. */
373 private JPanel control_pane = null;
374 /** The edit pane contains the list of possible sources. */
375 private JPanel edit_pane = null;
376 /** A panel used to correctly lay out the default index label. */
377 private JPanel default_pane = null;
378 /** The panel containing the title label and instructions. */
379 private JPanel header_pane = null;
380 /** The lvel panel contains the control for adjusting index level. */
381 private JPanel level_pane = null;
382 /** The pane in which the name control is set. */
383 private JPanel name_pane = null;
384 /** The scrollpane containing the instructions text area. */
385 private JScrollPane instructions_scroll = null;
386 /** A text area containing inline instructions. */
387 private JTextArea instructions = null;
388 /** A text field naming the default index. Non-editable. */
389 private JTextField default_value = null;
390 /** A text field for controlling the name of the new index. */
391 private JTextField name = null;
392 /** A model containing all of the available index sources. */
393 private Vector source_model = null;
394 /** Constructor.
395 * @see org.greenstone.gatherer.Configuration
396 * @see org.greenstone.gatherer.Gatherer
397 * @see org.greenstone.gatherer.cdm.IndexManager.Control.AddListener
398 * @see org.greenstone.gatherer.cdm.IndexManager.Control.ClearDefaultListener
399 * @see org.greenstone.gatherer.cdm.IndexManager.Control.NameListener
400 * @see org.greenstone.gatherer.cdm.IndexManager.Control.RemoveListener
401 * @see org.greenstone.gatherer.cdm.IndexManager.Control.SetDefaultListener
402 * @see org.greenstone.gatherer.collection.CollectionManager
403 * @see org.greenstone.gatherer.gui.Coloring
404 * @see org.greenstone.gatherer.msm.MetadataSetManager
405 * @see org.greenstone.gatherer.util.ExclusiveListSelectionListener
406 */
407 public Control() {
408 super();
409 source_model = new Vector();
410 source_model.add("text");
411 source_model.addAll(gatherer.c_man.msm.getAssignedElements());
412 //source_model.addAll(gatherer.c_man.msm.getElements());
413 // Creation
414 add = new JButton(get("Add"));
415 add.setEnabled(false);
416 add.setMnemonic(KeyEvent.VK_A);
417 assigned_pane = new JPanel();
418 button_pane = new JPanel();
419 central_pane = new JPanel();
420 control_pane = new JPanel();
421 clear_default = new JButton(get("Clear_Default"));
422 clear_default.setMnemonic(KeyEvent.VK_C);
423 if(default_index == null) {
424 clear_default.setEnabled(false);
425 }
426 default_label = new JLabel(get("Default_Index"));
427 default_pane = new JPanel();
428 if(default_index != null) {
429 default_value = new JTextField(default_index.toString(true));
430 default_value.setCaretPosition(0);
431 }
432 else {
433 default_value = new JTextField();
434 }
435 default_value.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
436 default_value.setEditable(false);
437 edit_pane = new JPanel();
438 header_pane = new JPanel();
439 index_label = new JLabel(get("Indexes"));
440 index_list = new JList(model);
441 index_list.setCellRenderer(new IndexListCellRenderer());
442 instructions = new JTextArea(get("Instructions"));
443 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
444 instructions.setEditable(false);
445 instructions.setLineWrap(true);
446 instructions.setRows(5);
447 instructions.setWrapStyleWord(true);
448 level = new JComboBox();
449 level.addItem(get("Document"));
450 level.addItem(get("Paragraph"));
451 level.addItem(get("Section"));
452 level.setEditable(false);
453 level_label = new JLabel(get("Level"));
454 level_label.setPreferredSize(LABEL_SIZE);
455 level_pane = new JPanel();
456 name = new JTextField();
457 name_label = new JLabel(get("Name"));
458 name_label.setPreferredSize(LABEL_SIZE);
459 name_pane = new JPanel();
460 remove = new JButton(get("Remove"));
461 remove.setEnabled(false);
462 remove.setMnemonic(KeyEvent.VK_R);
463 set_default = new JButton(get("Set_Default"));
464 set_default.setEnabled(false);
465 set_default.setMnemonic(KeyEvent.VK_S);
466 source_label = new JLabel(get("Source"));
467 source_list = new JList(source_model);
468 title = new JLabel(get("Title"));
469 title.setHorizontalAlignment(JLabel.CENTER);
470
471 // Listeners
472 add.addActionListener(new AddListener());
473 clear_default.addActionListener(new ClearDefaultListener());
474 remove.addActionListener(new RemoveListener());
475 set_default.addActionListener(new SetDefaultListener());
476 name.addKeyListener(new NameListener());
477 ListListener ll = new ListListener();
478 index_list.addListSelectionListener(ll);
479 source_list.addListSelectionListener(ll);
480 ExclusiveListSelectionListener elsl = new ExclusiveListSelectionListener();
481 elsl.add(index_list);
482 elsl.add(source_list);
483
484 // Layout
485 title.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
486
487 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
488
489 header_pane.setLayout(new BorderLayout());
490 header_pane.add(title, BorderLayout.NORTH);
491 header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
492
493 name_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
494
495 name_pane.setBorder(BorderFactory.createEmptyBorder(4,2,4,2));
496 name_pane.setLayout(new BorderLayout());
497 name_pane.add(name_label, BorderLayout.WEST);
498 name_pane.add(name, BorderLayout.CENTER);
499
500 control_pane.setLayout(new BorderLayout());
501 control_pane.add(source_label, BorderLayout.NORTH);
502 control_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
503
504 level_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
505
506 level_pane.setBorder(BorderFactory.createEmptyBorder(4,2,4,2));
507 level_pane.setLayout(new BorderLayout());
508 level_pane.add(level_label, BorderLayout.WEST);
509 level_pane.add(level, BorderLayout.CENTER);
510
511 edit_pane.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
512 edit_pane.setLayout(new BorderLayout());
513 edit_pane.add(name_pane, BorderLayout.NORTH);
514 edit_pane.add(control_pane, BorderLayout.CENTER);
515 edit_pane.add(level_pane, BorderLayout.SOUTH);
516
517 default_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
518
519 default_pane.setBorder
520 (BorderFactory.createCompoundBorder
521 (BorderFactory.createCompoundBorder
522 (BorderFactory.createEmptyBorder(2,2,2,2),
523 BorderFactory.createRaisedBevelBorder()),
524 BorderFactory.createEmptyBorder(2,2,2,2)));
525 default_pane.setLayout(new BorderLayout());
526 default_pane.add(default_label, BorderLayout.WEST);
527 default_pane.add(default_value, BorderLayout.CENTER);
528
529 assigned_pane.setLayout(new BorderLayout());
530 assigned_pane.add(index_label, BorderLayout.NORTH);
531 assigned_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
532 assigned_pane.add(default_pane, BorderLayout.SOUTH);
533
534 central_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
535 central_pane.setLayout(new GridLayout(1,2));
536 central_pane.add(edit_pane);
537 central_pane.add(assigned_pane);
538
539 button_pane.setLayout(new GridLayout(2,2));
540 button_pane.add(add);
541 button_pane.add(remove);
542 button_pane.add(clear_default);
543 button_pane.add(set_default);
544
545 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
546 setLayout(new BorderLayout());
547 add(header_pane, BorderLayout.NORTH);
548 add(central_pane, BorderLayout.CENTER);
549 add(button_pane, BorderLayout.SOUTH);
550 }
551 /* Destructor, removes persistant listeners from the Dictionary.
552 */
553 public void destroy() {
554 }
555 /** We override the updateUI method so that we can ensure we are scrolled to the top of the instructions box first.
556 * @see org.greenstone.gatherer.Gatherer
557 * @see org.greenstone.gatherer.collection.CollectionManager
558 * @see org.greenstone.gatherer.msm.MetadataSetManager
559 */
560 public void updateUI() {
561 if(instructions != null) {
562 instructions.setCaretPosition(0);
563 }
564 // Reload the assigned indexes list.
565 if(source_model != null) {
566 source_model.clear();
567 source_model.add("text");
568 source_model.addAll(gatherer.c_man.msm.getAssignedElements());
569 }
570 // Faster than a NPE its the - 'Super class'.
571 super.updateUI();
572 }
573 /** Listens for actions apon the 'add' button in the IndexManager controls, and if detected calls the add method of the manager with a newly created index. */
574 private class AddListener
575 implements ActionListener {
576 /** Method called when an action is performed on a registered component, and when it does we check if we have enough data to create a new index, and if so we create one.
577 * @param event An <strong>ActionEvent</strong> providing extra information about the event.
578 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
579 * @see org.greenstone.gatherer.cdm.CollectionMeta
580 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
581 * @see org.greenstone.gatherer.cdm.Index
582 * @see org.greenstone.gatherer.cdm.Language
583 * @see org.greenstone.gatherer.cdm.LanguageManager
584 */
585 public void actionPerformed(ActionEvent event) {
586 if(!source_list.isSelectionEmpty() && name.getText().length() != 0) {
587 Object object[] = source_list.getSelectedValues();
588 Vector sources = new Vector();
589 for(int i = 0; i < object.length; i++) {
590 sources.add(object[i]);
591 }
592 Index index = new Index(level.getSelectedIndex(), sources, manager);
593 // Before we add the index to the model, we have to add the collection metadata for this.
594 Language language = manager.languages.getDefaultLanguage();
595 CollectionMeta metadata = new CollectionMeta(manager, index, language, name.getText());
596 manager.collectionmetadatum.addMetadata(metadata);
597 // Finally add index.
598 addIndex(index);
599 }
600 }
601 }
602 /** Listens for actions apon the 'clear default' button in the IndexManager controls, and if detected calls the setDefault method of the manager with <i>null</i>. */
603 private class ClearDefaultListener
604 implements ActionListener {
605 /** If called when an action occurs on a registered component, we clear the default index.
606 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
607 */
608 public void actionPerformed(ActionEvent event) {
609 setDefault(null);
610 clear_default.setEnabled(false);
611 default_value.setText("");
612 }
613 }
614 /** Listens for actions apon the 'remove' button in the IndexManager controls, and if detected calls the remove method of the manager with the index selected for removal. */
615 private class RemoveListener
616 implements ActionListener {
617 /** If called when an action occurs on a registered component, we remove the currently selected index, if there is one.
618 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
619 * @see org.greenstone.gatherer.cdm.Index
620 */
621 public void actionPerformed(ActionEvent event) {
622 if(!index_list.isSelectionEmpty()) {
623 removeIndex((Index)index_list.getSelectedValue());
624 if(default_index == null) {
625 clear_default.setEnabled(false);
626 default_value.setText("");
627 }
628 }
629 }
630 }
631 /** Listens for actions apon the 'set default' button in the IndexManager controls, and if detected calls the setDefault method of the manager with the index selected for default. */
632 private class SetDefaultListener
633 implements ActionListener {
634 /** If called when an action occurs on a registered component, we set the default index to the index currently selected.
635 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
636 * @see org.greenstone.gatherer.cdm.Index
637 */
638 public void actionPerformed(ActionEvent event) {
639 setDefault((Index)index_list.getSelectedValue());
640 if(default_index != null) {
641 clear_default.setEnabled(true);
642 default_value.setText(default_index.toString(true));
643 default_value.setCaretPosition(0);
644 }
645 else {
646 clear_default.setEnabled(false);
647 default_value.setText("");
648 }
649 }
650 }
651 /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */
652 private class NameListener
653 extends KeyAdapter {
654 /** Called when a key is released, this is the perfect time to enable the add button if the fields are appropriately set.
655 * @param event A <strong>KeyEvent</strong> containing information about the key released.
656 */
657 public void keyReleased(KeyEvent event) {
658 if(source_list.isSelectionEmpty() || name.getText().length() == 0) {
659 add.setEnabled(false);
660 }
661 else {
662 add.setEnabled(true);
663 }
664 if(index_list.isSelectionEmpty()) {
665 remove.setEnabled(false);
666 set_default.setEnabled(false);
667 }
668 else {
669 remove.setEnabled(true);
670 set_default.setEnabled(true);
671 }
672 }
673 }
674 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
675 private class ListListener
676 implements ListSelectionListener {
677 /** This method is called whenever the source list selection changes. When it does we need to check if the add button should now be enabled.
678 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
679 */
680 public void valueChanged(ListSelectionEvent event) {
681 if(source_list.isSelectionEmpty() || name.getText().length() == 0) {
682 add.setEnabled(false);
683 }
684 else {
685 add.setEnabled(true);
686 }
687 if(index_list.isSelectionEmpty()) {
688 remove.setEnabled(false);
689 set_default.setEnabled(false);
690 }
691 else {
692 remove.setEnabled(true);
693 set_default.setEnabled(true);
694 }
695 }
696 }
697
698
699 private class IndexListCellRenderer
700 extends DefaultListCellRenderer {
701
702 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
703 StringBuffer text = new StringBuffer(value.toString());
704 // Retrieve the indexes name if any.
705 CollectionMeta metadata = manager.collectionmetadatum.getMetadata(value, manager.languages.getDefaultLanguage(), true);
706 if(metadata != null) {
707 text.append(" \"");
708 text.append(metadata.getValue());
709 text.append("\"");
710 }
711 return super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);
712 }
713
714 }
715
716 }
717}
718
719
Note: See TracBrowser for help on using the repository browser.