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

Last change on this file since 8853 was 8802, checked in by mdewsnip, 19 years ago

Fixed up some button sizes on the Search Indexes page and removed some dead constants.

  • Property svn:keywords set to Author Date Id Revision
File size: 66.3 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.checklist.CheckList;
39import org.greenstone.gatherer.gui.GComboBox;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.metadata.MetadataElement;
42import org.greenstone.gatherer.metadata.MetadataSetManager;
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 * @see org.greenstone.gatherer.gui.Coloring
516 */
517 public IndexControl() {
518 super();
519
520 ArrayList new_data = new ArrayList();
521 new_data.add(CollectionConfiguration.TEXT_STR);
522 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
523 // Creation
524 JPanel mgindexes_panel = new JPanel();
525 card_layout = new CardLayout();
526
527 // MG Index Controls
528
529 JPanel mg_indexes_pane = new JPanel();
530
531 JPanel instruction_pane = new JPanel();
532 JLabel title_label = new JLabel();
533 title_label.setHorizontalAlignment(JLabel.CENTER);
534 Dictionary.registerText(title_label, "CDM.IndexManager.Title");
535
536 instruction_textarea = new JTextArea();
537 instruction_textarea.setEditable(false);
538 instruction_textarea.setLineWrap(true);
539 instruction_textarea.setRows(6);
540 instruction_textarea.setWrapStyleWord(true);
541 Dictionary.registerText(instruction_textarea, "CDM.IndexManager.Instructions");
542
543 JPanel assigned_indexes_pane = new JPanel();
544 JLabel index_label = new JLabel();
545 Dictionary.registerText(index_label, "CDM.IndexManager.Indexes");
546 index_list = new JList(model);
547 index_list.setCellRenderer(new IndexListRenderer());
548 index_list.setVisibleRowCount(2);
549 JPanel movement_pane = new JPanel();
550
551 move_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
552 move_up_button.setEnabled(false);
553 move_up_button.setMnemonic(KeyEvent.VK_U);
554 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
555
556 move_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
557 move_down_button.setEnabled(false);
558 move_down_button.setMnemonic(KeyEvent.VK_D);
559 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
560
561 set_default_button = new GLIButton();
562 set_default_button.setEnabled(false);
563 set_default_button.setMnemonic(KeyEvent.VK_S);
564 Dictionary.registerBoth(set_default_button, "CDM.IndexManager.Set_Default", "CDM.IndexManager.Set_Default_Tooltip");
565
566 JPanel index_pane = new JPanel();
567 JPanel details_pane = new JPanel();
568 JPanel labels_pane = new JPanel();
569 JPanel boxes_pane = new JPanel();
570 JPanel content_pane = new JPanel();
571
572 JLabel name_label = new JLabel();
573 Dictionary.registerText(name_label, "CDM.IndexManager.Index_Name");
574 name_textfield = new JTextField();
575 name_textfield.setPreferredSize(FIELD_SIZE);
576 Dictionary.registerTooltip(name_textfield, "CDM.IndexManager.Index_Name_Tooltip");
577
578 JLabel source_label = new JLabel();
579 Dictionary.registerText(source_label, "CDM.IndexManager.Source");
580 source_list = new CheckList(false);
581 source_list.setListData(new_data);
582 Dictionary.registerTooltip(source_list, "CDM.IndexManager.Source_Tooltip");
583
584 JLabel level_label = new JLabel();
585 Dictionary.registerText(level_label, "CDM.IndexManager.Level");
586 level_combobox = new JComboBox();
587 level_combobox.setPreferredSize(FIELD_SIZE);
588 level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document"));
589 level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph"));
590 level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section"));
591 level_combobox.setEditable(false);
592 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
593
594 JPanel button_pane = new JPanel();
595 add_button = new GLIButton();
596 add_button.setEnabled(false);
597 add_button.setMnemonic(KeyEvent.VK_A);
598 Dictionary.registerBoth(add_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
599
600 remove_button = new GLIButton();
601 remove_button.setEnabled(false);
602 remove_button.setMnemonic(KeyEvent.VK_R);
603 Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
604 replace_button = new GLIButton();
605 replace_button.setEnabled(false);
606 replace_button.setMnemonic(KeyEvent.VK_P);
607 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
608
609 // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class.
610 mgppindexes_control = new MGPPControl();
611
612 // Listeners
613 add_button.addActionListener(new AddListener());
614 move_down_button.addActionListener(new MoveListener(false));
615 move_up_button.addActionListener(new MoveListener(true));
616 remove_button.addActionListener(new RemoveListener());
617 replace_button.addActionListener(new ReplaceListener());
618 set_default_button.addActionListener(new SetDefaultListener());
619 name_textfield.getDocument().addDocumentListener(new NameListener());
620 level_combobox.addItemListener(new LevelListener());
621 index_list.addListSelectionListener(new IndexListListener());
622 source_list.addListSelectionListener(new SourceListListener());
623
624 // Layout
625 instruction_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
626 instruction_pane.setLayout(new BorderLayout());
627 instruction_pane.add(title_label, BorderLayout.NORTH);
628 instruction_pane.add(new JScrollPane(instruction_textarea), BorderLayout.CENTER);
629
630 movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
631 movement_pane.setLayout(new GridLayout(3,1));
632 movement_pane.add(move_up_button);
633 movement_pane.add(move_down_button);
634 movement_pane.add(set_default_button);
635
636 assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
637 assigned_indexes_pane.setLayout(new BorderLayout());
638 assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
639 assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
640 assigned_indexes_pane.add(movement_pane, BorderLayout.EAST);
641
642 labels_pane.setLayout(new BorderLayout());
643 labels_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5));
644 labels_pane.add(name_label, BorderLayout.NORTH);
645 labels_pane.add(source_label, BorderLayout.CENTER);
646 labels_pane.add(level_label, BorderLayout.SOUTH);
647
648 boxes_pane.setLayout(new BorderLayout());
649 boxes_pane.add(name_textfield, BorderLayout.NORTH);
650 boxes_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
651 boxes_pane.add(level_combobox, BorderLayout.SOUTH);
652
653 details_pane.setLayout(new BorderLayout());
654 details_pane.add(labels_pane, BorderLayout.WEST);
655 details_pane.add(boxes_pane, BorderLayout.CENTER);
656
657 button_pane.setLayout(new GridLayout(1,3));
658 button_pane.add(add_button);
659 button_pane.add(replace_button);
660 button_pane.add(remove_button);
661
662 index_pane.setLayout(new BorderLayout());
663 index_pane.add(details_pane, BorderLayout.CENTER);
664 index_pane.add(button_pane, BorderLayout.SOUTH);
665
666 content_pane.setLayout(new BorderLayout());
667 content_pane.add(assigned_indexes_pane, BorderLayout.NORTH);
668 content_pane.add(index_pane, BorderLayout.CENTER);
669
670 mgindexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
671 mgindexes_panel.setLayout(new BorderLayout());
672 mgindexes_panel.add(instruction_pane, BorderLayout.NORTH);
673 mgindexes_panel.add(content_pane, BorderLayout.CENTER);
674
675 setLayout(card_layout);
676 add(mgindexes_panel, MGINDEXES);
677 add(mgppindexes_control, MGPPINDEXES);
678 }
679
680 /* Destructor, removes persistant listeners from the Dictionary.
681 */
682 public void destroy() {
683 mgppindexes_control.destroy();
684 mgppindexes_control = null;
685 }
686
687 public void gainFocus() {
688 boolean mgpp_enabled = CollectionDesignManager.searchtype_manager.isMGPPEnabled();
689 if(instruction_textarea != null) {
690 instruction_textarea.setCaretPosition(0);
691 }
692 if(mgpp_enabled) {
693 mgppindexes_control.gainFocus();
694 }
695 else {
696 // Reload the assigned indexes list.
697 ArrayList new_data = new ArrayList();
698 new_data.add(CollectionConfiguration.TEXT_STR);
699 new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
700 // reset the model in the list and combobox
701 source_list.setListData(new_data);
702 new_data = null;
703 // refresh the indexList
704 index_list.updateUI();
705 // if there is one selected, fill in the controls
706 updateControlsWithSelectedIndex();
707 }
708 // Bring the appropriate card to the front
709 if(mgpp_enabled) {
710 card_layout.show(this, MGPPINDEXES);
711 }
712 else {
713 card_layout.show(this, MGINDEXES);
714 }
715 }
716
717 public void loseFocus() {
718 }
719
720 private void updateControlsWithSelectedIndex() {
721
722 Index selected_index = (Index)index_list.getSelectedValue();
723 if (selected_index == null) {
724 return;
725 }
726 String id = selected_index.getID();
727 if (id == null || id.equals("")) {
728 return;
729 }
730 String name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true);
731 name_textfield.setText(name);
732 level_combobox.setSelectedIndex(selected_index.getLevel());
733 source_list.clearSelection();
734 ArrayList sources = selected_index.getSources();
735 source_list.setSelectedObjects(sources.toArray());
736
737 }
738
739 private void validateAddButton() {
740 boolean add_enabled = false;
741 boolean replace_enabled = false;
742 // Indexes must have a name
743 if(name_textfield.getText().length() == 0) {
744 add_enabled = false;
745 }
746 // Can't add a new index if no sources are selected
747 else if (source_list.getSelected().size() == 0) {
748 add_enabled = false;
749 }
750 // If we get this far, create a dummy index and see if its already assigned in the collection
751 else {
752 Object object[] = source_list.getSelected().toArray();
753 ArrayList sources = new ArrayList();
754 for(int i = 0; i < object.length; i++) {
755 sources.add(object[i]);
756 }
757 object = null;
758 Index index = new Index(level_combobox.getSelectedIndex(), sources);
759 sources = null;
760 if(model.contains(index)) {
761 add_enabled = false;
762 // here we need to check if we have changed the name - if so, we can enable the replace button
763 if (index_list.getSelectedIndex() != -1) {
764 Index selected_index = (Index)index_list.getSelectedValue();
765 String name = name_textfield.getText();
766 String index_name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true);
767 if (!index_name.equals(name)){
768 replace_enabled = true;
769 }
770 }
771 }
772 else {
773 add_enabled = true;
774 // we have a new index, do we have something selected in the index list? if so, enable the replace button
775 if (index_list.getSelectedIndex() != -1) {
776 replace_enabled = true;
777 }
778
779 }
780 }
781 // We should now know the add_button state
782 add_button.setEnabled(add_enabled);
783 replace_button.setEnabled(replace_enabled);
784 }
785
786 /** 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. */
787 private class AddListener
788 implements ActionListener {
789 /** 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.
790 * @param event An <strong>ActionEvent</strong> providing extra information about the event.
791 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
792 * @see org.greenstone.gatherer.cdm.CollectionMeta
793 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
794 * @see org.greenstone.gatherer.cdm.Index
795 * @see org.greenstone.gatherer.cdm.Language
796 * @see org.greenstone.gatherer.cdm.LanguageManager
797 */
798 public void actionPerformed(ActionEvent event) {
799 String name = name_textfield.getText();
800 System.err.println("Add button height: " + add_button.getHeight());
801 System.err.println("Set default button height: " + set_default_button.getHeight());
802 if (source_list.getSelected().size() > 0 && name.length() != 0) {
803 ArrayList sources = source_list.getSelected();
804 Index index = new Index(level_combobox.getSelectedIndex(), sources);
805 sources = null;
806 // Before we add the index to the model, we have to add the collection metadata for this.
807 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
808 metadatum.setValue(name);
809 // Finally add index.
810 addIndex(index, metadatum);
811 index_list.setSelectedValue(index, true);
812 metadatum = null;
813 index = null;
814 }
815 name = null;
816 }
817 }
818
819 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
820 private class IndexListListener
821 implements ListSelectionListener {
822 /** 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
823 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
824 */
825 public void valueChanged(ListSelectionEvent event)
826 {
827 if (event.getValueIsAdjusting()) {
828 return;
829 }
830
831 Object value = index_list.getSelectedValue();
832 if (value == null) {
833 move_down_button.setEnabled(false);
834 move_up_button.setEnabled(false);
835 remove_button.setEnabled(false);
836 replace_button.setEnabled(false);
837 set_default_button.setEnabled(false);
838 return;
839 }
840 // enable the buttons appropriately
841 remove_button.setEnabled(true);
842 set_default_button.setEnabled(default_index == null || !default_index.equals(value));
843 int i = index_list.getSelectedIndex();
844 int size = index_list.getModel().getSize();
845 if (i > 0) {
846 move_up_button.setEnabled(true);
847 } else {
848 move_up_button.setEnabled(false);
849 }
850 if (i<size-1){
851 move_down_button.setEnabled(true);
852 } else {
853 move_down_button.setEnabled(false);
854 }
855
856 // need to fill in the rest of the bits
857 updateControlsWithSelectedIndex();
858 }
859 }
860
861 private class IndexListRenderer
862 extends DefaultListCellRenderer {
863
864 /** Return a component that has been configured to display the specified value. */
865 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
866 JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
867 if(default_index != null && default_index.equals(value)) {
868 component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
869 }
870 return component;
871 }
872
873 }
874
875 private class LevelListener
876 implements ItemListener {
877 public void itemStateChanged(ItemEvent event) {
878 if(event.getStateChange() == ItemEvent.SELECTED) {
879 // just want to validate the add button
880 validateAddButton();
881
882 }
883 }
884 }
885
886 private class MoveListener
887 implements ActionListener {
888
889 private boolean move_up;
890
891 public MoveListener(boolean move_up) {
892 this.move_up = move_up;
893 }
894
895 public void actionPerformed(ActionEvent event) {
896 // Retrieve the selected index
897 Index index = (Index) index_list.getSelectedValue();
898 if(index != null) {
899 moveIndex(index, move_up);
900 // Ensure the index that moved is still selected
901 index_list.setSelectedValue(index, true);
902 index = null;
903 }
904 }
905 }
906
907 /** 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. */
908 private class RemoveListener
909 implements ActionListener {
910 /** If called when an action occurs on a registered component, we remove the currently selected index, if there is one.
911 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
912 * @see org.greenstone.gatherer.cdm.Index
913 */
914 public void actionPerformed(ActionEvent event) {
915 int i = index_list.getSelectedIndex();
916 if(i != -1) {
917 removeIndex((Index)index_list.getSelectedValue());
918 }
919 int size = index_list.getModel().getSize();
920 if (i == size) {
921 i--;
922 }
923
924 index_list.setSelectedIndex(i);
925 // this will produce an event on the list, updating the other buttons
926 if (size == 0) {
927 // we have removed the last index, should be able to add whats filled in currently, if valid
928 validateAddButton();
929 }
930 }
931 }
932
933 /** 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. */
934 private class ReplaceListener
935 implements ActionListener {
936 /** If called when an action occurs on a registered component, we replace the currently selected index, with the new details
937 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
938 * @see org.greenstone.gatherer.cdm.Index
939 */
940 public void actionPerformed(ActionEvent event) {
941 if(index_list.isSelectionEmpty()) {
942 // this should never happen, but just in case..
943 replace_button.setEnabled(false);
944 return;
945 }
946 // we'll just remove the old one and add the new one
947 removeIndex((Index)index_list.getSelectedValue());
948 replace_button.setEnabled(false);
949 add_button.setEnabled(true);
950 add_button.doClick();
951 add_button.setEnabled(false);
952 }
953 }
954
955 private class SetDefaultListener
956 implements ActionListener {
957
958 public void actionPerformed(ActionEvent event) {
959 Index index = (Index) index_list.getSelectedValue();
960 if(index != null) {
961 setDefault(index);
962 // This should cause a repaint of just the desired row
963 index_list.setSelectedValue(index, true);
964 }
965 set_default_button.setEnabled(false);
966 }
967 }
968
969 /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */
970 private class SourceListListener
971 implements ListSelectionListener {
972 /** 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.
973 * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
974 */
975 public void valueChanged(ListSelectionEvent event) {
976 validateAddButton();
977 }
978 }
979
980 /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */
981 private class NameListener
982 implements DocumentListener {
983 /** Gives notification that an attribute or set of attributes changed. */
984 public void changedUpdate(DocumentEvent e) {
985 validateAddButton();
986 }
987
988 /** Gives notification that there was an insert into the document. */
989 public void insertUpdate(DocumentEvent e) {
990 validateAddButton();
991 }
992
993 /** Gives notification that a portion of the document has been removed. */
994 public void removeUpdate(DocumentEvent e) {
995 validateAddButton();
996 }
997 }
998 }
999
1000 private class MGPPControl
1001 extends JPanel {
1002
1003 private GComboBox index_combobox;
1004 private GComboBox level_combobox;
1005 private JButton add_index_button;
1006 private JButton add_all_button;
1007 private JButton add_level_button;
1008 private JButton move_index_down_button;
1009 private JButton move_level_down_button;
1010 private JButton move_index_up_button;
1011 private JButton move_level_up_button;
1012 private JButton replace_button;
1013 private JButton remove_index_button;
1014 private JButton remove_level_button;
1015 private JLabel current_levels_label;
1016 private JLabel current_indexes_label;
1017 private JLabel index_label;
1018 private JLabel index_name_label;
1019 private JLabel level_label;
1020 private JLabel level_name_label;
1021 private JLabel move_index_down_label;
1022 private JLabel move_level_down_label;
1023 private JLabel move_index_up_label;
1024 private JLabel move_level_up_label;
1025 private JLabel title_label;
1026 private JList current_levels_list;
1027 private JList current_indexes_list;
1028 private JTabbedPane tabbed_pane;
1029 private JTextArea instructions_textarea;
1030 private JTextField index_name_field;
1031 private JTextField level_name_field;
1032
1033 public MGPPControl() {
1034 // Create Indexes
1035 JPanel indexes_panel = new JPanel();
1036
1037 JPanel current_indexes_panel = new JPanel();
1038
1039 current_indexes_label = new JLabel();
1040 Dictionary.registerText(current_indexes_label, "CDM.IndexManager.MGPP.Current_Indexes");
1041 current_indexes_list = new JList(model);
1042 current_indexes_list.setVisibleRowCount(5);
1043
1044 JPanel index_movement_panel = new JPanel();
1045
1046 move_index_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
1047 move_index_up_button.setEnabled(false);
1048 move_index_up_button.setMnemonic(KeyEvent.VK_U);
1049 //move_index_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1050 Dictionary.registerBoth(move_index_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1051
1052 move_index_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1053 move_index_down_button.setEnabled(false);
1054 move_index_down_button.setMnemonic(KeyEvent.VK_D);
1055 //move_index_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1056 Dictionary.registerBoth(move_index_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1057
1058 JPanel index_spacer_panel = new JPanel();
1059 JPanel index_body_panel = new JPanel();
1060 JPanel index_details_panel = new JPanel();
1061 JPanel index_labels_panel = new JPanel();
1062
1063 index_name_label = new JLabel();
1064 Dictionary.registerText(index_name_label, "CDM.IndexManager.Index_Name");
1065
1066 index_name_field = new JTextField();
1067 index_name_field.setPreferredSize(FIELD_SIZE);
1068 Dictionary.registerTooltip(index_name_field, "CDM.IndexManager.Index_Name_Tooltip");
1069
1070 JPanel index_boxes_panel = new JPanel();
1071
1072 index_label = new JLabel("CDM.IndexManager.MGPP.Index");
1073 Dictionary.registerText(index_label, "CDM.IndexManager.MGPP.Index");
1074
1075 index_combobox = new GComboBox();
1076 index_combobox.setPreferredSize(FIELD_SIZE);
1077 index_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1078 index_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1079 index_combobox.setEditable(true);
1080 index_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1081 index_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1082 Dictionary.registerTooltip(index_combobox, "CDM.IndexManager.MGPP.Index_Tooltip");
1083
1084 JPanel index_button_panel = new JPanel();
1085
1086 add_index_button = new GLIButton();
1087 add_index_button.setEnabled(false);
1088 add_index_button.setMnemonic(KeyEvent.VK_A);
1089 Dictionary.registerBoth(add_index_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip");
1090
1091 add_all_button = new GLIButton();
1092 add_all_button.setEnabled(true);
1093 add_all_button.setMnemonic(KeyEvent.VK_L);
1094 Dictionary.registerBoth(add_all_button, "CDM.IndexManager.MGPP.Add_All_Metadata", "CDM.IndexManager.MGPP.Add_All_Metadata_Tooltip");
1095
1096 replace_button = new GLIButton();
1097 replace_button.setEnabled(false);
1098 replace_button.setMnemonic(KeyEvent.VK_C);
1099 Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
1100
1101 remove_index_button = new GLIButton();
1102 remove_index_button.setEnabled(false);
1103 remove_index_button.setMnemonic(KeyEvent.VK_A);
1104 Dictionary.registerBoth(remove_index_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
1105
1106 JPanel index_empty_panel = new JPanel();
1107
1108 // Connect Indexes
1109 EnableAddIndexListener index_eal = new EnableAddIndexListener();
1110 add_index_button.addActionListener(new AddIndexActionListener());
1111 add_all_button.addActionListener(new AddAllActionListener());
1112 current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener());
1113 index_combobox.addActionListener(index_eal);
1114 ((JTextField)index_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(index_eal);
1115 index_name_field.getDocument().addDocumentListener(index_eal);
1116 move_index_down_button.addActionListener(new MoveIndexDownListener());
1117 move_index_up_button.addActionListener(new MoveIndexUpListener());
1118 replace_button.addActionListener(new ReplaceIndexActionListener());
1119 remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal));
1120
1121 // Layout Indexes
1122 index_movement_panel.setLayout(new GridLayout(2,1));
1123 index_movement_panel.add(move_index_up_button);
1124 index_movement_panel.add(move_index_down_button);
1125
1126 current_indexes_panel.setLayout(new BorderLayout());
1127 current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH);
1128 current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER);
1129 current_indexes_panel.add(index_movement_panel, BorderLayout.EAST);
1130
1131
1132 index_labels_panel.setLayout(new GridLayout(2,1));
1133 index_labels_panel.add(index_name_label);
1134 index_labels_panel.add(index_label);
1135
1136 index_boxes_panel.setLayout(new GridLayout(2,1));
1137 index_boxes_panel.add(index_name_field);
1138 index_boxes_panel.add(index_combobox);
1139
1140 index_details_panel.setLayout(new BorderLayout(5,0));
1141 index_details_panel.add(index_labels_panel, BorderLayout.WEST);
1142 index_details_panel.add(index_boxes_panel, BorderLayout.CENTER);
1143
1144 index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1145 index_button_panel.setLayout(new GridLayout(2,2,5,0));
1146 index_button_panel.add(add_index_button);
1147 index_button_panel.add(add_all_button);
1148 index_button_panel.add(replace_button);
1149 index_button_panel.add(remove_index_button);
1150
1151 index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1152 index_body_panel.setLayout(new BorderLayout());
1153 index_body_panel.add(index_details_panel, BorderLayout.CENTER);
1154 index_body_panel.add(index_button_panel, BorderLayout.SOUTH);
1155
1156 index_spacer_panel.setLayout(new BorderLayout());
1157 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH);
1158 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER);
1159
1160 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1161 indexes_panel.setLayout(new BorderLayout());
1162 indexes_panel.add(current_indexes_panel, BorderLayout.NORTH);
1163 indexes_panel.add(index_spacer_panel, BorderLayout.CENTER);
1164
1165 // Create Levels
1166 JPanel levels_panel = new JPanel();
1167
1168 JPanel current_levels_panel = new JPanel();
1169
1170 current_levels_label = new JLabel();
1171 Dictionary.registerText(current_levels_label, "CDM.IndexManager.MGPP.Current_Levels");
1172
1173 current_levels_list = new JList(levels_model);
1174 current_levels_list.setVisibleRowCount(5);
1175
1176 JPanel level_movement_panel = new JPanel();
1177
1178 move_level_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
1179 move_level_up_button.setEnabled(false);
1180 move_level_up_button.setMnemonic(KeyEvent.VK_U);
1181 //move_level_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1182 Dictionary.registerBoth(move_level_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
1183
1184 move_level_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
1185 move_level_down_button.setEnabled(false);
1186 move_level_down_button.setMnemonic(KeyEvent.VK_D);
1187 //move_level_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
1188 Dictionary.registerBoth(move_level_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
1189
1190 JPanel level_spacer_panel = new JPanel();
1191 JPanel level_body_panel = new JPanel();
1192 JPanel level_details_panel = new JPanel();
1193 JPanel level_labels_panel = new JPanel();
1194 JPanel level_boxes_panel = new JPanel();
1195
1196 level_name_label = new JLabel();
1197 Dictionary.registerText(level_name_label, "CDM.IndexManager.MGPP.Level_Name");
1198
1199 level_name_field = new JTextField();
1200 level_name_field.setPreferredSize(FIELD_SIZE);
1201 Dictionary.registerTooltip(level_name_field, "CDM.IndexManager.MGPP.Level_Name_Tooltip");
1202
1203
1204 level_label = new JLabel();
1205 Dictionary.registerText(level_label, "CDM.IndexManager.MGPP.Level");
1206
1207 level_combobox = new GComboBox(Index.LEVEL);
1208 level_combobox.setPreferredSize(FIELD_SIZE);
1209 level_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
1210 level_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
1211 level_combobox.setEditable(true);
1212 level_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
1213 level_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
1214 Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip");
1215
1216 JPanel level_button_panel = new JPanel();
1217
1218 add_level_button = new GLIButton("CDM.IndexManager.MGPP.Add_Level");
1219 add_level_button.setEnabled(false);
1220 add_level_button.setMnemonic(KeyEvent.VK_A);
1221 Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip");
1222
1223 remove_level_button = new GLIButton("CDM.IndexManager.MGPP.Remove_Level");
1224 remove_level_button.setEnabled(false);
1225 remove_level_button.setMnemonic(KeyEvent.VK_A);
1226 Dictionary.registerBoth(remove_level_button, "CDM.IndexManager.MGPP.Remove_Level", "CDM.IndexManager.MGPP.Remove_Level_Tooltip");
1227
1228 JPanel level_empty_panel = new JPanel();
1229
1230 // Connect Levels
1231 EnableAddLevelListener level_eal = new EnableAddLevelListener();
1232 add_level_button.addActionListener(new AddLevelActionListener());
1233 current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener());
1234 level_combobox.addActionListener(level_eal);
1235 ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal);
1236 level_name_field.getDocument().addDocumentListener(level_eal);
1237 move_level_down_button.addActionListener(new MoveLevelDownListener());
1238 move_level_up_button.addActionListener(new MoveLevelUpListener());
1239 remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal));
1240 // Layout Levels
1241
1242 level_movement_panel.setLayout(new GridLayout(2,1));
1243 level_movement_panel.add(move_level_up_button);
1244 level_movement_panel.add(move_level_down_button);
1245
1246 current_levels_panel.setLayout(new BorderLayout());
1247 current_levels_panel.add(current_levels_label, BorderLayout.NORTH);
1248 current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER);
1249 current_levels_panel.add(level_movement_panel, BorderLayout.EAST);
1250
1251 level_labels_panel.setLayout(new GridLayout(2,1));
1252 level_labels_panel.add(level_name_label);
1253 level_labels_panel.add(level_label);
1254
1255 level_boxes_panel.setLayout(new GridLayout(2,1));
1256 level_boxes_panel.add(level_name_field);
1257 level_boxes_panel.add(level_combobox);
1258
1259 level_details_panel.setLayout(new BorderLayout(5,0));
1260 level_details_panel.add(level_labels_panel, BorderLayout.WEST);
1261 level_details_panel.add(level_boxes_panel, BorderLayout.CENTER);
1262
1263 level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1264 level_button_panel.setLayout(new GridLayout(1,2,5,0));
1265 level_button_panel.add(add_level_button);
1266 level_button_panel.add(remove_level_button);
1267
1268 level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
1269 level_body_panel.setLayout(new BorderLayout());
1270 level_body_panel.add(level_details_panel, BorderLayout.CENTER);
1271 level_body_panel.add(level_button_panel, BorderLayout.SOUTH);
1272
1273 level_spacer_panel.setLayout(new BorderLayout());
1274 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH);
1275 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER);
1276
1277 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1278 levels_panel.setLayout(new BorderLayout());
1279 levels_panel.add(current_levels_panel, BorderLayout.NORTH);
1280 levels_panel.add(level_spacer_panel, BorderLayout.CENTER);
1281
1282 // Create General
1283 JPanel header_panel = new JPanel();
1284
1285 title_label = new JLabel();
1286 title_label.setHorizontalAlignment(JLabel.CENTER);
1287 Dictionary.registerText(title_label, "CDM.IndexManager.MGPP.Title");
1288
1289 instructions_textarea = new JTextArea();
1290 instructions_textarea.setEditable(false);
1291 instructions_textarea.setLineWrap(true);
1292 instructions_textarea.setRows(6);
1293 instructions_textarea.setWrapStyleWord(true);
1294 Dictionary.registerText(instructions_textarea, "CDM.IndexManager.MGPP.Instructions");
1295
1296 tabbed_pane = new JTabbedPane();
1297
1298 // Layout General
1299 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
1300 header_panel.setLayout(new BorderLayout());
1301 header_panel.add(title_label, BorderLayout.NORTH);
1302 header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
1303
1304 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Indexes"), indexes_panel);
1305 tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Levels"), levels_panel);
1306
1307 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
1308 setLayout(new BorderLayout());
1309 add(header_panel, BorderLayout.NORTH);
1310 add(tabbed_pane, BorderLayout.CENTER);
1311 }
1312
1313 public void destroy() {
1314 }
1315
1316 public void gainFocus() {
1317 // Reload the assigned indexes list.
1318 index_combobox.removeAllItems();
1319 index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR);
1320 index_combobox.addItem(CollectionConfiguration.METADATA_STR);
1321 index_combobox.addItem(CollectionConfiguration.TEXT_STR);
1322 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
1323 for(int i = 0; i < every_metadata_set_element.size(); i++) {
1324 index_combobox.addItem(every_metadata_set_element.get(i));
1325 }
1326 // Ensure the level manager has at least documents assigned
1327 if(levels_model.getSize() == 0) {
1328 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1329 // Create new metadatum
1330 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1331 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1332 // Assign new level
1333 addLevel(level, metadatum);
1334 level = null;
1335 }
1336
1337 // refresh
1338 current_indexes_list.updateUI();
1339 }
1340
1341 private class AddIndexActionListener
1342 implements ActionListener {
1343 public void actionPerformed(ActionEvent event) {
1344 // Retrieve the name
1345 String name = index_name_field.getText();
1346 // Retrieve the source
1347 Object source = index_combobox.getSelectedItem();
1348 // If this object isn't yet in the combobox add it.
1349 if(index_combobox.getSelectedIndex() == -1) {
1350 index_combobox.insertItemAt(source, index_combobox.getItemCount());
1351 }
1352 // Create new index
1353 ArrayList sources = new ArrayList();
1354 sources.add(source);
1355 Index index = new Index(sources);
1356 if(!model.contains(index)) {
1357 // Create new metadatum
1358 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1359 metadatum.setValue(name);
1360 // Assign new index
1361 addIndex(index, metadatum);
1362 }
1363 // Done. Disable add
1364 add_index_button.setEnabled(false);
1365 }
1366 }
1367
1368 private class AddAllActionListener
1369 implements ActionListener {
1370
1371 public void actionPerformed(ActionEvent event) {
1372 for(int i = 0; i < index_combobox.getItemCount(); i++) {
1373 Object source = index_combobox.getItemAt(i);
1374 // Create new index
1375 ArrayList sources = new ArrayList();
1376 sources.add(source);
1377 Index index = new Index(sources);
1378 sources = null;
1379 if(!model.contains(index)) {
1380 // Determine the metadatum value
1381 String name = source.toString();
1382 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
1383 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1384 }
1385 // Create new metadatum
1386 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1387 metadatum.setValue(name);
1388 name = null;
1389 // Assign new index
1390 addIndex(index, metadatum);
1391 }
1392 source = null;
1393 index = null;
1394 }
1395 // Done. Disable add
1396 add_index_button.setEnabled(false);
1397 }
1398 }
1399
1400 private class AddLevelActionListener
1401 implements ActionListener {
1402 public void actionPerformed(ActionEvent event) {
1403 // Retrieve the name
1404 String name = level_name_field.getText();
1405 // Retrieve the source
1406 String source = (String)level_combobox.getSelectedItem();
1407 // If this object isn't yet in the combobox add it.
1408 if(level_combobox.getSelectedIndex() == -1) {
1409 level_combobox.insertItemAt(source, level_combobox.getItemCount());
1410 }
1411 Level level = new Level(source);
1412 // Create new metadatum
1413 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source);
1414 metadatum.setValue(name);
1415 // Assign new level
1416 addLevel(level, metadatum);
1417 // Done. Disable add
1418 add_level_button.setEnabled(false);
1419 }
1420 }
1421
1422 private class CurrentIndexesListSelectionListener
1423 implements ListSelectionListener {
1424 public void valueChanged(ListSelectionEvent event) {
1425 if(!event.getValueIsAdjusting()) {
1426 Index index = (Index)current_indexes_list.getSelectedValue();
1427 Object[] selected_objects = current_indexes_list.getSelectedValues();
1428 if(selected_objects.length == 1) {
1429 String full_text = index.toString();
1430 if(full_text.indexOf("\"") != -1) {
1431 index_name_field.setText(index.getName());
1432 }
1433 ArrayList sources = index.getSources();
1434 index_combobox.setSelectedItem(sources.get(0));
1435 }
1436 if(index != null) {
1437 move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1));
1438 move_index_up_button.setEnabled((model.indexOf(index) > 0));
1439 remove_index_button.setEnabled(true);
1440 }
1441 else {
1442 move_index_down_button.setEnabled(false);
1443 move_index_up_button.setEnabled(false);
1444 remove_index_button.setEnabled(false);
1445 }
1446 }
1447 }
1448 }
1449
1450 private class CurrentLevelsListSelectionListener
1451 implements ListSelectionListener {
1452 public void valueChanged(ListSelectionEvent event) {
1453 if(!event.getValueIsAdjusting()) {
1454 Level level = (Level)current_levels_list.getSelectedValue();
1455 if(level != null) {
1456 move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1));
1457 move_level_up_button.setEnabled((levels_model.indexOf(level) > 0));
1458 remove_level_button.setEnabled(!level.getName().equals(CollectionConfiguration.DOCUMENT_STR) || levels_model.getSize() > 1);
1459 }
1460 else {
1461 move_level_down_button.setEnabled(false);
1462 move_level_up_button.setEnabled(false);
1463 remove_level_button.setEnabled(false);
1464 }
1465 }
1466 }
1467 }
1468
1469 private class EnableAddIndexListener
1470 implements ActionListener, DocumentListener {
1471 /** Called whenever a selection action occurs on the combobox.
1472 * @param event an ActionEvent containing information about the selection event
1473 */
1474 public void actionPerformed(ActionEvent event) {
1475 validateAddButtonIndex();
1476 }
1477
1478 /** Gives notification that an attribute or set of attributes changed.
1479 * @param event a DocumentEvent containing information about the text changed
1480 */
1481 public void changedUpdate(DocumentEvent event) {
1482 validateAddButtonIndex();
1483 }
1484
1485 /** Gives notification that there was an insert into the document.
1486 * @param event a DocumentEvent containing information about the text added
1487 */
1488 public void insertUpdate(DocumentEvent event) {
1489 validateAddButtonIndex();
1490 }
1491
1492 /** Gives notification that a portion of the document has been removed.
1493 * @param event a DocumentEvent containing information about the text removed
1494 */
1495 public void removeUpdate(DocumentEvent event) {
1496 validateAddButtonIndex();
1497 }
1498
1499 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1500 public void validateAddButtonIndex() {
1501 String name = index_name_field.getText();
1502 Object selected_object = index_combobox.getSelectedItem();
1503 if(name.length() > 0 && selected_object != null) {
1504 // Unfortunately we have to generate a valid id
1505 String id = null;
1506 if (selected_object instanceof MetadataElement) {
1507 id = ((MetadataElement) selected_object).getFullName();
1508 }
1509 else {
1510 id = selected_object.toString();
1511 }
1512 if(id.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
1513 id = id.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1514 }
1515 Index index = getIndex(id);
1516 if(index == null) {
1517 add_index_button.setEnabled(true);
1518 replace_button.setEnabled(false);
1519 }
1520 else {
1521 add_index_button.setEnabled(false);
1522 replace_button.setEnabled(!name.equals(index.getName()));
1523 }
1524 }
1525 else {
1526 add_index_button.setEnabled(false);
1527 }
1528 }
1529 }
1530
1531 private class EnableAddLevelListener
1532 implements ActionListener, DocumentListener {
1533 /** Called whenever a selection action occurs on the combobox.
1534 * @param event an ActionEvent containing information about the selection event
1535 */
1536 public void actionPerformed(ActionEvent event) {
1537 validateAddButtonLevel();
1538 }
1539
1540 /** Gives notification that an attribute or set of attributes changed.
1541 * @param event a DocumentEvent containing information about the text changed
1542 */
1543 public void changedUpdate(DocumentEvent event) {
1544 validateAddButtonLevel();
1545 }
1546
1547 /** Gives notification that there was an insert into the document.
1548 * @param event a DocumentEvent containing information about the text added
1549 */
1550 public void insertUpdate(DocumentEvent event) {
1551 validateAddButtonLevel();
1552 }
1553
1554 /** Gives notification that a portion of the document has been removed.
1555 * @param event a DocumentEvent containing information about the text removed
1556 */
1557 public void removeUpdate(DocumentEvent event) {
1558 validateAddButtonLevel();
1559 }
1560
1561 /** Change the enable state of the add button depending on the current value in the search type combobox. */
1562 public void validateAddButtonLevel() {
1563 String name = level_name_field.getText();
1564 Object selected_object = level_combobox.getSelectedItem();
1565 if(name.length() > 0 && selected_object != null) {
1566 add_level_button.setEnabled(getLevel((String)selected_object) == null);
1567 }
1568 else {
1569 add_level_button.setEnabled(false);
1570 }
1571 }
1572 }
1573
1574 private class MoveIndexDownListener
1575 implements ActionListener {
1576 public void actionPerformed(ActionEvent event) {
1577 // Retrieve the first selected item
1578 Index index = (Index) current_indexes_list.getSelectedValue();
1579 moveIndex(index, false);
1580 current_indexes_list.setSelectedValue(index, true);
1581 }
1582 }
1583
1584 private class MoveLevelDownListener
1585 implements ActionListener {
1586 public void actionPerformed(ActionEvent event) {
1587 // Retrieve the first selected item
1588 Level level = (Level) current_levels_list.getSelectedValue();
1589 moveLevel(level, false);
1590 current_levels_list.setSelectedValue(level, true);
1591 }
1592 }
1593
1594 private class MoveIndexUpListener
1595 implements ActionListener {
1596 public void actionPerformed(ActionEvent event) {
1597 // Retrieve the first selected item
1598 Index index = (Index) current_indexes_list.getSelectedValue();
1599 moveIndex(index, true);
1600 current_indexes_list.setSelectedValue(index, true);
1601 }
1602 }
1603
1604 private class MoveLevelUpListener
1605 implements ActionListener {
1606 public void actionPerformed(ActionEvent event) {
1607 // Retrieve the first selected item
1608 Level level = (Level) current_levels_list.getSelectedValue();
1609 moveLevel(level, true);
1610 current_levels_list.setSelectedValue(level, true);
1611 }
1612 }
1613
1614 /** Replace really only replaces the string. */
1615 private class ReplaceIndexActionListener
1616 implements ActionListener {
1617
1618 public void actionPerformed(ActionEvent event) {
1619 Object[] selected_objects = current_indexes_list.getSelectedValues();
1620 if(selected_objects.length == 1) {
1621 Index index = (Index) selected_objects[0];
1622 // Remove old name
1623 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1624 // Enter new name
1625 String name = index_name_field.getText();
1626 // Create new metadatum
1627 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID());
1628 index = null;
1629 metadatum.setValue(name);
1630 name = null;
1631 // Assign new index
1632 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
1633 metadatum = null;
1634 }
1635 current_indexes_list.setSelectedValue(selected_objects[0], true);
1636 // Done. Disable add
1637 add_index_button.setEnabled(false);
1638 replace_button.setEnabled(false);
1639 }
1640 }
1641
1642 private class RemoveIndexActionListener
1643 implements ActionListener {
1644
1645 private EnableAddIndexListener eal = null;
1646
1647 public RemoveIndexActionListener(EnableAddIndexListener eal) {
1648 this.eal = eal;
1649 }
1650
1651 public void actionPerformed(ActionEvent event) {
1652 // Retrieve the selected items
1653 Object[] selected_objects = current_indexes_list.getSelectedValues();
1654 // Clear selection
1655 current_indexes_list.clearSelection();
1656 for(int i = 0; i < selected_objects.length; i++) {
1657 Index index = (Index) selected_objects[i];
1658 // Remove any related metadata
1659 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID());
1660 // Remove the index
1661 removeIndex(index);
1662 }
1663 // Disable remove button
1664 remove_index_button.setEnabled(false);
1665 // Check if add should be reenabled
1666 eal.validateAddButtonIndex();
1667 }
1668 }
1669
1670 private class RemoveLevelActionListener
1671 implements ActionListener {
1672
1673 private EnableAddLevelListener eal = null;
1674
1675 public RemoveLevelActionListener(EnableAddLevelListener eal) {
1676 this.eal = eal;
1677 }
1678
1679 public void actionPerformed(ActionEvent event) {
1680 // Retrieve the selected items
1681 Object[] selected_objects = current_levels_list.getSelectedValues();
1682 // Clear selection
1683 current_levels_list.clearSelection();
1684 for(int i = 0; i < selected_objects.length; i++) {
1685 Level level = (Level) selected_objects[i];
1686 // Remove any related metadata
1687 CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName());
1688 // Remove the index
1689 removeLevel(level);
1690 }
1691 // Disable remove button
1692 remove_level_button.setEnabled(false);
1693 // If there are no levels left, put document back in
1694 if(levels_model.getSize() == 0) {
1695 Level level = new Level(CollectionConfiguration.DOCUMENT_STR);
1696 // Create new metadatum
1697 CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR);
1698 metadatum.setValue(CollectionConfiguration.DOCUMENT_STR);
1699 // Assign new level
1700 addLevel(level, metadatum);
1701 level = null;
1702 }
1703 // Check if add should be reenabled
1704 eal.validateAddButtonLevel();
1705 }
1706 }
1707 } // MGPPControls
1708
1709}
Note: See TracBrowser for help on using the repository browser.