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

Last change on this file since 4613 was 4611, checked in by mdewsnip, 21 years ago

Changed index name text field to have a white background (editable).

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