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

Last change on this file since 9184 was 9161, checked in by mdewsnip, 19 years ago

Made the "assign partitions" tab in the "Partition Indexes" section look a lot like the "Search Indexes" pane.

  • Property svn:keywords set to Author Date Id Revision
File size: 63.7 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * 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.clearSelection();
745 ArrayList sources = selected_index.getSources();
746 source_list.setSelectedObjects(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.getSelected().size() == 0) {
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.getSelected().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.getSelected().size() > 0 && name.length() != 0) {
809 ArrayList sources = source_list.getSelected();
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 //move_index_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1059 Dictionary.registerBoth(move_index_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1060
1061 move_index_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1062 move_index_down_button.setEnabled(false);
1063 move_index_down_button.setMnemonic(KeyEvent.VK_D);
1064 //move_index_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1065 Dictionary.registerBoth(move_index_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1066
1067 JPanel index_spacer_panel = new JPanel();
1068 JPanel index_body_panel = new JPanel();
1069 JPanel index_details_panel = new JPanel();
1070 JPanel index_labels_panel = new JPanel();
1071
1072 index_name_label = new JLabel();
1073 Dictionary.registerText(index_name_label, "CDM.IndexManager.Index_Name");
1074
1075 index_name_field = new JTextField();
1076 index_name_field.setPreferredSize(FIELD_SIZE);
1077 Dictionary.registerTooltip(index_name_field, "CDM.IndexManager.Index_Name_Tooltip");
1078
1079 JPanel index_boxes_panel = new JPanel();
1080
1081 index_label = new JLabel("CDM.IndexManager.MGPP.Index");
1082 Dictionary.registerText(index_label, "CDM.IndexManager.MGPP.Index");
1083
1084 index_combobox = new GComboBox();
1085 index_combobox.setPreferredSize(FIELD_SIZE);
1086 index_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1087 index_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1088 index_combobox.setEditable(true);
1089 index_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1090 index_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1091 Dictionary.registerTooltip(index_combobox, "CDM.IndexManager.MGPP.Index_Tooltip");
1092
1093 JPanel index_button_panel = new JPanel();
1094
1095 add_index_button = new GLIButton();
1096 add_index_button.setEnabled(false);
1097 add_index_button.setMnemonic(KeyEvent.VK_A);
1098 Dictionary.registerBoth(add_index_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
1099
1100 add_all_button = new GLIButton();
1101 add_all_button.setEnabled(true);
1102 add_all_button.setMnemonic(KeyEvent.VK_L);
1103 Dictionary.registerBoth(add_all_button, "CDM.IndexManager.MGPP.Add_All_Metadata", "CDM.IndexManager.MGPP.Add_All_Metadata_Tooltip");
1104
1105 replace_button = new GLIButton();
1106 replace_button.setEnabled(false);
1107 replace_button.setMnemonic(KeyEvent.VK_C);
1108 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
1109
1110 remove_index_button = new GLIButton();
1111 remove_index_button.setEnabled(false);
1112 remove_index_button.setMnemonic(KeyEvent.VK_A);
1113 Dictionary.registerBoth(remove_index_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
1114
1115 JPanel index_empty_panel = new JPanel();
1116
1117 // Connect Indexes
1118 EnableAddIndexListener index_eal = new EnableAddIndexListener();
1119 add_index_button.addActionListener(new AddIndexActionListener());
1120 add_all_button.addActionListener(new AddAllActionListener());
1121 current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener());
1122 index_combobox.addActionListener(index_eal);
1123 ((JTextField)index_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(index_eal);
1124 index_name_field.getDocument().addDocumentListener(index_eal);
1125 move_index_down_button.addActionListener(new MoveIndexDownListener());
1126 move_index_up_button.addActionListener(new MoveIndexUpListener());
1127 replace_button.addActionListener(new ReplaceIndexActionListener());
1128 remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal));
1129
1130 // Layout Indexes
1131 index_movement_panel.setLayout(new GridLayout(2,1));
1132 index_movement_panel.add(move_index_up_button);
1133 index_movement_panel.add(move_index_down_button);
1134
1135 current_indexes_panel.setLayout(new BorderLayout());
1136 current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH);
1137 current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER);
1138 current_indexes_panel.add(index_movement_panel, BorderLayout.EAST);
1139
1140
1141 index_labels_panel.setLayout(new GridLayout(2,1));
1142 index_labels_panel.add(index_name_label);
1143 index_labels_panel.add(index_label);
1144
1145 index_boxes_panel.setLayout(new GridLayout(2,1));
1146 index_boxes_panel.add(index_name_field);
1147 index_boxes_panel.add(index_combobox);
1148
1149 index_details_panel.setLayout(new BorderLayout(5,0));
1150 index_details_panel.add(index_labels_panel, BorderLayout.WEST);
1151 index_details_panel.add(index_boxes_panel, BorderLayout.CENTER);
1152
1153 index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1154 index_button_panel.setLayout(new GridLayout(2,2,5,0));
1155 index_button_panel.add(add_index_button);
1156 index_button_panel.add(add_all_button);
1157 index_button_panel.add(replace_button);
1158 index_button_panel.add(remove_index_button);
1159
1160 index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1161 index_body_panel.setLayout(new BorderLayout());
1162 index_body_panel.add(index_details_panel, BorderLayout.CENTER);
1163 index_body_panel.add(index_button_panel, BorderLayout.SOUTH);
1164
1165 index_spacer_panel.setLayout(new BorderLayout());
1166 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH);
1167 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER);
1168
1169 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1170 indexes_panel.setLayout(new BorderLayout());
1171 indexes_panel.add(current_indexes_panel, BorderLayout.NORTH);
1172 indexes_panel.add(index_spacer_panel, BorderLayout.CENTER);
1173
1174 // Create Levels
1175 JPanel levels_panel = new JPanel();
1176
1177 JPanel current_levels_panel = new JPanel();
1178
1179 current_levels_label = new JLabel();
1180 Dictionary.registerText(current_levels_label, "CDM.IndexManager.MGPP.Current_Levels");
1181
1182 current_levels_list = new JList(levels_model);
1183 current_levels_list.setVisibleRowCount(5);
1184
1185 JPanel level_movement_panel = new JPanel();
1186
1187 move_level_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
1188 move_level_up_button.setEnabled(false);
1189 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1190 //move_level_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1191 Dictionary.registerBoth(move_level_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1192
1193 move_level_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1194 move_level_down_button.setEnabled(false);
1195 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1196 //move_level_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1197 Dictionary.registerBoth(move_level_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1198
1199 JPanel level_spacer_panel = new JPanel();
1200 JPanel level_body_panel = new JPanel();
1201 JPanel level_details_panel = new JPanel();
1202 JPanel level_labels_panel = new JPanel();
1203 JPanel level_boxes_panel = new JPanel();
1204
1205 level_name_label = new JLabel();
1206 Dictionary.registerText(level_name_label, "CDM.IndexManager.MGPP.Level_Name");
1207
1208 level_name_field = new JTextField();
1209 level_name_field.setPreferredSize(FIELD_SIZE);
1210 Dictionary.registerTooltip(level_name_field, "CDM.IndexManager.MGPP.Level_Name_Tooltip");
1211
1212
1213 level_label = new JLabel();
1214 Dictionary.registerText(level_label, "CDM.IndexManager.MGPP.Level");
1215
1216 level_combobox = new GComboBox(Index.LEVEL);
1217 level_combobox.setPreferredSize(FIELD_SIZE);
1218 level_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1219 level_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1220 level_combobox.setEditable(true);
1221 level_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1222 level_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1223 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
1224
1225 JPanel level_button_panel = new JPanel();
1226
1227 add_level_button = new GLIButton("CDM.IndexManager.MGPP.Add_Level");
1228 add_level_button.setEnabled(false);
1229 add_level_button.setMnemonic(KeyEvent.VK_A);
1230 Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip");
1231
1232 remove_level_button = new GLIButton("CDM.IndexManager.MGPP.Remove_Level");
1233 remove_level_button.setEnabled(false);
1234 remove_level_button.setMnemonic(KeyEvent.VK_A);
1235 Dictionary.registerBoth(remove_level_button, "CDM.IndexManager.MGPP.Remove_Level", "CDM.IndexManager.MGPP.Remove_Level_Tooltip");
1236
1237 JPanel level_empty_panel = new JPanel();
1238
1239 // Connect Levels
1240 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1241 add_level_button.addActionListener(new AddLevelActionListener());
1242 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1243 level_combobox.addActionListener(level_eal);
1244 ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1245 level_name_field.getDocument().addDocumentListener(level_eal);
1246 move_level_down_button.addActionListener(new MoveLevelDownListener());
1247 move_level_up_button.addActionListener(new MoveLevelUpListener());
1248 remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal));
1249 // Layout Levels
1250
1251 level_movement_panel.setLayout(new GridLayout(2,1));
1252 level_movement_panel.add(move_level_up_button);
1253 level_movement_panel.add(move_level_down_button);
1254
1255 current_levels_panel.setLayout(new BorderLayout());
1256 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1257 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1258 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1259
1260 level_labels_panel.setLayout(new GridLayout(2,1));
1261 level_labels_panel.add(level_name_label);
1262 level_labels_panel.add(level_label);
1263
1264 level_boxes_panel.setLayout(new GridLayout(2,1));
1265 level_boxes_panel.add(level_name_field);
1266 level_boxes_panel.add(level_combobox);
1267
1268 level_details_panel.setLayout(new BorderLayout(5,0));
1269 level_details_panel.add(level_labels_panel, BorderLayout.WEST);
1270 level_details_panel.add(level_boxes_panel, BorderLayout.CENTER);
1271
1272 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1273 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1274 level_button_panel.add(add_level_button);
1275 level_button_panel.add(remove_level_button);
1276
1277 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1278 level_body_panel.setLayout(new BorderLayout());
1279 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1280 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1281
1282 level_spacer_panel.setLayout(new BorderLayout());
1283 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1284 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1285
1286 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1287 levels_panel.setLayout(new BorderLayout());
1288 levels_panel.add(current_levels_panel, BorderLayout.NORTH);
1289 levels_panel.add(level_spacer_panel, BorderLayout.CENTER);
1290
1291 // Create General
1292 JPanel header_panel = new JPanel();
1293
1294 title_label = new JLabel();
1295 title_label.setHorizontalAlignment(JLabel.CENTER);
1296 Dictionary.registerText(title_label, "CDM.IndexManager.MGPP.Title");
1297
1298 instructions_textarea = new JTextArea();
1299 instructions_textarea.setEditable(false);
1300 instructions_textarea.setLineWrap(true);
1301 instructions_textarea.setRows(6);
1302 instructions_textarea.setWrapStyleWord(true);
1303 Dictionary.registerText(instructions_textarea, "CDM.IndexManager.MGPP.Instructions");
1304
1305 tabbed_pane = new JTabbedPane();
1306
1307 // Layout General
1308 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
1309 header_panel.setLayout(new BorderLayout());
1310 header_panel.add(title_label, BorderLayout.NORTH);
1311 header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
1312
1313 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Indexes"), indexes_panel);
1314 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Levels"), levels_panel);
1315
1316 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1317 setLayout(new BorderLayout());
1318 add(header_panel, BorderLayout.NORTH);
1319 add(tabbed_pane, BorderLayout.CENTER);
1320 }
1321
1322 public void destroy() {
1323 }
1324
1325 public void gainFocus() {
1326 // Reload the assigned indexes list.
1327 index_combobox.removeAllItems();
1328 index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR);
1329 index_combobox.addItem(CollectionConfiguration.METADATA_STR);
1330 index_combobox.addItem(CollectionConfiguration.TEXT_STR);
1331 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
1332 for(int i = 0; i < every_metadata_set_element.size(); i++) {
1333 index_combobox.addItem(every_metadata_set_element.get(i));
1334 }
1335 // Ensure the level manager has at least documents assigned
1336 if(levels_model.getSize() == 0) {
1337 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1338 // Create new metadatum
1339 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1340 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1341 // Assign new level
1342 addLevel(level, metadatum);
1343 level = null;
1344 }
1345
1346 // refresh
1347 current_indexes_list.updateUI();
1348 }
1349
1350 private class AddIndexActionListener
1351 implements ActionListener {
1352 public void actionPerformed(ActionEvent event) {
1353 // Retrieve the name
1354 String name = index_name_field.getText();
1355 // Retrieve the source
1356 Object source = index_combobox.getSelectedItem();
1357 // If this object isn't yet in the combobox add it.
1358 if(index_combobox.getSelectedIndex() == -1) {
1359 index_combobox.insertItemAt(source, index_combobox.getItemCount());
1360 }
1361 // Create new index
1362 ArrayList sources = new ArrayList();
1363 sources.add(source);
1364 Index index = new Index(sources);
1365 if(!model.contains(index)) {
1366 // Create new metadatum
1367 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1368 metadatum.setValue(name);
1369 // Assign new index
1370 addIndex(index, metadatum);
1371 }
1372 // Done. Disable add
1373 add_index_button.setEnabled(false);
1374 }
1375 }
1376
1377 private class AddAllActionListener
1378 implements ActionListener {
1379
1380 public void actionPerformed(ActionEvent event) {
1381 for(int i = 0; i < index_combobox.getItemCount(); i++) {
1382 Object source = index_combobox.getItemAt(i);
1383 // Create new index
1384 ArrayList sources = new ArrayList();
1385 sources.add(source);
1386 Index index = new Index(sources);
1387 sources = null;
1388 if(!model.contains(index)) {
1389 // Determine the metadatum value
1390 String name = source.toString();
1391 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
1392 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1393 }
1394 // Create new metadatum
1395 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1396 metadatum.setValue(name);
1397 name = null;
1398 // Assign new index
1399 addIndex(index, metadatum);
1400 }
1401 source = null;
1402 index = null;
1403 }
1404 // Done. Disable add
1405 add_index_button.setEnabled(false);
1406 }
1407 }
1408
1409 private class AddLevelActionListener
1410 implements ActionListener {
1411 public void actionPerformed(ActionEvent event) {
1412 // Retrieve the name
1413 String name = level_name_field.getText();
1414 // Retrieve the source
1415 String source = (String)level_combobox.getSelectedItem();
1416 // If this object isn't yet in the combobox add it.
1417 if(level_combobox.getSelectedIndex() == -1) {
1418 level_combobox.insertItemAt(source, level_combobox.getItemCount());
1419 }
1420 Level level = new Level(source);
1421 // Create new metadatum
1422 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1423 metadatum.setValue(name);
1424 // Assign new level
1425 addLevel(level, metadatum);
1426 // Done. Disable add
1427 add_level_button.setEnabled(false);
1428 }
1429 }
1430
1431 private class CurrentIndexesListSelectionListener
1432 implements ListSelectionListener {
1433 public void valueChanged(ListSelectionEvent event) {
1434 if(!event.getValueIsAdjusting()) {
1435 Index index = (Index)current_indexes_list.getSelectedValue();
1436 Object[] selected_objects = current_indexes_list.getSelectedValues();
1437 if(selected_objects.length == 1) {
1438 String full_text = index.toString();
1439 if(full_text.indexOf("\"") != -1) {
1440 index_name_field.setText(index.getName());
1441 }
1442 ArrayList sources = index.getSources();
1443 index_combobox.setSelectedItem(sources.get(0));
1444 }
1445 if(index != null) {
1446 move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1));
1447 move_index_up_button.setEnabled((model.indexOf(index) > 0));
1448 remove_index_button.setEnabled(true);
1449 }
1450 else {
1451 move_index_down_button.setEnabled(false);
1452 move_index_up_button.setEnabled(false);
1453 remove_index_button.setEnabled(false);
1454 }
1455 }
1456 }
1457 }
1458
1459 private class CurrentLevelsListSelectionListener
1460 implements ListSelectionListener {
1461 public void valueChanged(ListSelectionEvent event) {
1462 if(!event.getValueIsAdjusting()) {
1463 Level level = (Level)current_levels_list.getSelectedValue();
1464 if(level != null) {
1465 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1466 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1467 remove_level_button.setEnabled(!level.getName().equals(CollectionConfiguration.DOCUMENT_STR) || levels_model.getSize() > 1);
1468 }
1469 else {
1470 move_level_down_button.setEnabled(false);
1471 move_level_up_button.setEnabled(false);
1472 remove_level_button.setEnabled(false);
1473 }
1474 }
1475 }
1476 }
1477
1478 private class EnableAddIndexListener
1479 implements ActionListener, DocumentListener {
1480 /** Called whenever a selection action occurs on the combobox.
1481 * @param event an ActionEvent containing information about the selection event
1482 */
1483 public void actionPerformed(ActionEvent event) {
1484 validateAddButtonIndex();
1485 }
1486
1487 /** Gives notification that an attribute or set of attributes changed.
1488 * @param event a DocumentEvent containing information about the text changed
1489 */
1490 public void changedUpdate(DocumentEvent event) {
1491 validateAddButtonIndex();
1492 }
1493
1494 /** Gives notification that there was an insert into the document.
1495 * @param event a DocumentEvent containing information about the text added
1496 */
1497 public void insertUpdate(DocumentEvent event) {
1498 validateAddButtonIndex();
1499 }
1500
1501 /** Gives notification that a portion of the document has been removed.
1502 * @param event a DocumentEvent containing information about the text removed
1503 */
1504 public void removeUpdate(DocumentEvent event) {
1505 validateAddButtonIndex();
1506 }
1507
1508 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1509 public void validateAddButtonIndex() {
1510 String name = index_name_field.getText();
1511 Object selected_object = index_combobox.getSelectedItem();
1512 if(name.length() > 0 && selected_object != null) {
1513 // Unfortunately we have to generate a valid id
1514 String id = null;
1515 if (selected_object instanceof MetadataElement) {
1516 id = ((MetadataElement) selected_object).getFullName();
1517 }
1518 else {
1519 id = selected_object.toString();
1520 }
1521 if(id.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
1522 id = id.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1523 }
1524 Index index = getIndex(id);
1525 if(index == null) {
1526 add_index_button.setEnabled(true);
1527 replace_button.setEnabled(false);
1528 }
1529 else {
1530 add_index_button.setEnabled(false);
1531 replace_button.setEnabled(!name.equals(index.getName()));
1532 }
1533 }
1534 else {
1535 add_index_button.setEnabled(false);
1536 }
1537 }
1538 }
1539
1540 private class EnableAddLevelListener
1541 implements ActionListener, DocumentListener {
1542 /** Called whenever a selection action occurs on the combobox.
1543 * @param event an ActionEvent containing information about the selection event
1544 */
1545 public void actionPerformed(ActionEvent event) {
1546 validateAddButtonLevel();
1547 }
1548
1549 /** Gives notification that an attribute or set of attributes changed.
1550 * @param event a DocumentEvent containing information about the text changed
1551 */
1552 public void changedUpdate(DocumentEvent event) {
1553 validateAddButtonLevel();
1554 }
1555
1556 /** Gives notification that there was an insert into the document.
1557 * @param event a DocumentEvent containing information about the text added
1558 */
1559 public void insertUpdate(DocumentEvent event) {
1560 validateAddButtonLevel();
1561 }
1562
1563 /** Gives notification that a portion of the document has been removed.
1564 * @param event a DocumentEvent containing information about the text removed
1565 */
1566 public void removeUpdate(DocumentEvent event) {
1567 validateAddButtonLevel();
1568 }
1569
1570 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1571 public void validateAddButtonLevel() {
1572 String name = level_name_field.getText();
1573 Object selected_object = level_combobox.getSelectedItem();
1574 if(name.length() > 0 && selected_object != null) {
1575 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1576 }
1577 else {
1578 add_level_button.setEnabled(false);
1579 }
1580 }
1581 }
1582
1583 private class MoveIndexDownListener
1584 implements ActionListener {
1585 public void actionPerformed(ActionEvent event) {
1586 // Retrieve the first selected item
1587 Index index = (Index) current_indexes_list.getSelectedValue();
1588 moveIndex(index, false);
1589 current_indexes_list.setSelectedValue(index, true);
1590 }
1591 }
1592
1593 private class MoveLevelDownListener
1594 implements ActionListener {
1595 public void actionPerformed(ActionEvent event) {
1596 // Retrieve the first selected item
1597 Level level = (Level) current_levels_list.getSelectedValue();
1598 moveLevel(level, false);
1599 current_levels_list.setSelectedValue(level, true);
1600 }
1601 }
1602
1603 private class MoveIndexUpListener
1604 implements ActionListener {
1605 public void actionPerformed(ActionEvent event) {
1606 // Retrieve the first selected item
1607 Index index = (Index) current_indexes_list.getSelectedValue();
1608 moveIndex(index, true);
1609 current_indexes_list.setSelectedValue(index, true);
1610 }
1611 }
1612
1613 private class MoveLevelUpListener
1614 implements ActionListener {
1615 public void actionPerformed(ActionEvent event) {
1616 // Retrieve the first selected item
1617 Level level = (Level) current_levels_list.getSelectedValue();
1618 moveLevel(level, true);
1619 current_levels_list.setSelectedValue(level, true);
1620 }
1621 }
1622
1623 /** Replace really only replaces the string. */
1624 private class ReplaceIndexActionListener
1625 implements ActionListener {
1626
1627 public void actionPerformed(ActionEvent event) {
1628 Object[] selected_objects = current_indexes_list.getSelectedValues();
1629 if(selected_objects.length == 1) {
1630 Index index = (Index) selected_objects[0];
1631 // Remove old name
1632 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1633 // Enter new name
1634 String name = index_name_field.getText();
1635 // Create new metadatum
1636 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1637 index = null;
1638 metadatum.setValue(name);
1639 name = null;
1640 // Assign new index
1641 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
1642 metadatum = null;
1643 }
1644 current_indexes_list.setSelectedValue(selected_objects[0], true);
1645 // Done. Disable add
1646 add_index_button.setEnabled(false);
1647 replace_button.setEnabled(false);
1648 }
1649 }
1650
1651 private class RemoveIndexActionListener
1652 implements ActionListener {
1653
1654 private EnableAddIndexListener eal = null;
1655
1656 public RemoveIndexActionListener(EnableAddIndexListener eal) {
1657 this.eal = eal;
1658 }
1659
1660 public void actionPerformed(ActionEvent event) {
1661 // Retrieve the selected items
1662 Object[] selected_objects = current_indexes_list.getSelectedValues();
1663 // Clear selection
1664 current_indexes_list.clearSelection();
1665 for(int i = 0; i < selected_objects.length; i++) {
1666 Index index = (Index) selected_objects[i];
1667 // Remove any related metadata
1668 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1669 // Remove the index
1670 removeIndex(index);
1671 }
1672 // Disable remove button
1673 remove_index_button.setEnabled(false);
1674 // Check if add should be reenabled
1675 eal.validateAddButtonIndex();
1676 }
1677 }
1678
1679 private class RemoveLevelActionListener
1680 implements ActionListener {
1681
1682 private EnableAddLevelListener eal = null;
1683
1684 public RemoveLevelActionListener(EnableAddLevelListener eal) {
1685 this.eal = eal;
1686 }
1687
1688 public void actionPerformed(ActionEvent event) {
1689 // Retrieve the selected items
1690 Object[] selected_objects = current_levels_list.getSelectedValues();
1691 // Clear selection
1692 current_levels_list.clearSelection();
1693 for(int i = 0; i < selected_objects.length; i++) {
1694 Level level = (Level) selected_objects[i];
1695 // Remove any related metadata
1696 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
1697 // Remove the index
1698 removeLevel(level);
1699 }
1700 // Disable remove button
1701 remove_level_button.setEnabled(false);
1702 // If there are no levels left, put document back in
1703 if(levels_model.getSize() == 0) {
1704 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1705 // Create new metadatum
1706 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1707 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1708 // Assign new level
1709 addLevel(level, metadatum);
1710 level = null;
1711 }
1712 // Check if add should be reenabled
1713 eal.validateAddButtonLevel();
1714 }
1715 }
1716 } // MGPPControls
1717
1718}
Note: See TracBrowser for help on using the repository browser.