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

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

unfixed the size of labels so that other langs fit in

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