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

Last change on this file since 5234 was 5219, checked in by mdewsnip, 21 years ago

Changed size of DoubleImageButtons to be consistent with others in the CDM.

  • Property svn:keywords set to Author Date Id Revision
File size: 60.1 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28import java.awt.*;
29import java.awt.event.*;
30import java.util.*;
31import javax.swing.*;
32import javax.swing.event.*;
33import org.greenstone.gatherer.Gatherer;
34import org.greenstone.gatherer.cdm.CollectionMeta;
35import org.greenstone.gatherer.cdm.CommandTokenizer;
36import org.greenstone.gatherer.cdm.Control;
37import org.greenstone.gatherer.cdm.DOMProxyListModel;
38import org.greenstone.gatherer.cdm.Index;
39import org.greenstone.gatherer.checklist.CheckList;
40import org.greenstone.gatherer.gui.DoubleImageButton;
41import org.greenstone.gatherer.gui.GComboBox;
42import org.greenstone.gatherer.msm.ElementWrapper;
43import org.greenstone.gatherer.msm.MSMUtils;
44import org.greenstone.gatherer.util.ExclusiveListSelectionListener;
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 LABEL_SIZE = new Dimension(150,30);
55 static final private String MGINDEXES = "mg indexes";
56 static final private String MGPPINDEXES = "mgpp indexes";
57
58 /** The controls for editing the indexes. */
59 private Control controls = null;
60 /** A model of the levels, also based on the DOM. */
61 private DOMProxyListModel levels_model = null;
62 /** A reference to ourselves so our inner methods have access. */
63 private DOMProxyListModel model = null;
64 /** The default index. */
65 private Index default_index = null;
66
67 /** Constructor. */
68 public IndexManager(Element indexes) {
69 super(indexes, CollectionConfiguration.INDEX_ELEMENT, new Index());
70 Gatherer.println("IndexManager: " + getSize() + " indexes parsed.");
71 model = this;
72 // Parse and retrieve the default index
73 NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
74 if(default_index_elements.getLength() > 0) {
75 default_index = new Index((Element)default_index_elements.item(0));
76 }
77 // Parse and retrieve the levels element
78 Element levels_element = CollectionDesignManager.collect_config.getLevels();
79 levels_model = new DOMProxyListModel(levels_element, CollectionConfiguration.CONTENT_ELEMENT, new Level());
80 Gatherer.println(" + " + levels_model.getSize() + " levels parsed.");
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 public 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, get("CDM.IndexManager.Index_Exists"), get("General.Warning"), JOptionPane.WARNING_MESSAGE);
107 }
108 }
109
110 public 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(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 add(level);
120 }
121 Gatherer.c_man.configurationChanged();
122 }
123 else {
124 JOptionPane.showMessageDialog(Gatherer.g_man, get("CDM.IndexManager.Level_Exists"), 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 public 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 public 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 public 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 public 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 default_index.setAssigned(false);
432 }
433 else {
434 Element mgpp_element = root;
435 // Retrieve and assign MG element and default index element
436 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
437 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
438 if(default_index != null) {
439 default_index.setAssigned(true);
440 }
441 // 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.
442 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
443 if(indexes.getLength() == 0) {
444 Index index = getIndex(CollectionConfiguration.TEXT_STR);
445 if(index != null) {
446 // Replace mgpp element with mg element
447 setRoot(mg_element);
448 int level_size = levels_model.getSize();
449 for(int i = 0; i < level_size; i++) {
450 Level level = (Level) levels_model.getElementAt(i);
451 Index new_index = new Index(level.getName(), index.getSources());
452 // Try to retrieve existing metadatum
453 String source_str = new_index.getID();
454 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
455 // If no metadata was found, add new pseudo metadata using the id
456 if(metadatum == null) {
457 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
458 metadatum.setAssigned(true);
459 metadatum.setValue(source_str);
460 }
461 // If it was found, ensure it is assigned
462 else {
463 metadatum.setAssigned(true);
464 }
465 source_str = null;
466 addIndex(new_index, metadatum);
467 new_index = null;
468 level = null;
469 }
470 }
471 }
472 else {
473 // Replace mgpp element with mg element
474 setRoot(mg_element);
475 }
476 // Unassign mgpp element and levels
477 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
478 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
479 }
480 }
481 }
482
483 /** Overloaded to call get with both a key and an empty argument array.
484 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
485 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
486 */
487 private String get(String key) {
488 return get(key, null);
489 }
490
491 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
492 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.
493 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
494 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
495 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatically populated with formatting Strings of with argument String provided in the get call.
496 * @see org.greenstone.gatherer.Gatherer
497 * @see org.greenstone.gatherer.Dictionary
498 */
499 private String get(String key, String args[]) {
500 if(key.indexOf('.') == -1) {
501 key = "CDM.IndexManager." + key;
502 }
503 return Gatherer.dictionary.get(key, args);
504 }
505
506 /** This class creates a set of controls for editing the indexes. */
507 private class IndexControl
508 extends JPanel
509 implements Control {
510
511 private CardLayout card_layout;
512 private CheckList source_list;
513 private JButton add_button;
514 private JButton move_down_button;
515 private JButton move_up_button;
516 private JButton remove_button;
517 private JButton set_default_button;
518 private JComboBox level_combobox;
519 private JList index_list;
520 private JTextArea instruction_textarea;
521 private JTextField name_textfield ;
522
523 private MGPPControl mgppindexes_control;
524
525 /** Constructor.
526 * @see org.greenstone.gatherer.Configuration
527 * @see org.greenstone.gatherer.Gatherer
528 * @see org.greenstone.gatherer.cdm.IndexManager.Control.AddListener
529 * @see org.greenstone.gatherer.cdm.IndexManager.Control.ClearDefaultListener
530 * @see org.greenstone.gatherer.cdm.IndexManager.Control.NameListener
531 * @see org.greenstone.gatherer.cdm.IndexManager.Control.RemoveListener
532 * @see org.greenstone.gatherer.cdm.IndexManager.Control.SetDefaultListener
533 * @see org.greenstone.gatherer.collection.CollectionManager
534 * @see org.greenstone.gatherer.gui.Coloring
535 * @see org.greenstone.gatherer.msm.MetadataSetManager
536 * @see org.greenstone.gatherer.util.ExclusiveListSelectionListener
537 */
538 public IndexControl() {
539 super();
540
541 ArrayList new_data = new ArrayList();
542 new_data.add(CollectionConfiguration.TEXT_STR);
543 new_data.addAll(Gatherer.c_man.getCollection().msm.getAssignedElements());
544
545 // Creation
546 JPanel mgindexes_panel = new JPanel();
547 card_layout = new CardLayout();
548
549 // MG Index Controls
550
551 JPanel mg_indexes_pane = new JPanel();
552
553 JPanel instruction_pane = new JPanel();
554 JLabel title_label = new JLabel(get("Title"));
555 title_label.setHorizontalAlignment(JLabel.CENTER);
556 instruction_textarea = new JTextArea(get("Instructions"));
557 instruction_textarea.setEditable(false);
558 instruction_textarea.setLineWrap(true);
559 instruction_textarea.setRows(6);
560 instruction_textarea.setWrapStyleWord(true);
561
562 JPanel assigned_indexes_pane = new JPanel();
563 JLabel index_label = new JLabel(get("Indexes"));
564 index_list = new JList(model);
565 index_list.setCellRenderer(new IndexListRenderer());
566
567 JPanel movement_pane = new JPanel();
568
569 move_up_button = new DoubleImageButton(get("CDM.Move.Move_Up"), Utility.getImage("arrow-up.gif"), Utility.getImage("arrow-up-disabled.gif"));
570 move_up_button.setEnabled(false);
571 move_up_button.setMnemonic(KeyEvent.VK_U);
572 move_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
573
574 move_down_button = new DoubleImageButton(get("CDM.Move.Move_Down"), Utility.getImage("arrow-down.gif"), Utility.getImage("arrow-down-disabled.gif"));
575 move_down_button.setEnabled(false);
576 move_down_button.setMnemonic(KeyEvent.VK_D);
577 move_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
578
579 JPanel index_pane = new JPanel();
580
581 JPanel details_pane = new JPanel();
582
583 JPanel name_pane = new JPanel();
584 JLabel name_label = new JLabel(get("Name"));
585 name_label.setPreferredSize(Utility.LABEL_SIZE);
586 name_textfield = new JTextField();
587
588 JPanel source_pane = new JPanel();
589 JLabel source_label = new JLabel(get("Source"));
590 source_label.setPreferredSize(Utility.LABEL_SIZE);
591 source_list = new CheckList(new_data, false);
592
593 JPanel level_pane = new JPanel();
594 JLabel level_label = new JLabel(get("Level"));
595 level_label.setPreferredSize(Utility.LABEL_SIZE);
596 level_combobox = new JComboBox();
597 level_combobox.addItem(get("Document"));
598 level_combobox.addItem(get("Paragraph"));
599 level_combobox.addItem(get("Section"));
600 level_combobox.setEditable(false);
601
602 JPanel button_pane = new JPanel();
603 add_button = new JButton(get("Add"));
604 add_button.setEnabled(false);
605 add_button.setMnemonic(KeyEvent.VK_A);
606 set_default_button = new JButton(get("Set_Default"));
607 set_default_button.setEnabled(false);
608 set_default_button.setMnemonic(KeyEvent.VK_S);
609 remove_button = new JButton(get("Remove"));
610 remove_button.setEnabled(false);
611 remove_button.setMnemonic(KeyEvent.VK_R);
612
613 // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class.
614 mgppindexes_control = new MGPPControl();
615
616 // Listeners
617 add_button.addActionListener(new AddListener());
618 move_down_button.addActionListener(new MoveListener(false));
619 move_up_button.addActionListener(new MoveListener(true));
620 remove_button.addActionListener(new RemoveListener());
621 set_default_button.addActionListener(new SetDefaultListener());
622 name_textfield.getDocument().addDocumentListener(new NameListener());
623 index_list.addListSelectionListener(new IndexListListener());
624 source_list.addListSelectionListener(new SourceListListener());
625 ExclusiveListSelectionListener elsl = new ExclusiveListSelectionListener();
626 elsl.add(index_list);
627 elsl.add(source_list);
628 elsl = null; // We don't want to hold a reference to this
629
630 // Layout
631 instruction_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
632 instruction_pane.setLayout(new BorderLayout());
633 instruction_pane.add(title_label, BorderLayout.NORTH);
634 instruction_pane.add(new JScrollPane(instruction_textarea), BorderLayout.CENTER);
635
636 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
637 movement_pane.setLayout(new GridLayout(2,1));
638 movement_pane.add(move_up_button);
639 movement_pane.add(move_down_button);
640
641 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
642 assigned_indexes_pane.setLayout(new BorderLayout());
643 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
644 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
645 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
646
647 name_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
648 name_pane.setLayout(new BorderLayout());
649 name_pane.add(name_label, BorderLayout.WEST);
650 name_pane.add(name_textfield, BorderLayout.CENTER);
651
652 source_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
653 source_pane.setLayout(new BorderLayout());
654 source_pane.add(source_label, BorderLayout.WEST);
655 source_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
656
657 level_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
658 level_pane.setLayout(new BorderLayout());
659 level_pane.add(level_label, BorderLayout.WEST);
660 level_pane.add(level_combobox, BorderLayout.CENTER);
661
662 details_pane.setLayout(new BorderLayout());
663 details_pane.add(name_pane, BorderLayout.NORTH);
664 details_pane.add(source_pane, BorderLayout.CENTER);
665 details_pane.add(level_pane, BorderLayout.SOUTH);
666
667 button_pane.setLayout(new GridLayout(1,3));
668 button_pane.add(add_button);
669 button_pane.add(set_default_button);
670 button_pane.add(remove_button);
671
672 index_pane.setLayout(new BorderLayout());
673 index_pane.add(details_pane, BorderLayout.CENTER);
674 index_pane.add(button_pane, BorderLayout.SOUTH);
675
676 mgindexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
677 mgindexes_panel.setLayout(new BorderLayout());
678 mgindexes_panel.add(instruction_pane, BorderLayout.NORTH);
679 mgindexes_panel.add(assigned_indexes_pane, BorderLayout.CENTER);
680 mgindexes_panel.add(index_pane, BorderLayout.SOUTH);
681
682 setLayout(card_layout);
683 add(mgindexes_panel, MGINDEXES);
684 add(mgppindexes_control, MGPPINDEXES);
685 }
686 /* Destructor, removes persistant listeners from the Dictionary.
687 */
688 public void destroy() {
689 mgppindexes_control.destroy();
690 mgppindexes_control = null;
691 }
692
693 public void gainFocus() {
694 boolean mgpp_enabled = CollectionDesignManager.searchtype_manager.isMGPPEnabled();
695 if(instruction_textarea != null) {
696 instruction_textarea.setCaretPosition(0);
697 }
698 if(mgpp_enabled) {
699 mgppindexes_control.gainFocus();
700 }
701 else {
702 // Reload the assigned indexes list.
703 ArrayList new_data = new ArrayList();
704 new_data.add(CollectionConfiguration.TEXT_STR);
705 new_data.addAll(Gatherer.c_man.getCollection().msm.getAssignedElements());
706 // reset the model in the list and combobox
707 source_list.setListData(new_data.toArray());
708 new_data = null;
709 }
710 // Bring the appropriate card to the front
711 if(mgpp_enabled) {
712 card_layout.show(this, MGPPINDEXES);
713 }
714 else {
715 card_layout.show(this, MGINDEXES);
716 }
717 }
718
719 public void loseFocus() {
720 }
721
722 private void validateAddButton() {
723 boolean add_enabled = false;
724 // Indexes must have a name
725 if(name_textfield.getText().length() == 0) {
726 add_enabled = false;
727 }
728 // Can't add a new index if no sources are selected
729 else if(source_list.isSelectionEmpty()) {
730 add_enabled = false;
731 }
732 // If we get this far, create a dummy index and see if its already assigned in the collection
733 else {
734 Object object[] = source_list.getSelected().toArray();
735 ArrayList sources = new ArrayList();
736 for(int i = 0; i < object.length; i++) {
737 sources.add(object[i]);
738 }
739 object = null;
740 Index index = new Index(level_combobox.getSelectedIndex(), sources);
741 sources = null;
742 if(model.contains(index)) {
743 add_enabled = false;
744 }
745 else {
746 add_enabled = true;
747 }
748 }
749 // We should now know the add_button state
750 add_button.setEnabled(add_enabled);
751 }
752
753 /** 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. */
754 private class AddListener
755 implements ActionListener {
756 /** 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.
757 * @param event An <strong>ActionEvent</strong> providing extra information about the event.
758 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
759 * @see org.greenstone.gatherer.cdm.CollectionMeta
760 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
761 * @see org.greenstone.gatherer.cdm.Index
762 * @see org.greenstone.gatherer.cdm.Language
763 * @see org.greenstone.gatherer.cdm.LanguageManager
764 */
765 public void actionPerformed(ActionEvent event) {
766 String name = name_textfield.getText();
767 if(!source_list.isSelectionEmpty() && name.length() != 0) {
768 ArrayList sources = source_list.getSelected();
769 Index index = new Index(level_combobox.getSelectedIndex(), sources);
770 sources = null;
771 // Before we add the index to the model, we have to add the collection metadata for this.
772 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
773 metadatum.setValue(name);
774 // Finally add index.
775 addIndex(index, metadatum);
776 metadatum = null;
777 index = null;
778 }
779 name = null;
780 }
781 }
782
783 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
784 private class IndexListListener
785 implements ListSelectionListener {
786 /** 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.
787 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
788 */
789 public void valueChanged(ListSelectionEvent event) {
790 Object value = index_list.getSelectedValue();
791 move_down_button.setEnabled(value != null);
792 move_up_button.setEnabled(value != null);
793 remove_button.setEnabled(value != null);
794 set_default_button.setEnabled(value != null && (default_index == null || !default_index.equals(value)));
795 }
796 }
797
798 private class IndexListRenderer
799 extends DefaultListCellRenderer {
800
801 /** Return a component that has been configured to display the specified value. */
802 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
803 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
804 if(default_index != null && default_index.equals(value)) {
805 component.setText(component.getText() + " " + get("Default_Index_Indicator"));
806 }
807 return component;
808 }
809
810 }
811
812 private class MoveListener
813 implements ActionListener {
814
815 private boolean move_up;
816
817 public MoveListener(boolean move_up) {
818 this.move_up = move_up;
819 }
820
821 public void actionPerformed(ActionEvent event) {
822 // Retrieve the selected index
823 Index index = (Index) index_list.getSelectedValue();
824 if(index != null) {
825 moveIndex(index, move_up);
826 // Ensure the index that moved is still selected
827 index_list.setSelectedValue(index, true);
828 index = null;
829 }
830 }
831 }
832
833 /** 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. */
834 private class RemoveListener
835 implements ActionListener {
836 /** If called when an action occurs on a registered component, we remove the currently selected index, if there is one.
837 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
838 * @see org.greenstone.gatherer.cdm.Index
839 */
840 public void actionPerformed(ActionEvent event) {
841 if(!index_list.isSelectionEmpty()) {
842 removeIndex((Index)index_list.getSelectedValue());
843 }
844 move_down_button.setEnabled(false);
845 move_up_button.setEnabled(false);
846 remove_button.setEnabled(false);
847 }
848 }
849
850 private class SetDefaultListener
851 implements ActionListener {
852
853 public void actionPerformed(ActionEvent event) {
854 Index index = (Index) index_list.getSelectedValue();
855 if(index != null) {
856 setDefault(index);
857 // This should cause a repaint of just the desired row
858 index_list.setSelectedValue(index, true);
859 }
860 set_default_button.setEnabled(false);
861 }
862 }
863
864 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
865 private class SourceListListener
866 implements ListSelectionListener {
867 /** 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.
868 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
869 */
870 public void valueChanged(ListSelectionEvent event) {
871 validateAddButton();
872 }
873 }
874
875 /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */
876 private class NameListener
877 implements DocumentListener {
878 /** Gives notification that an attribute or set of attributes changed. */
879 public void changedUpdate(DocumentEvent e) {
880 validateAddButton();
881 }
882
883 /** Gives notification that there was an insert into the document. */
884 public void insertUpdate(DocumentEvent e) {
885 validateAddButton();
886 }
887
888 /** Gives notification that a portion of the document has been removed. */
889 public void removeUpdate(DocumentEvent e) {
890 validateAddButton();
891 }
892 }
893 }
894
895 private class MGPPControl
896 extends JPanel {
897
898 private GComboBox index_combobox;
899 private GComboBox level_combobox;
900 private JButton add_index_button;
901 private JButton add_level_button;
902 private JButton move_index_down_button;
903 private JButton move_level_down_button;
904 private JButton move_index_up_button;
905 private JButton move_level_up_button;
906 private JButton remove_index_button;
907 private JButton remove_level_button;
908 private JLabel current_levels_label;
909 private JLabel current_indexes_label;
910 private JLabel index_label;
911 private JLabel index_name_label;
912 private JLabel level_label;
913 private JLabel level_name_label;
914 private JLabel move_index_down_label;
915 private JLabel move_level_down_label;
916 private JLabel move_index_up_label;
917 private JLabel move_level_up_label;
918 private JLabel title_label;
919 private JList current_levels_list;
920 private JList current_indexes_list;
921 private JTabbedPane tabbed_pane;
922 private JTextArea instructions_textarea;
923 private JTextField index_name_field;
924 private JTextField level_name_field;
925
926 public MGPPControl() {
927 // Create Indexes
928 JPanel indexes_panel = new JPanel();
929
930 JPanel current_indexes_panel = new JPanel();
931
932 current_indexes_label = new JLabel("CDM.IndexManager.MGPP.Current_Indexes");
933
934 current_indexes_list = new JList(model);
935 current_indexes_list.setVisibleRowCount(5);
936
937 JPanel index_movement_panel = new JPanel();
938
939 ImageIcon move_index_up_icon = Utility.getImage("arrow-up.gif");
940 move_index_up_button = new JButton();
941 move_index_up_label = new JLabel("CDM.Move.Move_Up");
942 move_index_up_label.setHorizontalAlignment(JLabel.CENTER);
943 move_index_up_label.setPreferredSize(LABEL_SIZE);
944 move_index_up_button.setLayout(new BorderLayout());
945 move_index_up_button.add(new JLabel(move_index_up_icon), BorderLayout.WEST);
946 move_index_up_button.add(move_index_up_label, BorderLayout.CENTER);
947 move_index_up_button.add(new JLabel(move_index_up_icon), BorderLayout.EAST);
948 move_index_up_button.setMnemonic(KeyEvent.VK_U);
949
950 ImageIcon move_index_down_icon = Utility.getImage("arrow-down.gif");
951 move_index_down_button = new JButton();
952 move_index_down_label = new JLabel("CDM.Move.Move_Down");
953 move_index_down_label.setHorizontalAlignment(JLabel.CENTER);
954 move_index_down_label.setPreferredSize(LABEL_SIZE);
955 move_index_down_button.setLayout(new BorderLayout());
956 move_index_down_button.add(new JLabel(move_index_down_icon), BorderLayout.WEST);
957 move_index_down_button.add(move_index_down_label, BorderLayout.CENTER);
958 move_index_down_button.add(new JLabel(move_index_down_icon), BorderLayout.EAST);
959 move_index_down_button.setMnemonic(KeyEvent.VK_D);
960
961 JPanel index_spacer_panel = new JPanel();
962
963 JPanel index_body_panel = new JPanel();
964
965 JPanel index_details_panel = new JPanel();
966
967 JPanel index_name_panel = new JPanel();
968
969 index_name_label = new JLabel("CDM.IndexManager.MGPP.Index_Name");
970 index_name_label.setPreferredSize(LABEL_SIZE);
971
972 index_name_field = new JTextField();
973
974 JPanel index_panel = new JPanel();
975
976 index_label = new JLabel("CDM.IndexManager.MGPP.Index");
977 index_label.setPreferredSize(LABEL_SIZE);
978
979 index_combobox = new GComboBox();
980 index_combobox.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.editable_background", false));
981 index_combobox.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
982 index_combobox.setEditable(true);
983 index_combobox.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
984 index_combobox.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
985
986 JPanel index_button_panel = new JPanel();
987
988 add_index_button = new JButton("CDM.IndexManager.MGPP.Add_Index");
989 add_index_button.setEnabled(false);
990 add_index_button.setMnemonic(KeyEvent.VK_A);
991
992 remove_index_button = new JButton("CDM.IndexManager.MGPP.Remove_Index");
993 remove_index_button.setEnabled(false);
994 remove_index_button.setMnemonic(KeyEvent.VK_A);
995
996 JPanel index_empty_panel = new JPanel();
997
998 // Connect Indexes
999 EnableAddIndexListener index_eal = new EnableAddIndexListener();
1000 add_index_button.addActionListener(new AddIndexActionListener());
1001 current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener());
1002 Gatherer.dictionary.register(add_index_button, null, false);
1003 Gatherer.dictionary.register(current_indexes_label, null, false);
1004 Gatherer.dictionary.register(index_label, null, false);
1005 Gatherer.dictionary.register(index_name_label, null, false);
1006 Gatherer.dictionary.register(move_index_down_label, null, false);
1007 Gatherer.dictionary.register(move_index_up_label, null, false);
1008 Gatherer.dictionary.register(remove_index_button, null, false);
1009 index_combobox.addActionListener(index_eal);
1010 ((JTextField)index_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(index_eal);
1011 index_name_field.getDocument().addDocumentListener(index_eal);
1012 move_index_down_button.addActionListener(new MoveIndexDownListener());
1013 move_index_up_button.addActionListener(new MoveIndexUpListener());
1014 remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal));
1015 // Layout Indexes
1016 index_movement_panel.setLayout(new GridLayout(2,1));
1017 index_movement_panel.add(move_index_up_button);
1018 index_movement_panel.add(move_index_down_button);
1019
1020 current_indexes_panel.setLayout(new BorderLayout());
1021 current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH);
1022 current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER);
1023 current_indexes_panel.add(index_movement_panel, BorderLayout.EAST);
1024
1025 index_name_panel.setLayout(new BorderLayout());
1026 index_name_panel.add(index_name_label, BorderLayout.WEST);
1027 index_name_panel.add(index_name_field, BorderLayout.CENTER);
1028
1029 index_panel.setLayout(new BorderLayout());
1030 index_panel.add(index_label, BorderLayout.WEST);
1031 index_panel.add(index_combobox, BorderLayout.CENTER);
1032
1033 index_details_panel.setLayout(new GridLayout(2,1,0,5));
1034 index_details_panel.add(index_name_panel);
1035 index_details_panel.add(index_panel);
1036
1037 index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1038 index_button_panel.setLayout(new GridLayout(1,2,5,0));
1039 index_button_panel.add(add_index_button);
1040 index_button_panel.add(remove_index_button);
1041
1042 index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1043 index_body_panel.setLayout(new BorderLayout());
1044 index_body_panel.add(index_details_panel, BorderLayout.CENTER);
1045 index_body_panel.add(index_button_panel, BorderLayout.SOUTH);
1046
1047 index_spacer_panel.setLayout(new BorderLayout());
1048 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH);
1049 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER);
1050
1051 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1052 indexes_panel.setLayout(new BorderLayout());
1053 indexes_panel.add(current_indexes_panel, BorderLayout.NORTH);
1054 indexes_panel.add(index_spacer_panel, BorderLayout.CENTER);
1055
1056 // Create Levels
1057 JPanel levels_panel = new JPanel();
1058
1059 JPanel current_levels_panel = new JPanel();
1060
1061 current_levels_label = new JLabel("CDM.IndexManager.MGPP.Current_Levels");
1062
1063 current_levels_list = new JList(levels_model);
1064 current_levels_list.setVisibleRowCount(5);
1065
1066 JPanel level_movement_panel = new JPanel();
1067
1068 ImageIcon move_level_up_icon = Utility.getImage("arrow-up.gif");
1069 move_level_up_button = new JButton();
1070 move_level_up_label = new JLabel("CDM.Move.Move_Up");
1071 move_level_up_label.setHorizontalAlignment(JLabel.CENTER);
1072 move_level_up_label.setPreferredSize(LABEL_SIZE);
1073 move_level_up_button.setLayout(new BorderLayout());
1074 move_level_up_button.add(new JLabel(move_level_up_icon), BorderLayout.WEST);
1075 move_level_up_button.add(move_level_up_label, BorderLayout.CENTER);
1076 move_level_up_button.add(new JLabel(move_level_up_icon), BorderLayout.EAST);
1077 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1078
1079 ImageIcon move_level_down_icon = Utility.getImage("arrow-down.gif");
1080 move_level_down_button = new JButton();
1081 move_level_down_label = new JLabel("CDM.Move.Move_Down");
1082 move_level_down_label.setHorizontalAlignment(JLabel.CENTER);
1083 move_level_down_label.setPreferredSize(LABEL_SIZE);
1084 move_level_down_button.setLayout(new BorderLayout());
1085 move_level_down_button.add(new JLabel(move_level_down_icon), BorderLayout.WEST);
1086 move_level_down_button.add(move_level_down_label, BorderLayout.CENTER);
1087 move_level_down_button.add(new JLabel(move_level_down_icon), BorderLayout.EAST);
1088 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1089
1090 JPanel level_spacer_panel = new JPanel();
1091
1092 JPanel level_body_panel = new JPanel();
1093
1094 JPanel level_details_panel = new JPanel();
1095
1096 JPanel level_name_panel = new JPanel();
1097
1098 level_name_label = new JLabel("CDM.IndexManager.MGPP.Level_Name");
1099 level_name_label.setPreferredSize(LABEL_SIZE);
1100
1101 level_name_field = new JTextField();
1102
1103 JPanel level_panel = new JPanel();
1104
1105 level_label = new JLabel("CDM.IndexManager.MGPP.Level");
1106 level_label.setPreferredSize(LABEL_SIZE);
1107
1108 level_combobox = new GComboBox(Index.LEVEL);
1109 level_combobox.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.editable_background", false));
1110 level_combobox.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
1111 level_combobox.setEditable(true);
1112 level_combobox.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
1113 level_combobox.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
1114
1115 JPanel level_button_panel = new JPanel();
1116
1117 add_level_button = new JButton("CDM.IndexManager.MGPP.Add_Level");
1118 add_level_button.setEnabled(false);
1119 add_level_button.setMnemonic(KeyEvent.VK_A);
1120
1121 remove_level_button = new JButton("CDM.IndexManager.MGPP.Remove_Level");
1122 remove_level_button.setEnabled(false);
1123 remove_level_button.setMnemonic(KeyEvent.VK_A);
1124
1125 JPanel level_empty_panel = new JPanel();
1126
1127 // Connect Levels
1128 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1129 add_level_button.addActionListener(new AddLevelActionListener());
1130 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1131 Gatherer.dictionary.register(add_level_button, null, false);
1132 Gatherer.dictionary.register(current_levels_label, null, false);
1133 Gatherer.dictionary.register(level_label, null, false);
1134 Gatherer.dictionary.register(level_name_label, null, false);
1135 Gatherer.dictionary.register(move_level_down_label, null, false);
1136 Gatherer.dictionary.register(move_level_up_label, null, false);
1137 Gatherer.dictionary.register(remove_level_button, null, false);
1138 level_combobox.addActionListener(level_eal);
1139 ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1140 level_name_field.getDocument().addDocumentListener(level_eal);
1141 move_level_down_button.addActionListener(new MoveLevelDownListener());
1142 move_level_up_button.addActionListener(new MoveLevelUpListener());
1143 remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal));
1144 // Layout Levels
1145
1146 level_movement_panel.setLayout(new GridLayout(2,1));
1147 level_movement_panel.add(move_level_up_button);
1148 level_movement_panel.add(move_level_down_button);
1149
1150 current_levels_panel.setLayout(new BorderLayout());
1151 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1152 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1153 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1154
1155 level_name_panel.setLayout(new BorderLayout());
1156 level_name_panel.add(level_name_label, BorderLayout.WEST);
1157 level_name_panel.add(level_name_field, BorderLayout.CENTER);
1158
1159 level_panel.setLayout(new BorderLayout());
1160 level_panel.add(level_label, BorderLayout.WEST);
1161 level_panel.add(level_combobox, BorderLayout.CENTER);
1162
1163 level_details_panel.setLayout(new GridLayout(2,1,0,5));
1164 level_details_panel.add(level_name_panel);
1165 level_details_panel.add(level_panel);
1166
1167 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1168 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1169 level_button_panel.add(add_level_button);
1170 level_button_panel.add(remove_level_button);
1171
1172 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1173 level_body_panel.setLayout(new BorderLayout());
1174 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1175 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1176
1177 level_spacer_panel.setLayout(new BorderLayout());
1178 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1179 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1180
1181 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1182 levels_panel.setLayout(new BorderLayout());
1183 levels_panel.add(current_levels_panel, BorderLayout.NORTH);
1184 levels_panel.add(level_spacer_panel, BorderLayout.CENTER);
1185
1186 // Create General
1187 JPanel header_panel = new JPanel();
1188
1189 title_label = new JLabel("CDM.IndexManager.MGPP.Title");
1190
1191 instructions_textarea = new JTextArea("CDM.IndexManager.MGPP.Instructions");
1192 instructions_textarea.setEditable(false);
1193 instructions_textarea.setLineWrap(true);
1194 instructions_textarea.setRows(6);
1195 instructions_textarea.setWrapStyleWord(true);
1196
1197 tabbed_pane = new JTabbedPane();
1198
1199 // Layout General
1200 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
1201 header_panel.setLayout(new BorderLayout());
1202 header_panel.add(title_label, BorderLayout.NORTH);
1203 header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
1204
1205 tabbed_pane.add("CDM.IndexManager.MGPP.Indexes", indexes_panel);
1206 tabbed_pane.add("CDM.IndexManager.MGPP.Levels", levels_panel);
1207
1208 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1209 setLayout(new BorderLayout());
1210 add(header_panel, BorderLayout.NORTH);
1211 add(tabbed_pane, BorderLayout.CENTER);
1212
1213 // Connect General
1214 Gatherer.dictionary.register(instructions_textarea, null, false);
1215 Gatherer.dictionary.register(tabbed_pane, null, false);
1216 Gatherer.dictionary.register(title_label, null, false);
1217 }
1218
1219 public void destroy() {
1220 Gatherer.dictionary.deregister(add_index_button);
1221 Gatherer.dictionary.deregister(current_indexes_label);
1222 Gatherer.dictionary.deregister(index_label);
1223 Gatherer.dictionary.deregister(index_name_label);
1224 Gatherer.dictionary.deregister(move_index_down_label);
1225 Gatherer.dictionary.deregister(move_index_up_label);
1226 Gatherer.dictionary.deregister(remove_index_button);
1227
1228 Gatherer.dictionary.deregister(instructions_textarea);
1229 Gatherer.dictionary.deregister(tabbed_pane);
1230 Gatherer.dictionary.deregister(title_label);
1231 }
1232
1233 public void gainFocus() {
1234 // Reload the assigned indexes list.
1235 index_combobox.removeAllItems();
1236 index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR);
1237 index_combobox.addItem(CollectionConfiguration.TEXT_STR);
1238 java.util.List assigned_elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
1239 int assigned_elements_size = assigned_elements.size();
1240 for(int i = 0; i < assigned_elements_size; i++) {
1241 index_combobox.addItem(assigned_elements.get(i));
1242 }
1243 assigned_elements = null;
1244 }
1245
1246 private class AddIndexActionListener
1247 implements ActionListener {
1248 public void actionPerformed(ActionEvent event) {
1249 // Retrieve the name
1250 String name = index_name_field.getText();
1251 // Retrieve the source
1252 Object source = index_combobox.getSelectedItem();
1253 // If this object isn't yet in the combobox add it.
1254 if(index_combobox.getSelectedIndex() == -1) {
1255 index_combobox.insertItemAt(source, index_combobox.getItemCount());
1256 }
1257 // Create new index
1258 ArrayList sources = new ArrayList();
1259 sources.add(source);
1260 Index index = new Index(sources);
1261 // Create new metadatum
1262 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1263 metadatum.setValue(name);
1264 // Assign new index
1265 addIndex(index, metadatum);
1266 // Done. Disable add
1267 add_index_button.setEnabled(false);
1268 }
1269 }
1270
1271 private class AddLevelActionListener
1272 implements ActionListener {
1273 public void actionPerformed(ActionEvent event) {
1274 // Retrieve the name
1275 String name = level_name_field.getText();
1276 // Retrieve the source
1277 String source = (String)level_combobox.getSelectedItem();
1278 // If this object isn't yet in the combobox add it.
1279 if(level_combobox.getSelectedIndex() == -1) {
1280 level_combobox.insertItemAt(source, level_combobox.getItemCount());
1281 }
1282 Level level = new Level(source);
1283 // Create new metadatum
1284 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1285 metadatum.setValue(name);
1286 // Assign new level
1287 addLevel(level, metadatum);
1288 // Done. Disable add
1289 add_level_button.setEnabled(false);
1290 }
1291 }
1292
1293 private class CurrentIndexesListSelectionListener
1294 implements ListSelectionListener {
1295 public void valueChanged(ListSelectionEvent event) {
1296 if(!event.getValueIsAdjusting()) {
1297 Index index = (Index)current_indexes_list.getSelectedValue();
1298 if(index != null) {
1299 move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1));
1300 move_index_up_button.setEnabled((model.indexOf(index) > 0));
1301 remove_index_button.setEnabled(true);
1302 }
1303 else {
1304 move_index_down_button.setEnabled(false);
1305 move_index_up_button.setEnabled(false);
1306 remove_index_button.setEnabled(false);
1307 }
1308 }
1309 }
1310 }
1311
1312 private class CurrentLevelsListSelectionListener
1313 implements ListSelectionListener {
1314 public void valueChanged(ListSelectionEvent event) {
1315 if(!event.getValueIsAdjusting()) {
1316 Level level = (Level)current_levels_list.getSelectedValue();
1317 if(level != null) {
1318 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1319 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1320 remove_level_button.setEnabled(true);
1321 }
1322 else {
1323 move_level_down_button.setEnabled(false);
1324 move_level_up_button.setEnabled(false);
1325 remove_level_button.setEnabled(false);
1326 }
1327 }
1328 }
1329 }
1330
1331 private class EnableAddIndexListener
1332 implements ActionListener, DocumentListener {
1333 /** Called whenever a selection action occurs on the combobox.
1334 * @param event an ActionEvent containing information about the selection event
1335 */
1336 public void actionPerformed(ActionEvent event) {
1337 validateAddButton();
1338 }
1339
1340 /** Gives notification that an attribute or set of attributes changed.
1341 * @param event a DocumentEvent containing information about the text changed
1342 */
1343 public void changedUpdate(DocumentEvent event) {
1344 validateAddButton();
1345 }
1346
1347 /** Gives notification that there was an insert into the document.
1348 * @param event a DocumentEvent containing information about the text added
1349 */
1350 public void insertUpdate(DocumentEvent event) {
1351 validateAddButton();
1352 }
1353
1354 /** Gives notification that a portion of the document has been removed.
1355 * @param event a DocumentEvent containing information about the text removed
1356 */
1357 public void removeUpdate(DocumentEvent e) {
1358 validateAddButton();
1359 }
1360
1361 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1362 public void validateAddButton() {
1363 String name = index_name_field.getText();
1364 Object selected_object = index_combobox.getSelectedItem();
1365 if(name.length() > 0 && selected_object != null) {
1366 // Unfortunately we have to generate a valid id
1367 String id = null;
1368 if(selected_object instanceof ElementWrapper) {
1369 id = ((ElementWrapper)selected_object).getName();
1370 }
1371 else {
1372 id = selected_object.toString();
1373 }
1374 add_index_button.setEnabled(getIndex(id) == null);
1375 }
1376 else {
1377 add_index_button.setEnabled(false);
1378 }
1379 }
1380 }
1381
1382 private class EnableAddLevelListener
1383 implements ActionListener, DocumentListener {
1384 /** Called whenever a selection action occurs on the combobox.
1385 * @param event an ActionEvent containing information about the selection event
1386 */
1387 public void actionPerformed(ActionEvent event) {
1388 validateAddButton();
1389 }
1390
1391 /** Gives notification that an attribute or set of attributes changed.
1392 * @param event a DocumentEvent containing information about the text changed
1393 */
1394 public void changedUpdate(DocumentEvent event) {
1395 validateAddButton();
1396 }
1397
1398 /** Gives notification that there was an insert into the document.
1399 * @param event a DocumentEvent containing information about the text added
1400 */
1401 public void insertUpdate(DocumentEvent event) {
1402 validateAddButton();
1403 }
1404
1405 /** Gives notification that a portion of the document has been removed.
1406 * @param event a DocumentEvent containing information about the text removed
1407 */
1408 public void removeUpdate(DocumentEvent e) {
1409 validateAddButton();
1410 }
1411
1412 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1413 public void validateAddButton() {
1414 String name = level_name_field.getText();
1415 Object selected_object = level_combobox.getSelectedItem();
1416 if(name.length() > 0 && selected_object != null) {
1417 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1418 }
1419 else {
1420 add_level_button.setEnabled(false);
1421 }
1422 }
1423 }
1424
1425 private class MoveIndexDownListener
1426 implements ActionListener {
1427 public void actionPerformed(ActionEvent event) {
1428 // Retrieve the first selected item
1429 Index index = (Index) current_indexes_list.getSelectedValue();
1430 moveIndex(index, false);
1431 current_indexes_list.setSelectedValue(index, true);
1432 }
1433 }
1434
1435 private class MoveLevelDownListener
1436 implements ActionListener {
1437 public void actionPerformed(ActionEvent event) {
1438 // Retrieve the first selected item
1439 Level level = (Level) current_levels_list.getSelectedValue();
1440 moveLevel(level, false);
1441 current_levels_list.setSelectedValue(level, true);
1442 }
1443 }
1444
1445 private class MoveIndexUpListener
1446 implements ActionListener {
1447 public void actionPerformed(ActionEvent event) {
1448 // Retrieve the first selected item
1449 Index index = (Index) current_indexes_list.getSelectedValue();
1450 moveIndex(index, true);
1451 current_indexes_list.setSelectedValue(index, true);
1452 }
1453 }
1454
1455 private class MoveLevelUpListener
1456 implements ActionListener {
1457 public void actionPerformed(ActionEvent event) {
1458 // Retrieve the first selected item
1459 Level level = (Level) current_levels_list.getSelectedValue();
1460 moveLevel(level, true);
1461 current_levels_list.setSelectedValue(level, true);
1462 }
1463 }
1464
1465 private class RemoveIndexActionListener
1466 implements ActionListener {
1467
1468 private EnableAddIndexListener eal = null;
1469
1470 public RemoveIndexActionListener(EnableAddIndexListener eal) {
1471 this.eal = eal;
1472 }
1473
1474 public void actionPerformed(ActionEvent event) {
1475 // Retrieve the selected items
1476 Object[] selected_objects = current_indexes_list.getSelectedValues();
1477 // Clear selection
1478 current_indexes_list.clearSelection();
1479 for(int i = 0; i < selected_objects.length; i++) {
1480 Index index = (Index) selected_objects[i];
1481 // Remove any related metadata
1482 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1483 // Remove the index
1484 removeIndex(index);
1485 }
1486 // Disable remove button
1487 remove_index_button.setEnabled(false);
1488 // Check if add should be reenabled
1489 eal.validateAddButton();
1490 }
1491 }
1492
1493 private class RemoveLevelActionListener
1494 implements ActionListener {
1495
1496 private EnableAddLevelListener eal = null;
1497
1498 public RemoveLevelActionListener(EnableAddLevelListener eal) {
1499 this.eal = eal;
1500 }
1501
1502 public void actionPerformed(ActionEvent event) {
1503 // Retrieve the selected items
1504 Object[] selected_objects = current_levels_list.getSelectedValues();
1505 // Clear selection
1506 current_levels_list.clearSelection();
1507 for(int i = 0; i < selected_objects.length; i++) {
1508 Level level = (Level) selected_objects[i];
1509 // Remove any related metadata
1510 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
1511 // Remove the index
1512 removeLevel(level);
1513 }
1514 // Disable remove button
1515 remove_level_button.setEnabled(false);
1516 // Check if add should be reenabled
1517 eal.validateAddButton();
1518 }
1519 }
1520 }
1521}
1522
1523
Note: See TracBrowser for help on using the repository browser.