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

Last change on this file since 6844 was 6844, checked in by kjdon, 20 years ago

removed the fixed sizing from the move up and down buttons so they expand to fit the text

  • Property svn:keywords set to Author Date Id Revision
File size: 62.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.cdm.CollectionMeta;
37import org.greenstone.gatherer.cdm.CommandTokenizer;
38import org.greenstone.gatherer.cdm.Control;
39import org.greenstone.gatherer.cdm.DOMProxyListModel;
40import org.greenstone.gatherer.cdm.Index;
41import org.greenstone.gatherer.checklist.CheckList;
42import org.greenstone.gatherer.gui.GComboBox;
43import org.greenstone.gatherer.gui.GLIButton;
44import org.greenstone.gatherer.msm.ElementWrapper;
45import org.greenstone.gatherer.msm.MSMUtils;
46import org.greenstone.gatherer.util.ExclusiveListSelectionListener;
47import org.greenstone.gatherer.util.Utility;
48import org.w3c.dom.*;
49/** 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.
50 * @author John Thompson, Greenstone Digital Library, University of Waikato
51 * @version 2.3
52 */
53public class IndexManager
54 extends DOMProxyListModel {
55
56 static final private Dimension LABEL_SIZE = new Dimension(150,30);
57 static final private String MGINDEXES = "mg indexes";
58 static final private String MGPPINDEXES = "mgpp indexes";
59
60 /** The controls for editing the indexes. */
61 private Control controls = null;
62 /** A model of the levels, also based on the DOM. */
63 private DOMProxyListModel levels_model = null;
64 /** A reference to ourselves so our inner methods have access. */
65 private DOMProxyListModel model = null;
66 /** The default index. */
67 private Index default_index = null;
68
69 /** Constructor. */
70 public IndexManager(Element indexes) {
71 super(indexes, CollectionConfiguration.INDEX_ELEMENT, new Index());
72 Gatherer.println("IndexManager: " + getSize() + " indexes parsed.");
73 model = this;
74 // Parse and retrieve the default index
75 NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
76 if(default_index_elements.getLength() > 0) {
77 default_index = new Index((Element)default_index_elements.item(0));
78 }
79 // Parse and retrieve the levels element
80 Element levels_element = CollectionDesignManager.collect_config.getLevels();
81 levels_model = new DOMProxyListModel(levels_element, CollectionConfiguration.CONTENT_ELEMENT, new Level());
82 Gatherer.println(" + " + levels_model.getSize() + " levels parsed.");
83 }
84
85 /** Method to add a new index.
86 * @param index The <strong>Index</strong> to add.
87 * @see org.greenstone.gatherer.Gatherer
88 * @see org.greenstone.gatherer.collection.CollectionManager
89 */
90 public void addIndex(Index index, CollectionMeta metadatum) {
91 ///ystem.err.println("Adding an index: " + index.toString());
92 if(!contains(index)) {
93 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
94 // Retrieve the currently last index
95 if(getSize() > 0) {
96 Index last_index = (Index)getElementAt(getSize() - 1);
97 addAfter(index, last_index);
98 }
99 else {
100 add(index);
101 // Also set this index as the default one, but only if there are levels available (ie mg only)
102 if(index.getLevel() != -1) {
103 setDefault(index);
104 }
105 }
106 Gatherer.c_man.configurationChanged();
107 }
108 else {
109 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
110 }
111 }
112
113 public void addLevel(Level level, CollectionMeta metadatum) {
114 if(!levels_model.contains(level)) {
115 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
116 // Retrieve the currently last index
117 if(levels_model.getSize() > 0) {
118 Level last_level = (Level)levels_model.getElementAt(levels_model.getSize() - 1);
119 levels_model.addAfter(level, last_level);
120 }
121 else {
122 levels_model.add(level);
123 }
124 Gatherer.c_man.configurationChanged();
125 }
126 else {
127 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Level_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
128 }
129 }
130
131 public void destroy() {
132 if(controls != null) {
133 controls.destroy();
134 controls = null;
135 }
136 default_index = null;
137 model = null;
138 }
139
140 /** Method to acquire the controls for editing the indexes.
141 * @return the Control
142 */
143 public Control getControls() {
144 if(controls == null) {
145 // Build controls
146 controls = new IndexControl();
147 }
148 return controls;
149 }
150
151 /** Method to get the default index.
152 * @return The default <strong>Index</strong>.
153 */
154 public Index getDefault() {
155 if(default_index != null && default_index.isAssigned()) {
156 return default_index;
157 }
158 else {
159 return null;
160 }
161 }
162
163 /** Method to retrieve a certain index, as referenced by an index number.
164 * @param index An <i>int</i> which indicates the position of the desired index.
165 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
166 */
167 public Index getIndex(int index) {
168 if(0 <= index && index < getSize()) {
169 return (Index)getElementAt(index);
170 }
171 return null;
172 }
173
174 /** Method to retrieve a certain index, given its id.
175 * @param id the id of the index as a String
176 * @return the Index that matches id, or null if no such index exists
177 */
178 public Index getIndex(String id) {
179 int size = getSize();
180 for(int i = 0; i < size; i++) {
181 Index index = (Index) getElementAt(i);
182 if(index.getID().equals(id)) {
183 return index;
184 }
185 }
186 return null;
187 }
188
189 public ArrayList getIndexes() {
190 return children();
191 }
192
193 public Level getLevel(String name) {
194 int levels_model_size = levels_model.getSize();
195 for(int i = 0; i < levels_model_size; i++) {
196 Level level = (Level) levels_model.getElementAt(i);
197 if(level.getName().equals(name)) {
198 return level;
199 }
200 }
201 return null;
202 }
203
204 public void moveIndex(Index index, boolean move_up) {
205 // Determine the indexes current position
206 int position = indexOf(index);
207 // Determine if it can be moved, ie if its not already at the top trying to move up, or at the bottom trying to move down.
208 if(position == -1) {
209 return;
210 }
211 if(position == 0 && move_up) {
212 return;
213 }
214 if(position == (getSize()) - 1 && !move_up) {
215 return;
216 }
217 // Ok, move the index
218 if(move_up) {
219 // Retrieve the index at position - 1
220 Index previous_index = (Index) getElementAt(position - 1);
221 // And add before. This will automatically remove the index first, as an Element can only exist once in a particular document
222 addBefore(index, previous_index);
223 }
224 else {
225 // Retrieve the index at position + 1
226 Index next_index = (Index) getElementAt(position + 1);
227 // And add after. This will automatically remove the index first, as an Element can only exist once in a particular document
228 addAfter(index, next_index);
229 }
230 // Schedule the collection for saving
231 Gatherer.c_man.configurationChanged();
232 }
233
234 public void moveLevel(Level level, boolean move_up) {
235 // Determine the leveles current position
236 int position = levels_model.indexOf(level);
237 // Determine if it can be moved, ie if its not already at the top trying to move up, or at the bottom trying to move down.
238 if(position == -1) {
239 return;
240 }
241 if(position == 0 && move_up) {
242 return;
243 }
244 if(position == (levels_model.getSize()) - 1 && !move_up) {
245 return;
246 }
247 // Ok, move the level
248 if(move_up) {
249 // Retrieve the level at position - 1
250 Level previous_level = (Level) levels_model.getElementAt(position - 1);
251 // And add before. This will automatically remove the level first, as an Element can only exist once in a particular document
252 levels_model.addBefore(level, previous_level);
253 }
254 else {
255 // Retrieve the level at position + 1
256 Level next_level = (Level) levels_model.getElementAt(position + 1);
257 // And add after. This will automatically remove the level first, as an Element can only exist once in a particular document
258 levels_model.addAfter(level, next_level);
259 }
260 // Schedule the collection for saving
261 Gatherer.c_man.configurationChanged();
262 }
263
264 /** Method to remove a certain index.
265 * @param index the Index to remove.
266 * @see org.greenstone.gatherer.Gatherer
267 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
268 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
269 * @see org.greenstone.gatherer.collection.CollectionManager
270 */
271 public void removeIndex(Index index) {
272 if(index != null) {
273 // Remove any current metadata from this index
274 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
275 // Check if the index removed happens to be the default index
276 if(default_index != null && default_index.equals(index)) {
277 // If so our first solution is to set the first index to be default
278 if(getSize() > 0) {
279 Index another_index = (Index) getElementAt(0);
280 setDefault(another_index);
281 another_index = null;
282 }
283 else {
284 default_index.setAssigned(false);
285 }
286 }
287 // Remove the index
288 remove(index);
289 Gatherer.c_man.configurationChanged();
290 }
291 }
292
293 public void removeLevel(Level level) {
294 if(level != null) {
295 // Remove any current metadata from this level
296 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
297 // Remove the level
298 levels_model.remove(level);
299 Gatherer.c_man.configurationChanged();
300 }
301 }
302
303 /** Method to set the default index.
304 * @param index the new default Index
305 * @see org.greenstone.gatherer.Gatherer
306 * @see org.greenstone.gatherer.collection.CollectionManager
307 */
308 public void setDefault(Index index) {
309 if(index != null) {
310 if(default_index == null) {
311 // Create the default index element, and place immediately after indexes element.
312 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.INDEX_DEFAULT_ELEMENT);
313 default_index = new Index(default_index_element);
314 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
315 if(target_node != null) {
316 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
317 }
318 else {
319 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
320 }
321 }
322 default_index.setAssigned(true);
323 default_index.setLevel(index.getLevel());
324 default_index.setSources(index.getSources());
325 }
326 else {
327 if(default_index != null) {
328 default_index.setAssigned(false);
329 }
330 }
331 Gatherer.c_man.configurationChanged();
332 }
333
334 /** This method is reponsible for changing the underlying Index commands from MG to MGPP and back again. This turns out to be easyish for MG->MGPP and very hard for the reverse. For the former we remove the level fragment and make sure the same levels are set, then we produce a list of the sources involved, breaking down comma seperated lists and making sure each item it unique. Changing back the other way turns out to be impossible, so we don't (beyond generating document:text, section:text and paragraph:text if text is an index and the respective levels are present). In either case we start by creating a comment containing the old index information.
335 * @param state true to enable MGPP indexes, false to use standard MG style ones
336 */
337 public void setMGPPEnabled(boolean state) {
338 if(state != root.getAttribute(CollectionConfiguration.MGPP_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR)) {
339 if(state) {
340 Element mg_element = root;
341 // Retrieve and assign the MGPP indexes element.
342 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
343 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
344 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
345 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
346 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
347 if(indexes.getLength() == 0) {
348 ArrayList levels_list = new ArrayList();
349 ArrayList sources_list = new ArrayList();
350 // We first use details from the default index if any
351 if(default_index != null) {
352 int level_int = default_index.getLevel();
353 if(0 <= level_int && level_int < 3) {
354 String level = Index.LEVEL[level_int];
355 if(!levels_list.contains(level)) {
356 levels_list.add(level);
357 }
358 level = null;
359 }
360 ArrayList sources = default_index.getSources();
361 sources.removeAll(sources_list);
362 sources_list.addAll(sources);
363 }
364 int size = getSize();
365 for(int i = 0; i < size; i++) {
366 Index index = (Index) getElementAt(i);
367 int level_int = index.getLevel();
368 if(0 <= level_int && level_int < 3) {
369 String level = Index.LEVEL[level_int];
370 if(!levels_list.contains(level)) {
371 levels_list.add(level);
372 }
373 level = null;
374 }
375 ArrayList sources = index.getSources();
376 sources.removeAll(sources_list);
377 sources_list.addAll(sources);
378 index = null;
379 }
380 // Replace mg element with mgpp element
381 setRoot(mgpp_element);
382
383 // We now have a list of sources and a list of levels, so create new indexes and levels based on these
384 int sources_list_size = sources_list.size();
385 for(int j = 0; j < sources_list_size; j++) {
386 Object source_object = sources_list.get(j);
387 String source_str = null;
388 if(source_object instanceof ElementWrapper) {
389 source_str = ((ElementWrapper)source_object).getName();
390 }
391 else {
392 source_str = source_object.toString();
393 }
394 ArrayList new_sources = new ArrayList();
395 new_sources.add(source_object);
396 source_object = null;
397 Index new_index = new Index(new_sources);
398 // Try to retrieve existing metadatum
399 source_str = new_index.getID();
400 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
401 // If no metadata was found, add new pseudo metadata using the id
402 if(metadatum == null) {
403 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
404 metadatum.setAssigned(true);
405 metadatum.setValue(source_str);
406 }
407 // If it was found, ensure it is assigned
408 else {
409 metadatum.setAssigned(true);
410 }
411 source_str = null;
412 addIndex(new_index, metadatum);
413 metadatum = null;
414 new_index = null;
415 new_sources = null;
416 source_str = null;
417 }
418 int levels_list_size = levels_list.size();
419 for(int k = 0; k < levels_list_size; k++) {
420 Level new_level = new Level((String)levels_list.get(k));
421 if(!levels_model.contains(new_level)) {
422 levels_model.add(levels_model.getSize(), new_level);
423 }
424 new_level = null;
425 }
426 }
427 else {
428 // Replace mg element with mgpp element
429 setRoot(mgpp_element);
430 }
431 // Unassign MG element and default index
432 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
433 mg_element = null;
434 if(default_index != null) {
435 default_index.setAssigned(false);
436 }
437 }
438 else {
439 Element mgpp_element = root;
440 // Retrieve and assign MG element and default index element
441 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
442 mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR);
443 if(default_index != null) {
444 default_index.setAssigned(true);
445 }
446 // If mg element has no indexes, and the current mgpp index include a text one, then generate text indexes for each of the registered levels.
447 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT);
448 if(indexes.getLength() == 0) {
449 Index index = getIndex(CollectionConfiguration.TEXT_STR);
450 if(index != null) {
451 // Replace mgpp element with mg element
452 setRoot(mg_element);
453 int level_size = levels_model.getSize();
454 for(int i = 0; i < level_size; i++) {
455 Level level = (Level) levels_model.getElementAt(i);
456 Index new_index = new Index(level.getName(), index.getSources());
457 // Try to retrieve existing metadatum
458 String source_str = new_index.getID();
459 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false);
460 // If no metadata was found, add new pseudo metadata using the id
461 if(metadatum == null) {
462 metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str);
463 metadatum.setAssigned(true);
464 metadatum.setValue(source_str);
465 }
466 // If it was found, ensure it is assigned
467 else {
468 metadatum.setAssigned(true);
469 }
470 source_str = null;
471 addIndex(new_index, metadatum);
472 new_index = null;
473 level = null;
474 }
475 }
476 }
477 else {
478 // Replace mgpp element with mg element
479 setRoot(mg_element);
480 }
481 // Unassign mgpp element and levels
482 mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
483 levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR);
484 }
485 }
486 }
487
488
489 /** This class creates a set of controls for editing the indexes. */
490 private class IndexControl
491 extends JPanel
492 implements Control {
493
494 private CardLayout card_layout;
495 private CheckList source_list;
496 private JButton add_button;
497 private JButton move_down_button;
498 private JButton move_up_button;
499 private JButton remove_button;
500 private JButton set_default_button;
501 private JComboBox level_combobox;
502 private JList index_list;
503 private JTextArea instruction_textarea;
504 private JTextField name_textfield ;
505
506 private MGPPControl mgppindexes_control;
507
508 /** Constructor.
509 * @see org.greenstone.gatherer.Configuration
510 * @see org.greenstone.gatherer.Gatherer
511 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.AddListener
512 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.NameListener
513 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.RemoveListener
514 * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.SetDefaultListener
515 * @see org.greenstone.gatherer.collection.CollectionManager
516 * @see org.greenstone.gatherer.gui.Coloring
517 * @see org.greenstone.gatherer.msm.MetadataSetManager
518 * @see org.greenstone.gatherer.util.ExclusiveListSelectionListener
519 */
520 public IndexControl() {
521 super();
522
523 ArrayList new_data = new ArrayList();
524 new_data.add(CollectionConfiguration.TEXT_STR);
525 new_data.addAll(Gatherer.c_man.getCollection().msm.getAssignedElements());
526
527 // Creation
528 JPanel mgindexes_panel = new JPanel();
529 card_layout = new CardLayout();
530
531 // MG Index Controls
532
533 JPanel mg_indexes_pane = new JPanel();
534
535 JPanel instruction_pane = new JPanel();
536 JLabel title_label = new JLabel();
537 title_label.setHorizontalAlignment(JLabel.CENTER);
538 Dictionary.registerText(title_label, "CDM.IndexManager.Title");
539
540 instruction_textarea = new JTextArea();
541 instruction_textarea.setEditable(false);
542 instruction_textarea.setLineWrap(true);
543 instruction_textarea.setRows(6);
544 instruction_textarea.setWrapStyleWord(true);
545 Dictionary.registerText(instruction_textarea, "CDM.IndexManager.Instructions");
546
547 JPanel assigned_indexes_pane = new JPanel();
548 JLabel index_label = new JLabel();
549 Dictionary.registerText(index_label, "CDM.IndexManager.Indexes");
550 index_list = new JList(model);
551 index_list.setCellRenderer(new IndexListRenderer());
552
553 JPanel movement_pane = new JPanel();
554
555 move_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
556 move_up_button.setEnabled(false);
557 move_up_button.setMnemonic(KeyEvent.VK_U);
558 //move_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
559 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
560
561 move_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
562 move_down_button.setEnabled(false);
563 move_down_button.setMnemonic(KeyEvent.VK_D);
564 //move_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
565 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
566
567 JPanel index_pane = new JPanel();
568
569 JPanel details_pane = new JPanel();
570
571 JPanel name_pane = new JPanel();
572 JLabel name_label = new JLabel();
573 name_label.setPreferredSize(Utility.LABEL_SIZE);
574 Dictionary.registerText(name_label, "CDM.IndexManager.Index_Name");
575 name_textfield = new JTextField();
576 Dictionary.registerTooltip(name_textfield, "CDM.IndexManager.Index_Name_Tooltip");
577
578 JPanel source_pane = new JPanel();
579 JLabel source_label = new JLabel();
580 source_label.setPreferredSize(Utility.LABEL_SIZE);
581 Dictionary.registerText(source_label, "CDM.IndexManager.Source");
582 source_list = new CheckList(new_data, false);
583 Dictionary.registerTooltip(source_list, "CDM.IndexManager.Source_Tooltip");
584
585 JPanel level_pane = new JPanel();
586 JLabel level_label = new JLabel();
587 level_label.setPreferredSize(Utility.LABEL_SIZE);
588 Dictionary.registerText(level_label, "CDM.IndexManager.Level");
589 level_combobox = new JComboBox();
590 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
591 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
592 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
593 level_combobox.setEditable(false);
594 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
595
596 JPanel button_pane = new JPanel();
597 add_button = new GLIButton();
598 add_button.setEnabled(false);
599 add_button.setMnemonic(KeyEvent.VK_A);
600 Dictionary.registerBoth(add_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
601 set_default_button = new GLIButton();
602 set_default_button.setEnabled(false);
603 set_default_button.setMnemonic(KeyEvent.VK_S);
604 Dictionary.registerBoth(set_default_button, "CDM.IndexManager.Set_Default", "CDM.IndexManager.Set_Default_Tooltip");
605 remove_button = new GLIButton();
606 remove_button.setEnabled(false);
607 remove_button.setMnemonic(KeyEvent.VK_R);
608 Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
609
610 // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class.
611 mgppindexes_control = new MGPPControl();
612
613 // Listeners
614 add_button.addActionListener(new AddListener());
615 move_down_button.addActionListener(new MoveListener(false));
616 move_up_button.addActionListener(new MoveListener(true));
617 remove_button.addActionListener(new RemoveListener());
618 set_default_button.addActionListener(new SetDefaultListener());
619 name_textfield.getDocument().addDocumentListener(new NameListener());
620 index_list.addListSelectionListener(new IndexListListener());
621 source_list.addListSelectionListener(new SourceListListener());
622 ExclusiveListSelectionListener elsl = new ExclusiveListSelectionListener();
623 elsl.add(index_list);
624 elsl.add(source_list);
625 elsl = null; // We don't want to hold a reference to this
626
627 // Layout
628 instruction_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
629 instruction_pane.setLayout(new BorderLayout());
630 instruction_pane.add(title_label, BorderLayout.NORTH);
631 instruction_pane.add(new JScrollPane(instruction_textarea), BorderLayout.CENTER);
632
633 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
634 movement_pane.setLayout(new GridLayout(2,1));
635 movement_pane.add(move_up_button);
636 movement_pane.add(move_down_button);
637
638 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
639 assigned_indexes_pane.setLayout(new BorderLayout());
640 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
641 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
642 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
643
644 name_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
645 name_pane.setLayout(new BorderLayout());
646 name_pane.add(name_label, BorderLayout.WEST);
647 name_pane.add(name_textfield, BorderLayout.CENTER);
648
649 source_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
650 source_pane.setLayout(new BorderLayout());
651 source_pane.add(source_label, BorderLayout.WEST);
652 source_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
653
654 level_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
655 level_pane.setLayout(new BorderLayout());
656 level_pane.add(level_label, BorderLayout.WEST);
657 level_pane.add(level_combobox, BorderLayout.CENTER);
658
659 details_pane.setLayout(new BorderLayout());
660 details_pane.add(name_pane, BorderLayout.NORTH);
661 details_pane.add(source_pane, BorderLayout.CENTER);
662 details_pane.add(level_pane, BorderLayout.SOUTH);
663
664 button_pane.setLayout(new GridLayout(1,3));
665 button_pane.add(add_button);
666 button_pane.add(set_default_button);
667 button_pane.add(remove_button);
668
669 index_pane.setLayout(new BorderLayout());
670 index_pane.add(details_pane, BorderLayout.CENTER);
671 index_pane.add(button_pane, BorderLayout.SOUTH);
672
673 mgindexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
674 mgindexes_panel.setLayout(new BorderLayout());
675 mgindexes_panel.add(instruction_pane, BorderLayout.NORTH);
676 mgindexes_panel.add(assigned_indexes_pane, BorderLayout.CENTER);
677 mgindexes_panel.add(index_pane, BorderLayout.SOUTH);
678
679 setLayout(card_layout);
680 add(mgindexes_panel, MGINDEXES);
681 add(mgppindexes_control, MGPPINDEXES);
682 }
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() + " " + Dictionary.get("CDM.IndexManager.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_all_button;
900 private JButton add_level_button;
901 private JButton move_index_down_button;
902 private JButton move_level_down_button;
903 private JButton move_index_up_button;
904 private JButton move_level_up_button;
905 private JButton replace_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();
933 Dictionary.registerText(current_indexes_label, "CDM.IndexManager.MGPP.Current_Indexes");
934 current_indexes_list = new JList(model);
935 current_indexes_list.setVisibleRowCount(5);
936
937 JPanel index_movement_panel = new JPanel();
938
939 move_index_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
940 move_index_up_button.setEnabled(false);
941 move_index_up_button.setMnemonic(KeyEvent.VK_U);
942 //move_index_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
943 Dictionary.registerBoth(move_index_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
944
945 move_index_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
946 move_index_down_button.setEnabled(false);
947 move_index_down_button.setMnemonic(KeyEvent.VK_D);
948 //move_index_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
949 Dictionary.registerBoth(move_index_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
950
951 JPanel index_spacer_panel = new JPanel();
952
953 JPanel index_body_panel = new JPanel();
954
955 JPanel index_details_panel = new JPanel();
956
957 JPanel index_name_panel = new JPanel();
958
959 index_name_label = new JLabel();
960 index_name_label.setPreferredSize(LABEL_SIZE);
961 Dictionary.registerText(index_name_label, "CDM.IndexManager.Index_Name");
962
963 index_name_field = new JTextField();
964 Dictionary.registerTooltip(index_name_field, "CDM.IndexManager.Index_Name_Tooltip");
965
966 JPanel index_panel = new JPanel();
967
968 index_label = new JLabel("CDM.IndexManager.MGPP.Index");
969 index_label.setPreferredSize(LABEL_SIZE);
970 Dictionary.registerText(index_label, "CDM.IndexManager.MGPP.Index");
971
972 index_combobox = new GComboBox();
973 index_combobox.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.editable_background", false));
974 index_combobox.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
975 index_combobox.setEditable(true);
976 index_combobox.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
977 index_combobox.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
978 Dictionary.registerTooltip(index_combobox, "CDM.IndexManager.MGPP.Index_Tooltip");
979
980 JPanel index_button_panel = new JPanel();
981
982 add_index_button = new GLIButton();
983 add_index_button.setEnabled(false);
984 add_index_button.setMnemonic(KeyEvent.VK_A);
985 Dictionary.registerBoth(add_index_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
986
987 add_all_button = new GLIButton();
988 add_all_button.setEnabled(true);
989 add_all_button.setMnemonic(KeyEvent.VK_L);
990 Dictionary.registerBoth(add_all_button, "CDM.IndexManager.MGPP.Add_All_Metadata", "CDM.IndexManager.MGPP.Add_All_Metadata_Tooltip");
991
992 replace_button = new GLIButton();
993 replace_button.setEnabled(false);
994 replace_button.setMnemonic(KeyEvent.VK_C);
995 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
996
997 remove_index_button = new GLIButton();
998 remove_index_button.setEnabled(false);
999 remove_index_button.setMnemonic(KeyEvent.VK_A);
1000 Dictionary.registerBoth(remove_index_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
1001
1002 JPanel index_empty_panel = new JPanel();
1003
1004 // Connect Indexes
1005 EnableAddIndexListener index_eal = new EnableAddIndexListener();
1006 add_index_button.addActionListener(new AddIndexActionListener());
1007 add_all_button.addActionListener(new AddAllActionListener());
1008 current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener());
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 replace_button.addActionListener(new ReplaceIndexActionListener());
1015 remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal));
1016
1017 // Layout Indexes
1018 index_movement_panel.setLayout(new GridLayout(2,1));
1019 index_movement_panel.add(move_index_up_button);
1020 index_movement_panel.add(move_index_down_button);
1021
1022 current_indexes_panel.setLayout(new BorderLayout());
1023 current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH);
1024 current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER);
1025 current_indexes_panel.add(index_movement_panel, BorderLayout.EAST);
1026
1027 index_name_panel.setLayout(new BorderLayout());
1028 index_name_panel.add(index_name_label, BorderLayout.WEST);
1029 index_name_panel.add(index_name_field, BorderLayout.CENTER);
1030
1031 index_panel.setLayout(new BorderLayout());
1032 index_panel.add(index_label, BorderLayout.WEST);
1033 index_panel.add(index_combobox, BorderLayout.CENTER);
1034
1035 index_details_panel.setLayout(new GridLayout(2,1,0,5));
1036 index_details_panel.add(index_name_panel);
1037 index_details_panel.add(index_panel);
1038
1039 index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1040 index_button_panel.setLayout(new GridLayout(2,2,5,0));
1041 index_button_panel.add(add_index_button);
1042 index_button_panel.add(add_all_button);
1043 index_button_panel.add(replace_button);
1044 index_button_panel.add(remove_index_button);
1045
1046 index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1047 index_body_panel.setLayout(new BorderLayout());
1048 index_body_panel.add(index_details_panel, BorderLayout.CENTER);
1049 index_body_panel.add(index_button_panel, BorderLayout.SOUTH);
1050
1051 index_spacer_panel.setLayout(new BorderLayout());
1052 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH);
1053 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER);
1054
1055 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1056 indexes_panel.setLayout(new BorderLayout());
1057 indexes_panel.add(current_indexes_panel, BorderLayout.NORTH);
1058 indexes_panel.add(index_spacer_panel, BorderLayout.CENTER);
1059
1060 // Create Levels
1061 JPanel levels_panel = new JPanel();
1062
1063 JPanel current_levels_panel = new JPanel();
1064
1065 current_levels_label = new JLabel();
1066 Dictionary.registerText(current_levels_label, "CDM.IndexManager.MGPP.Current_Levels");
1067
1068 current_levels_list = new JList(levels_model);
1069 current_levels_list.setVisibleRowCount(5);
1070
1071 JPanel level_movement_panel = new JPanel();
1072
1073 move_level_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
1074 move_level_up_button.setEnabled(false);
1075 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1076 //move_level_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1077 Dictionary.registerBoth(move_level_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1078
1079 move_level_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1080 move_level_down_button.setEnabled(false);
1081 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1082 //move_level_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1083 Dictionary.registerBoth(move_level_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1084
1085 JPanel level_spacer_panel = new JPanel();
1086
1087 JPanel level_body_panel = new JPanel();
1088
1089 JPanel level_details_panel = new JPanel();
1090
1091 JPanel level_name_panel = new JPanel();
1092
1093 level_name_label = new JLabel();
1094 level_name_label.setPreferredSize(LABEL_SIZE);
1095 Dictionary.registerText(level_name_label, "CDM.IndexManager.MGPP.Level_Name");
1096
1097 level_name_field = new JTextField();
1098 Dictionary.registerTooltip(level_name_field, "CDM.IndexManager.MGPP.Level_Name_Tooltip");
1099
1100 JPanel level_panel = new JPanel();
1101
1102 level_label = new JLabel();
1103 level_label.setPreferredSize(LABEL_SIZE);
1104 Dictionary.registerText(level_label, "CDM.IndexManager.MGPP.Level");
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 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
1113
1114 JPanel level_button_panel = new JPanel();
1115
1116 add_level_button = new GLIButton("CDM.IndexManager.MGPP.Add_Level");
1117 add_level_button.setEnabled(false);
1118 add_level_button.setMnemonic(KeyEvent.VK_A);
1119 Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip");
1120
1121 remove_level_button = new GLIButton("CDM.IndexManager.MGPP.Remove_Level");
1122 remove_level_button.setEnabled(false);
1123 remove_level_button.setMnemonic(KeyEvent.VK_A);
1124 Dictionary.registerBoth(remove_level_button, "CDM.IndexManager.MGPP.Remove_Level", "CDM.IndexManager.MGPP.Remove_Level_Tooltip");
1125
1126 JPanel level_empty_panel = new JPanel();
1127
1128 // Connect Levels
1129 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1130 add_level_button.addActionListener(new AddLevelActionListener());
1131 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1132 level_combobox.addActionListener(level_eal);
1133 ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1134 level_name_field.getDocument().addDocumentListener(level_eal);
1135 move_level_down_button.addActionListener(new MoveLevelDownListener());
1136 move_level_up_button.addActionListener(new MoveLevelUpListener());
1137 remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal));
1138 // Layout Levels
1139
1140 level_movement_panel.setLayout(new GridLayout(2,1));
1141 level_movement_panel.add(move_level_up_button);
1142 level_movement_panel.add(move_level_down_button);
1143
1144 current_levels_panel.setLayout(new BorderLayout());
1145 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1146 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1147 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1148
1149 level_name_panel.setLayout(new BorderLayout());
1150 level_name_panel.add(level_name_label, BorderLayout.WEST);
1151 level_name_panel.add(level_name_field, BorderLayout.CENTER);
1152
1153 level_panel.setLayout(new BorderLayout());
1154 level_panel.add(level_label, BorderLayout.WEST);
1155 level_panel.add(level_combobox, BorderLayout.CENTER);
1156
1157 level_details_panel.setLayout(new GridLayout(2,1,0,5));
1158 level_details_panel.add(level_name_panel);
1159 level_details_panel.add(level_panel);
1160
1161 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1162 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1163 level_button_panel.add(add_level_button);
1164 level_button_panel.add(remove_level_button);
1165
1166 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1167 level_body_panel.setLayout(new BorderLayout());
1168 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1169 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1170
1171 level_spacer_panel.setLayout(new BorderLayout());
1172 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1173 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1174
1175 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1176 levels_panel.setLayout(new BorderLayout());
1177 levels_panel.add(current_levels_panel, BorderLayout.NORTH);
1178 levels_panel.add(level_spacer_panel, BorderLayout.CENTER);
1179
1180 // Create General
1181 JPanel header_panel = new JPanel();
1182
1183 title_label = new JLabel();
1184 title_label.setHorizontalAlignment(JLabel.CENTER);
1185 Dictionary.registerText(title_label, "CDM.IndexManager.MGPP.Title");
1186
1187 instructions_textarea = new JTextArea();
1188 instructions_textarea.setEditable(false);
1189 instructions_textarea.setLineWrap(true);
1190 instructions_textarea.setRows(6);
1191 instructions_textarea.setWrapStyleWord(true);
1192 Dictionary.registerText(instructions_textarea, "CDM.IndexManager.MGPP.Instructions");
1193
1194 tabbed_pane = new JTabbedPane();
1195
1196 // Layout General
1197 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
1198 header_panel.setLayout(new BorderLayout());
1199 header_panel.add(title_label, BorderLayout.NORTH);
1200 header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
1201
1202 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Indexes"), indexes_panel);
1203 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Levels"), levels_panel);
1204
1205 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1206 setLayout(new BorderLayout());
1207 add(header_panel, BorderLayout.NORTH);
1208 add(tabbed_pane, BorderLayout.CENTER);
1209 }
1210
1211 public void destroy() {
1212 }
1213
1214 public void gainFocus() {
1215 // Reload the assigned indexes list.
1216 index_combobox.removeAllItems();
1217 index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR);
1218 index_combobox.addItem(CollectionConfiguration.METADATA_STR);
1219 index_combobox.addItem(CollectionConfiguration.TEXT_STR);
1220 java.util.List assigned_elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
1221 int assigned_elements_size = assigned_elements.size();
1222 for(int i = 0; i < assigned_elements_size; i++) {
1223 index_combobox.addItem(assigned_elements.get(i));
1224 }
1225 assigned_elements = null;
1226 // Ensure the level manager has at least documents assigned
1227 if(levels_model.getSize() == 0) {
1228 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1229 // Create new metadatum
1230 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1231 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1232 // Assign new level
1233 addLevel(level, metadatum);
1234 level = null;
1235 }
1236 }
1237
1238 private class AddIndexActionListener
1239 implements ActionListener {
1240 public void actionPerformed(ActionEvent event) {
1241 // Retrieve the name
1242 String name = index_name_field.getText();
1243 // Retrieve the source
1244 Object source = index_combobox.getSelectedItem();
1245 // If this object isn't yet in the combobox add it.
1246 if(index_combobox.getSelectedIndex() == -1) {
1247 index_combobox.insertItemAt(source, index_combobox.getItemCount());
1248 }
1249 // Create new index
1250 ArrayList sources = new ArrayList();
1251 sources.add(source);
1252 Index index = new Index(sources);
1253 if(!model.contains(index)) {
1254 // Create new metadatum
1255 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1256 metadatum.setValue(name);
1257 // Assign new index
1258 addIndex(index, metadatum);
1259 }
1260 // Done. Disable add
1261 add_index_button.setEnabled(false);
1262 }
1263 }
1264
1265 private class AddAllActionListener
1266 implements ActionListener {
1267
1268 public void actionPerformed(ActionEvent event) {
1269 for(int i = 0; i < index_combobox.getItemCount(); i++) {
1270 Object source = index_combobox.getItemAt(i);
1271 // Create new index
1272 ArrayList sources = new ArrayList();
1273 sources.add(source);
1274 Index index = new Index(sources);
1275 sources = null;
1276 if(!model.contains(index)) {
1277 // Determine the metadatum value
1278 String name = source.toString();
1279 if(name.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE)) {
1280 name = name.substring(Utility.EXTRACTED_METADATA_NAMESPACE.length() + 1);
1281 }
1282 // Create new metadatum
1283 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1284 metadatum.setValue(name);
1285 name = null;
1286 // Assign new index
1287 addIndex(index, metadatum);
1288 }
1289 source = null;
1290 index = null;
1291 }
1292 // Done. Disable add
1293 add_index_button.setEnabled(false);
1294 }
1295 }
1296
1297 private class AddLevelActionListener
1298 implements ActionListener {
1299 public void actionPerformed(ActionEvent event) {
1300 // Retrieve the name
1301 String name = level_name_field.getText();
1302 // Retrieve the source
1303 String source = (String)level_combobox.getSelectedItem();
1304 // If this object isn't yet in the combobox add it.
1305 if(level_combobox.getSelectedIndex() == -1) {
1306 level_combobox.insertItemAt(source, level_combobox.getItemCount());
1307 }
1308 Level level = new Level(source);
1309 // Create new metadatum
1310 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1311 metadatum.setValue(name);
1312 // Assign new level
1313 addLevel(level, metadatum);
1314 // Done. Disable add
1315 add_level_button.setEnabled(false);
1316 }
1317 }
1318
1319 private class CurrentIndexesListSelectionListener
1320 implements ListSelectionListener {
1321 public void valueChanged(ListSelectionEvent event) {
1322 if(!event.getValueIsAdjusting()) {
1323 Index index = (Index)current_indexes_list.getSelectedValue();
1324 Object[] selected_objects = current_indexes_list.getSelectedValues();
1325 if(selected_objects.length == 1) {
1326 String full_text = index.toString();
1327 if(full_text.indexOf("\"") != -1) {
1328 index_name_field.setText(index.getName());
1329 }
1330 ArrayList sources = index.getSources();
1331 index_combobox.setSelectedItem(sources.get(0));
1332 }
1333 if(index != null) {
1334 move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1));
1335 move_index_up_button.setEnabled((model.indexOf(index) > 0));
1336 remove_index_button.setEnabled(true);
1337 }
1338 else {
1339 move_index_down_button.setEnabled(false);
1340 move_index_up_button.setEnabled(false);
1341 remove_index_button.setEnabled(false);
1342 }
1343 }
1344 }
1345 }
1346
1347 private class CurrentLevelsListSelectionListener
1348 implements ListSelectionListener {
1349 public void valueChanged(ListSelectionEvent event) {
1350 if(!event.getValueIsAdjusting()) {
1351 Level level = (Level)current_levels_list.getSelectedValue();
1352 if(level != null) {
1353 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1354 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1355 remove_level_button.setEnabled(!level.getName().equals(CollectionConfiguration.DOCUMENT_STR) || levels_model.getSize() > 1);
1356 }
1357 else {
1358 move_level_down_button.setEnabled(false);
1359 move_level_up_button.setEnabled(false);
1360 remove_level_button.setEnabled(false);
1361 }
1362 }
1363 }
1364 }
1365
1366 private class EnableAddIndexListener
1367 implements ActionListener, DocumentListener {
1368 /** Called whenever a selection action occurs on the combobox.
1369 * @param event an ActionEvent containing information about the selection event
1370 */
1371 public void actionPerformed(ActionEvent event) {
1372 validateAddButton();
1373 }
1374
1375 /** Gives notification that an attribute or set of attributes changed.
1376 * @param event a DocumentEvent containing information about the text changed
1377 */
1378 public void changedUpdate(DocumentEvent event) {
1379 validateAddButton();
1380 }
1381
1382 /** Gives notification that there was an insert into the document.
1383 * @param event a DocumentEvent containing information about the text added
1384 */
1385 public void insertUpdate(DocumentEvent event) {
1386 validateAddButton();
1387 }
1388
1389 /** Gives notification that a portion of the document has been removed.
1390 * @param event a DocumentEvent containing information about the text removed
1391 */
1392 public void removeUpdate(DocumentEvent event) {
1393 validateAddButton();
1394 }
1395
1396 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1397 public void validateAddButton() {
1398 String name = index_name_field.getText();
1399 Object selected_object = index_combobox.getSelectedItem();
1400 if(name.length() > 0 && selected_object != null) {
1401 // Unfortunately we have to generate a valid id
1402 String id = null;
1403 if(selected_object instanceof ElementWrapper) {
1404 id = ((ElementWrapper)selected_object).getName();
1405 }
1406 else {
1407 id = selected_object.toString();
1408 }
1409 if(id.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE)) {
1410 id = id.substring(Utility.EXTRACTED_METADATA_NAMESPACE.length() + 1);
1411 }
1412 Index index = getIndex(id);
1413 if(index == null) {
1414 add_index_button.setEnabled(true);
1415 replace_button.setEnabled(false);
1416 }
1417 else {
1418 add_index_button.setEnabled(false);
1419 replace_button.setEnabled(!name.equals(index.getName()));
1420 }
1421 }
1422 else {
1423 add_index_button.setEnabled(false);
1424 }
1425 }
1426 }
1427
1428 private class EnableAddLevelListener
1429 implements ActionListener, DocumentListener {
1430 /** Called whenever a selection action occurs on the combobox.
1431 * @param event an ActionEvent containing information about the selection event
1432 */
1433 public void actionPerformed(ActionEvent event) {
1434 validateAddButton();
1435 }
1436
1437 /** Gives notification that an attribute or set of attributes changed.
1438 * @param event a DocumentEvent containing information about the text changed
1439 */
1440 public void changedUpdate(DocumentEvent event) {
1441 validateAddButton();
1442 }
1443
1444 /** Gives notification that there was an insert into the document.
1445 * @param event a DocumentEvent containing information about the text added
1446 */
1447 public void insertUpdate(DocumentEvent event) {
1448 validateAddButton();
1449 }
1450
1451 /** Gives notification that a portion of the document has been removed.
1452 * @param event a DocumentEvent containing information about the text removed
1453 */
1454 public void removeUpdate(DocumentEvent event) {
1455 validateAddButton();
1456 }
1457
1458 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1459 public void validateAddButton() {
1460 String name = level_name_field.getText();
1461 Object selected_object = level_combobox.getSelectedItem();
1462 if(name.length() > 0 && selected_object != null) {
1463 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1464 }
1465 else {
1466 add_level_button.setEnabled(false);
1467 }
1468 }
1469 }
1470
1471 private class MoveIndexDownListener
1472 implements ActionListener {
1473 public void actionPerformed(ActionEvent event) {
1474 // Retrieve the first selected item
1475 Index index = (Index) current_indexes_list.getSelectedValue();
1476 moveIndex(index, false);
1477 current_indexes_list.setSelectedValue(index, true);
1478 }
1479 }
1480
1481 private class MoveLevelDownListener
1482 implements ActionListener {
1483 public void actionPerformed(ActionEvent event) {
1484 // Retrieve the first selected item
1485 Level level = (Level) current_levels_list.getSelectedValue();
1486 moveLevel(level, false);
1487 current_levels_list.setSelectedValue(level, true);
1488 }
1489 }
1490
1491 private class MoveIndexUpListener
1492 implements ActionListener {
1493 public void actionPerformed(ActionEvent event) {
1494 // Retrieve the first selected item
1495 Index index = (Index) current_indexes_list.getSelectedValue();
1496 moveIndex(index, true);
1497 current_indexes_list.setSelectedValue(index, true);
1498 }
1499 }
1500
1501 private class MoveLevelUpListener
1502 implements ActionListener {
1503 public void actionPerformed(ActionEvent event) {
1504 // Retrieve the first selected item
1505 Level level = (Level) current_levels_list.getSelectedValue();
1506 moveLevel(level, true);
1507 current_levels_list.setSelectedValue(level, true);
1508 }
1509 }
1510
1511 /** Replace really only replaces the string. */
1512 private class ReplaceIndexActionListener
1513 implements ActionListener {
1514
1515 public void actionPerformed(ActionEvent event) {
1516 Object[] selected_objects = current_indexes_list.getSelectedValues();
1517 if(selected_objects.length == 1) {
1518 Index index = (Index) selected_objects[0];
1519 // Remove old name
1520 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1521 // Enter new name
1522 String name = index_name_field.getText();
1523 // Create new metadatum
1524 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1525 index = null;
1526 metadatum.setValue(name);
1527 name = null;
1528 // Assign new index
1529 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
1530 metadatum = null;
1531 }
1532 current_indexes_list.setSelectedValue(selected_objects[0], true);
1533 // Done. Disable add
1534 add_index_button.setEnabled(false);
1535 replace_button.setEnabled(false);
1536 }
1537 }
1538
1539 private class RemoveIndexActionListener
1540 implements ActionListener {
1541
1542 private EnableAddIndexListener eal = null;
1543
1544 public RemoveIndexActionListener(EnableAddIndexListener eal) {
1545 this.eal = eal;
1546 }
1547
1548 public void actionPerformed(ActionEvent event) {
1549 // Retrieve the selected items
1550 Object[] selected_objects = current_indexes_list.getSelectedValues();
1551 // Clear selection
1552 current_indexes_list.clearSelection();
1553 for(int i = 0; i < selected_objects.length; i++) {
1554 Index index = (Index) selected_objects[i];
1555 // Remove any related metadata
1556 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1557 // Remove the index
1558 removeIndex(index);
1559 }
1560 // Disable remove button
1561 remove_index_button.setEnabled(false);
1562 // Check if add should be reenabled
1563 eal.validateAddButton();
1564 }
1565 }
1566
1567 private class RemoveLevelActionListener
1568 implements ActionListener {
1569
1570 private EnableAddLevelListener eal = null;
1571
1572 public RemoveLevelActionListener(EnableAddLevelListener eal) {
1573 this.eal = eal;
1574 }
1575
1576 public void actionPerformed(ActionEvent event) {
1577 // Retrieve the selected items
1578 Object[] selected_objects = current_levels_list.getSelectedValues();
1579 // Clear selection
1580 current_levels_list.clearSelection();
1581 for(int i = 0; i < selected_objects.length; i++) {
1582 Level level = (Level) selected_objects[i];
1583 // Remove any related metadata
1584 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
1585 // Remove the index
1586 removeLevel(level);
1587 }
1588 // Disable remove button
1589 remove_level_button.setEnabled(false);
1590 // If there are no levels left, put document back in
1591 if(levels_model.getSize() == 0) {
1592 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1593 // Create new metadatum
1594 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1595 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1596 // Assign new level
1597 addLevel(level, metadatum);
1598 level = null;
1599 }
1600 // Check if add should be reenabled
1601 eal.validateAddButton();
1602 }
1603 }
1604 }
1605}
Note: See TracBrowser for help on using the repository browser.