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

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

Removed all occurrences of classes explicitly importing other classes in the same package.

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