source: trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMetaManager.java@ 5859

Last change on this file since 5859 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.0 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.Gatherer;
35import org.greenstone.gatherer.cdm.CollectionDesignManager;
36import org.greenstone.gatherer.cdm.CollectionMeta;
37import org.greenstone.gatherer.cdm.DOMProxyListModel;
38import org.greenstone.gatherer.cdm.Index;
39import org.greenstone.gatherer.util.StaticStrings;
40import org.greenstone.gatherer.util.Utility;
41import org.w3c.dom.*;
42
43/** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data.
44 * @author John Thompson, Greenstone Digital Library, University of Waikato
45 * @version 2.3d
46 */
47public class CollectionMetaManager
48 extends DOMProxyListModel {
49
50 /** Constructor. */
51 public CollectionMetaManager() {
52 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.COLLECTIONMETADATA_ELEMENT, new CollectionMeta(""));
53 Gatherer.println("CollectionMetaManager: " + getSize() + " metadata parsed.");
54 }
55 /** Method to add a new piece of metadata.
56 * @param metadata the new CollectionMeta
57 */
58 public void addMetadatum(CollectionMeta metadata) {
59 if(!contains(metadata)) {
60 Element element = metadata.getElement();
61 // Locate where we should insert this new subcollection.
62 Node target_node = CollectionConfiguration.findInsertionPoint(element);
63 // Failing that we insert immediately after a language string
64 add(root, metadata, target_node);
65 Gatherer.c_man.configurationChanged();
66 }
67 Gatherer.c_man.configurationChanged();
68 }
69
70 public CollectionMeta get(int i) {
71 return (CollectionMeta) getElementAt(i);
72 }
73
74 /** Retrieve all of the general metadata. */
75 public ArrayList getMetadata() {
76 ArrayList result = new ArrayList();
77 int size = getSize();
78 for(int i = 0; i < size; i++) {
79 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
80 if(!metadata.getName().startsWith(StaticStrings.STOP_CHARACTER)) {
81 result.add(metadata);
82 }
83 }
84 return result;
85 }
86
87 /** Retrieve all of the metadata for the given feature, regardless of language. */
88 public ArrayList getMetadata(String name) {
89 ArrayList result = new ArrayList();
90 int size = getSize(); // Refresh DOM Model
91 for(int i = 0; i < size; i++) {
92 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
93 if(metadata.getName().equals(name)) {
94 result.add(metadata);
95 }
96 }
97 return result;
98 }
99
100 /** Retrieve the named piece of metadata, in the default language, if available. If no such metadata is available then it is created.
101 * @param name the name of the metadatum to retrieve as a String
102 * @return the dom Element containing the specified metadatum
103 */
104 public CollectionMeta getMetadatum(String name) {
105 return getMetadatum(name, true);
106 }
107
108 public CollectionMeta getMetadatum(String name, boolean add_if_not_found) {
109 //Gatherer.println("Get the metadata for " + name + " in the default language.");
110 int size = getSize();
111 for(int i = 0; i < size; i++) {
112 CollectionMeta metadatum = (CollectionMeta) getElementAt(i);
113 if(metadatum.getName().equals(name) && metadatum.getLanguage().equals(Gatherer.config.interface_language)) {
114 //Gatherer.println("Found '" + metadatum + "'");
115 return metadatum;
116 }
117 else {
118 //Gatherer.println("No match with: " + metadatum.getName() + " [l=" + metadatum.getLanguage() + "] \"" + metadatum.getValue() + "\"");
119 }
120 metadatum = null;
121 }
122 if(add_if_not_found) {
123 CollectionMeta result = new CollectionMeta(name);
124 addMetadatum(result);
125 //Gatherer.println("Added new metadata.");
126 return result;
127 }
128 else {
129 return null;
130 }
131 }
132
133 /** Method to retrieve a certain piece of metadata based on its name and language.
134 * @param name the name of the metadata as an Object (as it may actually be a refernce to an Index or SubIndex)
135 * @param language the language of the metadata.
136 * @param partial <i>true</i> to return the first partial match (ie matches name but not language).
137 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata.
138 */
139 public CollectionMeta getMetadata(String name, String language, boolean partial) {
140 CollectionMeta partial_match = null;
141 for(int i = 0; i < getSize(); i++) {
142 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
143 Object metadata_name = metadata.getName();
144 // We test the case of an object match (ie Index to Index)...
145 if(metadata_name.equals(name)) {
146 if (metadata.getLanguage().equals(language)) {
147 return metadata;
148 }
149 partial_match = metadata;
150 }
151 }
152 if(partial) {
153 return partial_match;
154 }
155 return null;
156 }
157
158 /** Method to remove a piece of metadata.
159 * @param metadata metadata
160 */
161 public void removeMetadata(CollectionMeta metadata) {
162 if(metadata != null) {
163 String name = metadata.getName();
164 String language = metadata.getLanguage();
165 for(int i = 0; i < getSize(); i++) {
166 CollectionMeta other = (CollectionMeta) getElementAt(i);
167 if(name.equals(other.getName()) && language.equals(other.getLanguage())) {
168 remove(i);
169 Gatherer.c_man.configurationChanged();
170 return;
171 }
172 other = null;
173 }
174 language = null;
175 name = null;
176 }
177 }
178
179 /** Removes all of the metadata with a certain name, regardless of language or value. */
180 public void removeMetadata(String name) {
181 boolean removed_entry = false;
182 for(int i = getSize(); i != 0; i--) {
183 CollectionMeta other = (CollectionMeta) getElementAt(i - 1);
184 if(name.equals(other.getName())) {
185 remove(i - 1);
186 removed_entry = true;
187 }
188 other = null;
189 }
190 if(removed_entry) {
191 Gatherer.c_man.configurationChanged();
192 }
193 }
194
195 public int size() {
196 return getSize();
197 }
198}
Note: See TracBrowser for help on using the repository browser.