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

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

Started off fixing a bug where the loaded collection wasn't being ticked on in the cross-collection searching page. Ended up removing a ton of stuff from the CheckList class, some of which was duplicated code and buggy.

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