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

Last change on this file since 8576 was 8576, checked in by mdewsnip, 20 years ago

Removed some redundant files and made a few minor tidy ups.

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