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

Last change on this file since 11031 was 11031, checked in by kjdon, 18 years ago

removed instructions, changed title to be the same string as in the contents, and use a new class DesignPaneHeader to create teh header which has title and help button

  • Property svn:keywords set to Author Date Id Revision
File size: 52.0 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.event.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Dictionary;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.gui.DesignPaneHeader;
39import org.greenstone.gatherer.gui.GComboBox;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.metadata.MetadataElement;
42import org.greenstone.gatherer.metadata.MetadataSetManager;
43import org.greenstone.gatherer.util.CheckList;
44import org.greenstone.gatherer.util.JarTools;
45import org.greenstone.gatherer.util.StaticStrings;
46import org.w3c.dom.*;
47/** 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.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.3
50 */
51public class IndexManager
52 extends DOMProxyListModel {
53
54 static final private Dimension FIELD_SIZE = new Dimension(200,30);
55
56 static final private String ALLFIELDS = "allfields";
57 /** The controls for editing the indexes. */
58 private Control controls = null;
59 /** A model of the levels, also based on the DOM. */
60 private DOMProxyListModel levels_model = null;
61 /** A reference to ourselves so our inner methods have access. */
62 private DOMProxyListModel model = null;
63 /** The default index. */
64 private Index default_index = null;
65
66 /** Constructor. */
67 public IndexManager(Element indexes) {
68 super(indexes, CollectionConfiguration.INDEX_ELEMENT, new Index());
69 DebugStream.println("IndexManager: " + getSize() + " indexes parsed.");
70 model = this;
71 // Parse and retrieve the default index
72 NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
73 if(default_index_elements.getLength() > 0) {
74 default_index = new Index((Element)default_index_elements.item(0));
75 }
76 // Parse and retrieve the levels element
77 Element levels_element = CollectionDesignManager.collect_config.getLevels();
78 levels_model = new DOMProxyListModel(levels_element, CollectionConfiguration.CONTENT_ELEMENT, new Level());
79 DebugStream.println(" + " + levels_model.getSize() + " levels parsed.");
80 }
81
82 /** Method to add a new index.
83 * @param index The <strong>Index</strong> to add.
84 * @see org.greenstone.gatherer.Gatherer
85 * @see org.greenstone.gatherer.collection.CollectionManager
86 */
87 private void addIndex(Index index, CollectionMeta metadatum) {
88 ///ystem.err.println("Adding an index: " + index.toString());
89 if(!contains(index)) {
90 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
91 // Retrieve the currently last index
92 if(getSize() > 0) {
93 Index last_index = (Index)getElementAt(getSize() - 1);
94 addAfter(index, last_index);
95
96 }
97 else {
98 add(index);
99 // Also set this index as the default one,
100 setDefault(index);
101 }
102 Gatherer.c_man.configurationChanged();
103 }
104 else {
105 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
106 }
107 }
108
109 private void addLevel(Level level, CollectionMeta metadatum) {
110 if(!levels_model.contains(level)) {
111 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
112 // Retrieve the currently last level
113 if(levels_model.getSize() > 0) {
114 Level last_level = (Level)levels_model.getElementAt(levels_model.getSize() - 1);
115 levels_model.addAfter(level, last_level);
116 }
117 else {
118 levels_model.add(level);
119 }
120 Gatherer.c_man.configurationChanged();
121 }
122 else {
123 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Level_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
124 }
125 }
126
127 public void destroy() {
128 if(controls != null) {
129 controls.destroy();
130 controls = null;
131 }
132 default_index = null;
133 model = null;
134 }
135
136 /** Method to acquire the controls for editing the indexes.
137 * @return the Control
138 */
139 public Control getControls() {
140 if(controls == null) {
141 // Build controls
142 controls = new IndexControl();
143 }
144 return controls;
145 }
146
147 /** Method to get the default index.
148 * @return The default <strong>Index</strong>.
149 */
150 public Index getDefault() {
151 if(default_index != null && default_index.isAssigned()) {
152 return default_index;
153 }
154 else {
155 return null;
156 }
157 }
158
159 /** Method to retrieve a certain index, as referenced by an index number.
160 * @param index An <i>int</i> which indicates the position of the desired index.
161 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
162 */
163 public Index getIndex(int index) {
164 if(0 <= index && index < getSize()) {
165 return (Index)getElementAt(index);
166 }
167 return null;
168 }
169
170 /** Method to retrieve a certain index, given its id.
171 * @param id the id of the index as a String
172 * @return the Index that matches id, or null if no such index exists
173 */
174 public Index getIndex(String id) {
175 int size = getSize();
176 for(int i = 0; i < size; i++) {
177 Index index = (Index) getElementAt(i);
178 if(index.getID().equals(id)) {
179 return index;
180 }
181 }
182 return null;
183 }
184
185 public ArrayList getIndexes() {
186 return children();
187 }
188
189 public Level getLevel(String name) {
190 int levels_model_size = levels_model.getSize();
191 for(int i = 0; i < levels_model_size; i++) {
192 Level level = (Level) levels_model.getElementAt(i);
193 if(level.getLevel().equals(name)) {
194 return level;
195 }
196 }
197 return null;
198 }
199
200 public int getNumLevels() {
201 return levels_model.getSize();
202 }
203 private void moveIndex(Index index, boolean move_up)
204 {
205 // Determine the current position of the index
206 int position = indexOf(index);
207 // 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.
208 if(position == -1) {
209 return;
210 }
211 if(position == 0 && move_up) {
212 return;
213 }
214 if(position == (getSize()) - 1 && !move_up) {
215 return;
216 }
217
218 // Ok, move up
219 if (move_up) {
220 position--;
221 remove(index);
222 add(position, index);
223 }
224
225 // Or move down
226 else {
227 position++;
228 remove(index);
229 add(position, index);
230 }
231
232 // Schedule the collection for saving
233 Gatherer.c_man.configurationChanged();
234 }
235
236
237 private void moveLevel(Level level, boolean move_up) {
238 // Determine the leveles current position
239 int position = levels_model.indexOf(level);
240 // 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.
241 if(position == -1) {
242 return;
243 }
244 if(position == 0 && move_up) {
245 return;
246 }
247 if(position == (levels_model.getSize()) - 1 && !move_up) {
248 return;
249 }
250 // Ok, move up
251 if(move_up) {
252 position--;
253 levels_model.remove(level);
254 levels_model.add(position, level);
255 }
256 // Or move down
257 else {
258 position++;
259 levels_model.remove(level);
260 levels_model.add(position, level);
261 }
262 // Schedule the collection for saving
263 Gatherer.c_man.configurationChanged();
264 }
265
266 /** Method to remove a certain index.
267 * @param index the Index to remove.
268 * @see org.greenstone.gatherer.Gatherer
269 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
270 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
271 * @see org.greenstone.gatherer.collection.CollectionManager
272 */
273 private void removeIndex(Index index) {
274 if(index != null) {
275 // Remove any current metadata from this index
276 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
277 // Remove the index
278 remove(index);
279 // Check if the index removed happens to be the default index
280 if(default_index != null && default_index.equals(index)) {
281 // If so our first solution is to set the first index to be default
282 if(getSize() > 0) {
283 Index another_index = (Index) getElementAt(0);
284 setDefault(another_index);
285 another_index = null;
286 }
287 else {
288 default_index.setAssigned(false);
289 }
290 }
291 Gatherer.c_man.configurationChanged();
292 }
293 }
294
295 private void removeLevel(Level level) {
296 if(level != null) {
297 // Remove any current metadata from this level
298 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getLevel());
299 // Remove the level
300 levels_model.remove(level);
301 Gatherer.c_man.configurationChanged();
302 }
303 }
304
305 /* replace an index in the list. new index may have the same sources but a
306 different name, or may be a new index altogether */
307 private void replaceIndex(Index old_index, Index new_index,
308 CollectionMeta coll_meta) {
309 if (old_index == null || new_index == null || coll_meta == null) {
310 return;
311 }
312 if (!old_index.getID().equals(new_index.getID()) && contains(new_index)) {
313 // shoudl we output an error??
314 return;
315 }
316 // Remove the old index coll meta
317 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + old_index.getID());
318 // Add the new coll meta
319 CollectionDesignManager.collectionmeta_manager.addMetadatum(coll_meta);
320
321 // get the position of the old one
322 int position = indexOf(old_index);
323 // remove it
324 remove(old_index);
325 // add the new one at that position
326 add(position, new_index);
327
328 // Schedule the collection for saving
329 Gatherer.c_man.configurationChanged();
330 }
331
332 /** Method to set the default index.
333 * @param index the new default Index
334 * @see org.greenstone.gatherer.Gatherer
335 * @see org.greenstone.gatherer.collection.CollectionManager
336 */
337 public void setDefault(Index index) {
338 if(index != null) {
339 if(default_index == null) {
340 // Create the default index element, and place immediately after indexes element.
341 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
342 default_index = new Index(default_index_element);
343 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
344 if(target_node != null) {
345 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
346 }
347 else {
348 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
349 }
350 }
351 default_index.setAssigned(true);
352 default_index.setLevel(index.getLevel());
353 default_index.setSources(index.getSources());
354
355 }
356 else {
357 if(default_index != null) {
358 default_index.setAssigned(false);
359 }
360 }
361 Gatherer.c_man.configurationChanged();
362 }
363
364 /** This method is reponsible for changing the underlying Index commands from MG to MGPP and back again. This turns out to be easyish for MG->MGPP and very hard for the reverse. For the former we remove the level fragment and make sure the same levels are set, then we produce a list of the sources involved, breaking down comma seperated lists and making sure each item it unique. Changing back the other way turns out to be impossible, so we don't (beyond generating document:text, section:text and paragraph:text if text is an index and the respective levels are present). In either case we start by creating a comment containing the old index information.
365 * @param state true to enable MGPP indexes, false to use standard MG style ones
366 */
367 public void setMGPPEnabled(boolean state) {
368 if(state == root.getAttribute(CollectionConfiguration.MGPP_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR)) {
369 // we are not changing state
370 return;
371
372 }
373 // change MG -> MGPP
374 if(state) {
375 Element mg_element = root;
376 // Retrieve and assign the MGPP indexes element.
377 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
378 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
379 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
380 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
381 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
382 if(indexes.getLength() == 0) {
383 ArrayList levels_list = new ArrayList();
384 ArrayList sources_list = new ArrayList();
385 // We first use details from the default index if any
386 if(default_index != null) {
387 int level_int = default_index.getLevel();
388 if(0 <= level_int && level_int < 3) {
389 String level = Index.LEVEL[level_int];
390 levels_list.add(level);
391 level = null;
392 }
393 ArrayList sources = default_index.getSources();
394 sources_list.addAll(sources);
395 }
396 int size = getSize();
397 for(int i = 0; i < size; i++) {
398 Index index = (Index) getElementAt(i);
399 int level_int = index.getLevel();
400 if(0 <= level_int && level_int < 3) {
401 String level = Index.LEVEL[level_int];
402 if(!levels_list.contains(level)) {
403 levels_list.add(level);
404 }
405 level = null;
406 }
407 ArrayList sources = index.getSources();
408 sources.removeAll(sources_list);
409 sources_list.addAll(sources);
410 index = null;
411 }
412 // Replace mg element with mgpp element
413 setRoot(mgpp_element);
414
415 // We now have a list of sources and a list of levels, so create new indexes and levels based on these
416 int sources_list_size = sources_list.size();
417 for(int j = 0; j < sources_list_size; j++) {
418 Object source_object = sources_list.get(j);
419 String source_str = null;
420 if(source_object instanceof MetadataElement) {
421 source_str = ((MetadataElement) source_object).getFullName();
422 }
423 else {
424 source_str = source_object.toString();
425 }
426 ArrayList new_sources = new ArrayList();
427 new_sources.add(source_object);
428 source_object = null;
429 Index new_index = new Index(new_sources);
430 // Try to retrieve existing metadatum
431 source_str = new_index.getID();
432 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
433 // If no metadata was found, add new pseudo metadata using the id
434 if(metadatum == null) {
435 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
436 metadatum.setAssigned(true);
437 metadatum.setValue(source_str);
438 }
439 // If it was found, ensure it is assigned
440 else {
441 metadatum.setAssigned(true);
442 }
443 source_str = null;
444 addIndex(new_index, metadatum);
445 metadatum = null;
446 new_index = null;
447 new_sources = null;
448 source_str = null;
449 }
450 int levels_list_size = levels_list.size();
451 for(int k = 0; k < levels_list_size; k++) {
452 String level_name = (String)levels_list.get(k);
453 Level new_level = new Level(level_name);
454 if(!levels_model.contains(new_level)) {
455 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + level_name, false);
456 // If no metadata was found, add new pseudo metadata using the id
457 if(metadatum == null) {
458 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + level_name);
459 metadatum.setAssigned(true);
460 metadatum.setValue(level_name);
461 }
462 addLevel(new_level, metadatum);
463 }
464 new_level = null;
465
466 }
467 }
468 else {
469 // we already have mgpp indexes assigned
470 // Replace mg element with mgpp element
471 setRoot(mgpp_element);
472 }
473 // Unassign MG element and default index
474 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
475 mg_element = null;
476 }
477 // change MGPP -> MG
478 else {
479 Element mgpp_element = root;
480 // Retrieve and assign MG element and default index element
481 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
482 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
483 // If mg element has no indexes, and the current mgpp index include a text one, then generate text indexes for each of the registered levels.
484 NodeList indexes = mg_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
485 if(indexes.getLength() == 0) {
486 Index index = getIndex(CollectionConfiguration.TEXT_STR);
487 if(index != null) {
488 // Replace mgpp element with mg element
489 setRoot(mg_element);
490 int level_size = levels_model.getSize();
491 for(int i = 0; i < level_size; i++) {
492 Level level = (Level) levels_model.getElementAt(i);
493 Index new_index = new Index(level.getLevel(), index.getSources());
494 // Try to retrieve existing metadatum
495 String source_str = new_index.getID();
496 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
497 // If no metadata was found, add new pseudo metadata using the id
498 if(metadatum == null) {
499 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
500 metadatum.setAssigned(true);
501 metadatum.setValue(source_str);
502 }
503 // If it was found, ensure it is assigned
504 else {
505 metadatum.setAssigned(true);
506 }
507 source_str = null;
508 addIndex(new_index, metadatum);
509 new_index = null;
510 level = null;
511 }
512 }
513 }
514 else {
515 // Replace mgpp element with mg element
516 setRoot(mg_element);
517 }
518 // Unassign mgpp element and levels
519 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
520 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
521 }
522 // its really hard to transfer defaults between mg and mgpp. so we'll just assign the first one to be the default.
523 Index first_index = (Index) getElementAt(0);
524 setDefault(first_index);
525 first_index = null;
526
527
528 }
529
530
531 /** This class creates a set of controls for editing the indexes. */
532 private class IndexControl
533 extends JPanel
534 implements Control {
535
536 // private JTextArea instruction_textarea;
537
538 private JList index_list;
539
540 private JButton move_down_button;
541 private JButton move_up_button;
542 private JButton set_default_button;
543
544 private JTextField name_textfield ;
545 private CheckList source_list;
546 // mg uses a level box
547 private JComboBox level_combobox;
548 // mgpp has a allfields selector
549 private JCheckBox allfields_box;
550
551 private JButton add_button;
552 private JButton add_all_button;
553 private JButton remove_button;
554 private JButton replace_button;
555
556 // soem panels that we need to manipulate later on
557 private JPanel boxes_pane;
558 private JPanel labels_pane;
559 private JLabel level_label;
560 private JPanel allfields_pane;
561
562 // we add in a tabbed pane for mgpp mode
563 private JTabbedPane tabbed_pane;
564 // the main index set up pane
565 private JPanel main_index_pane;
566
567 // for the levels tab in mgpp mode
568 private MGPPLevelsPanel mgpplevels_pane;
569
570 private boolean mgpp_enabled = false;
571 /** Constructor.
572 * @see org.greenstone.gatherer.Configuration
573 * @see org.greenstone.gatherer.Gatherer
574 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.AddListener
575 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.NameListener
576 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.RemoveListener
577 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.SetDefaultListener
578 * @see org.greenstone.gatherer.collection.CollectionManager
579 */
580 public IndexControl() {
581 super();
582
583 ArrayList new_data = new ArrayList();
584 new_data.add(CollectionConfiguration.TEXT_STR);
585 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
586
587 // Creation
588 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Indexes", "searchindexes");
589 JPanel assigned_indexes_pane = new JPanel();
590 JLabel index_label = new JLabel();
591 Dictionary.registerText(index_label, "CDM.IndexManager.Indexes");
592 index_list = new JList(model);
593 index_list.setCellRenderer(new IndexListRenderer());
594 index_list.setVisibleRowCount(2);
595 index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
596
597
598 JPanel movement_pane = new JPanel();
599 move_up_button = new JButton("", JarTools.getImage("arrow-up.gif"));
600 move_up_button.setEnabled(false);
601 move_up_button.setMnemonic(KeyEvent.VK_U);
602 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
603
604 move_down_button = new JButton("", JarTools.getImage("arrow-down.gif"));
605 move_down_button.setEnabled(false);
606 move_down_button.setMnemonic(KeyEvent.VK_D);
607 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
608
609 set_default_button = new GLIButton();
610 set_default_button.setEnabled(false);
611 set_default_button.setMnemonic(KeyEvent.VK_S);
612 Dictionary.registerBoth(set_default_button, "CDM.IndexManager.Set_Default", "CDM.IndexManager.Set_Default_Tooltip");
613
614 JPanel new_index_pane = new JPanel();
615 JPanel details_pane = new JPanel();
616 labels_pane = new JPanel();
617 boxes_pane = new JPanel();
618 main_index_pane = new JPanel();
619
620 JLabel name_label = new JLabel();
621 Dictionary.registerText(name_label, "CDM.IndexManager.Index_Name");
622 name_textfield = new JTextField();
623 name_textfield.setPreferredSize(FIELD_SIZE);
624 Dictionary.registerTooltip(name_textfield, "CDM.IndexManager.Index_Name_Tooltip");
625
626 JLabel source_label = new JLabel();
627 Dictionary.registerText(source_label, "CDM.IndexManager.Source");
628 source_list = new CheckList(false);
629 source_list.setListData(new_data);
630 Dictionary.registerTooltip(source_list, "CDM.IndexManager.Source_Tooltip");
631
632 level_label = new JLabel();
633 Dictionary.registerText(level_label, "CDM.IndexManager.Level");
634 level_combobox = new JComboBox();
635 level_combobox.setPreferredSize(FIELD_SIZE);
636 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
637 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
638 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
639 level_combobox.setEditable(false);
640 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
641
642 JPanel button_pane = new JPanel();
643 add_button = new GLIButton();
644 add_button.setEnabled(false);
645 add_button.setMnemonic(KeyEvent.VK_A);
646 Dictionary.registerBoth(add_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
647
648 add_all_button = new GLIButton();
649 add_all_button.setEnabled(true);
650 add_all_button.setMnemonic(KeyEvent.VK_L);
651 Dictionary.registerBoth(add_all_button, "CDM.IndexManager.MGPP.Add_All_Metadata", "CDM.IndexManager.MGPP.Add_All_Metadata_Tooltip");
652
653 remove_button = new GLIButton();
654 remove_button.setEnabled(false);
655 remove_button.setMnemonic(KeyEvent.VK_R);
656 Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
657 replace_button = new GLIButton();
658 replace_button.setEnabled(false);
659 replace_button.setMnemonic(KeyEvent.VK_P);
660 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
661
662 allfields_pane = new JPanel();
663 allfields_box = new JCheckBox();
664 JLabel allfields_label = new JLabel();
665 Dictionary.registerText(allfields_label, "CDM.IndexManager.Allfields_Index");
666 allfields_pane.setLayout(new BorderLayout());
667 allfields_pane.add(allfields_box, BorderLayout.WEST);
668 allfields_pane.add(allfields_label, BorderLayout.CENTER);
669
670 // Listeners
671 add_button.addActionListener(new AddListener());
672 add_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
673 add_all_button.addActionListener(new AddAllActionListener());
674 add_all_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
675 remove_button.addActionListener(new RemoveListener());
676 remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
677 replace_button.addActionListener(new ReplaceListener());
678 replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
679 move_down_button.addActionListener(new MoveListener(false));
680 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
681 move_up_button.addActionListener(new MoveListener(true));
682 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
683 set_default_button.addActionListener(new SetDefaultListener());
684 set_default_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
685
686 name_textfield.getDocument().addDocumentListener(new NameListener());
687 level_combobox.addItemListener(new LevelListener());
688 index_list.addListSelectionListener(new IndexListListener());
689 source_list.addListSelectionListener(new SourceListListener());
690 allfields_box.addItemListener(new AllfieldsCheckBoxListener());
691
692 // Layout
693
694 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
695 movement_pane.setLayout(new GridLayout(3,1));
696 movement_pane.add(move_up_button);
697 movement_pane.add(move_down_button);
698 movement_pane.add(set_default_button);
699
700 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
701 assigned_indexes_pane.setLayout(new BorderLayout());
702 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
703 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
704 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
705
706 labels_pane.setLayout(new BorderLayout());
707 labels_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5));
708 labels_pane.add(name_label, BorderLayout.NORTH);
709 labels_pane.add(source_label, BorderLayout.CENTER);
710 labels_pane.add(level_label, BorderLayout.SOUTH);
711
712 boxes_pane.setLayout(new BorderLayout());
713 boxes_pane.add(name_textfield, BorderLayout.NORTH);
714 boxes_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
715 boxes_pane.add(level_combobox, BorderLayout.SOUTH);
716
717 details_pane.setLayout(new BorderLayout());
718 details_pane.add(labels_pane, BorderLayout.WEST);
719 details_pane.add(boxes_pane, BorderLayout.CENTER);
720
721 button_pane.setLayout(new GridLayout(2,2,5,0));
722 button_pane.add(add_button);
723 button_pane.add(add_all_button);
724 button_pane.add(replace_button);
725 button_pane.add(remove_button);
726
727 new_index_pane.setLayout(new BorderLayout());
728 new_index_pane.add(details_pane, BorderLayout.CENTER);
729 new_index_pane.add(button_pane, BorderLayout.SOUTH);
730
731 main_index_pane.setLayout(new BorderLayout());
732 main_index_pane.add(assigned_indexes_pane, BorderLayout.NORTH);
733 main_index_pane.add(new_index_pane, BorderLayout.CENTER);
734
735 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
736 setLayout(new BorderLayout());
737 add(header_pane, BorderLayout.NORTH);
738 add(main_index_pane, BorderLayout.CENTER);
739
740 // for later, if we go to MGPP mode
741 tabbed_pane = new JTabbedPane();
742 }
743
744
745 /* Destructor, removes persistant listeners from the Dictionary.
746 */
747 public void destroy() {
748 }
749
750 public void gainFocus() {
751 boolean old_mgpp_enabled = mgpp_enabled;
752 mgpp_enabled = CollectionDesignManager.searchtype_manager.isSearchTypeEnabled();
753 boolean changed_state = (old_mgpp_enabled != mgpp_enabled);
754 if (changed_state) {
755 // reset the underlying indexes and levels elements
756 setMGPPEnabled(mgpp_enabled);
757 }
758 /* reset the source list - may have built between then and now */
759 ArrayList new_data = new ArrayList();
760 new_data.add(CollectionConfiguration.TEXT_STR);
761 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
762 source_list.setListData(new_data);
763 new_data = null;
764
765 if(mgpp_enabled) {
766 if (changed_state) {
767 // switch the variable bits
768 boxes_pane.remove(level_combobox);
769 labels_pane.remove(level_label);
770 boxes_pane.add(allfields_pane, BorderLayout.SOUTH);
771 //put back the tabbed pane
772 remove(main_index_pane);
773 add(tabbed_pane, BorderLayout.CENTER);
774 tabbed_pane.insertTab(Dictionary.get("CDM.IndexManager.MGPP.Indexes"), null, main_index_pane, null, 0);
775 if (mgpplevels_pane == null) {
776 mgpplevels_pane = new MGPPLevelsPanel();
777 tabbed_pane.insertTab(Dictionary.get("CDM.IndexManager.MGPP.Levels"), null, mgpplevels_pane, null, 1);
778 }
779 index_list.setSelectedIndex(0);
780
781 }
782 }
783 else {
784 if (changed_state) {
785 boxes_pane.remove(allfields_pane);
786 boxes_pane.add(level_combobox, BorderLayout.SOUTH);
787 labels_pane.add(level_label, BorderLayout.SOUTH);
788 remove(tabbed_pane);
789 tabbed_pane.remove(0);
790 add(main_index_pane, BorderLayout.CENTER);
791 source_list.setEnabled(true); // in case it had been disabled
792 allfields_box.setSelected(false);
793 index_list.setSelectedIndex(0);
794
795 }
796 }
797 updateControlsWithSelectedIndex();
798
799 }
800
801 public void loseFocus() {
802 }
803
804 private void updateControlsWithSelectedIndex() {
805
806 Index selected_index = (Index)index_list.getSelectedValue();
807 if (selected_index == null) {
808 return;
809 }
810 String id = selected_index.getID();
811 if (id == null || id.equals("")) {
812 return;
813 }
814 String name = CollectionDesignManager.collectionmeta_manager.getMetadatum("." + id).getValue(true);
815 name_textfield.setText(name);
816 if (!mgpp_enabled) {
817 level_combobox.setSelectedIndex(selected_index.getLevel());
818 }
819 // TODO: need to set all fields if necessary
820 source_list.clearTicked();
821 ArrayList sources = selected_index.getSources();
822 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
823 source_list.setEnabled(false);
824 allfields_box.setSelected(true);
825 } else {
826 source_list.setTickedObjects(sources.toArray());
827 source_list.setEnabled(true);
828 allfields_box.setSelected(false);
829 }
830 }
831
832 private void validateAddButton() {
833
834 String new_index_name = name_textfield.getText();
835 // indexes must have a name
836 if (new_index_name.length() == 0) {
837 add_button.setEnabled(false);
838 replace_button.setEnabled(false);
839 return;
840 }
841
842 boolean add_enabled = false;
843 boolean replace_enabled = false;
844
845 Index index;
846 ArrayList sources;
847 if (mgpp_enabled && allfields_box.isSelected()) {
848 sources = new ArrayList();
849 sources.add(ALLFIELDS);
850 index = new Index(sources);
851
852 } else if (!source_list.isNothingTicked()) {
853 Object object[] = source_list.getTicked().toArray();
854 sources = new ArrayList();
855 for(int i = 0; i < object.length; i++) {
856 sources.add(object[i]);
857 }
858 object = null;
859
860 if (mgpp_enabled) {
861 index = new Index(sources);
862 } else {
863 index = new Index(level_combobox.getSelectedIndex(), sources);
864 }
865 } else {
866 add_button.setEnabled(false);
867 replace_button.setEnabled(false);
868 return;
869 }
870 sources = null;
871 if (model.contains(index)) {
872 add_enabled = false;
873 // may be able to replace if the index selected is the same but with a different name
874 if (index_list.getSelectedIndex() != -1) {
875 String id = index.getID();
876
877 String selected_index_id = ((Index)index_list.getSelectedValue()).getID();
878 if (id.equals(selected_index_id)) {
879 // check the name
880 String existing_index_name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+id).getValue(true);
881 if (!existing_index_name.equals(new_index_name)) {
882 replace_enabled = true;
883 }
884 }
885 }
886 } else {
887 add_enabled = true;
888 if (index_list.getSelectedIndex() != -1) {
889 // there is something selected, so we can replace
890 replace_enabled = true;
891 }
892 }
893
894 // We should now know the button state
895 add_button.setEnabled(add_enabled);
896 replace_button.setEnabled(replace_enabled);
897 }
898
899 private class AddListener
900 implements ActionListener
901 {
902 public void actionPerformed(ActionEvent event)
903 {
904 String name = name_textfield.getText();
905 if (name.length() == 0) return;
906 Index index;
907 if (mgpp_enabled && allfields_box.isSelected()) {
908 ArrayList sources = new ArrayList();
909 sources.add(ALLFIELDS);
910 index = new Index(sources);
911 }
912 else if (!source_list.isNothingTicked()) {
913 ArrayList sources = source_list.getTicked();
914 if(mgpp_enabled) {
915 index = new Index(sources);
916 } else {
917 index = new Index(level_combobox.getSelectedIndex(), sources);
918 }
919 } else {
920 return;
921 }
922
923 // Before we add the index to the model, we have to add the collection metadata for this
924 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
925 metadatum.setValue(name);
926
927 // Finally, add the index
928 addIndex(index, metadatum);
929 index_list.setSelectedValue(index, true);
930 add_button.setEnabled(false);
931
932 }
933 }
934
935 /** add all sources as indexes. for MG, this adds them in one combined
936 index, for MGPP this adds them as individual indexes (fields) */
937 private class AddAllActionListener
938 implements ActionListener {
939
940 public void actionPerformed(ActionEvent event) {
941 ArrayList all_sources = source_list.getAll();
942 if (!mgpp_enabled) {
943 Index index = new Index(level_combobox.getSelectedIndex(), all_sources);
944 if (!model.contains(index)) {
945 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
946 metadatum.setValue("all fields"); // need a good name
947 addIndex(index, metadatum);
948 }
949 all_sources = null;
950 index = null;
951 return;
952 }
953
954 ArrayList new_sources = new ArrayList();
955 for(int i = 0; i < all_sources.size(); i++) {
956 Object source = all_sources.get(i);
957
958 // Create new index
959 new_sources.clear();
960 new_sources.add(source);
961 Index index = new Index(new_sources);
962 if(!model.contains(index)) {
963 // Determine the metadatum value
964 String name = source.toString();
965 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
966 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
967 }
968 // Create new metadatum
969 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
970 metadatum.setValue(name);
971 name = null;
972 // Assign new index
973 addIndex(index, metadatum);
974 }
975 source = null;
976 index = null;
977 }
978 new_sources = null;
979 // Done. Disable add
980 add_button.setEnabled(false);
981 }
982 }
983
984 private class AllfieldsCheckBoxListener
985 implements ItemListener
986 {
987 public void itemStateChanged(ItemEvent event) {
988 if (event.getStateChange() == ItemEvent.SELECTED) {
989 source_list.setEnabled(false);
990 } else if (event.getStateChange() == ItemEvent.DESELECTED) {
991 source_list.setEnabled(true);
992 }
993 validateAddButton();
994 }
995
996 }
997 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
998 private class IndexListListener
999 implements ListSelectionListener
1000 {
1001 /** 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
1002 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
1003 */
1004 public void valueChanged(ListSelectionEvent event)
1005 {
1006 if (event.getValueIsAdjusting()) {
1007 return;
1008 }
1009
1010 Object value = index_list.getSelectedValue();
1011 if (value == null) {
1012 move_down_button.setEnabled(false);
1013 move_up_button.setEnabled(false);
1014 remove_button.setEnabled(false);
1015 replace_button.setEnabled(false);
1016 set_default_button.setEnabled(false);
1017 return;
1018 }
1019
1020 // Enable the buttons appropriately
1021 remove_button.setEnabled(true);
1022 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
1023 int i = index_list.getSelectedIndex();
1024 int size = index_list.getModel().getSize();
1025 move_up_button.setEnabled((i>0));
1026 move_down_button.setEnabled((i<size-1));
1027
1028 // Need to fill in the rest of the bits
1029 updateControlsWithSelectedIndex();
1030 }
1031 }
1032
1033 private class IndexListRenderer
1034 extends DefaultListCellRenderer {
1035
1036 /** Return a component that has been configured to display the specified value. */
1037 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
1038 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1039 if(default_index != null && default_index.equals(value)) {
1040 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
1041 }
1042 return component;
1043 }
1044
1045 }
1046
1047 private class LevelListener
1048 implements ItemListener {
1049 public void itemStateChanged(ItemEvent event) {
1050 if(event.getStateChange() == ItemEvent.SELECTED) {
1051 // just want to validate the add button
1052 validateAddButton();
1053
1054 }
1055 }
1056 }
1057
1058 private class MoveListener
1059 implements ActionListener {
1060
1061 private boolean move_up;
1062
1063 public MoveListener(boolean move_up) {
1064 this.move_up = move_up;
1065 }
1066
1067 public void actionPerformed(ActionEvent event) {
1068 // Retrieve the selected index
1069 Index index = (Index) index_list.getSelectedValue();
1070 if(index != null) {
1071 moveIndex(index, move_up);
1072 // Ensure the index that moved is still selected
1073 index_list.setSelectedValue(index, true);
1074 index = null;
1075 }
1076 }
1077 }
1078
1079
1080 /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */
1081 private class NameListener
1082 implements DocumentListener
1083 {
1084 public void changedUpdate(DocumentEvent e)
1085 {
1086 validateAddButton();
1087 }
1088
1089 public void insertUpdate(DocumentEvent e)
1090 {
1091 validateAddButton();
1092 }
1093
1094 public void removeUpdate(DocumentEvent e)
1095 {
1096 validateAddButton();
1097 }
1098 }
1099
1100
1101 private class RemoveListener
1102 implements ActionListener
1103 {
1104 public void actionPerformed(ActionEvent event)
1105 {
1106 int i = index_list.getSelectedIndex();
1107 if (i != -1) {
1108 removeIndex((Index) index_list.getSelectedValue());
1109 }
1110 int size = index_list.getModel().getSize();
1111 if (i == size) {
1112 i--;
1113 }
1114 index_list.setSelectedIndex(i);
1115 // This will produce an event on the list, updating the other buttons
1116 if (size == 0) {
1117 // We have removed the last index, should be able to add what's filled in currently, if valid
1118 validateAddButton();
1119 }
1120 }
1121 }
1122
1123
1124 private class ReplaceListener
1125 implements ActionListener
1126 {
1127 public void actionPerformed(ActionEvent event)
1128 {
1129 if (index_list.isSelectionEmpty()) {
1130 // This should never happen, but just in case...
1131 replace_button.setEnabled(false);
1132 return;
1133 }
1134
1135 String name = name_textfield.getText();
1136 if (!source_list.isNothingTicked() && name.length() != 0) {
1137 ArrayList sources = source_list.getTicked();
1138 Index index;
1139 if (mgpp_enabled) {
1140 index = new Index(sources);
1141 } else {
1142 index = new Index(level_combobox.getSelectedIndex(), sources);
1143 }
1144 // Create the new collection meta
1145 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1146 metadatum.setValue(name);
1147
1148 // replace the index
1149 replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
1150 index_list.setSelectedValue(index, true);
1151
1152 }
1153 add_button.setEnabled(false);
1154 replace_button.setEnabled(false);
1155 }
1156 }
1157
1158
1159 private class SetDefaultListener
1160 implements ActionListener {
1161
1162 public void actionPerformed(ActionEvent event) {
1163 Index index = (Index) index_list.getSelectedValue();
1164 if(index != null) {
1165 setDefault(index);
1166 // This should cause a repaint of just the desired row
1167 index_list.setSelectedValue(index, true);
1168 }
1169 set_default_button.setEnabled(false);
1170 }
1171 }
1172
1173
1174 private class SourceListListener
1175 implements ListSelectionListener
1176 {
1177 public void valueChanged(ListSelectionEvent event)
1178 {
1179 validateAddButton();
1180 }
1181 }
1182 }
1183
1184 private class MGPPLevelsPanel
1185 extends JPanel {
1186
1187 private JList current_levels_list;
1188
1189 private JButton move_level_down_button;
1190 private JButton move_level_up_button;
1191
1192 private GComboBox level_combobox;
1193 private JTextField level_name_field;
1194
1195 private JButton add_level_button;
1196 private JButton remove_level_button;
1197
1198 public MGPPLevelsPanel() {
1199
1200 JPanel current_levels_panel = new JPanel();
1201
1202 JLabel current_levels_label = new JLabel();
1203 Dictionary.registerText(current_levels_label, "CDM.IndexManager.MGPP.Current_Levels");
1204
1205 current_levels_list = new JList(levels_model);
1206 current_levels_list.setVisibleRowCount(5);
1207 current_levels_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1208 JPanel level_movement_panel = new JPanel();
1209
1210 move_level_up_button = new JButton("", JarTools.getImage("arrow-up.gif"));
1211 move_level_up_button.setEnabled(false);
1212 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1213 Dictionary.registerBoth(move_level_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1214
1215 move_level_down_button = new JButton("", JarTools.getImage("arrow-down.gif"));
1216 move_level_down_button.setEnabled(false);
1217 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1218 Dictionary.registerBoth(move_level_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1219
1220 JPanel level_spacer_panel = new JPanel();
1221 JPanel level_body_panel = new JPanel();
1222 JPanel level_details_panel = new JPanel();
1223 JPanel level_labels_panel = new JPanel();
1224 JPanel level_boxes_panel = new JPanel();
1225
1226 JLabel level_name_label = new JLabel();
1227 Dictionary.registerText(level_name_label, "CDM.IndexManager.MGPP.Level_Name");
1228
1229 level_name_field = new JTextField();
1230 level_name_field.setPreferredSize(FIELD_SIZE);
1231 Dictionary.registerTooltip(level_name_field, "CDM.IndexManager.MGPP.Level_Name_Tooltip");
1232
1233
1234 JLabel level_label = new JLabel();
1235 Dictionary.registerText(level_label, "CDM.IndexManager.MGPP.Level");
1236
1237 level_combobox = new GComboBox(Index.LEVEL);
1238 level_combobox.setPreferredSize(FIELD_SIZE);
1239 level_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1240 level_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1241 level_combobox.setEditable(false);
1242 level_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1243 level_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1244 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
1245
1246 JPanel level_button_panel = new JPanel();
1247
1248 add_level_button = new GLIButton("CDM.IndexManager.MGPP.Add_Level");
1249 add_level_button.setEnabled(false);
1250 add_level_button.setMnemonic(KeyEvent.VK_A);
1251 Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip");
1252
1253 remove_level_button = new GLIButton("CDM.IndexManager.MGPP.Remove_Level");
1254 remove_level_button.setEnabled(false);
1255 remove_level_button.setMnemonic(KeyEvent.VK_A);
1256 Dictionary.registerBoth(remove_level_button, "CDM.IndexManager.MGPP.Remove_Level", "CDM.IndexManager.MGPP.Remove_Level_Tooltip");
1257
1258 JPanel level_empty_panel = new JPanel();
1259
1260 // Connect Levels
1261 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1262 add_level_button.addActionListener(new AddLevelActionListener());
1263 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1264 level_combobox.addActionListener(level_eal);
1265 // not editable
1266 //((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1267 level_name_field.getDocument().addDocumentListener(level_eal);
1268 move_level_down_button.addActionListener(new MoveLevelDownListener());
1269 move_level_up_button.addActionListener(new MoveLevelUpListener());
1270 remove_level_button.addActionListener(new RemoveLevelActionListener());
1271 // Layout Levels
1272
1273 level_movement_panel.setLayout(new GridLayout(2,1));
1274 level_movement_panel.add(move_level_up_button);
1275 level_movement_panel.add(move_level_down_button);
1276
1277 current_levels_panel.setLayout(new BorderLayout());
1278 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1279 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1280 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1281
1282 level_labels_panel.setLayout(new GridLayout(2,1));
1283 level_labels_panel.add(level_name_label);
1284 level_labels_panel.add(level_label);
1285
1286 level_boxes_panel.setLayout(new GridLayout(2,1));
1287 level_boxes_panel.add(level_name_field);
1288 level_boxes_panel.add(level_combobox);
1289
1290 level_details_panel.setLayout(new BorderLayout(5,0));
1291 level_details_panel.add(level_labels_panel, BorderLayout.WEST);
1292 level_details_panel.add(level_boxes_panel, BorderLayout.CENTER);
1293
1294 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1295 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1296 level_button_panel.add(add_level_button);
1297 level_button_panel.add(remove_level_button);
1298
1299 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1300 level_body_panel.setLayout(new BorderLayout());
1301 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1302 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1303
1304 level_spacer_panel.setLayout(new BorderLayout());
1305 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1306 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1307
1308 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1309 setLayout(new BorderLayout());
1310 add(current_levels_panel, BorderLayout.NORTH);
1311 add(level_spacer_panel, BorderLayout.CENTER);
1312 }
1313
1314 public void gainFocus() {
1315 // Ensure the level manager has at least documents assigned
1316 if(levels_model.getSize() == 0) {
1317 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1318 // Create new metadatum
1319 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1320 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1321 // Assign new level
1322 addLevel(level, metadatum);
1323 level = null;
1324 }
1325 }
1326
1327 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1328 public void validateAddButtonLevel() {
1329 String name = level_name_field.getText();
1330 Object selected_object = level_combobox.getSelectedItem();
1331 if(name.length() > 0 && selected_object != null) {
1332 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1333 }
1334 else {
1335 add_level_button.setEnabled(false);
1336 }
1337 }
1338
1339 private class AddLevelActionListener
1340 implements ActionListener {
1341 public void actionPerformed(ActionEvent event) {
1342 // Retrieve the name
1343 String name = level_name_field.getText();
1344 // Retrieve the source
1345 String source = (String)level_combobox.getSelectedItem();
1346 Level level = new Level(source);
1347 // Create new metadatum
1348 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1349 metadatum.setValue(name);
1350 // Assign new level
1351 int size = levels_model.getSize();
1352 addLevel(level, metadatum);
1353 current_levels_list.setSelectedValue(level, true);
1354 add_level_button.setEnabled(false);
1355 }
1356 }
1357
1358 private class CurrentLevelsListSelectionListener
1359 implements ListSelectionListener {
1360 public void valueChanged(ListSelectionEvent event) {
1361
1362 if(event.getValueIsAdjusting()) {
1363 return;
1364 }
1365
1366 Level level = (Level)current_levels_list.getSelectedValue();
1367 if(level != null) {
1368 String full_text = level.toString();
1369 if(full_text.indexOf("\"") != -1) {
1370 level_name_field.setText(level.getName());
1371 }
1372 String level_id = level.getLevel();
1373 level_combobox.setSelectedItem(level_id);
1374 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1375 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1376 remove_level_button.setEnabled(true);
1377 }
1378 else {
1379 move_level_down_button.setEnabled(false);
1380 move_level_up_button.setEnabled(false);
1381 remove_level_button.setEnabled(false);
1382 }
1383 }
1384 }
1385
1386 private class EnableAddLevelListener
1387 implements ActionListener, DocumentListener {
1388 /** Called whenever a selection action occurs on the combobox.
1389 * @param event an ActionEvent containing information about the selection event
1390 */
1391 public void actionPerformed(ActionEvent event) {
1392 validateAddButtonLevel();
1393 }
1394
1395 /** Gives notification that an attribute or set of attributes changed.
1396 * @param event a DocumentEvent containing information about the text changed
1397 */
1398 public void changedUpdate(DocumentEvent event) {
1399 validateAddButtonLevel();
1400 }
1401
1402 /** Gives notification that there was an insert into the document.
1403 * @param event a DocumentEvent containing information about the text added
1404 */
1405 public void insertUpdate(DocumentEvent event) {
1406 validateAddButtonLevel();
1407 }
1408
1409 /** Gives notification that a portion of the document has been removed.
1410 * @param event a DocumentEvent containing information about the text removed
1411 */
1412 public void removeUpdate(DocumentEvent event) {
1413 validateAddButtonLevel();
1414 }
1415
1416 }
1417 private class MoveLevelDownListener
1418 implements ActionListener {
1419 public void actionPerformed(ActionEvent event) {
1420 // Retrieve the first selected item
1421 Level level = (Level) current_levels_list.getSelectedValue();
1422 moveLevel(level, false);
1423 current_levels_list.setSelectedValue(level, true);
1424 }
1425 }
1426 private class MoveLevelUpListener
1427 implements ActionListener {
1428 public void actionPerformed(ActionEvent event) {
1429 // Retrieve the first selected item
1430 Level level = (Level) current_levels_list.getSelectedValue();
1431 moveLevel(level, true);
1432 current_levels_list.setSelectedValue(level, true);
1433 }
1434 }
1435 private class RemoveLevelActionListener
1436 implements ActionListener {
1437
1438 public void actionPerformed(ActionEvent event) {
1439
1440 int i = current_levels_list.getSelectedIndex();
1441 if (i != -1) {
1442 Level level = (Level) current_levels_list.getSelectedValue();
1443 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getLevel());
1444 removeLevel(level);
1445 if (i == current_levels_list.getModel().getSize()) {
1446 i--;
1447 }
1448 current_levels_list.setSelectedIndex(i);
1449 }
1450 if (i==-1) {
1451 // Disable remove button
1452 remove_level_button.setEnabled(false);
1453 }
1454 validateAddButtonLevel();
1455 }
1456 }
1457
1458 }
1459
1460}
1461
Note: See TracBrowser for help on using the repository browser.