- Timestamp:
- 2003-07-15T13:55:22+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java
r4675 r4932 6 6 * University of Waikato, New Zealand. 7 7 * 8 * <BR><BR>9 *10 8 * Author: John Thompson, Greenstone Digital Library, University of Waikato 11 9 * 12 * <BR><BR>13 *14 10 * Copyright (C) 1999 New Zealand Digital Library Project 15 *16 * <BR><BR>17 11 * 18 12 * This program is free software; you can redistribute it and/or modify … … 21 15 * (at your option) any later version. 22 16 * 23 * <BR><BR>24 *25 17 * This program is distributed in the hope that it will be useful, 26 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 27 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 20 * GNU General Public License for more details. 29 *30 * <BR><BR>31 21 * 32 22 * You should have received a copy of the GNU General Public License … … 35 25 *######################################################################## 36 26 */ 37 38 39 40 41 42 43 27 package org.greenstone.gatherer.cdm; 44 /************************************************************************************** 45 * Title: Gatherer 46 * Description: The Gatherer: a tool for gathering and enriching a digital collection. 47 * Copyright: Copyright (c) 2001 48 * Company: The University of Waikato 49 * Written: 03/05/02 50 * Revised: 17/11/02 - Commented 51 **************************************************************************************/ 52 import java.awt.BorderLayout; 53 import java.awt.Color; 54 import java.awt.Component; 55 import java.awt.Dimension; 56 import java.awt.GridLayout; 57 import java.awt.event.ActionEvent; 58 import java.awt.event.ActionListener; 59 import java.awt.event.KeyAdapter; 60 import java.awt.event.KeyEvent; 61 import java.util.Collections; 62 import java.util.StringTokenizer; 63 import java.util.Vector; 64 import javax.swing.BorderFactory; 65 import javax.swing.DefaultListCellRenderer; 66 import javax.swing.DefaultListModel; 67 import javax.swing.JButton; 68 import javax.swing.JComboBox; 69 import javax.swing.JLabel; 70 import javax.swing.JList; 71 import javax.swing.JOptionPane; 72 import javax.swing.JPanel; 73 import javax.swing.JScrollPane; 74 import javax.swing.JTextArea; 75 import javax.swing.JTextField; 76 import javax.swing.event.ListDataEvent; 77 import javax.swing.event.ListDataListener; 78 import javax.swing.event.ListSelectionEvent; 79 import javax.swing.event.ListSelectionListener; 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.util.*; 31 import javax.swing.*; 32 import javax.swing.event.*; 80 33 import org.greenstone.gatherer.Gatherer; 34 import org.greenstone.gatherer.cdm.CollectionMeta; 81 35 import org.greenstone.gatherer.cdm.CommandTokenizer; 36 import org.greenstone.gatherer.cdm.Control; 37 import org.greenstone.gatherer.cdm.DOMProxyListModel; 82 38 import org.greenstone.gatherer.cdm.Index; 83 39 import org.greenstone.gatherer.msm.ElementWrapper; 84 40 import org.greenstone.gatherer.msm.MSMUtils; 85 41 import org.greenstone.gatherer.util.ExclusiveListSelectionListener; 86 import org.w3c.dom.Element; 42 import org.greenstone.gatherer.util.StaticStrings; 43 import org.w3c.dom.*; 87 44 /** 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. 88 45 * @author John Thompson, Greenstone Digital Library, University of Waikato … … 90 47 */ 91 48 public class IndexManager 92 extends DefaultListModel { 93 /** A reference to our creator, the collection design manager. */ 94 private CollectionDesignManager manager = null; 49 extends DOMProxyListModel { 95 50 /** The controls for editing the indexes. */ 96 51 private Control controls = null; 97 52 /** A reference to ourselves so our inner methods have access. */ 98 private DefaultListModel model = null; 99 /** A reference to the Gatherer, for access to the Dictionary and messaging purposes. */ 100 private Gatherer gatherer = null; 53 private DOMProxyListModel model = null; 101 54 /** The default index. */ 102 55 private Index default_index = null; 103 /** Constructor. 104 * @param gatherer A reference to the <strong>Gatherer</strong>. 105 * @param manager A reference to the <strong>CollectionDesignManager</strong>. 106 */ 107 public IndexManager(Gatherer gatherer, CollectionDesignManager manager) { 108 super(); 109 this.gatherer = gatherer; 110 this.manager = manager; 111 this.model = this; 56 /** Constructor. */ 57 public IndexManager(Element indexes) { 58 super(indexes, CollectionConfiguration.INDEX_ELEMENT, new Index()); 59 Gatherer.println("IndexManager: " + getSize() + " indexes parsed."); 60 model = this; 61 // Parse and retrieve the default index 62 NodeList default_index_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.INDEX_DEFAULT_ELEMENT); 63 if(default_index_elements.getLength() > 0) { 64 default_index = new Index((Element)default_index_elements.item(0)); 65 } 112 66 } 113 67 /** Method to add a new index. 114 * @param index The <strong>Index</strong> to add. 115 * @see org.greenstone.gatherer.Gatherer 116 * @see org.greenstone.gatherer.collection.CollectionManager 117 */ 118 public void addIndex(Index index) { 68 * @param index The <strong>Index</strong> to add. 69 * @see org.greenstone.gatherer.Gatherer 70 * @see org.greenstone.gatherer.collection.CollectionManager 71 */ 72 public void addIndex(Index index, CollectionMeta metadatum) { 73 ///ystem.err.println("Adding an index: " + index.toString()); 119 74 if(!contains(index)) { 120 String index_str = index.toString(false).toLowerCase(); 121 // Add alphabetically. 122 for(int i = 0; i < size(); i++) { 123 Index sibling = (Index) get(i); 124 String sibling_str = sibling.toString(false).toLowerCase(); 125 int position = index_str.compareTo(sibling_str); 126 // Sibling is before index. 127 if(position > 0) { 128 // Carry on. 129 } 130 // Index is equal to, or before sibling. Insert it. 131 else if(position == 0 || position < 0) { 132 add(i, index); 133 gatherer.c_man.configurationChanged(); 134 return; 135 } 136 } 137 // If we got this far, we haven't inserted index, and we are out of model so. 138 addElement(index); 139 gatherer.c_man.configurationChanged(); 75 CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum); 76 // Retrieve the currently last index 77 Index last_index = (Index)getElementAt(getSize() - 1); 78 addAfter(index, last_index); 79 Gatherer.c_man.configurationChanged(); 140 80 } 141 81 else { 142 JOptionPane.showMessageDialog(manager.gui, get("CDM.IndexManager.Index_Exists"), get("General.Warning"), JOptionPane.WARNING_MESSAGE); 143 } 144 } 145 /** Method to acquire the controls for editing the indexes. 146 * @return A <strong>JPanel</strong> containing the controls. 147 * @see org.greenstone.gatherer.cdm.IndexManager.Control 148 */ 149 public JPanel getControls() { 150 if(controls == null) { 151 controls = new Control(); 152 } 153 return controls; 154 } 155 /** Method to get the default index. 156 * @return The default <strong>Index</strong>. 157 */ 158 public Index getDefault() { 159 return default_index; 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 < size()) { 167 return (Index)get(index); 168 } 169 return null; 170 } 171 /** Method to retrieve a certain index, given its name. 172 * @param name The name of the index as a <Strong>String</strong>. 173 * @return The <strong>Index</strong> that matches name, or <i>null</i> if no such index exists. 174 */ 175 public Index getIndex(String name) { 176 ///ystem.err.println("Searching for index " + name); 177 for(int i = 0; i < size(); i++) { 178 Index index = (Index) get(i); 179 if(index.toString(false).equals(name)) { 180 ///ystem.err.println("Found."); 181 return (Index)get(i); 182 } 183 } 184 ///ystem.err.println("No such index."); 185 return null; 186 } 187 /** A method to retrieve all of the indexes associated with this manager. 188 * @return A <strong>Vector</strong> of <strong>Index</strong>es. 189 */ 190 public Vector getIndexes() { 191 Vector indexes = new Vector(); 192 for(int i = 0; i < size(); i++) { 193 indexes.add(get(i)); 194 } 195 Collections.sort(indexes); 196 return indexes; 197 } 198 /** Mark the current set of controls, if any, as obsolete and deallocate them. If further need of the controls will cause new controls to be created. 199 * @see org.greenstone.gatherer.cdm.IndexManager.Control 200 */ 201 public void invalidateControls() { 82 JOptionPane.showMessageDialog(Gatherer.g_man, get("CDM.IndexManager.Index_Exists"), get("General.Warning"), JOptionPane.WARNING_MESSAGE); 83 } 84 } 85 86 public void destroy() { 202 87 if(controls != null) { 203 88 controls.destroy(); 204 } 205 controls = null; 206 } 207 /** Method that attempts to parse an index related command from the given command. If such a command is parsed, it is immediately registered with this manager. 208 * @param command The <strong>String</strong> to parse. 209 * @return A <i>boolean</i> which is <i>true</i> if a command was parsed, <i>false</i> otherwise. 210 * @see org.greenstone.gatherer.cdm.CommandTokenizer 211 * @see org.greenstone.gatherer.cdm.Index 212 */ 213 public boolean parse(String command) { 214 String temp = command.toLowerCase(); 215 if(temp.startsWith("indexes")) { 216 CommandTokenizer ct = new CommandTokenizer(command); 217 ct.nextToken(); // Throw away indexes. 218 while(ct.hasMoreTokens()) { 219 String entry = ct.nextToken(); 220 Index index = Index.parseIndexConfig(entry, manager); 221 if (index != null) { 222 addIndex(index); 223 224 } else { // return false if there is one error?? 225 return false; 226 } 227 } 228 229 return true; 230 } 231 else if(temp.startsWith("defaultindex")) { 232 CommandTokenizer ct = new CommandTokenizer(command); 233 ct.nextToken(); 234 String entry = ct.nextToken(); 235 Index index = Index.parseIndexConfig(entry, manager); 236 if (index != null) { 237 setDefault(index); 238 return true; 239 } 240 return false; 241 } 242 243 return false; 244 } 245 246 public void removeAll() { 247 removeAllElements(); 89 controls = null; 90 } 248 91 default_index = null; 249 gatherer.c_man.configurationChanged(); 92 model = null; 93 } 94 95 /** Method to acquire the controls for editing the indexes. 96 * @return the Control 97 */ 98 public Control getControls() { 99 if(controls == null) { 100 // Build controls 101 controls = new IndexControl(); 102 } 103 return controls; 104 } 105 106 /** Method to get the default index. 107 * @return The default <strong>Index</strong>. 108 */ 109 public Index getDefault() { 110 if(default_index != null && default_index.isAssigned()) { 111 return default_index; 112 } 113 else { 114 return null; 115 } 116 } 117 118 /** Method to retrieve a certain index, as referenced by an index number. 119 * @param index An <i>int</i> which indicates the position of the desired index. 120 * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists. 121 */ 122 public Index getIndex(int index) { 123 if(0 <= index && index < getSize()) { 124 return (Index)getElementAt(index); 125 } 126 return null; 127 } 128 129 /** Method to retrieve a certain index, given its id. 130 * @param id the id of the index as a String 131 * @return the Index that matches id, or null if no such index exists 132 */ 133 public Index getIndex(String id) { 134 int size = getSize(); 135 for(int i = 0; i < size; i++) { 136 Index index = (Index) getElementAt(i); 137 if(index.getID().equals(id)) { 138 return index; 139 } 140 } 141 return null; 142 } 143 144 public ArrayList getIndexes() { 145 return children(); 250 146 } 251 147 252 148 /** Method to remove a certain index. 253 * @param index The <Strong>Index</strong>to remove.254 255 256 257 258 149 * @param index the Index to remove. 150 * @see org.greenstone.gatherer.Gatherer 151 * @see org.greenstone.gatherer.cdm.CollectionDesignManager 152 * @see org.greenstone.gatherer.cdm.CollectionMetaManager 153 * @see org.greenstone.gatherer.collection.CollectionManager 154 */ 259 155 public void removeIndex(Index index) { 260 156 if(index != null) { 261 String name = index.getName(); 262 if(name != null) { 263 Language default_language = manager.languages.getDefaultLanguage(); 264 CollectionMeta metadata = new CollectionMeta(manager, index, default_language, name); 265 manager.collectionmetadatum.removeMetadata(metadata); 266 } 267 removeElement(index); 157 // Remove any current metadata from this index 158 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + index.getID()); 159 // Check if the index removed happens to be the default index 268 160 if(default_index != null && default_index.equals(index)) { 269 default_index = null; 270 } 271 gatherer.c_man.configurationChanged(); 272 } 273 } 161 default_index.setAssigned(false); 162 } 163 // Remove the index 164 remove(index); 165 Gatherer.c_man.configurationChanged(); 166 } 167 } 168 274 169 /** Method to set the default index. 275 * @param index The new default <strong>Index</strong>. 276 277 278 170 * @param index the new default Index 171 * @see org.greenstone.gatherer.Gatherer 172 * @see org.greenstone.gatherer.collection.CollectionManager 173 */ 279 174 public void setDefault(Index index) { 280 default_index = index; 281 if(index != null && !contains(index)) { 282 addElement(index); 283 } 284 gatherer.c_man.configurationChanged(); 285 } 286 /** Method to print out the contents of this class as a string. 287 * @return A <strong>String</strong> just as it would appear in the colleciton configuration file. 288 */ 289 public String toString() { 290 String text = ""; 291 // First the indexes. 292 if(size() > 0) { 293 text = "indexes "; 294 for(int i = 0; i < size(); i++) { 295 Index index = (Index) get(i); 296 text = text + index.toStringConfig(); 297 if(i < size() - 1) { 298 text = text + " "; 299 } 300 } 301 // Now the default index if there is one, or just the first index 302 // if there isn't 175 if(index != null) { 176 if(default_index == null) { 177 // Create the default index element, and place immediately after indexes element. 178 Element default_index_element = root.getOwnerDocument().createElement(CollectionConfiguration.INDEX_DEFAULT_ELEMENT); 179 default_index = new Index(default_index_element); 180 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element); 181 if(target_node != null) { 182 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node); 183 } 184 else { 185 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element); 186 } 187 } 188 default_index.setAssigned(true); 189 default_index.setLevel(index.getLevel()); 190 default_index.setSources(index.getSources()); 191 } 192 else { 303 193 if(default_index != null) { 304 text = text + "\ndefaultindex " + default_index.toStringConfig() + "\n"; 305 } 306 text = text + "\n"; 307 } 308 return text; 309 } 310 311 312 313 194 default_index.setAssigned(false); 195 } 196 } 197 Gatherer.c_man.configurationChanged(); 198 } 314 199 315 200 /** Overloaded to call get with both a key and an empty argument array. 316 317 318 201 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle. 202 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call. 203 */ 319 204 private String get(String key) { 320 205 return get(key, null); 321 206 } 207 322 208 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR> 323 324 325 326 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.327 328 329 209 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>. 210 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle. 211 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String. 212 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatically populated with formatting Strings of with argument String provided in the get call. 213 * @see org.greenstone.gatherer.Gatherer 214 * @see org.greenstone.gatherer.Dictionary 215 */ 330 216 private String get(String key, String args[]) { 331 217 if(key.indexOf('.') == -1) { 332 218 key = "CDM.IndexManager." + key; 333 219 } 334 return gatherer.dictionary.get(key, args); 335 } 220 return Gatherer.dictionary.get(key, args); 221 } 222 336 223 /** This class creates a set of controls for editing the indexes. */ 337 private class Control 338 extends JPanel { 224 private class IndexControl 225 extends JPanel 226 implements Control { 339 227 /** The default size of a label on this control. */ 340 228 private Dimension LABEL_SIZE = new Dimension(120,25); … … 406 294 * @see org.greenstone.gatherer.util.ExclusiveListSelectionListener 407 295 */ 408 public Control() {296 public IndexControl() { 409 297 super(); 410 298 source_model = new Vector(); 411 299 source_model.add("text"); 412 source_model.addAll(gatherer.c_man.msm.getAssignedElements()); 413 //source_model.addAll(gatherer.c_man.msm.getElements()); 300 source_model.addAll(Gatherer.c_man.getCollection().msm.getAssignedElements()); 414 301 // Creation 415 302 add = new JButton(get("Add")); … … 428 315 default_pane = new JPanel(); 429 316 if(default_index != null) { 430 default_value = new JTextField(default_index.toString( true));317 default_value = new JTextField(default_index.toString()); 431 318 default_value.setCaretPosition(0); 432 319 } … … 440 327 index_label = new JLabel(get("Indexes")); 441 328 index_list = new JList(model); 442 index_list.setCellRenderer(new IndexListCellRenderer());443 329 instructions = new JTextArea(get("Instructions")); 444 330 instructions.setEditable(false); … … 550 436 } 551 437 /* Destructor, removes persistant listeners from the Dictionary. 552 438 */ 553 439 public void destroy() { 554 440 } 555 /** We override the updateUI method so that we can ensure we are scrolled to the top of the instructions box first. 556 * @see org.greenstone.gatherer.Gatherer 557 * @see org.greenstone.gatherer.collection.CollectionManager 558 * @see org.greenstone.gatherer.msm.MetadataSetManager 559 */ 560 public void updateUI() { 441 442 public void gainFocus() { 561 443 if(instructions != null) { 562 444 instructions.setCaretPosition(0); … … 566 448 source_model.clear(); 567 449 source_model.add("text"); 568 source_model.addAll( gatherer.c_man.msm.getAssignedElements());450 source_model.addAll(Gatherer.c_man.getCollection().msm.getAssignedElements()); 569 451 // reset the model in the list - needed if the model is larger than when it was first created, the list doesn't display 570 452 source_list.setListData(source_model); 571 453 572 454 } 573 // Faster than a NPE its the - 'Super class'. 574 super.updateUI(); 575 } 455 } 456 457 public void loseFocus() { 458 } 459 576 460 /** 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. */ 577 461 private class AddListener … … 589 473 if(!source_list.isSelectionEmpty() && name.getText().length() != 0) { 590 474 Object object[] = source_list.getSelectedValues(); 591 Vector sources = new Vector();475 ArrayList sources = new ArrayList(); 592 476 for(int i = 0; i < object.length; i++) { 593 477 sources.add(object[i]); 594 478 } 595 Index index = new Index(level.getSelectedIndex(), sources, manager); 479 object = null; 480 Index index = new Index(level.getSelectedIndex(), sources); 481 sources = null; 596 482 // Before we add the index to the model, we have to add the collection metadata for this. 597 Language language = manager.languages.getDefaultLanguage(); 598 CollectionMeta metadata = new CollectionMeta(manager, index, language, name.getText()); 599 manager.collectionmetadatum.addMetadata(metadata); 483 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID()); 484 metadatum.setValue(name.getText()); 600 485 // Finally add index. 601 addIndex(index); 486 addIndex(index, metadatum); 487 metadatum = null; 488 index = null; 602 489 } 603 490 } … … 606 493 private class ClearDefaultListener 607 494 implements ActionListener { 608 609 610 495 /** If called when an action occurs on a registered component, we clear the default index. 496 * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured. 497 */ 611 498 public void actionPerformed(ActionEvent event) { 612 499 setDefault(null); … … 643 530 if(default_index != null) { 644 531 clear_default.setEnabled(true); 645 default_value.setText(default_index.toString( true));532 default_value.setText(default_index.toString()); 646 533 default_value.setCaretPosition(0); 647 534 } … … 698 585 } 699 586 } 700 701 702 private class IndexListCellRenderer703 extends DefaultListCellRenderer {704 705 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {706 StringBuffer text = new StringBuffer(value.toString());707 // Retrieve the indexes name if any.708 CollectionMeta metadata = manager.collectionmetadatum.getMetadata(value, manager.languages.getDefaultLanguage(), true);709 if(metadata != null) {710 text.append(" \"");711 text.append(metadata.getValue());712 text.append("\"");713 }714 return super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);715 }716 717 }718 719 587 } 720 588 }
Note:
See TracChangeset
for help on using the changeset viewer.