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

Last change on this file since 4293 was 4293, checked in by jmt12, 21 years ago

Initial revision

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