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

Last change on this file since 5211 was 5211, checked in by jmt12, 21 years ago

Moved buttons and borders etc to make views more consistant

  • 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 move_up_button = new DoubleImageButton(get("CDM.Move.Move_Up"), Utility.getImage("arrow-up.gif"), Utility.getImage("arrow-up-disabled.gif"));
569 move_up_button.setEnabled(false);
570 move_up_button.setMnemonic(KeyEvent.VK_U);
571 move_up_button.setPreferredSize(Utility.BUTTON_SIZE);
572 move_down_button = new DoubleImageButton(get("CDM.Move.Move_Down"), Utility.getImage("arrow-down.gif"), Utility.getImage("arrow-down-disabled.gif"));
573 move_down_button.setEnabled(false);
574 move_down_button.setMnemonic(KeyEvent.VK_D);
575 move_down_button.setPreferredSize(Utility.BUTTON_SIZE);
576
577 JPanel index_pane = new JPanel();
578
579 JPanel details_pane = new JPanel();
580
581 JPanel name_pane = new JPanel();
582 JLabel name_label = new JLabel(get("Name"));
583 name_label.setPreferredSize(Utility.LABEL_SIZE);
584 name_textfield = new JTextField();
585
586 JPanel source_pane = new JPanel();
587 JLabel source_label = new JLabel(get("Source"));
588 source_label.setPreferredSize(Utility.LABEL_SIZE);
589 source_list = new CheckList(new_data, false);
590
591 JPanel level_pane = new JPanel();
592 JLabel level_label = new JLabel(get("Level"));
593 level_label.setPreferredSize(Utility.LABEL_SIZE);
594 level_combobox = new JComboBox();
595 level_combobox.addItem(get("Document"));
596 level_combobox.addItem(get("Paragraph"));
597 level_combobox.addItem(get("Section"));
598 level_combobox.setEditable(false);
599
600 JPanel button_pane = new JPanel();
601 add_button = new JButton(get("Add"));
602 add_button.setEnabled(false);
603 add_button.setMnemonic(KeyEvent.VK_A);
604 set_default_button = new JButton(get("Set_Default"));
605 set_default_button.setEnabled(false);
606 set_default_button.setMnemonic(KeyEvent.VK_S);
607 remove_button = new JButton(get("Remove"));
608 remove_button.setEnabled(false);
609 remove_button.setMnemonic(KeyEvent.VK_R);
610
611 // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class.
612 mgppindexes_control = new MGPPControl();
613
614 // Listeners
615 add_button.addActionListener(new AddListener());
616 move_down_button.addActionListener(new MoveListener(false));
617 move_up_button.addActionListener(new MoveListener(true));
618 remove_button.addActionListener(new RemoveListener());
619 set_default_button.addActionListener(new SetDefaultListener());
620 name_textfield.getDocument().addDocumentListener(new NameListener());
621 index_list.addListSelectionListener(new IndexListListener());
622 source_list.addListSelectionListener(new SourceListListener());
623 ExclusiveListSelectionListener elsl = new ExclusiveListSelectionListener();
624 elsl.add(index_list);
625 elsl.add(source_list);
626 elsl = null; // We don't want to hold a reference to this
627
628 // Layout
629 instruction_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
630 instruction_pane.setLayout(new BorderLayout());
631 instruction_pane.add(title_label, BorderLayout.NORTH);
632 instruction_pane.add(new JScrollPane(instruction_textarea), BorderLayout.CENTER);
633
634 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
635 movement_pane.setLayout(new GridLayout(2,1));
636 movement_pane.add(move_up_button);
637 movement_pane.add(move_down_button);
638
639 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
640 assigned_indexes_pane.setLayout(new BorderLayout());
641 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
642 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
643 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
644
645 name_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
646 name_pane.setLayout(new BorderLayout());
647 name_pane.add(name_label, BorderLayout.WEST);
648 name_pane.add(name_textfield, BorderLayout.CENTER);
649
650 source_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
651 source_pane.setLayout(new BorderLayout());
652 source_pane.add(source_label, BorderLayout.WEST);
653 source_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
654
655 level_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
656 level_pane.setLayout(new BorderLayout());
657 level_pane.add(level_label, BorderLayout.WEST);
658 level_pane.add(level_combobox, BorderLayout.CENTER);
659
660 details_pane.setLayout(new BorderLayout());
661 details_pane.add(name_pane, BorderLayout.NORTH);
662 details_pane.add(source_pane, BorderLayout.CENTER);
663 details_pane.add(level_pane, BorderLayout.SOUTH);
664
665 button_pane.setLayout(new GridLayout(1,3));
666 button_pane.add(add_button);
667 button_pane.add(set_default_button);
668 button_pane.add(remove_button);
669
670 index_pane.setLayout(new BorderLayout());
671 index_pane.add(details_pane, BorderLayout.CENTER);
672 index_pane.add(button_pane, BorderLayout.SOUTH);
673
674 mgindexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
675 mgindexes_panel.setLayout(new BorderLayout());
676 mgindexes_panel.add(instruction_pane, BorderLayout.NORTH);
677 mgindexes_panel.add(assigned_indexes_pane, BorderLayout.CENTER);
678 mgindexes_panel.add(index_pane, BorderLayout.SOUTH);
679
680 setLayout(card_layout);
681 add(mgindexes_panel, MGINDEXES);
682 add(mgppindexes_control, MGPPINDEXES);
683 }
684 /* Destructor, removes persistant listeners from the Dictionary.
685 */
686 public void destroy() {
687 mgppindexes_control.destroy();
688 mgppindexes_control = null;
689 }
690
691 public void gainFocus() {
692 boolean mgpp_enabled = CollectionDesignManager.searchtype_manager.isMGPPEnabled();
693 if(instruction_textarea != null) {
694 instruction_textarea.setCaretPosition(0);
695 }
696 if(mgpp_enabled) {
697 mgppindexes_control.gainFocus();
698 }
699 else {
700 // Reload the assigned indexes list.
701 ArrayList new_data = new ArrayList();
702 new_data.add(CollectionConfiguration.TEXT_STR);
703 new_data.addAll(Gatherer.c_man.getCollection().msm.getAssignedElements());
704 // reset the model in the list and combobox
705 source_list.setListData(new_data.toArray());
706 new_data = null;
707 }
708 // Bring the appropriate card to the front
709 if(mgpp_enabled) {
710 card_layout.show(this, MGPPINDEXES);
711 }
712 else {
713 card_layout.show(this, MGINDEXES);
714 }
715 }
716
717 public void loseFocus() {
718 }
719
720 private void validateAddButton() {
721 boolean add_enabled = false;
722 // Indexes must have a name
723 if(name_textfield.getText().length() == 0) {
724 add_enabled = false;
725 }
726 // Can't add a new index if no sources are selected
727 else if(source_list.isSelectionEmpty()) {
728 add_enabled = false;
729 }
730 // If we get this far, create a dummy index and see if its already assigned in the collection
731 else {
732 Object object[] = source_list.getSelected().toArray();
733 ArrayList sources = new ArrayList();
734 for(int i = 0; i < object.length; i++) {
735 sources.add(object[i]);
736 }
737 object = null;
738 Index index = new Index(level_combobox.getSelectedIndex(), sources);
739 sources = null;
740 if(model.contains(index)) {
741 add_enabled = false;
742 }
743 else {
744 add_enabled = true;
745 }
746 }
747 // We should now know the add_button state
748 add_button.setEnabled(add_enabled);
749 }
750
751 /** 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. */
752 private class AddListener
753 implements ActionListener {
754 /** 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.
755 * @param event An <strong>ActionEvent</strong> providing extra information about the event.
756 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
757 * @see org.greenstone.gatherer.cdm.CollectionMeta
758 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
759 * @see org.greenstone.gatherer.cdm.Index
760 * @see org.greenstone.gatherer.cdm.Language
761 * @see org.greenstone.gatherer.cdm.LanguageManager
762 */
763 public void actionPerformed(ActionEvent event) {
764 String name = name_textfield.getText();
765 if(!source_list.isSelectionEmpty() && name.length() != 0) {
766 ArrayList sources = source_list.getSelected();
767 Index index = new Index(level_combobox.getSelectedIndex(), sources);
768 sources = null;
769 // Before we add the index to the model, we have to add the collection metadata for this.
770 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
771 metadatum.setValue(name);
772 // Finally add index.
773 addIndex(index, metadatum);
774 metadatum = null;
775 index = null;
776 }
777 name = null;
778 }
779 }
780
781 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
782 private class IndexListListener
783 implements ListSelectionListener {
784 /** 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.
785 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
786 */
787 public void valueChanged(ListSelectionEvent event) {
788 Object value = index_list.getSelectedValue();
789 move_down_button.setEnabled(value != null);
790 move_up_button.setEnabled(value != null);
791 remove_button.setEnabled(value != null);
792 set_default_button.setEnabled(value != null && (default_index == null || !default_index.equals(value)));
793 }
794 }
795
796 private class IndexListRenderer
797 extends DefaultListCellRenderer {
798
799 /** Return a component that has been configured to display the specified value. */
800 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
801 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
802 if(default_index != null && default_index.equals(value)) {
803 component.setText(component.getText() + " " + get("Default_Index_Indicator"));
804 }
805 return component;
806 }
807
808 }
809
810 private class MoveListener
811 implements ActionListener {
812
813 private boolean move_up;
814
815 public MoveListener(boolean move_up) {
816 this.move_up = move_up;
817 }
818
819 public void actionPerformed(ActionEvent event) {
820 // Retrieve the selected index
821 Index index = (Index) index_list.getSelectedValue();
822 if(index != null) {
823 moveIndex(index, move_up);
824 // Ensure the index that moved is still selected
825 index_list.setSelectedValue(index, true);
826 index = null;
827 }
828 }
829 }
830
831 /** 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. */
832 private class RemoveListener
833 implements ActionListener {
834 /** If called when an action occurs on a registered component, we remove the currently selected index, if there is one.
835 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
836 * @see org.greenstone.gatherer.cdm.Index
837 */
838 public void actionPerformed(ActionEvent event) {
839 if(!index_list.isSelectionEmpty()) {
840 removeIndex((Index)index_list.getSelectedValue());
841 }
842 move_down_button.setEnabled(false);
843 move_up_button.setEnabled(false);
844 remove_button.setEnabled(false);
845 }
846 }
847
848 private class SetDefaultListener
849 implements ActionListener {
850
851 public void actionPerformed(ActionEvent event) {
852 Index index = (Index) index_list.getSelectedValue();
853 if(index != null) {
854 setDefault(index);
855 // This should cause a repaint of just the desired row
856 index_list.setSelectedValue(index, true);
857 }
858 set_default_button.setEnabled(false);
859 }
860 }
861
862 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
863 private class SourceListListener
864 implements ListSelectionListener {
865 /** 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.
866 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
867 */
868 public void valueChanged(ListSelectionEvent event) {
869 validateAddButton();
870 }
871 }
872
873 /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */
874 private class NameListener
875 implements DocumentListener {
876 /** Gives notification that an attribute or set of attributes changed. */
877 public void changedUpdate(DocumentEvent e) {
878 validateAddButton();
879 }
880
881 /** Gives notification that there was an insert into the document. */
882 public void insertUpdate(DocumentEvent e) {
883 validateAddButton();
884 }
885
886 /** Gives notification that a portion of the document has been removed. */
887 public void removeUpdate(DocumentEvent e) {
888 validateAddButton();
889 }
890 }
891 }
892
893 private class MGPPControl
894 extends JPanel {
895
896 private GComboBox index_combobox;
897 private GComboBox level_combobox;
898 private JButton add_index_button;
899 private JButton add_level_button;
900 private JButton move_index_down_button;
901 private JButton move_level_down_button;
902 private JButton move_index_up_button;
903 private JButton move_level_up_button;
904 private JButton remove_index_button;
905 private JButton remove_level_button;
906 private JLabel current_levels_label;
907 private JLabel current_indexes_label;
908 private JLabel index_label;
909 private JLabel index_name_label;
910 private JLabel level_label;
911 private JLabel level_name_label;
912 private JLabel move_index_down_label;
913 private JLabel move_level_down_label;
914 private JLabel move_index_up_label;
915 private JLabel move_level_up_label;
916 private JLabel title_label;
917 private JList current_levels_list;
918 private JList current_indexes_list;
919 private JTabbedPane tabbed_pane;
920 private JTextArea instructions_textarea;
921 private JTextField index_name_field;
922 private JTextField level_name_field;
923
924 public MGPPControl() {
925 // Create Indexes
926 JPanel indexes_panel = new JPanel();
927
928 JPanel current_indexes_panel = new JPanel();
929
930 current_indexes_label = new JLabel("CDM.IndexManager.MGPP.Current_Indexes");
931
932 current_indexes_list = new JList(model);
933 current_indexes_list.setVisibleRowCount(5);
934
935 JPanel index_movement_panel = new JPanel();
936
937 ImageIcon move_index_up_icon = Utility.getImage("arrow-up.gif");
938 move_index_up_button = new JButton();
939 move_index_up_label = new JLabel("CDM.Move.Move_Up");
940 move_index_up_label.setHorizontalAlignment(JLabel.CENTER);
941 move_index_up_label.setPreferredSize(LABEL_SIZE);
942 move_index_up_button.setLayout(new BorderLayout());
943 move_index_up_button.add(new JLabel(move_index_up_icon), BorderLayout.WEST);
944 move_index_up_button.add(move_index_up_label, BorderLayout.CENTER);
945 move_index_up_button.add(new JLabel(move_index_up_icon), BorderLayout.EAST);
946 move_index_up_button.setMnemonic(KeyEvent.VK_U);
947
948 ImageIcon move_index_down_icon = Utility.getImage("arrow-down.gif");
949 move_index_down_button = new JButton();
950 move_index_down_label = new JLabel("CDM.Move.Move_Down");
951 move_index_down_label.setHorizontalAlignment(JLabel.CENTER);
952 move_index_down_label.setPreferredSize(LABEL_SIZE);
953 move_index_down_button.setLayout(new BorderLayout());
954 move_index_down_button.add(new JLabel(move_index_down_icon), BorderLayout.WEST);
955 move_index_down_button.add(move_index_down_label, BorderLayout.CENTER);
956 move_index_down_button.add(new JLabel(move_index_down_icon), BorderLayout.EAST);
957 move_index_down_button.setMnemonic(KeyEvent.VK_D);
958
959 JPanel index_spacer_panel = new JPanel();
960
961 JPanel index_body_panel = new JPanel();
962
963 JPanel index_details_panel = new JPanel();
964
965 JPanel index_name_panel = new JPanel();
966
967 index_name_label = new JLabel("CDM.IndexManager.MGPP.Index_Name");
968 index_name_label.setPreferredSize(LABEL_SIZE);
969
970 index_name_field = new JTextField();
971
972 JPanel index_panel = new JPanel();
973
974 index_label = new JLabel("CDM.IndexManager.MGPP.Index");
975 index_label.setPreferredSize(LABEL_SIZE);
976
977 index_combobox = new GComboBox();
978 index_combobox.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.editable_background", false));
979 index_combobox.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
980 index_combobox.setEditable(true);
981 index_combobox.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
982 index_combobox.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
983
984 JPanel index_button_panel = new JPanel();
985
986 add_index_button = new JButton("CDM.IndexManager.MGPP.Add_Index");
987 add_index_button.setEnabled(false);
988 add_index_button.setMnemonic(KeyEvent.VK_A);
989
990 remove_index_button = new JButton("CDM.IndexManager.MGPP.Remove_Index");
991 remove_index_button.setEnabled(false);
992 remove_index_button.setMnemonic(KeyEvent.VK_A);
993
994 JPanel index_empty_panel = new JPanel();
995
996 // Connect Indexes
997 EnableAddIndexListener index_eal = new EnableAddIndexListener();
998 add_index_button.addActionListener(new AddIndexActionListener());
999 current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener());
1000 Gatherer.dictionary.register(add_index_button, null, false);
1001 Gatherer.dictionary.register(current_indexes_label, null, false);
1002 Gatherer.dictionary.register(index_label, null, false);
1003 Gatherer.dictionary.register(index_name_label, null, false);
1004 Gatherer.dictionary.register(move_index_down_label, null, false);
1005 Gatherer.dictionary.register(move_index_up_label, null, false);
1006 Gatherer.dictionary.register(remove_index_button, null, false);
1007 index_combobox.addActionListener(index_eal);
1008 ((JTextField)index_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(index_eal);
1009 index_name_field.getDocument().addDocumentListener(index_eal);
1010 move_index_down_button.addActionListener(new MoveIndexDownListener());
1011 move_index_up_button.addActionListener(new MoveIndexUpListener());
1012 remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal));
1013 // Layout Indexes
1014 index_movement_panel.setLayout(new GridLayout(2,1));
1015 index_movement_panel.add(move_index_up_button);
1016 index_movement_panel.add(move_index_down_button);
1017
1018 current_indexes_panel.setLayout(new BorderLayout());
1019 current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH);
1020 current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER);
1021 current_indexes_panel.add(index_movement_panel, BorderLayout.EAST);
1022
1023 index_name_panel.setLayout(new BorderLayout());
1024 index_name_panel.add(index_name_label, BorderLayout.WEST);
1025 index_name_panel.add(index_name_field, BorderLayout.CENTER);
1026
1027 index_panel.setLayout(new BorderLayout());
1028 index_panel.add(index_label, BorderLayout.WEST);
1029 index_panel.add(index_combobox, BorderLayout.CENTER);
1030
1031 index_details_panel.setLayout(new GridLayout(2,1,0,5));
1032 index_details_panel.add(index_name_panel);
1033 index_details_panel.add(index_panel);
1034
1035 index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1036 index_button_panel.setLayout(new GridLayout(1,2,5,0));
1037 index_button_panel.add(add_index_button);
1038 index_button_panel.add(remove_index_button);
1039
1040 index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1041 index_body_panel.setLayout(new BorderLayout());
1042 index_body_panel.add(index_details_panel, BorderLayout.CENTER);
1043 index_body_panel.add(index_button_panel, BorderLayout.SOUTH);
1044
1045 index_spacer_panel.setLayout(new BorderLayout());
1046 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH);
1047 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER);
1048
1049 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1050 indexes_panel.setLayout(new BorderLayout());
1051 indexes_panel.add(current_indexes_panel, BorderLayout.NORTH);
1052 indexes_panel.add(index_spacer_panel, BorderLayout.CENTER);
1053
1054 // Create Levels
1055 JPanel levels_panel = new JPanel();
1056
1057 JPanel current_levels_panel = new JPanel();
1058
1059 current_levels_label = new JLabel("CDM.IndexManager.MGPP.Current_Levels");
1060
1061 current_levels_list = new JList(levels_model);
1062 current_levels_list.setVisibleRowCount(5);
1063
1064 JPanel level_movement_panel = new JPanel();
1065
1066 ImageIcon move_level_up_icon = Utility.getImage("arrow-up.gif");
1067 move_level_up_button = new JButton();
1068 move_level_up_label = new JLabel("CDM.Move.Move_Up");
1069 move_level_up_label.setHorizontalAlignment(JLabel.CENTER);
1070 move_level_up_label.setPreferredSize(LABEL_SIZE);
1071 move_level_up_button.setLayout(new BorderLayout());
1072 move_level_up_button.add(new JLabel(move_level_up_icon), BorderLayout.WEST);
1073 move_level_up_button.add(move_level_up_label, BorderLayout.CENTER);
1074 move_level_up_button.add(new JLabel(move_level_up_icon), BorderLayout.EAST);
1075 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1076
1077 ImageIcon move_level_down_icon = Utility.getImage("arrow-down.gif");
1078 move_level_down_button = new JButton();
1079 move_level_down_label = new JLabel("CDM.Move.Move_Down");
1080 move_level_down_label.setHorizontalAlignment(JLabel.CENTER);
1081 move_level_down_label.setPreferredSize(LABEL_SIZE);
1082 move_level_down_button.setLayout(new BorderLayout());
1083 move_level_down_button.add(new JLabel(move_level_down_icon), BorderLayout.WEST);
1084 move_level_down_button.add(move_level_down_label, BorderLayout.CENTER);
1085 move_level_down_button.add(new JLabel(move_level_down_icon), BorderLayout.EAST);
1086 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1087
1088 JPanel level_spacer_panel = new JPanel();
1089
1090 JPanel level_body_panel = new JPanel();
1091
1092 JPanel level_details_panel = new JPanel();
1093
1094 JPanel level_name_panel = new JPanel();
1095
1096 level_name_label = new JLabel("CDM.IndexManager.MGPP.Level_Name");
1097 level_name_label.setPreferredSize(LABEL_SIZE);
1098
1099 level_name_field = new JTextField();
1100
1101 JPanel level_panel = new JPanel();
1102
1103 level_label = new JLabel("CDM.IndexManager.MGPP.Level");
1104 level_label.setPreferredSize(LABEL_SIZE);
1105
1106 level_combobox = new GComboBox(Index.LEVEL);
1107 level_combobox.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.editable_background", false));
1108 level_combobox.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
1109 level_combobox.setEditable(true);
1110 level_combobox.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
1111 level_combobox.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
1112
1113 JPanel level_button_panel = new JPanel();
1114
1115 add_level_button = new JButton("CDM.IndexManager.MGPP.Add_Level");
1116 add_level_button.setEnabled(false);
1117 add_level_button.setMnemonic(KeyEvent.VK_A);
1118
1119 remove_level_button = new JButton("CDM.IndexManager.MGPP.Remove_Level");
1120 remove_level_button.setEnabled(false);
1121 remove_level_button.setMnemonic(KeyEvent.VK_A);
1122
1123 JPanel level_empty_panel = new JPanel();
1124
1125 // Connect Levels
1126 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1127 add_level_button.addActionListener(new AddLevelActionListener());
1128 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1129 Gatherer.dictionary.register(add_level_button, null, false);
1130 Gatherer.dictionary.register(current_levels_label, null, false);
1131 Gatherer.dictionary.register(level_label, null, false);
1132 Gatherer.dictionary.register(level_name_label, null, false);
1133 Gatherer.dictionary.register(move_level_down_label, null, false);
1134 Gatherer.dictionary.register(move_level_up_label, null, false);
1135 Gatherer.dictionary.register(remove_level_button, null, false);
1136 level_combobox.addActionListener(level_eal);
1137 ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1138 level_name_field.getDocument().addDocumentListener(level_eal);
1139 move_level_down_button.addActionListener(new MoveLevelDownListener());
1140 move_level_up_button.addActionListener(new MoveLevelUpListener());
1141 remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal));
1142 // Layout Levels
1143
1144 level_movement_panel.setLayout(new GridLayout(2,1));
1145 level_movement_panel.add(move_level_up_button);
1146 level_movement_panel.add(move_level_down_button);
1147
1148 current_levels_panel.setLayout(new BorderLayout());
1149 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1150 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1151 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1152
1153 level_name_panel.setLayout(new BorderLayout());
1154 level_name_panel.add(level_name_label, BorderLayout.WEST);
1155 level_name_panel.add(level_name_field, BorderLayout.CENTER);
1156
1157 level_panel.setLayout(new BorderLayout());
1158 level_panel.add(level_label, BorderLayout.WEST);
1159 level_panel.add(level_combobox, BorderLayout.CENTER);
1160
1161 level_details_panel.setLayout(new GridLayout(2,1,0,5));
1162 level_details_panel.add(level_name_panel);
1163 level_details_panel.add(level_panel);
1164
1165 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1166 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1167 level_button_panel.add(add_level_button);
1168 level_button_panel.add(remove_level_button);
1169
1170 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1171 level_body_panel.setLayout(new BorderLayout());
1172 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1173 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1174
1175 level_spacer_panel.setLayout(new BorderLayout());
1176 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1177 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1178
1179 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1180 levels_panel.setLayout(new BorderLayout());
1181 levels_panel.add(current_levels_panel, BorderLayout.NORTH);
1182 levels_panel.add(level_spacer_panel, BorderLayout.CENTER);
1183
1184 // Create General
1185 JPanel header_panel = new JPanel();
1186
1187 title_label = new JLabel("CDM.IndexManager.MGPP.Title");
1188
1189 instructions_textarea = new JTextArea("CDM.IndexManager.MGPP.Instructions");
1190 instructions_textarea.setEditable(false);
1191 instructions_textarea.setLineWrap(true);
1192 instructions_textarea.setRows(6);
1193 instructions_textarea.setWrapStyleWord(true);
1194
1195 tabbed_pane = new JTabbedPane();
1196
1197 // Layout General
1198 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
1199 header_panel.setLayout(new BorderLayout());
1200 header_panel.add(title_label, BorderLayout.NORTH);
1201 header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
1202
1203 tabbed_pane.add("CDM.IndexManager.MGPP.Indexes", indexes_panel);
1204 tabbed_pane.add("CDM.IndexManager.MGPP.Levels", levels_panel);
1205
1206 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1207 setLayout(new BorderLayout());
1208 add(header_panel, BorderLayout.NORTH);
1209 add(tabbed_pane, BorderLayout.CENTER);
1210
1211 // Connect General
1212 Gatherer.dictionary.register(instructions_textarea, null, false);
1213 Gatherer.dictionary.register(tabbed_pane, null, false);
1214 Gatherer.dictionary.register(title_label, null, false);
1215 }
1216
1217 public void destroy() {
1218 Gatherer.dictionary.deregister(add_index_button);
1219 Gatherer.dictionary.deregister(current_indexes_label);
1220 Gatherer.dictionary.deregister(index_label);
1221 Gatherer.dictionary.deregister(index_name_label);
1222 Gatherer.dictionary.deregister(move_index_down_label);
1223 Gatherer.dictionary.deregister(move_index_up_label);
1224 Gatherer.dictionary.deregister(remove_index_button);
1225
1226 Gatherer.dictionary.deregister(instructions_textarea);
1227 Gatherer.dictionary.deregister(tabbed_pane);
1228 Gatherer.dictionary.deregister(title_label);
1229 }
1230
1231 public void gainFocus() {
1232 // Reload the assigned indexes list.
1233 index_combobox.removeAllItems();
1234 index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR);
1235 index_combobox.addItem(CollectionConfiguration.TEXT_STR);
1236 java.util.List assigned_elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
1237 int assigned_elements_size = assigned_elements.size();
1238 for(int i = 0; i < assigned_elements_size; i++) {
1239 index_combobox.addItem(assigned_elements.get(i));
1240 }
1241 assigned_elements = null;
1242 }
1243
1244 private class AddIndexActionListener
1245 implements ActionListener {
1246 public void actionPerformed(ActionEvent event) {
1247 // Retrieve the name
1248 String name = index_name_field.getText();
1249 // Retrieve the source
1250 Object source = index_combobox.getSelectedItem();
1251 // If this object isn't yet in the combobox add it.
1252 if(index_combobox.getSelectedIndex() == -1) {
1253 index_combobox.insertItemAt(source, index_combobox.getItemCount());
1254 }
1255 // Create new index
1256 ArrayList sources = new ArrayList();
1257 sources.add(source);
1258 Index index = new Index(sources);
1259 // Create new metadatum
1260 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1261 metadatum.setValue(name);
1262 // Assign new index
1263 addIndex(index, metadatum);
1264 // Done. Disable add
1265 add_index_button.setEnabled(false);
1266 }
1267 }
1268
1269 private class AddLevelActionListener
1270 implements ActionListener {
1271 public void actionPerformed(ActionEvent event) {
1272 // Retrieve the name
1273 String name = level_name_field.getText();
1274 // Retrieve the source
1275 String source = (String)level_combobox.getSelectedItem();
1276 // If this object isn't yet in the combobox add it.
1277 if(level_combobox.getSelectedIndex() == -1) {
1278 level_combobox.insertItemAt(source, level_combobox.getItemCount());
1279 }
1280 Level level = new Level(source);
1281 // Create new metadatum
1282 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1283 metadatum.setValue(name);
1284 // Assign new level
1285 addLevel(level, metadatum);
1286 // Done. Disable add
1287 add_level_button.setEnabled(false);
1288 }
1289 }
1290
1291 private class CurrentIndexesListSelectionListener
1292 implements ListSelectionListener {
1293 public void valueChanged(ListSelectionEvent event) {
1294 if(!event.getValueIsAdjusting()) {
1295 Index index = (Index)current_indexes_list.getSelectedValue();
1296 if(index != null) {
1297 move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1));
1298 move_index_up_button.setEnabled((model.indexOf(index) > 0));
1299 remove_index_button.setEnabled(true);
1300 }
1301 else {
1302 move_index_down_button.setEnabled(false);
1303 move_index_up_button.setEnabled(false);
1304 remove_index_button.setEnabled(false);
1305 }
1306 }
1307 }
1308 }
1309
1310 private class CurrentLevelsListSelectionListener
1311 implements ListSelectionListener {
1312 public void valueChanged(ListSelectionEvent event) {
1313 if(!event.getValueIsAdjusting()) {
1314 Level level = (Level)current_levels_list.getSelectedValue();
1315 if(level != null) {
1316 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1317 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1318 remove_level_button.setEnabled(true);
1319 }
1320 else {
1321 move_level_down_button.setEnabled(false);
1322 move_level_up_button.setEnabled(false);
1323 remove_level_button.setEnabled(false);
1324 }
1325 }
1326 }
1327 }
1328
1329 private class EnableAddIndexListener
1330 implements ActionListener, DocumentListener {
1331 /** Called whenever a selection action occurs on the combobox.
1332 * @param event an ActionEvent containing information about the selection event
1333 */
1334 public void actionPerformed(ActionEvent event) {
1335 validateAddButton();
1336 }
1337
1338 /** Gives notification that an attribute or set of attributes changed.
1339 * @param event a DocumentEvent containing information about the text changed
1340 */
1341 public void changedUpdate(DocumentEvent event) {
1342 validateAddButton();
1343 }
1344
1345 /** Gives notification that there was an insert into the document.
1346 * @param event a DocumentEvent containing information about the text added
1347 */
1348 public void insertUpdate(DocumentEvent event) {
1349 validateAddButton();
1350 }
1351
1352 /** Gives notification that a portion of the document has been removed.
1353 * @param event a DocumentEvent containing information about the text removed
1354 */
1355 public void removeUpdate(DocumentEvent e) {
1356 validateAddButton();
1357 }
1358
1359 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1360 public void validateAddButton() {
1361 String name = index_name_field.getText();
1362 Object selected_object = index_combobox.getSelectedItem();
1363 if(name.length() > 0 && selected_object != null) {
1364 // Unfortunately we have to generate a valid id
1365 String id = null;
1366 if(selected_object instanceof ElementWrapper) {
1367 id = ((ElementWrapper)selected_object).getName();
1368 }
1369 else {
1370 id = selected_object.toString();
1371 }
1372 add_index_button.setEnabled(getIndex(id) == null);
1373 }
1374 else {
1375 add_index_button.setEnabled(false);
1376 }
1377 }
1378 }
1379
1380 private class EnableAddLevelListener
1381 implements ActionListener, DocumentListener {
1382 /** Called whenever a selection action occurs on the combobox.
1383 * @param event an ActionEvent containing information about the selection event
1384 */
1385 public void actionPerformed(ActionEvent event) {
1386 validateAddButton();
1387 }
1388
1389 /** Gives notification that an attribute or set of attributes changed.
1390 * @param event a DocumentEvent containing information about the text changed
1391 */
1392 public void changedUpdate(DocumentEvent event) {
1393 validateAddButton();
1394 }
1395
1396 /** Gives notification that there was an insert into the document.
1397 * @param event a DocumentEvent containing information about the text added
1398 */
1399 public void insertUpdate(DocumentEvent event) {
1400 validateAddButton();
1401 }
1402
1403 /** Gives notification that a portion of the document has been removed.
1404 * @param event a DocumentEvent containing information about the text removed
1405 */
1406 public void removeUpdate(DocumentEvent e) {
1407 validateAddButton();
1408 }
1409
1410 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1411 public void validateAddButton() {
1412 String name = level_name_field.getText();
1413 Object selected_object = level_combobox.getSelectedItem();
1414 if(name.length() > 0 && selected_object != null) {
1415 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1416 }
1417 else {
1418 add_level_button.setEnabled(false);
1419 }
1420 }
1421 }
1422
1423 private class MoveIndexDownListener
1424 implements ActionListener {
1425 public void actionPerformed(ActionEvent event) {
1426 // Retrieve the first selected item
1427 Index index = (Index) current_indexes_list.getSelectedValue();
1428 moveIndex(index, false);
1429 current_indexes_list.setSelectedValue(index, true);
1430 }
1431 }
1432
1433 private class MoveLevelDownListener
1434 implements ActionListener {
1435 public void actionPerformed(ActionEvent event) {
1436 // Retrieve the first selected item
1437 Level level = (Level) current_levels_list.getSelectedValue();
1438 moveLevel(level, false);
1439 current_levels_list.setSelectedValue(level, true);
1440 }
1441 }
1442
1443 private class MoveIndexUpListener
1444 implements ActionListener {
1445 public void actionPerformed(ActionEvent event) {
1446 // Retrieve the first selected item
1447 Index index = (Index) current_indexes_list.getSelectedValue();
1448 moveIndex(index, true);
1449 current_indexes_list.setSelectedValue(index, true);
1450 }
1451 }
1452
1453 private class MoveLevelUpListener
1454 implements ActionListener {
1455 public void actionPerformed(ActionEvent event) {
1456 // Retrieve the first selected item
1457 Level level = (Level) current_levels_list.getSelectedValue();
1458 moveLevel(level, true);
1459 current_levels_list.setSelectedValue(level, true);
1460 }
1461 }
1462
1463 private class RemoveIndexActionListener
1464 implements ActionListener {
1465
1466 private EnableAddIndexListener eal = null;
1467
1468 public RemoveIndexActionListener(EnableAddIndexListener eal) {
1469 this.eal = eal;
1470 }
1471
1472 public void actionPerformed(ActionEvent event) {
1473 // Retrieve the selected items
1474 Object[] selected_objects = current_indexes_list.getSelectedValues();
1475 // Clear selection
1476 current_indexes_list.clearSelection();
1477 for(int i = 0; i < selected_objects.length; i++) {
1478 Index index = (Index) selected_objects[i];
1479 // Remove any related metadata
1480 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1481 // Remove the index
1482 removeIndex(index);
1483 }
1484 // Disable remove button
1485 remove_index_button.setEnabled(false);
1486 // Check if add should be reenabled
1487 eal.validateAddButton();
1488 }
1489 }
1490
1491 private class RemoveLevelActionListener
1492 implements ActionListener {
1493
1494 private EnableAddLevelListener eal = null;
1495
1496 public RemoveLevelActionListener(EnableAddLevelListener eal) {
1497 this.eal = eal;
1498 }
1499
1500 public void actionPerformed(ActionEvent event) {
1501 // Retrieve the selected items
1502 Object[] selected_objects = current_levels_list.getSelectedValues();
1503 // Clear selection
1504 current_levels_list.clearSelection();
1505 for(int i = 0; i < selected_objects.length; i++) {
1506 Level level = (Level) selected_objects[i];
1507 // Remove any related metadata
1508 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
1509 // Remove the index
1510 removeLevel(level);
1511 }
1512 // Disable remove button
1513 remove_level_button.setEnabled(false);
1514 // Check if add should be reenabled
1515 eal.validateAddButton();
1516 }
1517 }
1518 }
1519}
1520
1521
Note: See TracBrowser for help on using the repository browser.