/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.cdm; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.DebugStream; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.checklist.CheckList; import org.greenstone.gatherer.gui.GComboBox; import org.greenstone.gatherer.gui.GLIButton; import org.greenstone.gatherer.metadata.MetadataElement; import org.greenstone.gatherer.metadata.MetadataSetManager; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; import org.w3c.dom.*; /** 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. * @author John Thompson, Greenstone Digital Library, University of Waikato * @version 2.3 */ public class IndexManager extends DOMProxyListModel { static final private Dimension FIELD_SIZE = new Dimension(200,30); static final private String MGINDEXES = "mg indexes"; static final private String MGPPINDEXES = "mgpp indexes"; /** The controls for editing the indexes. */ private Control controls = null; /** A model of the levels, also based on the DOM. */ private DOMProxyListModel levels_model = null; /** A reference to ourselves so our inner methods have access. */ private DOMProxyListModel model = null; /** The default index. */ private Index default_index = null; /** Constructor. */ public IndexManager(Element indexes) { super(indexes, CollectionConfiguration.INDEX_ELEMENT, new Index()); DebugStream.println("IndexManager: " + getSize() + " indexes parsed."); model = this; // Parse and retrieve the default index NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.INDEX_DEFAULT_ELEMENT); if(default_index_elements.getLength() > 0) { default_index = new Index((Element)default_index_elements.item(0)); } // Parse and retrieve the levels element Element levels_element = CollectionDesignManager.collect_config.getLevels(); levels_model = new DOMProxyListModel(levels_element, CollectionConfiguration.CONTENT_ELEMENT, new Level()); DebugStream.println(" + " + levels_model.getSize() + " levels parsed."); } /** Method to add a new index. * @param index The Index to add. * @see org.greenstone.gatherer.Gatherer * @see org.greenstone.gatherer.collection.CollectionManager */ private void addIndex(Index index, CollectionMeta metadatum) { ///ystem.err.println("Adding an index: " + index.toString()); if(!contains(index)) { CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum); // Retrieve the currently last index if(getSize() > 0) { Index last_index = (Index)getElementAt(getSize() - 1); addAfter(index, last_index); } else { add(index); // Also set this index as the default one, but only if there are levels available (ie mg only) if(index.getLevel() != -1) { setDefault(index); } } Gatherer.c_man.configurationChanged(); } else { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE); } } private void addLevel(Level level, CollectionMeta metadatum) { if(!levels_model.contains(level)) { CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum); // Retrieve the currently last index if(levels_model.getSize() > 0) { Level last_level = (Level)levels_model.getElementAt(levels_model.getSize() - 1); levels_model.addAfter(level, last_level); } else { levels_model.add(level); } Gatherer.c_man.configurationChanged(); } else { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Level_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE); } } public void destroy() { if(controls != null) { controls.destroy(); controls = null; } default_index = null; model = null; } /** Method to acquire the controls for editing the indexes. * @return the Control */ public Control getControls() { if(controls == null) { // Build controls controls = new IndexControl(); } return controls; } /** Method to get the default index. * @return The default Index. */ public Index getDefault() { if(default_index != null && default_index.isAssigned()) { return default_index; } else { return null; } } /** Method to retrieve a certain index, as referenced by an index number. * @param index An int which indicates the position of the desired index. * @return The Index at the given index, or null if no such index exists. */ public Index getIndex(int index) { if(0 <= index && index < getSize()) { return (Index)getElementAt(index); } return null; } /** Method to retrieve a certain index, given its id. * @param id the id of the index as a String * @return the Index that matches id, or null if no such index exists */ public Index getIndex(String id) { int size = getSize(); for(int i = 0; i < size; i++) { Index index = (Index) getElementAt(i); if(index.getID().equals(id)) { return index; } } return null; } public ArrayList getIndexes() { return children(); } public Level getLevel(String name) { int levels_model_size = levels_model.getSize(); for(int i = 0; i < levels_model_size; i++) { Level level = (Level) levels_model.getElementAt(i); if(level.getName().equals(name)) { return level; } } return null; } private void moveIndex(Index index, boolean move_up) { // Determine the indexes current position int position = indexOf(index); // 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. if(position == -1) { return; } if(position == 0 && move_up) { return; } if(position == (getSize()) - 1 && !move_up) { return; } // Ok, move the index if(move_up) { // Retrieve the index at position - 1 Index previous_index = (Index) getElementAt(position - 1); // And add before. This will automatically remove the index first, as an Element can only exist once in a particular document addBefore(index, previous_index); } else { // Retrieve the index at position + 1 Index next_index = (Index) getElementAt(position + 1); // And add after. This will automatically remove the index first, as an Element can only exist once in a particular document addAfter(index, next_index); } // Schedule the collection for saving Gatherer.c_man.configurationChanged(); } private void moveLevel(Level level, boolean move_up) { // Determine the leveles current position int position = levels_model.indexOf(level); // 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. if(position == -1) { return; } if(position == 0 && move_up) { return; } if(position == (levels_model.getSize()) - 1 && !move_up) { return; } // Ok, move the level if(move_up) { // Retrieve the level at position - 1 Level previous_level = (Level) levels_model.getElementAt(position - 1); // And add before. This will automatically remove the level first, as an Element can only exist once in a particular document levels_model.addBefore(level, previous_level); } else { // Retrieve the level at position + 1 Level next_level = (Level) levels_model.getElementAt(position + 1); // And add after. This will automatically remove the level first, as an Element can only exist once in a particular document levels_model.addAfter(level, next_level); } // Schedule the collection for saving Gatherer.c_man.configurationChanged(); } /** Method to remove a certain index. * @param index the Index to remove. * @see org.greenstone.gatherer.Gatherer * @see org.greenstone.gatherer.cdm.CollectionDesignManager * @see org.greenstone.gatherer.cdm.CollectionMetaManager * @see org.greenstone.gatherer.collection.CollectionManager */ private void removeIndex(Index index) { if(index != null) { // Remove any current metadata from this index CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID()); // Check if the index removed happens to be the default index if(default_index != null && default_index.equals(index)) { // If so our first solution is to set the first index to be default if(getSize() > 0) { Index another_index = (Index) getElementAt(0); setDefault(another_index); another_index = null; } else { default_index.setAssigned(false); } } // Remove the index remove(index); Gatherer.c_man.configurationChanged(); } } private void removeLevel(Level level) { if(level != null) { // Remove any current metadata from this level CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName()); // Remove the level levels_model.remove(level); Gatherer.c_man.configurationChanged(); } } /** Method to set the default index. * @param index the new default Index * @see org.greenstone.gatherer.Gatherer * @see org.greenstone.gatherer.collection.CollectionManager */ public void setDefault(Index index) { if(index != null) { if(default_index == null) { // Create the default index element, and place immediately after indexes element. Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.INDEX_DEFAULT_ELEMENT); default_index = new Index(default_index_element); Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element); if(target_node != null) { root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node); } else { root.getOwnerDocument().getDocumentElement().appendChild(default_index_element); } } default_index.setAssigned(true); default_index.setLevel(index.getLevel()); default_index.setSources(index.getSources()); } else { if(default_index != null) { default_index.setAssigned(false); } } Gatherer.c_man.configurationChanged(); } /** 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. * @param state true to enable MGPP indexes, false to use standard MG style ones */ public void setMGPPEnabled(boolean state) { if(state != root.getAttribute(CollectionConfiguration.MGPP_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR)) { if(state) { Element mg_element = root; // Retrieve and assign the MGPP indexes element. Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes(); mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR); levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR); // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT); if(indexes.getLength() == 0) { ArrayList levels_list = new ArrayList(); ArrayList sources_list = new ArrayList(); // We first use details from the default index if any if(default_index != null) { int level_int = default_index.getLevel(); if(0 <= level_int && level_int < 3) { String level = Index.LEVEL[level_int]; if(!levels_list.contains(level)) { levels_list.add(level); } level = null; } ArrayList sources = default_index.getSources(); sources.removeAll(sources_list); sources_list.addAll(sources); } int size = getSize(); for(int i = 0; i < size; i++) { Index index = (Index) getElementAt(i); int level_int = index.getLevel(); if(0 <= level_int && level_int < 3) { String level = Index.LEVEL[level_int]; if(!levels_list.contains(level)) { levels_list.add(level); } level = null; } ArrayList sources = index.getSources(); sources.removeAll(sources_list); sources_list.addAll(sources); index = null; } // Replace mg element with mgpp element setRoot(mgpp_element); // We now have a list of sources and a list of levels, so create new indexes and levels based on these int sources_list_size = sources_list.size(); for(int j = 0; j < sources_list_size; j++) { Object source_object = sources_list.get(j); String source_str = null; if(source_object instanceof MetadataElement) { source_str = ((MetadataElement) source_object).getFullName(); } else { source_str = source_object.toString(); } ArrayList new_sources = new ArrayList(); new_sources.add(source_object); source_object = null; Index new_index = new Index(new_sources); // Try to retrieve existing metadatum source_str = new_index.getID(); CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false); // If no metadata was found, add new pseudo metadata using the id if(metadatum == null) { metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str); metadatum.setAssigned(true); metadatum.setValue(source_str); } // If it was found, ensure it is assigned else { metadatum.setAssigned(true); } source_str = null; addIndex(new_index, metadatum); metadatum = null; new_index = null; new_sources = null; source_str = null; } int levels_list_size = levels_list.size(); for(int k = 0; k < levels_list_size; k++) { Level new_level = new Level((String)levels_list.get(k)); if(!levels_model.contains(new_level)) { levels_model.add(levels_model.getSize(), new_level); } new_level = null; } } else { // Replace mg element with mgpp element setRoot(mgpp_element); } // Unassign MG element and default index mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR); mg_element = null; if(default_index != null) { default_index.setAssigned(false); } } else { Element mgpp_element = root; // Retrieve and assign MG element and default index element Element mg_element = CollectionDesignManager.collect_config.getMGIndexes(); mg_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.TRUE_STR); if(default_index != null) { default_index.setAssigned(true); } // 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. NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT); if(indexes.getLength() == 0) { Index index = getIndex(CollectionConfiguration.TEXT_STR); if(index != null) { // Replace mgpp element with mg element setRoot(mg_element); int level_size = levels_model.getSize(); for(int i = 0; i < level_size; i++) { Level level = (Level) levels_model.getElementAt(i); Index new_index = new Index(level.getName(), index.getSources()); // Try to retrieve existing metadatum String source_str = new_index.getID(); CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER + source_str, false); // If no metadata was found, add new pseudo metadata using the id if(metadatum == null) { metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source_str); metadatum.setAssigned(true); metadatum.setValue(source_str); } // If it was found, ensure it is assigned else { metadatum.setAssigned(true); } source_str = null; addIndex(new_index, metadatum); new_index = null; level = null; } } } else { // Replace mgpp element with mg element setRoot(mg_element); } // Unassign mgpp element and levels mgpp_element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR); levels_model.root.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, CollectionConfiguration.FALSE_STR); } } } /** This class creates a set of controls for editing the indexes. */ private class IndexControl extends JPanel implements Control { private CardLayout card_layout; private CheckList source_list; private JButton add_button; private JButton move_down_button; private JButton move_up_button; private JButton remove_button; private JButton replace_button; private JButton set_default_button; private JComboBox level_combobox; private JList index_list; private JTextArea instruction_textarea; private JTextField name_textfield ; private MGPPControl mgppindexes_control; /** Constructor. * @see org.greenstone.gatherer.Configuration * @see org.greenstone.gatherer.Gatherer * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.AddListener * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.NameListener * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.RemoveListener * @see org.greenstone.gatherer.cdm.IndexManager.IndexControl.SetDefaultListener * @see org.greenstone.gatherer.collection.CollectionManager * @see org.greenstone.gatherer.gui.Coloring */ public IndexControl() { super(); ArrayList new_data = new ArrayList(); new_data.add(CollectionConfiguration.TEXT_STR); new_data.addAll(MetadataSetManager.getEveryMetadataSetElement()); // Creation JPanel mgindexes_panel = new JPanel(); card_layout = new CardLayout(); // MG Index Controls JPanel mg_indexes_pane = new JPanel(); JPanel instruction_pane = new JPanel(); JLabel title_label = new JLabel(); title_label.setHorizontalAlignment(JLabel.CENTER); Dictionary.registerText(title_label, "CDM.IndexManager.Title"); instruction_textarea = new JTextArea(); instruction_textarea.setEditable(false); instruction_textarea.setLineWrap(true); instruction_textarea.setRows(6); instruction_textarea.setWrapStyleWord(true); Dictionary.registerText(instruction_textarea, "CDM.IndexManager.Instructions"); JPanel assigned_indexes_pane = new JPanel(); JLabel index_label = new JLabel(); Dictionary.registerText(index_label, "CDM.IndexManager.Indexes"); index_list = new JList(model); index_list.setCellRenderer(new IndexListRenderer()); index_list.setVisibleRowCount(2); JPanel movement_pane = new JPanel(); move_up_button = new JButton("", Utility.getImage("arrow-up.gif")); move_up_button.setEnabled(false); move_up_button.setMnemonic(KeyEvent.VK_U); Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip"); move_down_button = new JButton("", Utility.getImage("arrow-down.gif")); move_down_button.setEnabled(false); move_down_button.setMnemonic(KeyEvent.VK_D); Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip"); set_default_button = new GLIButton(); set_default_button.setEnabled(false); set_default_button.setMnemonic(KeyEvent.VK_S); Dictionary.registerBoth(set_default_button, "CDM.IndexManager.Set_Default", "CDM.IndexManager.Set_Default_Tooltip"); JPanel index_pane = new JPanel(); JPanel details_pane = new JPanel(); JPanel labels_pane = new JPanel(); JPanel boxes_pane = new JPanel(); JPanel content_pane = new JPanel(); JLabel name_label = new JLabel(); Dictionary.registerText(name_label, "CDM.IndexManager.Index_Name"); name_textfield = new JTextField(); name_textfield.setPreferredSize(FIELD_SIZE); Dictionary.registerTooltip(name_textfield, "CDM.IndexManager.Index_Name_Tooltip"); JLabel source_label = new JLabel(); Dictionary.registerText(source_label, "CDM.IndexManager.Source"); source_list = new CheckList(false); source_list.setListData(new_data); Dictionary.registerTooltip(source_list, "CDM.IndexManager.Source_Tooltip"); JLabel level_label = new JLabel(); Dictionary.registerText(level_label, "CDM.IndexManager.Level"); level_combobox = new JComboBox(); level_combobox.setPreferredSize(FIELD_SIZE); level_combobox.addItem(CollectionConfiguration.DOCUMENT_STR);//Dictionary.get("CDM.IndexManager.Document")); level_combobox.addItem(CollectionConfiguration.PARAGRAPH_STR);//Dictionary.get("CDM.IndexManager.Paragraph")); level_combobox.addItem(CollectionConfiguration.SECTION_STR);//Dictionary.get("CDM.IndexManager.Section")); level_combobox.setEditable(false); Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip"); JPanel button_pane = new JPanel(); add_button = new GLIButton(); add_button.setEnabled(false); add_button.setMnemonic(KeyEvent.VK_A); Dictionary.registerBoth(add_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip"); remove_button = new GLIButton(); remove_button.setEnabled(false); remove_button.setMnemonic(KeyEvent.VK_R); Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip"); replace_button = new GLIButton(); replace_button.setEnabled(false); replace_button.setMnemonic(KeyEvent.VK_P); Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip"); // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class. mgppindexes_control = new MGPPControl(); // Listeners add_button.addActionListener(new AddListener()); move_down_button.addActionListener(new MoveListener(false)); move_up_button.addActionListener(new MoveListener(true)); remove_button.addActionListener(new RemoveListener()); replace_button.addActionListener(new ReplaceListener()); set_default_button.addActionListener(new SetDefaultListener()); name_textfield.getDocument().addDocumentListener(new NameListener()); level_combobox.addItemListener(new LevelListener()); index_list.addListSelectionListener(new IndexListListener()); source_list.addListSelectionListener(new SourceListListener()); // Layout instruction_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0)); instruction_pane.setLayout(new BorderLayout()); instruction_pane.add(title_label, BorderLayout.NORTH); instruction_pane.add(new JScrollPane(instruction_textarea), BorderLayout.CENTER); movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0)); movement_pane.setLayout(new GridLayout(3,1)); movement_pane.add(move_up_button); movement_pane.add(move_down_button); movement_pane.add(set_default_button); assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0)); assigned_indexes_pane.setLayout(new BorderLayout()); assigned_indexes_pane.add(index_label, BorderLayout.NORTH); assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER); assigned_indexes_pane.add(movement_pane, BorderLayout.EAST); labels_pane.setLayout(new BorderLayout()); labels_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5)); labels_pane.add(name_label, BorderLayout.NORTH); labels_pane.add(source_label, BorderLayout.CENTER); labels_pane.add(level_label, BorderLayout.SOUTH); boxes_pane.setLayout(new BorderLayout()); boxes_pane.add(name_textfield, BorderLayout.NORTH); boxes_pane.add(new JScrollPane(source_list), BorderLayout.CENTER); boxes_pane.add(level_combobox, BorderLayout.SOUTH); details_pane.setLayout(new BorderLayout()); details_pane.add(labels_pane, BorderLayout.WEST); details_pane.add(boxes_pane, BorderLayout.CENTER); button_pane.setLayout(new GridLayout(1,3)); button_pane.add(add_button); button_pane.add(replace_button); button_pane.add(remove_button); index_pane.setLayout(new BorderLayout()); index_pane.add(details_pane, BorderLayout.CENTER); index_pane.add(button_pane, BorderLayout.SOUTH); content_pane.setLayout(new BorderLayout()); content_pane.add(assigned_indexes_pane, BorderLayout.NORTH); content_pane.add(index_pane, BorderLayout.CENTER); mgindexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); mgindexes_panel.setLayout(new BorderLayout()); mgindexes_panel.add(instruction_pane, BorderLayout.NORTH); mgindexes_panel.add(content_pane, BorderLayout.CENTER); setLayout(card_layout); add(mgindexes_panel, MGINDEXES); add(mgppindexes_control, MGPPINDEXES); } /* Destructor, removes persistant listeners from the Dictionary. */ public void destroy() { mgppindexes_control.destroy(); mgppindexes_control = null; } public void gainFocus() { boolean mgpp_enabled = CollectionDesignManager.searchtype_manager.isMGPPEnabled(); if(instruction_textarea != null) { instruction_textarea.setCaretPosition(0); } if(mgpp_enabled) { mgppindexes_control.gainFocus(); } else { // Reload the assigned indexes list. ArrayList new_data = new ArrayList(); new_data.add(CollectionConfiguration.TEXT_STR); new_data.addAll(MetadataSetManager.getEveryMetadataSetElement()); // reset the model in the list and combobox source_list.setListData(new_data); new_data = null; // refresh the indexList index_list.updateUI(); // if there is one selected, fill in the controls updateControlsWithSelectedIndex(); } // Bring the appropriate card to the front if(mgpp_enabled) { card_layout.show(this, MGPPINDEXES); } else { card_layout.show(this, MGINDEXES); } } public void loseFocus() { } private void updateControlsWithSelectedIndex() { Index selected_index = (Index)index_list.getSelectedValue(); if (selected_index == null) { return; } String id = selected_index.getID(); if (id == null || id.equals("")) { return; } String name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true); name_textfield.setText(name); level_combobox.setSelectedIndex(selected_index.getLevel()); source_list.clearSelection(); ArrayList sources = selected_index.getSources(); source_list.setSelectedObjects(sources.toArray()); } private void validateAddButton() { boolean add_enabled = false; boolean replace_enabled = false; // Indexes must have a name if(name_textfield.getText().length() == 0) { add_enabled = false; } // Can't add a new index if no sources are selected else if (source_list.getSelected().size() == 0) { add_enabled = false; } // If we get this far, create a dummy index and see if its already assigned in the collection else { Object object[] = source_list.getSelected().toArray(); ArrayList sources = new ArrayList(); for(int i = 0; i < object.length; i++) { sources.add(object[i]); } object = null; Index index = new Index(level_combobox.getSelectedIndex(), sources); sources = null; if(model.contains(index)) { add_enabled = false; // here we need to check if we have changed the name - if so, we can enable the replace button if (index_list.getSelectedIndex() != -1) { Index selected_index = (Index)index_list.getSelectedValue(); String name = name_textfield.getText(); String index_name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true); if (!index_name.equals(name)){ replace_enabled = true; } } } else { add_enabled = true; // we have a new index, do we have something selected in the index list? if so, enable the replace button if (index_list.getSelectedIndex() != -1) { replace_enabled = true; } } } // We should now know the add_button state add_button.setEnabled(add_enabled); replace_button.setEnabled(replace_enabled); } /** 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. */ private class AddListener implements ActionListener { /** 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. * @param event An ActionEvent providing extra information about the event. * @see org.greenstone.gatherer.cdm.CollectionDesignManager * @see org.greenstone.gatherer.cdm.CollectionMeta * @see org.greenstone.gatherer.cdm.CollectionMetaManager * @see org.greenstone.gatherer.cdm.Index * @see org.greenstone.gatherer.cdm.Language * @see org.greenstone.gatherer.cdm.LanguageManager */ public void actionPerformed(ActionEvent event) { String name = name_textfield.getText(); System.err.println("Add button height: " + add_button.getHeight()); System.err.println("Set default button height: " + set_default_button.getHeight()); if (source_list.getSelected().size() > 0 && name.length() != 0) { ArrayList sources = source_list.getSelected(); Index index = new Index(level_combobox.getSelectedIndex(), sources); sources = null; // Before we add the index to the model, we have to add the collection metadata for this. CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID()); metadatum.setValue(name); // Finally add index. addIndex(index, metadatum); index_list.setSelectedValue(index, true); metadatum = null; index = null; } name = null; } } /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */ private class IndexListListener implements ListSelectionListener { /** 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 * @param event A ListSelectionEvent containing further information about the list selection. */ public void valueChanged(ListSelectionEvent event) { if (event.getValueIsAdjusting()) { return; } Object value = index_list.getSelectedValue(); if (value == null) { move_down_button.setEnabled(false); move_up_button.setEnabled(false); remove_button.setEnabled(false); replace_button.setEnabled(false); set_default_button.setEnabled(false); return; } // enable the buttons appropriately remove_button.setEnabled(true); set_default_button.setEnabled(default_index == null || !default_index.equals(value)); int i = index_list.getSelectedIndex(); int size = index_list.getModel().getSize(); if (i > 0) { move_up_button.setEnabled(true); } else { move_up_button.setEnabled(false); } if (iActionEvent containing extra information about the action that occured. * @see org.greenstone.gatherer.cdm.Index */ public void actionPerformed(ActionEvent event) { int i = index_list.getSelectedIndex(); if(i != -1) { removeIndex((Index)index_list.getSelectedValue()); } int size = index_list.getModel().getSize(); if (i == size) { i--; } index_list.setSelectedIndex(i); // this will produce an event on the list, updating the other buttons if (size == 0) { // we have removed the last index, should be able to add whats filled in currently, if valid validateAddButton(); } } } /** 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. */ private class ReplaceListener implements ActionListener { /** If called when an action occurs on a registered component, we replace the currently selected index, with the new details * @param event An ActionEvent containing extra information about the action that occured. * @see org.greenstone.gatherer.cdm.Index */ public void actionPerformed(ActionEvent event) { if(index_list.isSelectionEmpty()) { // this should never happen, but just in case.. replace_button.setEnabled(false); return; } // we'll just remove the old one and add the new one removeIndex((Index)index_list.getSelectedValue()); replace_button.setEnabled(false); add_button.setEnabled(true); add_button.doClick(); add_button.setEnabled(false); } } private class SetDefaultListener implements ActionListener { public void actionPerformed(ActionEvent event) { Index index = (Index) index_list.getSelectedValue(); if(index != null) { setDefault(index); // This should cause a repaint of just the desired row index_list.setSelectedValue(index, true); } set_default_button.setEnabled(false); } } /** Listens for selections within the list on the IndexManager controls, and if a change is detected enables, or disables, controls appropriately. */ private class SourceListListener implements ListSelectionListener { /** 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. * @param event A ListSelectionEvent containing further information about the list selection. */ public void valueChanged(ListSelectionEvent event) { validateAddButton(); } } /** Listens for key presses within the name field, and enabled or disables controls as appropriate. */ private class NameListener implements DocumentListener { /** Gives notification that an attribute or set of attributes changed. */ public void changedUpdate(DocumentEvent e) { validateAddButton(); } /** Gives notification that there was an insert into the document. */ public void insertUpdate(DocumentEvent e) { validateAddButton(); } /** Gives notification that a portion of the document has been removed. */ public void removeUpdate(DocumentEvent e) { validateAddButton(); } } } private class MGPPControl extends JPanel { private GComboBox index_combobox; private GComboBox level_combobox; private JButton add_index_button; private JButton add_all_button; private JButton add_level_button; private JButton move_index_down_button; private JButton move_level_down_button; private JButton move_index_up_button; private JButton move_level_up_button; private JButton replace_button; private JButton remove_index_button; private JButton remove_level_button; private JLabel current_levels_label; private JLabel current_indexes_label; private JLabel index_label; private JLabel index_name_label; private JLabel level_label; private JLabel level_name_label; private JLabel move_index_down_label; private JLabel move_level_down_label; private JLabel move_index_up_label; private JLabel move_level_up_label; private JLabel title_label; private JList current_levels_list; private JList current_indexes_list; private JTabbedPane tabbed_pane; private JTextArea instructions_textarea; private JTextField index_name_field; private JTextField level_name_field; public MGPPControl() { // Create Indexes JPanel indexes_panel = new JPanel(); JPanel current_indexes_panel = new JPanel(); current_indexes_label = new JLabel(); Dictionary.registerText(current_indexes_label, "CDM.IndexManager.MGPP.Current_Indexes"); current_indexes_list = new JList(model); current_indexes_list.setVisibleRowCount(5); JPanel index_movement_panel = new JPanel(); move_index_up_button = new JButton("", Utility.getImage("arrow-up.gif")); move_index_up_button.setEnabled(false); move_index_up_button.setMnemonic(KeyEvent.VK_U); //move_index_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE); Dictionary.registerBoth(move_index_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip"); move_index_down_button = new JButton("", Utility.getImage("arrow-down.gif")); move_index_down_button.setEnabled(false); move_index_down_button.setMnemonic(KeyEvent.VK_D); //move_index_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE); Dictionary.registerBoth(move_index_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip"); JPanel index_spacer_panel = new JPanel(); JPanel index_body_panel = new JPanel(); JPanel index_details_panel = new JPanel(); JPanel index_labels_panel = new JPanel(); index_name_label = new JLabel(); Dictionary.registerText(index_name_label, "CDM.IndexManager.Index_Name"); index_name_field = new JTextField(); index_name_field.setPreferredSize(FIELD_SIZE); Dictionary.registerTooltip(index_name_field, "CDM.IndexManager.Index_Name_Tooltip"); JPanel index_boxes_panel = new JPanel(); index_label = new JLabel("CDM.IndexManager.MGPP.Index"); Dictionary.registerText(index_label, "CDM.IndexManager.MGPP.Index"); index_combobox = new GComboBox(); index_combobox.setPreferredSize(FIELD_SIZE); index_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false)); index_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false)); index_combobox.setEditable(true); index_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false)); index_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false)); Dictionary.registerTooltip(index_combobox, "CDM.IndexManager.MGPP.Index_Tooltip"); JPanel index_button_panel = new JPanel(); add_index_button = new GLIButton(); add_index_button.setEnabled(false); add_index_button.setMnemonic(KeyEvent.VK_A); Dictionary.registerBoth(add_index_button, "CDM.IndexManager.Add_Index", "CDM.IndexManager.Add_Index_Tooltip"); add_all_button = new GLIButton(); add_all_button.setEnabled(true); add_all_button.setMnemonic(KeyEvent.VK_L); Dictionary.registerBoth(add_all_button, "CDM.IndexManager.MGPP.Add_All_Metadata", "CDM.IndexManager.MGPP.Add_All_Metadata_Tooltip"); replace_button = new GLIButton(); replace_button.setEnabled(false); replace_button.setMnemonic(KeyEvent.VK_C); Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip"); remove_index_button = new GLIButton(); remove_index_button.setEnabled(false); remove_index_button.setMnemonic(KeyEvent.VK_A); Dictionary.registerBoth(remove_index_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip"); JPanel index_empty_panel = new JPanel(); // Connect Indexes EnableAddIndexListener index_eal = new EnableAddIndexListener(); add_index_button.addActionListener(new AddIndexActionListener()); add_all_button.addActionListener(new AddAllActionListener()); current_indexes_list.addListSelectionListener(new CurrentIndexesListSelectionListener()); index_combobox.addActionListener(index_eal); ((JTextField)index_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(index_eal); index_name_field.getDocument().addDocumentListener(index_eal); move_index_down_button.addActionListener(new MoveIndexDownListener()); move_index_up_button.addActionListener(new MoveIndexUpListener()); replace_button.addActionListener(new ReplaceIndexActionListener()); remove_index_button.addActionListener(new RemoveIndexActionListener(index_eal)); // Layout Indexes index_movement_panel.setLayout(new GridLayout(2,1)); index_movement_panel.add(move_index_up_button); index_movement_panel.add(move_index_down_button); current_indexes_panel.setLayout(new BorderLayout()); current_indexes_panel.add(current_indexes_label, BorderLayout.NORTH); current_indexes_panel.add(new JScrollPane(current_indexes_list), BorderLayout.CENTER); current_indexes_panel.add(index_movement_panel, BorderLayout.EAST); index_labels_panel.setLayout(new GridLayout(2,1)); index_labels_panel.add(index_name_label); index_labels_panel.add(index_label); index_boxes_panel.setLayout(new GridLayout(2,1)); index_boxes_panel.add(index_name_field); index_boxes_panel.add(index_combobox); index_details_panel.setLayout(new BorderLayout(5,0)); index_details_panel.add(index_labels_panel, BorderLayout.WEST); index_details_panel.add(index_boxes_panel, BorderLayout.CENTER); index_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); index_button_panel.setLayout(new GridLayout(2,2,5,0)); index_button_panel.add(add_index_button); index_button_panel.add(add_all_button); index_button_panel.add(replace_button); index_button_panel.add(remove_index_button); index_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); index_body_panel.setLayout(new BorderLayout()); index_body_panel.add(index_details_panel, BorderLayout.CENTER); index_body_panel.add(index_button_panel, BorderLayout.SOUTH); index_spacer_panel.setLayout(new BorderLayout()); index_spacer_panel.add(index_body_panel, BorderLayout.NORTH); index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER); indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); indexes_panel.setLayout(new BorderLayout()); indexes_panel.add(current_indexes_panel, BorderLayout.NORTH); indexes_panel.add(index_spacer_panel, BorderLayout.CENTER); // Create Levels JPanel levels_panel = new JPanel(); JPanel current_levels_panel = new JPanel(); current_levels_label = new JLabel(); Dictionary.registerText(current_levels_label, "CDM.IndexManager.MGPP.Current_Levels"); current_levels_list = new JList(levels_model); current_levels_list.setVisibleRowCount(5); JPanel level_movement_panel = new JPanel(); move_level_up_button = new JButton("", Utility.getImage("arrow-up.gif")); move_level_up_button.setEnabled(false); move_level_up_button.setMnemonic(KeyEvent.VK_U); //move_level_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE); Dictionary.registerBoth(move_level_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip"); move_level_down_button = new JButton("", Utility.getImage("arrow-down.gif")); move_level_down_button.setEnabled(false); move_level_down_button.setMnemonic(KeyEvent.VK_D); //move_level_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE); Dictionary.registerBoth(move_level_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip"); JPanel level_spacer_panel = new JPanel(); JPanel level_body_panel = new JPanel(); JPanel level_details_panel = new JPanel(); JPanel level_labels_panel = new JPanel(); JPanel level_boxes_panel = new JPanel(); level_name_label = new JLabel(); Dictionary.registerText(level_name_label, "CDM.IndexManager.MGPP.Level_Name"); level_name_field = new JTextField(); level_name_field.setPreferredSize(FIELD_SIZE); Dictionary.registerTooltip(level_name_field, "CDM.IndexManager.MGPP.Level_Name_Tooltip"); level_label = new JLabel(); Dictionary.registerText(level_label, "CDM.IndexManager.MGPP.Level"); level_combobox = new GComboBox(Index.LEVEL); level_combobox.setPreferredSize(FIELD_SIZE); level_combobox.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false)); level_combobox.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false)); level_combobox.setEditable(true); level_combobox.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false)); level_combobox.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false)); Dictionary.registerTooltip(level_combobox, "CDM.IndexManager.Level_Tooltip"); JPanel level_button_panel = new JPanel(); add_level_button = new GLIButton("CDM.IndexManager.MGPP.Add_Level"); add_level_button.setEnabled(false); add_level_button.setMnemonic(KeyEvent.VK_A); Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip"); remove_level_button = new GLIButton("CDM.IndexManager.MGPP.Remove_Level"); remove_level_button.setEnabled(false); remove_level_button.setMnemonic(KeyEvent.VK_A); Dictionary.registerBoth(remove_level_button, "CDM.IndexManager.MGPP.Remove_Level", "CDM.IndexManager.MGPP.Remove_Level_Tooltip"); JPanel level_empty_panel = new JPanel(); // Connect Levels EnableAddLevelListener level_eal = new EnableAddLevelListener(); add_level_button.addActionListener(new AddLevelActionListener()); current_levels_list.addListSelectionListener(new CurrentLevelsListSelectionListener()); level_combobox.addActionListener(level_eal); ((JTextField)level_combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(level_eal); level_name_field.getDocument().addDocumentListener(level_eal); move_level_down_button.addActionListener(new MoveLevelDownListener()); move_level_up_button.addActionListener(new MoveLevelUpListener()); remove_level_button.addActionListener(new RemoveLevelActionListener(level_eal)); // Layout Levels level_movement_panel.setLayout(new GridLayout(2,1)); level_movement_panel.add(move_level_up_button); level_movement_panel.add(move_level_down_button); current_levels_panel.setLayout(new BorderLayout()); current_levels_panel.add(current_levels_label, BorderLayout.NORTH); current_levels_panel.add(new JScrollPane(current_levels_list), BorderLayout.CENTER); current_levels_panel.add(level_movement_panel, BorderLayout.EAST); level_labels_panel.setLayout(new GridLayout(2,1)); level_labels_panel.add(level_name_label); level_labels_panel.add(level_label); level_boxes_panel.setLayout(new GridLayout(2,1)); level_boxes_panel.add(level_name_field); level_boxes_panel.add(level_combobox); level_details_panel.setLayout(new BorderLayout(5,0)); level_details_panel.add(level_labels_panel, BorderLayout.WEST); level_details_panel.add(level_boxes_panel, BorderLayout.CENTER); level_button_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); level_button_panel.setLayout(new GridLayout(1,2,5,0)); level_button_panel.add(add_level_button); level_button_panel.add(remove_level_button); level_body_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); level_body_panel.setLayout(new BorderLayout()); level_body_panel.add(level_details_panel, BorderLayout.CENTER); level_body_panel.add(level_button_panel, BorderLayout.SOUTH); level_spacer_panel.setLayout(new BorderLayout()); level_spacer_panel.add(level_body_panel, BorderLayout.NORTH); level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER); levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); levels_panel.setLayout(new BorderLayout()); levels_panel.add(current_levels_panel, BorderLayout.NORTH); levels_panel.add(level_spacer_panel, BorderLayout.CENTER); // Create General JPanel header_panel = new JPanel(); title_label = new JLabel(); title_label.setHorizontalAlignment(JLabel.CENTER); Dictionary.registerText(title_label, "CDM.IndexManager.MGPP.Title"); instructions_textarea = new JTextArea(); instructions_textarea.setEditable(false); instructions_textarea.setLineWrap(true); instructions_textarea.setRows(6); instructions_textarea.setWrapStyleWord(true); Dictionary.registerText(instructions_textarea, "CDM.IndexManager.MGPP.Instructions"); tabbed_pane = new JTabbedPane(); // Layout General header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,2,0)); header_panel.setLayout(new BorderLayout()); header_panel.add(title_label, BorderLayout.NORTH); header_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER); tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Indexes"), indexes_panel); tabbed_pane.add(Dictionary.get("CDM.IndexManager.MGPP.Levels"), levels_panel); setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); setLayout(new BorderLayout()); add(header_panel, BorderLayout.NORTH); add(tabbed_pane, BorderLayout.CENTER); } public void destroy() { } public void gainFocus() { // Reload the assigned indexes list. index_combobox.removeAllItems(); index_combobox.addItem(CollectionConfiguration.ALLFIELDS_STR); index_combobox.addItem(CollectionConfiguration.METADATA_STR); index_combobox.addItem(CollectionConfiguration.TEXT_STR); ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement(); for(int i = 0; i < every_metadata_set_element.size(); i++) { index_combobox.addItem(every_metadata_set_element.get(i)); } // Ensure the level manager has at least documents assigned if(levels_model.getSize() == 0) { Level level = new Level(CollectionConfiguration.DOCUMENT_STR); // Create new metadatum CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR); metadatum.setValue(CollectionConfiguration.DOCUMENT_STR); // Assign new level addLevel(level, metadatum); level = null; } // refresh current_indexes_list.updateUI(); } private class AddIndexActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the name String name = index_name_field.getText(); // Retrieve the source Object source = index_combobox.getSelectedItem(); // If this object isn't yet in the combobox add it. if(index_combobox.getSelectedIndex() == -1) { index_combobox.insertItemAt(source, index_combobox.getItemCount()); } // Create new index ArrayList sources = new ArrayList(); sources.add(source); Index index = new Index(sources); if(!model.contains(index)) { // Create new metadatum CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID()); metadatum.setValue(name); // Assign new index addIndex(index, metadatum); } // Done. Disable add add_index_button.setEnabled(false); } } private class AddAllActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { for(int i = 0; i < index_combobox.getItemCount(); i++) { Object source = index_combobox.getItemAt(i); // Create new index ArrayList sources = new ArrayList(); sources.add(source); Index index = new Index(sources); sources = null; if(!model.contains(index)) { // Determine the metadatum value String name = source.toString(); if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) { name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()); } // Create new metadatum CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID()); metadatum.setValue(name); name = null; // Assign new index addIndex(index, metadatum); } source = null; index = null; } // Done. Disable add add_index_button.setEnabled(false); } } private class AddLevelActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the name String name = level_name_field.getText(); // Retrieve the source String source = (String)level_combobox.getSelectedItem(); // If this object isn't yet in the combobox add it. if(level_combobox.getSelectedIndex() == -1) { level_combobox.insertItemAt(source, level_combobox.getItemCount()); } Level level = new Level(source); // Create new metadatum CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + source); metadatum.setValue(name); // Assign new level addLevel(level, metadatum); // Done. Disable add add_level_button.setEnabled(false); } } private class CurrentIndexesListSelectionListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent event) { if(!event.getValueIsAdjusting()) { Index index = (Index)current_indexes_list.getSelectedValue(); Object[] selected_objects = current_indexes_list.getSelectedValues(); if(selected_objects.length == 1) { String full_text = index.toString(); if(full_text.indexOf("\"") != -1) { index_name_field.setText(index.getName()); } ArrayList sources = index.getSources(); index_combobox.setSelectedItem(sources.get(0)); } if(index != null) { move_index_down_button.setEnabled((model.indexOf(index) < model.getSize() - 1)); move_index_up_button.setEnabled((model.indexOf(index) > 0)); remove_index_button.setEnabled(true); } else { move_index_down_button.setEnabled(false); move_index_up_button.setEnabled(false); remove_index_button.setEnabled(false); } } } } private class CurrentLevelsListSelectionListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent event) { if(!event.getValueIsAdjusting()) { Level level = (Level)current_levels_list.getSelectedValue(); if(level != null) { move_level_down_button.setEnabled((levels_model.indexOf(level) < levels_model.getSize() - 1)); move_level_up_button.setEnabled((levels_model.indexOf(level) > 0)); remove_level_button.setEnabled(!level.getName().equals(CollectionConfiguration.DOCUMENT_STR) || levels_model.getSize() > 1); } else { move_level_down_button.setEnabled(false); move_level_up_button.setEnabled(false); remove_level_button.setEnabled(false); } } } } private class EnableAddIndexListener implements ActionListener, DocumentListener { /** Called whenever a selection action occurs on the combobox. * @param event an ActionEvent containing information about the selection event */ public void actionPerformed(ActionEvent event) { validateAddButtonIndex(); } /** Gives notification that an attribute or set of attributes changed. * @param event a DocumentEvent containing information about the text changed */ public void changedUpdate(DocumentEvent event) { validateAddButtonIndex(); } /** Gives notification that there was an insert into the document. * @param event a DocumentEvent containing information about the text added */ public void insertUpdate(DocumentEvent event) { validateAddButtonIndex(); } /** Gives notification that a portion of the document has been removed. * @param event a DocumentEvent containing information about the text removed */ public void removeUpdate(DocumentEvent event) { validateAddButtonIndex(); } /** Change the enable state of the add button depending on the current value in the search type combobox. */ public void validateAddButtonIndex() { String name = index_name_field.getText(); Object selected_object = index_combobox.getSelectedItem(); if(name.length() > 0 && selected_object != null) { // Unfortunately we have to generate a valid id String id = null; if (selected_object instanceof MetadataElement) { id = ((MetadataElement) selected_object).getFullName(); } else { id = selected_object.toString(); } if(id.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) { id = id.substring(StaticStrings.EXTRACTED_NAMESPACE.length()); } Index index = getIndex(id); if(index == null) { add_index_button.setEnabled(true); replace_button.setEnabled(false); } else { add_index_button.setEnabled(false); replace_button.setEnabled(!name.equals(index.getName())); } } else { add_index_button.setEnabled(false); } } } private class EnableAddLevelListener implements ActionListener, DocumentListener { /** Called whenever a selection action occurs on the combobox. * @param event an ActionEvent containing information about the selection event */ public void actionPerformed(ActionEvent event) { validateAddButtonLevel(); } /** Gives notification that an attribute or set of attributes changed. * @param event a DocumentEvent containing information about the text changed */ public void changedUpdate(DocumentEvent event) { validateAddButtonLevel(); } /** Gives notification that there was an insert into the document. * @param event a DocumentEvent containing information about the text added */ public void insertUpdate(DocumentEvent event) { validateAddButtonLevel(); } /** Gives notification that a portion of the document has been removed. * @param event a DocumentEvent containing information about the text removed */ public void removeUpdate(DocumentEvent event) { validateAddButtonLevel(); } /** Change the enable state of the add button depending on the current value in the search type combobox. */ public void validateAddButtonLevel() { String name = level_name_field.getText(); Object selected_object = level_combobox.getSelectedItem(); if(name.length() > 0 && selected_object != null) { add_level_button.setEnabled(getLevel((String)selected_object) == null); } else { add_level_button.setEnabled(false); } } } private class MoveIndexDownListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the first selected item Index index = (Index) current_indexes_list.getSelectedValue(); moveIndex(index, false); current_indexes_list.setSelectedValue(index, true); } } private class MoveLevelDownListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the first selected item Level level = (Level) current_levels_list.getSelectedValue(); moveLevel(level, false); current_levels_list.setSelectedValue(level, true); } } private class MoveIndexUpListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the first selected item Index index = (Index) current_indexes_list.getSelectedValue(); moveIndex(index, true); current_indexes_list.setSelectedValue(index, true); } } private class MoveLevelUpListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Retrieve the first selected item Level level = (Level) current_levels_list.getSelectedValue(); moveLevel(level, true); current_levels_list.setSelectedValue(level, true); } } /** Replace really only replaces the string. */ private class ReplaceIndexActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object[] selected_objects = current_indexes_list.getSelectedValues(); if(selected_objects.length == 1) { Index index = (Index) selected_objects[0]; // Remove old name CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID()); // Enter new name String name = index_name_field.getText(); // Create new metadatum CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + index.getID()); index = null; metadatum.setValue(name); name = null; // Assign new index CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum); metadatum = null; } current_indexes_list.setSelectedValue(selected_objects[0], true); // Done. Disable add add_index_button.setEnabled(false); replace_button.setEnabled(false); } } private class RemoveIndexActionListener implements ActionListener { private EnableAddIndexListener eal = null; public RemoveIndexActionListener(EnableAddIndexListener eal) { this.eal = eal; } public void actionPerformed(ActionEvent event) { // Retrieve the selected items Object[] selected_objects = current_indexes_list.getSelectedValues(); // Clear selection current_indexes_list.clearSelection(); for(int i = 0; i < selected_objects.length; i++) { Index index = (Index) selected_objects[i]; // Remove any related metadata CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + index.getID()); // Remove the index removeIndex(index); } // Disable remove button remove_index_button.setEnabled(false); // Check if add should be reenabled eal.validateAddButtonIndex(); } } private class RemoveLevelActionListener implements ActionListener { private EnableAddLevelListener eal = null; public RemoveLevelActionListener(EnableAddLevelListener eal) { this.eal = eal; } public void actionPerformed(ActionEvent event) { // Retrieve the selected items Object[] selected_objects = current_levels_list.getSelectedValues(); // Clear selection current_levels_list.clearSelection(); for(int i = 0; i < selected_objects.length; i++) { Level level = (Level) selected_objects[i]; // Remove any related metadata CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level.getName()); // Remove the index removeLevel(level); } // Disable remove button remove_level_button.setEnabled(false); // If there are no levels left, put document back in if(levels_model.getSize() == 0) { Level level = new Level(CollectionConfiguration.DOCUMENT_STR); // Create new metadatum CollectionMeta metadatum = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + CollectionConfiguration.DOCUMENT_STR); metadatum.setValue(CollectionConfiguration.DOCUMENT_STR); // Assign new level addLevel(level, metadatum); level = null; } // Check if add should be reenabled eal.validateAddButtonLevel(); } } } // MGPPControls }