source: trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionIndex.java@ 7110

Last change on this file since 7110 was 5590, checked in by mdewsnip, 21 years ago

Could it be I've finished adding tooltips?? Why yes, very nearly... and a big "hallelulah" for that.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29/**************************************************************************************
30 * Written: 04/07/03
31 * Revised:
32 **************************************************************************************/
33import java.util.*;
34import org.greenstone.gatherer.Gatherer;
35import org.greenstone.gatherer.cdm.CollectionConfiguration;
36import org.greenstone.gatherer.cdm.CollectionDesignManager;
37import org.greenstone.gatherer.cdm.CollectionMeta;
38import org.greenstone.gatherer.cdm.DOMProxyListEntry;
39import org.greenstone.gatherer.msm.MSMUtils;
40import org.greenstone.gatherer.util.StaticStrings;
41import org.greenstone.gatherer.util.Utility;
42import org.w3c.dom.*;
43
44/** This class encapsulates a single indexing pair.
45 * @author John Thompson, Greenstone Digital Library, University of Waikato
46 * @version 2.4
47 */
48public class SubcollectionIndex
49 implements Comparable, DOMProxyListEntry {
50
51 private ArrayList sources = null;
52 /** The element this index is based upon. */
53 private Element element = null;
54 /** The unique, if cryptic, identifier of an index. */
55 private String id = null;
56
57 /** Default constructor, which should only be used during DOMProxyListModel creation. */
58 public SubcollectionIndex() {
59 }
60
61 /** Constructor. */
62 public SubcollectionIndex(Element element) {
63 this.element = element;
64 }
65
66 public SubcollectionIndex(Object[] raw_sources) {
67 this.sources = new ArrayList();
68 // Create a new element
69 Document document = CollectionDesignManager.collect_config.document;
70 element = document.createElement(CollectionConfiguration.INDEX_ELEMENT);
71 // For each source add a content element
72 int size = raw_sources.length;
73 for(int i = 0; i < size; i++) {
74 Subcollection subcollection = (Subcollection) raw_sources[i];
75 String subcollection_name = subcollection.getName();
76 sources.add(subcollection_name);
77 Element content_element = document.createElement(CollectionConfiguration.CONTENT_ELEMENT);
78 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, subcollection_name);
79 element.appendChild(content_element);
80 content_element = null;
81 }
82 document = null;
83 }
84
85 /** Method to compare two indexes.
86 * @param object The other index as an <strong>Object</strong>.
87 * @return An <i>int</i> which indicates how the indexes compare.
88 * @see java.lang.String
89 */
90 public int compareTo(Object object) {
91 return id.compareTo(((SubcollectionIndex)object).getID());
92 }
93
94 public DOMProxyListEntry create(Element element) {
95 return new SubcollectionIndex(element);
96 }
97
98 /** Method to test for the equality of two indexes.
99 * @param object The other index as an <strong>Object</strong>.
100 * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
101 */
102 public boolean equals(Object object) {
103 return (compareTo(object) == 0);
104 }
105
106 public Element getElement() {
107 return element;
108 }
109
110 public String getID() {
111 if(id == null) {
112 StringBuffer id_buffer = new StringBuffer();
113 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
114 ArrayList sources = getSources();
115 int sources_size = sources.size();
116 for(int i = 0; i < sources_size; i++) {
117 id_buffer.append((String)sources.get(i));
118 id_buffer.append(StaticStrings.COMMA_CHARACTER);
119 }
120 sources = null;
121 id = id_buffer.substring(0, id_buffer.length() - 1);
122 }
123 return id;
124 }
125
126 /** Retrieve the sources of this index.
127 * @return the sources as an ArrayList
128 */
129 public ArrayList getSources() {
130 if(sources == null) {
131 sources = new ArrayList();
132 NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT);
133 int content_elements_length = content_elements.getLength();
134 for(int i = 0; i < content_elements_length; i++) {
135 Element content_element = (Element) content_elements.item(i);
136 sources.add(content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE));
137 }
138 content_elements = null;
139 Collections.sort(sources);
140 }
141 return sources;
142 }
143
144 public boolean isAssigned() {
145 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
146 }
147
148 public void setAssigned(boolean assigned) {
149 if(element != null) {
150 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
151 }
152 }
153
154 public void setElement(Element element) {
155 this.element = element;
156 this.id = null;
157 this.sources = null;
158 }
159
160 /** Method to set the sources for this index which can only be used for the default index.
161 * @param sources an ArrayList of source names
162 */
163 public void setSources(ArrayList sources) {
164 if(element != null) {
165 // Erase old sources
166 MSMUtils.clear(element);
167 // For each entry in the sources array add a new content element.
168 int size = sources.size();
169 for(int i = 0; i < size; i++) {
170 Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
171 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, (String) sources.get(i));
172 element.appendChild(content_element);
173 content_element = null;
174 }
175 this.id = null; // Regenerate ID.
176 this.sources = sources;
177 }
178 else {
179 Gatherer.println("Error! Called setSource() of index other than the default.");
180 }
181 }
182
183 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
184 * @return A <strong>String</strong> containing the information of this class.
185 */
186 public String toString() {
187 String id = getID();
188 StringBuffer text = new StringBuffer(id);
189 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + id, false);
190 if(metadatum != null) {
191 text.append(" \"");
192 text.append(metadatum.getValue(CollectionMeta.TEXT));
193 text.append("\"");
194 }
195 return text.toString();
196 }
197}
Note: See TracBrowser for help on using the repository browser.