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

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

now change the instructions for mg/mgpp mode

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