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

Last change on this file since 9640 was 9552, checked in by mdewsnip, 19 years ago

Fixed a bug where adding indexes on metadata elements whose name didn't match their display name would be screwed up. This was caused by the combobox being editable and converting the contents into strings rather than keeping them as objects. The combobox has been changed to be non-editable and some extra code has been added to check everything stays frosty.

  • Property svn:keywords set to Author Date Id Revision
File size: 63.9 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.StaticStrings;
44import org.greenstone.gatherer.util.Utility;
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 static final private String MGINDEXES = "mg indexes";
55 static final private String MGPPINDEXES = "mgpp indexes";
56
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, but only if there are levels available (ie mg only)
100 if(index.getLevel() != -1) {
101 setDefault(index);
102 }
103 }
104 Gatherer.c_man.configurationChanged();
105 }
106 else {
107 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
108 }
109 }
110
111 private void addLevel(Level level, CollectionMeta metadatum) {
112 if(!levels_model.contains(level)) {
113 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
114 // Retrieve the currently last index
115 if(levels_model.getSize() > 0) {
116 Level last_level = (Level)levels_model.getElementAt(levels_model.getSize() - 1);
117 levels_model.addAfter(level, last_level);
118 }
119 else {
120 levels_model.add(level);
121 }
122 Gatherer.c_man.configurationChanged();
123 }
124 else {
125 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Level_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
126 }
127 }
128
129 public void destroy() {
130 if(controls != null) {
131 controls.destroy();
132 controls = null;
133 }
134 default_index = null;
135 model = null;
136 }
137
138 /** Method to acquire the controls for editing the indexes.
139 * @return the Control
140 */
141 public Control getControls() {
142 if(controls == null) {
143 // Build controls
144 controls = new IndexControl();
145 }
146 return controls;
147 }
148
149 /** Method to get the default index.
150 * @return The default <strong>Index</strong>.
151 */
152 public Index getDefault() {
153 if(default_index != null && default_index.isAssigned()) {
154 return default_index;
155 }
156 else {
157 return null;
158 }
159 }
160
161 /** Method to retrieve a certain index, as referenced by an index number.
162 * @param index An <i>int</i> which indicates the position of the desired index.
163 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
164 */
165 public Index getIndex(int index) {
166 if(0 <= index && index < getSize()) {
167 return (Index)getElementAt(index);
168 }
169 return null;
170 }
171
172 /** Method to retrieve a certain index, given its id.
173 * @param id the id of the index as a String
174 * @return the Index that matches id, or null if no such index exists
175 */
176 public Index getIndex(String id) {
177 int size = getSize();
178 for(int i = 0; i < size; i++) {
179 Index index = (Index) getElementAt(i);
180 if(index.getID().equals(id)) {
181 return index;
182 }
183 }
184 return null;
185 }
186
187 public ArrayList getIndexes() {
188 return children();
189 }
190
191 public Level getLevel(String name) {
192 int levels_model_size = levels_model.getSize();
193 for(int i = 0; i < levels_model_size; i++) {
194 Level level = (Level) levels_model.getElementAt(i);
195 if(level.getName().equals(name)) {
196 return level;
197 }
198 }
199 return null;
200 }
201
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 if (position == -1) {
208 return;
209 }
210
211 // Attempt to move the index up
212 if (move_up) {
213 // Check it's not already at the top
214 if (position == 0) {
215 return;
216 }
217
218 // Retrieve the index at position - 1
219 Index preceeding_index = (Index) getElementAt(position - 1);
220
221 // This automatically removes the index first, as an Element can only exist once in a particular document
222 addBefore(index, preceeding_index);
223 }
224
225 // Attempt to move the index down
226 else {
227 // Check it's not already at the bottom
228 if (position == (getSize()) - 1) {
229 return;
230 }
231
232 // Retrieve the index at position + 1
233 Index following_index = (Index) getElementAt(position + 1);
234
235 // This automatically removes the index first, as an Element can only exist once in a particular document
236 addAfter(index, following_index);
237 }
238
239 // Schedule the collection for saving
240 Gatherer.c_man.configurationChanged();
241 }
242
243
244 private void moveLevel(Level level, boolean move_up) {
245 // Determine the leveles current position
246 int position = levels_model.indexOf(level);
247 // 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.
248 if(position == -1) {
249 return;
250 }
251 if(position == 0 && move_up) {
252 return;
253 }
254 if(position == (levels_model.getSize()) - 1 && !move_up) {
255 return;
256 }
257 // Ok, move the level
258 if(move_up) {
259 // Retrieve the level at position - 1
260 Level previous_level = (Level) levels_model.getElementAt(position - 1);
261 // And add before. This will automatically remove the level first, as an Element can only exist once in a particular document
262 levels_model.addBefore(level, previous_level);
263 }
264 else {
265 // Retrieve the level at position + 1
266 Level next_level = (Level) levels_model.getElementAt(position + 1);
267 // And add after. This will automatically remove the level first, as an Element can only exist once in a particular document
268 levels_model.addAfter(level, next_level);
269 }
270 // Schedule the collection for saving
271 Gatherer.c_man.configurationChanged();
272 }
273
274 /** Method to remove a certain index.
275 * @param index the Index to remove.
276 * @see org.greenstone.gatherer.Gatherer
277 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
278 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
279 * @see org.greenstone.gatherer.collection.CollectionManager
280 */
281 private void removeIndex(Index index) {
282 if(index != null) {
283 // Remove any current metadata from this index
284 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
285 // Check if the index removed happens to be the default index
286 if(default_index != null && default_index.equals(index)) {
287 // If so our first solution is to set the first index to be default
288 if(getSize() > 0) {
289 Index another_index = (Index) getElementAt(0);
290 setDefault(another_index);
291 another_index = null;
292 }
293 else {
294 default_index.setAssigned(false);
295 }
296 }
297 // Remove the index
298 remove(index);
299 Gatherer.c_man.configurationChanged();
300 }
301 }
302
303 private void removeLevel(Level level) {
304 if(level != null) {
305 // Remove any current metadata from this level
306 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
307 // Remove the level
308 levels_model.remove(level);
309 Gatherer.c_man.configurationChanged();
310 }
311 }
312
313 /** Method to set the default index.
314 * @param index the new default Index
315 * @see org.greenstone.gatherer.Gatherer
316 * @see org.greenstone.gatherer.collection.CollectionManager
317 */
318 public void setDefault(Index index) {
319 if(index != null) {
320 if(default_index == null) {
321 // Create the default index element, and place immediately after indexes element.
322 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
323 default_index = new Index(default_index_element);
324 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
325 if(target_node != null) {
326 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
327 }
328 else {
329 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
330 }
331 }
332 default_index.setAssigned(true);
333 default_index.setLevel(index.getLevel());
334 default_index.setSources(index.getSources());
335 }
336 else {
337 if(default_index != null) {
338 default_index.setAssigned(false);
339 }
340 }
341 Gatherer.c_man.configurationChanged();
342 }
343
344 /** 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.
345 * @param state true to enable MGPP indexes, false to use standard MG style ones
346 */
347 public void setMGPPEnabled(boolean state) {
348 if(state != root.getAttribute(CollectionConfiguration.MGPP_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR)) {
349 if(state) {
350 Element mg_element = root;
351 // Retrieve and assign the MGPP indexes element.
352 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
353 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
354 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
355 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
356 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
357 if(indexes.getLength() == 0) {
358 ArrayList levels_list = new ArrayList();
359 ArrayList sources_list = new ArrayList();
360 // We first use details from the default index if any
361 if(default_index != null) {
362 int level_int = default_index.getLevel();
363 if(0 <= level_int && level_int < 3) {
364 String level = Index.LEVEL[level_int];
365 if(!levels_list.contains(level)) {
366 levels_list.add(level);
367 }
368 level = null;
369 }
370 ArrayList sources = default_index.getSources();
371 sources.removeAll(sources_list);
372 sources_list.addAll(sources);
373 }
374 int size = getSize();
375 for(int i = 0; i < size; i++) {
376 Index index = (Index) getElementAt(i);
377 int level_int = index.getLevel();
378 if(0 <= level_int && level_int < 3) {
379 String level = Index.LEVEL[level_int];
380 if(!levels_list.contains(level)) {
381 levels_list.add(level);
382 }
383 level = null;
384 }
385 ArrayList sources = index.getSources();
386 sources.removeAll(sources_list);
387 sources_list.addAll(sources);
388 index = null;
389 }
390 // Replace mg element with mgpp element
391 setRoot(mgpp_element);
392
393 // We now have a list of sources and a list of levels, so create new indexes and levels based on these
394 int sources_list_size = sources_list.size();
395 for(int j = 0; j < sources_list_size; j++) {
396 Object source_object = sources_list.get(j);
397 String source_str = null;
398 if(source_object instanceof MetadataElement) {
399 source_str = ((MetadataElement) source_object).getFullName();
400 }
401 else {
402 source_str = source_object.toString();
403 }
404 ArrayList new_sources = new ArrayList();
405 new_sources.add(source_object);
406 source_object = null;
407 Index new_index = new Index(new_sources);
408 // Try to retrieve existing metadatum
409 source_str = new_index.getID();
410 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
411 // If no metadata was found, add new pseudo metadata using the id
412 if(metadatum == null) {
413 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
414 metadatum.setAssigned(true);
415 metadatum.setValue(source_str);
416 }
417 // If it was found, ensure it is assigned
418 else {
419 metadatum.setAssigned(true);
420 }
421 source_str = null;
422 addIndex(new_index, metadatum);
423 metadatum = null;
424 new_index = null;
425 new_sources = null;
426 source_str = null;
427 }
428 int levels_list_size = levels_list.size();
429 for(int k = 0; k < levels_list_size; k++) {
430 Level new_level = new Level((String)levels_list.get(k));
431 if(!levels_model.contains(new_level)) {
432 levels_model.add(levels_model.getSize(), new_level);
433 }
434 new_level = null;
435 }
436 }
437 else {
438 // Replace mg element with mgpp element
439 setRoot(mgpp_element);
440 }
441 // Unassign MG element and default index
442 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
443 mg_element = null;
444 if(default_index != null) {
445 default_index.setAssigned(false);
446 }
447 }
448 else {
449 Element mgpp_element = root;
450 // Retrieve and assign MG element and default index element
451 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
452 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
453 if(default_index != null) {
454 default_index.setAssigned(true);
455 }
456 // 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.
457 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
458 if(indexes.getLength() == 0) {
459 Index index = getIndex(CollectionConfiguration.TEXT_STR);
460 if(index != null) {
461 // Replace mgpp element with mg element
462 setRoot(mg_element);
463 int level_size = levels_model.getSize();
464 for(int i = 0; i < level_size; i++) {
465 Level level = (Level) levels_model.getElementAt(i);
466 Index new_index = new Index(level.getName(), index.getSources());
467 // Try to retrieve existing metadatum
468 String source_str = new_index.getID();
469 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
470 // If no metadata was found, add new pseudo metadata using the id
471 if(metadatum == null) {
472 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
473 metadatum.setAssigned(true);
474 metadatum.setValue(source_str);
475 }
476 // If it was found, ensure it is assigned
477 else {
478 metadatum.setAssigned(true);
479 }
480 source_str = null;
481 addIndex(new_index, metadatum);
482 new_index = null;
483 level = null;
484 }
485 }
486 }
487 else {
488 // Replace mgpp element with mg element
489 setRoot(mg_element);
490 }
491 // Unassign mgpp element and levels
492 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
493 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
494 }
495 }
496 }
497
498
499 /** This class creates a set of controls for editing the indexes. */
500 private class IndexControl
501 extends JPanel
502 implements Control {
503
504 private CardLayout card_layout;
505 private CheckList source_list;
506 private JButton add_button;
507 private JButton move_down_button;
508 private JButton move_up_button;
509 private JButton remove_button;
510 private JButton replace_button;
511 private JButton set_default_button;
512 private JComboBox level_combobox;
513 private JList index_list;
514 private JTextArea instruction_textarea;
515 private JTextField name_textfield ;
516
517 private MGPPControl mgppindexes_control;
518
519 /** Constructor.
520 * @see org.greenstone.gatherer.Configuration
521 * @see org.greenstone.gatherer.Gatherer
522 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.AddListener
523 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.NameListener
524 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.RemoveListener
525 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.SetDefaultListener
526 * @see org.greenstone.gatherer.collection.CollectionManager
527 */
528 public IndexControl() {
529 super();
530
531 ArrayList new_data = new ArrayList();
532 new_data.add(CollectionConfiguration.TEXT_STR);
533 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
534 // Creation
535 JPanel mgindexes_panel = new JPanel();
536 card_layout = new CardLayout();
537
538 // MG Index Controls
539
540 JPanel mg_indexes_pane = new JPanel();
541
542 JPanel instruction_pane = new JPanel();
543 JLabel title_label = new JLabel();
544 title_label.setHorizontalAlignment(JLabel.CENTER);
545 Dictionary.registerText(title_label, "CDM.IndexManager.Title");
546
547 instruction_textarea = new JTextArea();
548 instruction_textarea.setEditable(false);
549 instruction_textarea.setLineWrap(true);
550 instruction_textarea.setRows(6);
551 instruction_textarea.setWrapStyleWord(true);
552 Dictionary.registerText(instruction_textarea, "CDM.IndexManager.Instructions");
553
554 JPanel assigned_indexes_pane = new JPanel();
555 JLabel index_label = new JLabel();
556 Dictionary.registerText(index_label, "CDM.IndexManager.Indexes");
557 index_list = new JList(model);
558 index_list.setCellRenderer(new IndexListRenderer());
559 index_list.setVisibleRowCount(2);
560 JPanel movement_pane = new JPanel();
561
562 move_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
563 move_up_button.setEnabled(false);
564 move_up_button.setMnemonic(KeyEvent.VK_U);
565 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
566
567 move_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
568 move_down_button.setEnabled(false);
569 move_down_button.setMnemonic(KeyEvent.VK_D);
570 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
571
572 set_default_button = new GLIButton();
573 set_default_button.setEnabled(false);
574 set_default_button.setMnemonic(KeyEvent.VK_S);
575 Dictionary.registerBoth(set_default_button, "CDM.IndexManager.Set_Default", "CDM.IndexManager.Set_Default_Tooltip");
576
577 JPanel index_pane = new JPanel();
578 JPanel details_pane = new JPanel();
579 JPanel labels_pane = new JPanel();
580 JPanel boxes_pane = new JPanel();
581 JPanel content_pane = new JPanel();
582
583 JLabel name_label = new JLabel();
584 Dictionary.registerText(name_label, "CDM.IndexManager.Index_Name");
585 name_textfield = new JTextField();
586 name_textfield.setPreferredSize(FIELD_SIZE);
587 Dictionary.registerTooltip(name_textfield, "CDM.IndexManager.Index_Name_Tooltip");
588
589 JLabel source_label = new JLabel();
590 Dictionary.registerText(source_label, "CDM.IndexManager.Source");
591 source_list = new CheckList(false);
592 source_list.setListData(new_data);
593 Dictionary.registerTooltip(source_list, "CDM.IndexManager.Source_Tooltip");
594
595 JLabel level_label = new JLabel();
596 Dictionary.registerText(level_label, "CDM.IndexManager.Level");
597 level_combobox = new JComboBox();
598 level_combobox.setPreferredSize(FIELD_SIZE);
599 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
600 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
601 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
602 level_combobox.setEditable(false);
603 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
604
605 JPanel button_pane = new JPanel();
606 add_button = new GLIButton();
607 add_button.setEnabled(false);
608 add_button.setMnemonic(KeyEvent.VK_A);
609 Dictionary.registerBoth(add_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
610
611 remove_button = new GLIButton();
612 remove_button.setEnabled(false);
613 remove_button.setMnemonic(KeyEvent.VK_R);
614 Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
615 replace_button = new GLIButton();
616 replace_button.setEnabled(false);
617 replace_button.setMnemonic(KeyEvent.VK_P);
618 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
619
620 // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class.
621 mgppindexes_control = new MGPPControl();
622
623 // Listeners
624 add_button.addActionListener(new AddListener());
625 move_down_button.addActionListener(new MoveListener(false));
626 move_up_button.addActionListener(new MoveListener(true));
627 remove_button.addActionListener(new RemoveListener());
628 replace_button.addActionListener(new ReplaceListener());
629 set_default_button.addActionListener(new SetDefaultListener());
630 name_textfield.getDocument().addDocumentListener(new NameListener());
631 level_combobox.addItemListener(new LevelListener());
632 index_list.addListSelectionListener(new IndexListListener());
633 source_list.addListSelectionListener(new SourceListListener());
634
635 // Layout
636 instruction_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
637 instruction_pane.setLayout(new BorderLayout());
638 instruction_pane.add(title_label, BorderLayout.NORTH);
639 instruction_pane.add(new JScrollPane(instruction_textarea), BorderLayout.CENTER);
640
641 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
642 movement_pane.setLayout(new GridLayout(3,1));
643 movement_pane.add(move_up_button);
644 movement_pane.add(move_down_button);
645 movement_pane.add(set_default_button);
646
647 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
648 assigned_indexes_pane.setLayout(new BorderLayout());
649 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
650 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
651 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
652
653 labels_pane.setLayout(new BorderLayout());
654 labels_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5));
655 labels_pane.add(name_label, BorderLayout.NORTH);
656 labels_pane.add(source_label, BorderLayout.CENTER);
657 labels_pane.add(level_label, BorderLayout.SOUTH);
658
659 boxes_pane.setLayout(new BorderLayout());
660 boxes_pane.add(name_textfield, BorderLayout.NORTH);
661 boxes_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
662 boxes_pane.add(level_combobox, BorderLayout.SOUTH);
663
664 details_pane.setLayout(new BorderLayout());
665 details_pane.add(labels_pane, BorderLayout.WEST);
666 details_pane.add(boxes_pane, BorderLayout.CENTER);
667
668 button_pane.setLayout(new GridLayout(1,3));
669 button_pane.add(add_button);
670 button_pane.add(replace_button);
671 button_pane.add(remove_button);
672
673 index_pane.setLayout(new BorderLayout());
674 index_pane.add(details_pane, BorderLayout.CENTER);
675 index_pane.add(button_pane, BorderLayout.SOUTH);
676
677 content_pane.setLayout(new BorderLayout());
678 content_pane.add(assigned_indexes_pane, BorderLayout.NORTH);
679 content_pane.add(index_pane, BorderLayout.CENTER);
680
681 mgindexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
682 mgindexes_panel.setLayout(new BorderLayout());
683 mgindexes_panel.add(instruction_pane, BorderLayout.NORTH);
684 mgindexes_panel.add(content_pane, BorderLayout.CENTER);
685
686 setLayout(card_layout);
687 add(mgindexes_panel, MGINDEXES);
688 add(mgppindexes_control, MGPPINDEXES);
689 }
690
691 /* Destructor, removes persistant listeners from the Dictionary.
692 */
693 public void destroy() {
694 mgppindexes_control.destroy();
695 mgppindexes_control = null;
696 }
697
698 public void gainFocus() {
699 boolean mgpp_enabled = CollectionDesignManager.searchtype_manager.isMGPPEnabled();
700 if(instruction_textarea != null) {
701 instruction_textarea.setCaretPosition(0);
702 }
703 if(mgpp_enabled) {
704 mgppindexes_control.gainFocus();
705 }
706 else {
707 // Reload the assigned indexes list.
708 ArrayList new_data = new ArrayList();
709 new_data.add(CollectionConfiguration.TEXT_STR);
710 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
711 // reset the model in the list and combobox
712 source_list.setListData(new_data);
713 new_data = null;
714 // refresh the indexList
715 index_list.updateUI();
716 // if there is one selected, fill in the controls
717 updateControlsWithSelectedIndex();
718 }
719 // Bring the appropriate card to the front
720 if(mgpp_enabled) {
721 card_layout.show(this, MGPPINDEXES);
722 }
723 else {
724 card_layout.show(this, MGINDEXES);
725 }
726 }
727
728 public void loseFocus() {
729 }
730
731 private void updateControlsWithSelectedIndex() {
732
733 Index selected_index = (Index)index_list.getSelectedValue();
734 if (selected_index == null) {
735 return;
736 }
737 String id = selected_index.getID();
738 if (id == null || id.equals("")) {
739 return;
740 }
741 String name = CollectionDesignManager.collectionmeta_manager.getMetadatum("." + id).getValue(true);
742 name_textfield.setText(name);
743 level_combobox.setSelectedIndex(selected_index.getLevel());
744 source_list.clearTicked();
745 ArrayList sources = selected_index.getSources();
746 source_list.setTickedObjects(sources.toArray());
747
748 }
749
750 private void validateAddButton()
751 {
752 boolean add_enabled = false;
753 boolean replace_enabled = false;
754
755 // Indexes must have a name
756 if (name_textfield.getText().length() == 0) {
757 add_enabled = false;
758 }
759 // Can't add a new index if no sources are selected
760 else if (source_list.isNothingTicked()) {
761 add_enabled = false;
762 }
763 // If we get this far, create a dummy index and see if it's already assigned in the collection
764 else {
765 Object object[] = source_list.getTicked().toArray();
766 ArrayList sources = new ArrayList();
767 for(int i = 0; i < object.length; i++) {
768 sources.add(object[i]);
769 }
770 object = null;
771 Index index = new Index(level_combobox.getSelectedIndex(), sources);
772 sources = null;
773 if (model.contains(index)) {
774 add_enabled = false;
775
776 // Here we need to check if we have changed the name - if so, we can enable the replace button
777 if (index_list.getSelectedIndex() != -1) {
778 Index selected_index = (Index)index_list.getSelectedValue();
779 String name = name_textfield.getText();
780 String index_name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true);
781 if (!index_name.equals(name)){
782 replace_enabled = true;
783 }
784 }
785 }
786 else {
787 add_enabled = true;
788
789 // We have a new index, do we have something selected in the index list? if so, enable the replace button
790 if (index_list.getSelectedIndex() != -1) {
791 replace_enabled = true;
792 }
793 }
794 }
795
796 // We should now know the add_button state
797 add_button.setEnabled(add_enabled);
798 replace_button.setEnabled(replace_enabled);
799 }
800
801
802 private class AddListener
803 implements ActionListener
804 {
805 public void actionPerformed(ActionEvent event)
806 {
807 String name = name_textfield.getText();
808 if (!source_list.isNothingTicked() && name.length() != 0) {
809 ArrayList sources = source_list.getTicked();
810 Index index = new Index(level_combobox.getSelectedIndex(), sources);
811
812 // Before we add the index to the model, we have to add the collection metadata for this
813 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
814 metadatum.setValue(name);
815
816 // Finally, add the index
817 addIndex(index, metadatum);
818 index_list.setSelectedValue(index, true);
819 }
820 }
821 }
822
823
824 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
825 private class IndexListListener
826 implements ListSelectionListener
827 {
828 /** 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
829 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
830 */
831 public void valueChanged(ListSelectionEvent event)
832 {
833 if (event.getValueIsAdjusting()) {
834 return;
835 }
836
837 Object value = index_list.getSelectedValue();
838 if (value == null) {
839 move_down_button.setEnabled(false);
840 move_up_button.setEnabled(false);
841 remove_button.setEnabled(false);
842 replace_button.setEnabled(false);
843 set_default_button.setEnabled(false);
844 return;
845 }
846
847 // Enable the buttons appropriately
848 remove_button.setEnabled(true);
849 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
850 int i = index_list.getSelectedIndex();
851 int size = index_list.getModel().getSize();
852 if (i > 0) {
853 move_up_button.setEnabled(true);
854 }
855 else {
856 move_up_button.setEnabled(false);
857 }
858 if (i < size-1){
859 move_down_button.setEnabled(true);
860 }
861 else {
862 move_down_button.setEnabled(false);
863 }
864
865 // Need to fill in the rest of the bits
866 updateControlsWithSelectedIndex();
867 }
868 }
869
870 private class IndexListRenderer
871 extends DefaultListCellRenderer {
872
873 /** Return a component that has been configured to display the specified value. */
874 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
875 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
876 if(default_index != null && default_index.equals(value)) {
877 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
878 }
879 return component;
880 }
881
882 }
883
884 private class LevelListener
885 implements ItemListener {
886 public void itemStateChanged(ItemEvent event) {
887 if(event.getStateChange() == ItemEvent.SELECTED) {
888 // just want to validate the add button
889 validateAddButton();
890
891 }
892 }
893 }
894
895 private class MoveListener
896 implements ActionListener {
897
898 private boolean move_up;
899
900 public MoveListener(boolean move_up) {
901 this.move_up = move_up;
902 }
903
904 public void actionPerformed(ActionEvent event) {
905 // Retrieve the selected index
906 Index index = (Index) index_list.getSelectedValue();
907 if(index != null) {
908 moveIndex(index, move_up);
909 // Ensure the index that moved is still selected
910 index_list.setSelectedValue(index, true);
911 index = null;
912 }
913 }
914 }
915
916
917 /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */
918 private class NameListener
919 implements DocumentListener
920 {
921 public void changedUpdate(DocumentEvent e)
922 {
923 validateAddButton();
924 }
925
926 public void insertUpdate(DocumentEvent e)
927 {
928 validateAddButton();
929 }
930
931 public void removeUpdate(DocumentEvent e)
932 {
933 validateAddButton();
934 }
935 }
936
937
938 private class RemoveListener
939 implements ActionListener
940 {
941 public void actionPerformed(ActionEvent event)
942 {
943 int i = index_list.getSelectedIndex();
944 if (i != -1) {
945 removeIndex((Index) index_list.getSelectedValue());
946 }
947
948 int size = index_list.getModel().getSize();
949 if (i == size) {
950 i--;
951 }
952 index_list.setSelectedIndex(i);
953
954 // This will produce an event on the list, updating the other buttons
955 if (size == 0) {
956 // We have removed the last index, should be able to add what's filled in currently, if valid
957 validateAddButton();
958 }
959 }
960 }
961
962
963 private class ReplaceListener
964 implements ActionListener
965 {
966 public void actionPerformed(ActionEvent event)
967 {
968 if (index_list.isSelectionEmpty()) {
969 // This should never happen, but just in case...
970 replace_button.setEnabled(false);
971 return;
972 }
973
974 // We'll just remove the old one and add the new one
975 removeIndex((Index) index_list.getSelectedValue());
976 replace_button.setEnabled(false);
977 add_button.setEnabled(true);
978 add_button.doClick();
979 add_button.setEnabled(false);
980 }
981 }
982
983
984 private class SetDefaultListener
985 implements ActionListener {
986
987 public void actionPerformed(ActionEvent event) {
988 Index index = (Index) index_list.getSelectedValue();
989 if(index != null) {
990 setDefault(index);
991 // This should cause a repaint of just the desired row
992 index_list.setSelectedValue(index, true);
993 }
994 set_default_button.setEnabled(false);
995 }
996 }
997
998
999 private class SourceListListener
1000 implements ListSelectionListener
1001 {
1002 public void valueChanged(ListSelectionEvent event)
1003 {
1004 validateAddButton();
1005 }
1006 }
1007 }
1008
1009 private class MGPPControl
1010 extends JPanel {
1011
1012 private GComboBox index_combobox;
1013 private GComboBox level_combobox;
1014 private JButton add_index_button;
1015 private JButton add_all_button;
1016 private JButton add_level_button;
1017 private JButton move_index_down_button;
1018 private JButton move_level_down_button;
1019 private JButton move_index_up_button;
1020 private JButton move_level_up_button;
1021 private JButton replace_button;
1022 private JButton remove_index_button;
1023 private JButton remove_level_button;
1024 private JLabel current_levels_label;
1025 private JLabel current_indexes_label;
1026 private JLabel index_label;
1027 private JLabel index_name_label;
1028 private JLabel level_label;
1029 private JLabel level_name_label;
1030 private JLabel move_index_down_label;
1031 private JLabel move_level_down_label;
1032 private JLabel move_index_up_label;
1033 private JLabel move_level_up_label;
1034 private JLabel title_label;
1035 private JList current_levels_list;
1036 private JList current_indexes_list;
1037 private JTabbedPane tabbed_pane;
1038 private JTextArea instructions_textarea;
1039 private JTextField index_name_field;
1040 private JTextField level_name_field;
1041
1042 public MGPPControl() {
1043 // Create Indexes
1044 JPanel indexes_panel = new JPanel();
1045
1046 JPanel current_indexes_panel = new JPanel();
1047
1048 current_indexes_label = new JLabel();
1049 Dictionary.registerText(current_indexes_label, "CDM.IndexManager.MGPP.Current_Indexes");
1050 current_indexes_list = new JList(model);
1051 current_indexes_list.setVisibleRowCount(5);
1052
1053 JPanel index_movement_panel = new JPanel();
1054
1055 move_index_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
1056 move_index_up_button.setEnabled(false);
1057 move_index_up_button.setMnemonic(KeyEvent.VK_U);
1058 Dictionary.registerBoth(move_index_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1059
1060 move_index_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1061 move_index_down_button.setEnabled(false);
1062 move_index_down_button.setMnemonic(KeyEvent.VK_D);
1063 Dictionary.registerBoth(move_index_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1064
1065 JPanel index_spacer_panel = new JPanel();
1066 JPanel index_body_panel = new JPanel();
1067 JPanel index_details_panel = new JPanel();
1068 JPanel index_labels_panel = new JPanel();
1069
1070 index_name_label = new JLabel();
1071 Dictionary.registerText(index_name_label, "CDM.IndexManager.Index_Name");
1072
1073 index_name_field = new JTextField();
1074 index_name_field.setPreferredSize(FIELD_SIZE);
1075 Dictionary.registerTooltip(index_name_field, "CDM.IndexManager.Index_Name_Tooltip");
1076
1077 JPanel index_boxes_panel = new JPanel();
1078
1079 index_label = new JLabel("CDM.IndexManager.MGPP.Index");
1080 Dictionary.registerText(index_label, "CDM.IndexManager.MGPP.Index");
1081
1082 index_combobox = new GComboBox();
1083 index_combobox.setPreferredSize(FIELD_SIZE);
1084 index_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1085 index_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1086 index_combobox.setEditable(false); // Warning: setting this to true opens a big can of worms
1087 index_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1088 index_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1089 Dictionary.registerTooltip(index_combobox, "CDM.IndexManager.MGPP.Index_Tooltip");
1090
1091 JPanel index_button_panel = new JPanel();
1092
1093 add_index_button = new GLIButton();
1094 add_index_button.setEnabled(false);
1095 add_index_button.setMnemonic(KeyEvent.VK_A);
1096 Dictionary.registerBoth(add_index_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
1097
1098 add_all_button = new GLIButton();
1099 add_all_button.setEnabled(true);
1100 add_all_button.setMnemonic(KeyEvent.VK_L);
1101 Dictionary.registerBoth(add_all_button, "CDM.IndexManager.MGPP.Add_All_Metadata", "CDM.IndexManager.MGPP.Add_All_Metadata_Tooltip");
1102
1103 replace_button = new GLIButton();
1104 replace_button.setEnabled(false);
1105 replace_button.setMnemonic(KeyEvent.VK_C);
1106 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
1107
1108 remove_index_button = new GLIButton();
1109 remove_index_button.setEnabled(false);
1110 remove_index_button.setMnemonic(KeyEvent.VK_A);
1111 Dictionary.registerBoth(remove_index_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
1112
1113 JPanel index_empty_panel = new JPanel();
1114
1115 // Connect Indexes
1116 EnableAddIndexListener index_eal = new EnableAddIndexListener();
1117 add_index_button.addActionListener(new AddIndexActionListener());
1118 add_all_button.addActionListener(new AddAllActionListener());
1119 current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener());
1120 index_combobox.addActionListener(index_eal);
1121 ((JTextField)index_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(index_eal);
1122 index_name_field.getDocument().addDocumentListener(index_eal);
1123 move_index_down_button.addActionListener(new MoveIndexDownListener());
1124 move_index_up_button.addActionListener(new MoveIndexUpListener());
1125 replace_button.addActionListener(new ReplaceIndexActionListener());
1126 remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal));
1127
1128 // Layout Indexes
1129 index_movement_panel.setLayout(new GridLayout(2,1));
1130 index_movement_panel.add(move_index_up_button);
1131 index_movement_panel.add(move_index_down_button);
1132
1133 current_indexes_panel.setLayout(new BorderLayout());
1134 current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH);
1135 current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER);
1136 current_indexes_panel.add(index_movement_panel, BorderLayout.EAST);
1137
1138
1139 index_labels_panel.setLayout(new GridLayout(2,1));
1140 index_labels_panel.add(index_name_label);
1141 index_labels_panel.add(index_label);
1142
1143 index_boxes_panel.setLayout(new GridLayout(2,1));
1144 index_boxes_panel.add(index_name_field);
1145 index_boxes_panel.add(index_combobox);
1146
1147 index_details_panel.setLayout(new BorderLayout(5,0));
1148 index_details_panel.add(index_labels_panel, BorderLayout.WEST);
1149 index_details_panel.add(index_boxes_panel, BorderLayout.CENTER);
1150
1151 index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1152 index_button_panel.setLayout(new GridLayout(2,2,5,0));
1153 index_button_panel.add(add_index_button);
1154 index_button_panel.add(add_all_button);
1155 index_button_panel.add(replace_button);
1156 index_button_panel.add(remove_index_button);
1157
1158 index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1159 index_body_panel.setLayout(new BorderLayout());
1160 index_body_panel.add(index_details_panel, BorderLayout.CENTER);
1161 index_body_panel.add(index_button_panel, BorderLayout.SOUTH);
1162
1163 index_spacer_panel.setLayout(new BorderLayout());
1164 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH);
1165 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER);
1166
1167 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1168 indexes_panel.setLayout(new BorderLayout());
1169 indexes_panel.add(current_indexes_panel, BorderLayout.NORTH);
1170 indexes_panel.add(index_spacer_panel, BorderLayout.CENTER);
1171
1172 // Create Levels
1173 JPanel levels_panel = new JPanel();
1174
1175 JPanel current_levels_panel = new JPanel();
1176
1177 current_levels_label = new JLabel();
1178 Dictionary.registerText(current_levels_label, "CDM.IndexManager.MGPP.Current_Levels");
1179
1180 current_levels_list = new JList(levels_model);
1181 current_levels_list.setVisibleRowCount(5);
1182
1183 JPanel level_movement_panel = new JPanel();
1184
1185 move_level_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
1186 move_level_up_button.setEnabled(false);
1187 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1188 //move_level_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1189 Dictionary.registerBoth(move_level_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1190
1191 move_level_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1192 move_level_down_button.setEnabled(false);
1193 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1194 //move_level_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1195 Dictionary.registerBoth(move_level_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1196
1197 JPanel level_spacer_panel = new JPanel();
1198 JPanel level_body_panel = new JPanel();
1199 JPanel level_details_panel = new JPanel();
1200 JPanel level_labels_panel = new JPanel();
1201 JPanel level_boxes_panel = new JPanel();
1202
1203 level_name_label = new JLabel();
1204 Dictionary.registerText(level_name_label, "CDM.IndexManager.MGPP.Level_Name");
1205
1206 level_name_field = new JTextField();
1207 level_name_field.setPreferredSize(FIELD_SIZE);
1208 Dictionary.registerTooltip(level_name_field, "CDM.IndexManager.MGPP.Level_Name_Tooltip");
1209
1210
1211 level_label = new JLabel();
1212 Dictionary.registerText(level_label, "CDM.IndexManager.MGPP.Level");
1213
1214 level_combobox = new GComboBox(Index.LEVEL);
1215 level_combobox.setPreferredSize(FIELD_SIZE);
1216 level_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1217 level_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1218 level_combobox.setEditable(true);
1219 level_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1220 level_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1221 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
1222
1223 JPanel level_button_panel = new JPanel();
1224
1225 add_level_button = new GLIButton("CDM.IndexManager.MGPP.Add_Level");
1226 add_level_button.setEnabled(false);
1227 add_level_button.setMnemonic(KeyEvent.VK_A);
1228 Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip");
1229
1230 remove_level_button = new GLIButton("CDM.IndexManager.MGPP.Remove_Level");
1231 remove_level_button.setEnabled(false);
1232 remove_level_button.setMnemonic(KeyEvent.VK_A);
1233 Dictionary.registerBoth(remove_level_button, "CDM.IndexManager.MGPP.Remove_Level", "CDM.IndexManager.MGPP.Remove_Level_Tooltip");
1234
1235 JPanel level_empty_panel = new JPanel();
1236
1237 // Connect Levels
1238 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1239 add_level_button.addActionListener(new AddLevelActionListener());
1240 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1241 level_combobox.addActionListener(level_eal);
1242 ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1243 level_name_field.getDocument().addDocumentListener(level_eal);
1244 move_level_down_button.addActionListener(new MoveLevelDownListener());
1245 move_level_up_button.addActionListener(new MoveLevelUpListener());
1246 remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal));
1247 // Layout Levels
1248
1249 level_movement_panel.setLayout(new GridLayout(2,1));
1250 level_movement_panel.add(move_level_up_button);
1251 level_movement_panel.add(move_level_down_button);
1252
1253 current_levels_panel.setLayout(new BorderLayout());
1254 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1255 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1256 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1257
1258 level_labels_panel.setLayout(new GridLayout(2,1));
1259 level_labels_panel.add(level_name_label);
1260 level_labels_panel.add(level_label);
1261
1262 level_boxes_panel.setLayout(new GridLayout(2,1));
1263 level_boxes_panel.add(level_name_field);
1264 level_boxes_panel.add(level_combobox);
1265
1266 level_details_panel.setLayout(new BorderLayout(5,0));
1267 level_details_panel.add(level_labels_panel, BorderLayout.WEST);
1268 level_details_panel.add(level_boxes_panel, BorderLayout.CENTER);
1269
1270 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1271 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1272 level_button_panel.add(add_level_button);
1273 level_button_panel.add(remove_level_button);
1274
1275 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1276 level_body_panel.setLayout(new BorderLayout());
1277 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1278 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1279
1280 level_spacer_panel.setLayout(new BorderLayout());
1281 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1282 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1283
1284 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1285 levels_panel.setLayout(new BorderLayout());
1286 levels_panel.add(current_levels_panel, BorderLayout.NORTH);
1287 levels_panel.add(level_spacer_panel, BorderLayout.CENTER);
1288
1289 // Create General
1290 JPanel header_panel = new JPanel();
1291
1292 title_label = new JLabel();
1293 title_label.setHorizontalAlignment(JLabel.CENTER);
1294 Dictionary.registerText(title_label, "CDM.IndexManager.MGPP.Title");
1295
1296 instructions_textarea = new JTextArea();
1297 instructions_textarea.setEditable(false);
1298 instructions_textarea.setLineWrap(true);
1299 instructions_textarea.setRows(6);
1300 instructions_textarea.setWrapStyleWord(true);
1301 Dictionary.registerText(instructions_textarea, "CDM.IndexManager.MGPP.Instructions");
1302
1303 tabbed_pane = new JTabbedPane();
1304
1305 // Layout General
1306 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
1307 header_panel.setLayout(new BorderLayout());
1308 header_panel.add(title_label, BorderLayout.NORTH);
1309 header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
1310
1311 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Indexes"), indexes_panel);
1312 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Levels"), levels_panel);
1313
1314 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1315 setLayout(new BorderLayout());
1316 add(header_panel, BorderLayout.NORTH);
1317 add(tabbed_pane, BorderLayout.CENTER);
1318 }
1319
1320 public void destroy() {
1321 }
1322
1323 public void gainFocus() {
1324 // Reload the assigned indexes list.
1325 index_combobox.removeAllItems();
1326 index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR);
1327 index_combobox.addItem(CollectionConfiguration.METADATA_STR);
1328 index_combobox.addItem(CollectionConfiguration.TEXT_STR);
1329 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
1330 for(int i = 0; i < every_metadata_set_element.size(); i++) {
1331 index_combobox.addItem(every_metadata_set_element.get(i));
1332 }
1333 // Ensure the level manager has at least documents assigned
1334 if(levels_model.getSize() == 0) {
1335 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1336 // Create new metadatum
1337 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1338 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1339 // Assign new level
1340 addLevel(level, metadatum);
1341 level = null;
1342 }
1343
1344 // refresh
1345 current_indexes_list.updateUI();
1346 }
1347
1348 private class AddIndexActionListener
1349 implements ActionListener {
1350 public void actionPerformed(ActionEvent event) {
1351 // Retrieve the name
1352 String name = index_name_field.getText();
1353 // Retrieve the source
1354 Object source = index_combobox.getSelectedItem();
1355
1356 // If this object isn't yet in the combobox add it.
1357 if(index_combobox.getSelectedIndex() == -1) {
1358 index_combobox.insertItemAt(source, index_combobox.getItemCount());
1359 }
1360 // Create new index
1361 ArrayList sources = new ArrayList();
1362 sources.add(source);
1363 Index index = new Index(sources);
1364 if(!model.contains(index)) {
1365 // Create new metadatum
1366 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1367 metadatum.setValue(name);
1368 // Assign new index
1369 addIndex(index, metadatum);
1370 }
1371 // Done. Disable add
1372 add_index_button.setEnabled(false);
1373 }
1374 }
1375
1376 private class AddAllActionListener
1377 implements ActionListener {
1378
1379 public void actionPerformed(ActionEvent event) {
1380 for(int i = 0; i < index_combobox.getItemCount(); i++) {
1381 Object source = index_combobox.getItemAt(i);
1382 // Create new index
1383 ArrayList sources = new ArrayList();
1384 sources.add(source);
1385 Index index = new Index(sources);
1386 sources = null;
1387 if(!model.contains(index)) {
1388 // Determine the metadatum value
1389 String name = source.toString();
1390 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
1391 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1392 }
1393 // Create new metadatum
1394 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1395 metadatum.setValue(name);
1396 name = null;
1397 // Assign new index
1398 addIndex(index, metadatum);
1399 }
1400 source = null;
1401 index = null;
1402 }
1403 // Done. Disable add
1404 add_index_button.setEnabled(false);
1405 }
1406 }
1407
1408 private class AddLevelActionListener
1409 implements ActionListener {
1410 public void actionPerformed(ActionEvent event) {
1411 // Retrieve the name
1412 String name = level_name_field.getText();
1413 // Retrieve the source
1414 String source = (String)level_combobox.getSelectedItem();
1415 // If this object isn't yet in the combobox add it.
1416 if(level_combobox.getSelectedIndex() == -1) {
1417 level_combobox.insertItemAt(source, level_combobox.getItemCount());
1418 }
1419 Level level = new Level(source);
1420 // Create new metadatum
1421 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1422 metadatum.setValue(name);
1423 // Assign new level
1424 addLevel(level, metadatum);
1425 // Done. Disable add
1426 add_level_button.setEnabled(false);
1427 }
1428 }
1429
1430 private class CurrentIndexesListSelectionListener
1431 implements ListSelectionListener {
1432 public void valueChanged(ListSelectionEvent event) {
1433 if(!event.getValueIsAdjusting()) {
1434 Index index = (Index)current_indexes_list.getSelectedValue();
1435 Object[] selected_objects = current_indexes_list.getSelectedValues();
1436 if(selected_objects.length == 1) {
1437 String full_text = index.toString();
1438 if(full_text.indexOf("\"") != -1) {
1439 index_name_field.setText(index.getName());
1440 }
1441 ArrayList sources = index.getSources();
1442 index_combobox.setSelectedItem(sources.get(0));
1443 }
1444 if(index != null) {
1445 move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1));
1446 move_index_up_button.setEnabled((model.indexOf(index) > 0));
1447 remove_index_button.setEnabled(true);
1448 }
1449 else {
1450 move_index_down_button.setEnabled(false);
1451 move_index_up_button.setEnabled(false);
1452 remove_index_button.setEnabled(false);
1453 }
1454 }
1455 }
1456 }
1457
1458 private class CurrentLevelsListSelectionListener
1459 implements ListSelectionListener {
1460 public void valueChanged(ListSelectionEvent event) {
1461 if(!event.getValueIsAdjusting()) {
1462 Level level = (Level)current_levels_list.getSelectedValue();
1463 if(level != null) {
1464 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1465 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1466 remove_level_button.setEnabled(!level.getName().equals(CollectionConfiguration.DOCUMENT_STR) || levels_model.getSize() > 1);
1467 }
1468 else {
1469 move_level_down_button.setEnabled(false);
1470 move_level_up_button.setEnabled(false);
1471 remove_level_button.setEnabled(false);
1472 }
1473 }
1474 }
1475 }
1476
1477 private class EnableAddIndexListener
1478 implements ActionListener, DocumentListener {
1479 /** Called whenever a selection action occurs on the combobox.
1480 * @param event an ActionEvent containing information about the selection event
1481 */
1482 public void actionPerformed(ActionEvent event) {
1483 validateAddButtonIndex();
1484 }
1485
1486 /** Gives notification that an attribute or set of attributes changed.
1487 * @param event a DocumentEvent containing information about the text changed
1488 */
1489 public void changedUpdate(DocumentEvent event) {
1490 validateAddButtonIndex();
1491 }
1492
1493 /** Gives notification that there was an insert into the document.
1494 * @param event a DocumentEvent containing information about the text added
1495 */
1496 public void insertUpdate(DocumentEvent event) {
1497 validateAddButtonIndex();
1498 }
1499
1500 /** Gives notification that a portion of the document has been removed.
1501 * @param event a DocumentEvent containing information about the text removed
1502 */
1503 public void removeUpdate(DocumentEvent event) {
1504 validateAddButtonIndex();
1505 }
1506
1507 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1508 public void validateAddButtonIndex() {
1509 String name = index_name_field.getText();
1510 Object selected_object = index_combobox.getSelectedItem();
1511 if(name.length() > 0 && selected_object != null) {
1512 // Unfortunately we have to generate a valid id
1513 String id = null;
1514 // Special cases
1515 if (selected_object instanceof String && (selected_object.equals(CollectionConfiguration.ALLFIELDS_STR) || selected_object.equals(CollectionConfiguration.METADATA_STR) || selected_object.equals(CollectionConfiguration.TEXT_STR))) {
1516 id = (String) selected_object;
1517 }
1518 else if (selected_object instanceof MetadataElement) {
1519 id = ((MetadataElement) selected_object).getFullName();
1520 }
1521 else {
1522 System.err.println("Internal Error: Unexpected selected_object in IndexManager.");
1523 return;
1524 }
1525 if(id.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
1526 id = id.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1527 }
1528 Index index = getIndex(id);
1529 if(index == null) {
1530 add_index_button.setEnabled(true);
1531 replace_button.setEnabled(false);
1532 }
1533 else {
1534 add_index_button.setEnabled(false);
1535 replace_button.setEnabled(!name.equals(index.getName()));
1536 }
1537 }
1538 else {
1539 add_index_button.setEnabled(false);
1540 }
1541 }
1542 }
1543
1544 private class EnableAddLevelListener
1545 implements ActionListener, DocumentListener {
1546 /** Called whenever a selection action occurs on the combobox.
1547 * @param event an ActionEvent containing information about the selection event
1548 */
1549 public void actionPerformed(ActionEvent event) {
1550 validateAddButtonLevel();
1551 }
1552
1553 /** Gives notification that an attribute or set of attributes changed.
1554 * @param event a DocumentEvent containing information about the text changed
1555 */
1556 public void changedUpdate(DocumentEvent event) {
1557 validateAddButtonLevel();
1558 }
1559
1560 /** Gives notification that there was an insert into the document.
1561 * @param event a DocumentEvent containing information about the text added
1562 */
1563 public void insertUpdate(DocumentEvent event) {
1564 validateAddButtonLevel();
1565 }
1566
1567 /** Gives notification that a portion of the document has been removed.
1568 * @param event a DocumentEvent containing information about the text removed
1569 */
1570 public void removeUpdate(DocumentEvent event) {
1571 validateAddButtonLevel();
1572 }
1573
1574 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1575 public void validateAddButtonLevel() {
1576 String name = level_name_field.getText();
1577 Object selected_object = level_combobox.getSelectedItem();
1578 if(name.length() > 0 && selected_object != null) {
1579 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1580 }
1581 else {
1582 add_level_button.setEnabled(false);
1583 }
1584 }
1585 }
1586
1587 private class MoveIndexDownListener
1588 implements ActionListener {
1589 public void actionPerformed(ActionEvent event) {
1590 // Retrieve the first selected item
1591 Index index = (Index) current_indexes_list.getSelectedValue();
1592 moveIndex(index, false);
1593 current_indexes_list.setSelectedValue(index, true);
1594 }
1595 }
1596
1597 private class MoveLevelDownListener
1598 implements ActionListener {
1599 public void actionPerformed(ActionEvent event) {
1600 // Retrieve the first selected item
1601 Level level = (Level) current_levels_list.getSelectedValue();
1602 moveLevel(level, false);
1603 current_levels_list.setSelectedValue(level, true);
1604 }
1605 }
1606
1607 private class MoveIndexUpListener
1608 implements ActionListener {
1609 public void actionPerformed(ActionEvent event) {
1610 // Retrieve the first selected item
1611 Index index = (Index) current_indexes_list.getSelectedValue();
1612 moveIndex(index, true);
1613 current_indexes_list.setSelectedValue(index, true);
1614 }
1615 }
1616
1617 private class MoveLevelUpListener
1618 implements ActionListener {
1619 public void actionPerformed(ActionEvent event) {
1620 // Retrieve the first selected item
1621 Level level = (Level) current_levels_list.getSelectedValue();
1622 moveLevel(level, true);
1623 current_levels_list.setSelectedValue(level, true);
1624 }
1625 }
1626
1627 /** Replace really only replaces the string. */
1628 private class ReplaceIndexActionListener
1629 implements ActionListener {
1630
1631 public void actionPerformed(ActionEvent event) {
1632 Object[] selected_objects = current_indexes_list.getSelectedValues();
1633 if(selected_objects.length == 1) {
1634 Index index = (Index) selected_objects[0];
1635 // Remove old name
1636 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1637 // Enter new name
1638 String name = index_name_field.getText();
1639 // Create new metadatum
1640 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1641 index = null;
1642 metadatum.setValue(name);
1643 name = null;
1644 // Assign new index
1645 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
1646 metadatum = null;
1647 }
1648 current_indexes_list.setSelectedValue(selected_objects[0], true);
1649 // Done. Disable add
1650 add_index_button.setEnabled(false);
1651 replace_button.setEnabled(false);
1652 }
1653 }
1654
1655 private class RemoveIndexActionListener
1656 implements ActionListener {
1657
1658 private EnableAddIndexListener eal = null;
1659
1660 public RemoveIndexActionListener(EnableAddIndexListener eal) {
1661 this.eal = eal;
1662 }
1663
1664 public void actionPerformed(ActionEvent event) {
1665 // Retrieve the selected items
1666 Object[] selected_objects = current_indexes_list.getSelectedValues();
1667 // Clear selection
1668 current_indexes_list.clearSelection();
1669 for(int i = 0; i < selected_objects.length; i++) {
1670 Index index = (Index) selected_objects[i];
1671 // Remove any related metadata
1672 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1673 // Remove the index
1674 removeIndex(index);
1675 }
1676 // Disable remove button
1677 remove_index_button.setEnabled(false);
1678 // Check if add should be reenabled
1679 eal.validateAddButtonIndex();
1680 }
1681 }
1682
1683 private class RemoveLevelActionListener
1684 implements ActionListener {
1685
1686 private EnableAddLevelListener eal = null;
1687
1688 public RemoveLevelActionListener(EnableAddLevelListener eal) {
1689 this.eal = eal;
1690 }
1691
1692 public void actionPerformed(ActionEvent event) {
1693 // Retrieve the selected items
1694 Object[] selected_objects = current_levels_list.getSelectedValues();
1695 // Clear selection
1696 current_levels_list.clearSelection();
1697 for(int i = 0; i < selected_objects.length; i++) {
1698 Level level = (Level) selected_objects[i];
1699 // Remove any related metadata
1700 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
1701 // Remove the index
1702 removeLevel(level);
1703 }
1704 // Disable remove button
1705 remove_level_button.setEnabled(false);
1706 // If there are no levels left, put document back in
1707 if(levels_model.getSize() == 0) {
1708 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1709 // Create new metadatum
1710 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1711 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1712 // Assign new level
1713 addLevel(level, metadatum);
1714 level = null;
1715 }
1716 // Check if add should be reenabled
1717 eal.validateAddButtonLevel();
1718 }
1719 }
1720 } // MGPPControls
1721
1722}
Note: See TracBrowser for help on using the repository browser.