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

Last change on this file since 9126 was 9077, checked in by mdewsnip, 19 years ago

Removed unused Coloring class.

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