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

Last change on this file since 4366 was 4366, checked in by kjdon, 21 years ago

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 14.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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Copyright: Copyright (c) 2001
49 * Company: The University of Waikato
50 * Written: /05/02
51 * Revised: 22/08/02 Revamped, Optimized and Commented.
52 **************************************************************************************/
53import java.awt.BorderLayout;
54import java.awt.Dimension;
55import java.awt.GridLayout;
56import java.awt.Toolkit;
57import java.awt.event.ActionEvent;
58import java.awt.event.ActionListener;
59import java.awt.event.FocusAdapter;
60import java.awt.event.FocusEvent;
61import java.awt.event.KeyAdapter;
62import java.awt.event.KeyEvent;
63import java.util.ArrayList;
64import java.util.Collections;
65import javax.swing.BorderFactory;
66import javax.swing.ButtonGroup;
67import javax.swing.DefaultListModel;
68import javax.swing.JButton;
69import javax.swing.JComboBox;
70import javax.swing.JDialog;
71import javax.swing.JLabel;
72import javax.swing.JList;
73import javax.swing.JPanel;
74import javax.swing.JScrollPane;
75import javax.swing.JTextArea;
76import javax.swing.JTextField;
77import javax.swing.ListSelectionModel;
78import javax.swing.event.ListSelectionEvent;
79import javax.swing.event.ListSelectionListener;
80import org.greenstone.gatherer.Gatherer;
81import org.greenstone.gatherer.cdm.CollectionDesignManager;
82import org.greenstone.gatherer.cdm.CollectionMeta;
83import org.greenstone.gatherer.cdm.Index;
84import org.greenstone.gatherer.cdm.Language;
85import org.greenstone.gatherer.gui.EditorDialog;
86import org.greenstone.gatherer.util.Utility;
87/** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data.
88 * @author John Thompson, Greenstone Digital Library, University of Waikato
89 * @version 2.3
90 */
91// ####################################################################################
92// Optimization Saving
93// ####################################################################################
94// Removed Vector and Hashtable + Memory, + Processor
95// ####################################################################################
96public class CollectionMetaManager
97 extends DefaultListModel {
98 /** A reference to the cdm manager so we can access indexes and languages. */
99 private CollectionDesignManager manager = null;
100 /** A reference to ourself so that inner classes can use us as a model. */
101 private DefaultListModel model = null;
102 /** A reference to the Gatherer. */
103 private Gatherer gatherer = null;
104 /** The language the most recent metadata returned was in. */
105 private Language current_language = null;
106 /** We can't safely parse metadata commands until after all the other commands have been parsed, so we store commands here for now. */
107 private ArrayList unresolved_commands = null;
108 /** Constructor.
109 * @param gatherer A reference to the <strong>Gatherer</strong>.
110 * @param manager A reference to the <strong>CollectionDesignManager</strong> for access to other configuration managers.
111 */
112 public CollectionMetaManager(Gatherer gatherer, CollectionDesignManager manager) {
113 super();
114 this.gatherer = gatherer;
115 this.manager = manager;
116 this.model = this;
117 this.unresolved_commands = new ArrayList();
118 }
119 /** Method to add a new piece of metadata.
120 * @param metadata The new <strong>CollectionMeta</strong>.
121 */
122 public void addMetadata(CollectionMeta metadata) {
123 CollectionMeta existing = getMetadata(metadata.getName().toString(), metadata.getLanguage(), false);
124 if(existing != null) {
125 removeElement(existing);
126 }
127 addElement(metadata);
128 gatherer.c_man.configurationChanged();
129 }
130 /** Retrieve the collectionextra metadata in the default language, if present.
131 * @return A <strong>CollectionMeta</strong> containing the collectionextra in the default language if present, or else the first collectionextra of any language, otherwise <i>null</i>.
132 * @see org.greenstone.gatherer.cdm.Language
133 */
134 public CollectionMeta getCollectionExtra() {
135 CollectionMeta result = getMetadata("collectionextra", manager.languages.getDefaultLanguage(), true);
136 if(result == null) {
137 result = new CollectionMeta(manager, "collectionextra", manager.languages.getDefaultLanguage(), "");
138 addMetadata(result);
139 }
140 return result;
141 }
142 /** Retrieve the collectionname metadata in the default language, if present.
143 * @return A <strong>CollectionMeta</strong> containing the collectionname in the default language if present, or else the first collectionname of any language, otherwise <i>null</i>.
144 * @see org.greenstone.gatherer.cdm.Language
145 */
146 public CollectionMeta getCollectionName() {
147 return getMetadata("collectionname", manager.languages.getDefaultLanguage(), true);
148 }
149 /** Retrieve the iconcollection metadata in the default language, if present.
150 * @return A <strong>CollectionMeta</strong> containing the iconcollection in the default language if present, or else the first iconcollection of any language, otherwise <i>null</i>.
151 * @see org.greenstone.gatherer.cdm.Language
152 */
153 public CollectionMeta getIconCollection() {
154 CollectionMeta result = getMetadata("iconcollection", manager.languages.getDefaultLanguage(), true);
155 if(result == null) {
156 result = new CollectionMeta(manager, "iconcollection", manager.languages.getDefaultLanguage(), "");
157 addMetadata(result);
158 }
159 return result;
160 }
161 /** Method to retrieve the list of metadata.
162 * @return An <strong>ArrayList</strong> containing the metadata.
163 */
164 public ArrayList getMetadata() {
165 ArrayList metadata = new ArrayList();
166 for(int i = 0; i < size(); i++) {
167 metadata.add(get(i));
168 }
169 Collections.sort(metadata);
170 return metadata;
171 }
172
173 /** Retrieve all of the metadata for the given feature, regardless of language. */
174 public ArrayList getMetadata(String name) {
175 ArrayList result = new ArrayList();
176 for(int i = 0; i < size(); i++) {
177 CollectionMeta metadata = (CollectionMeta) get(i);
178 if(metadata.getName().equals(name)) {
179 result.add(metadata);
180 }
181 }
182 return result;
183 }
184 /** Method to retrieve a certain piece of metadata based on its name and language.
185 * @param name The name of the metadata as a <strong>String</strong>.
186 * @param language The <strong>Language</strong> of the metadata.
187 * @param partial <i>true</i> to return the first partial match (ie matches name but not language).
188 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata.
189 */
190 public CollectionMeta getMetadata(String name, Language language, boolean partial) {
191 CollectionMeta partial_match = null;
192 for(int i = 0; i < size(); i++) {
193 CollectionMeta metadata = (CollectionMeta) get(i);
194 if(metadata.getName().equals(name)) {
195 if (metadata.getLanguage().equals(language)) {
196 return metadata;
197 }
198 partial_match = metadata;
199 }
200 }
201 if(partial) {
202 return partial_match;
203 }
204 return null;
205 }
206 /** Method that attempts to parse a collection metadata command from the given text. If a command is parsed successfully it is immediately added to the the collections metadata.
207 * @param command The command text we wish to parse, as a <strong>String</strong>.
208 * @return A <i>boolean</i> which is <i>true</i> if a collection metadata command was successfully parsed, <i>false</i> otherwise.
209 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
210 * @see org.greenstone.gatherer.cdm.CommandTokenizer
211 * @see org.greenstone.gatherer.cdm.Language
212 * @see org.greenstone.gatherer.cdm.LanguageManager
213 * @see org.greenstone.gatherer.util.Utility
214 */
215 public boolean parse(String command, boolean finished) {
216 String temp = command.toLowerCase();
217 if(temp.startsWith("collectionmeta")) {
218 if(finished) {
219 CommandTokenizer ct = new CommandTokenizer(command);
220 if(ct.countTokens() >= 3) {
221 ct.nextToken(); // Throw away collectionmeta
222 Object key = ct.nextToken();
223 Language language = null;
224 String language_code = null;
225 String value = ct.nextToken();
226 // Arg. Remember a language token will be '[l=<code>]'
227 if(value.startsWith("[") && value.endsWith("]")) {
228 language_code = value.substring(3, value.length() - 1);
229 ///ystem.err.println("Language code = " + language_code);
230 value = ct.nextToken();
231 }
232 // Check if the key is an index, an if so retrieve it.
233 if(((String)key).startsWith(".")) {
234 String key_str = (String)key;
235 key_str = key_str.substring(1);
236 key = manager.indexes.getIndex(key_str);
237 }
238 // An if we have a language code, retrieve its object too.
239 if(language_code != null) {
240 language = manager.languages.getLanguage(language_code, false);
241 }
242 // Otherwise set language to the default language.
243 else {
244 language = manager.languages.getDefaultLanguage();
245 }
246 if(key != null) {
247 // Trim any "
248 if(value.equals("\"\"")) {
249 value = "";
250 }
251 else {
252 if(value.startsWith("\"")) {
253 value = value.substring(1);
254 }
255 if(value.endsWith("\"")) {
256 value = value.substring(0, value.length() - 1);
257 }
258 }
259 CollectionMeta meta = new CollectionMeta(manager, key, language, Utility.decodeGreenstone(value));
260 addMetadata(meta);
261 }
262 return true;
263 }
264 }
265 else {
266 unresolved_commands.add(command);
267 return true;
268 }
269 }
270 return false;
271 }
272 /** Ensure that the values being showed are the most up to date. */
273 public void refresh() {
274 fireContentsChanged(this, 0, size());
275 }
276
277 /** Method to remove a piece of metadata.
278 * @param metadata The <strong>CollectionMeta</strong> you wish to remove.
279 */
280 public void removeMetadata(CollectionMeta metadata) {
281 removeElement(metadata);
282 gatherer.c_man.configurationChanged();
283 }
284 /** Method which attempts to reparse obvious metadata commands which used unresovable references.
285 */
286 public void reparseUnresolved() {
287 for(int i = 0; i < unresolved_commands.size(); i++) {
288 parse((String)unresolved_commands.get(i), true);
289 }
290 // Regardless of if they work, clear the commands.
291 unresolved_commands.clear();
292 }
293 /** Sets the value of a certain metadata.
294 * @param name The name of the metadata as a <strong>String</strong>.
295 * @param language The <strong>Language</strong> to use.
296 * @param value The value of the metadata also as a <strong>String</strong>.
297 */
298 public void setMetadata(String name, Language language, String value) {
299 addMetadata(new CollectionMeta(manager, name, language, value));
300 }
301 /** Method to produce the list of metadata in a string such as you would find in the collection configuration file.
302 * @return A <strong>String</strong> containing the list of collection metadata.
303 */
304 public String toString() {
305 StringBuffer text = new StringBuffer("");
306 for(int i = 0; i < size(); i++) {
307 CollectionMeta data = (CollectionMeta) get(i);
308 if(data.valid()) {
309 text.append(data.toString());
310 }
311 }
312 text.append("\n");
313 return text.toString();
314 }
315 /** Overloaded to call get with both a key and an empty argument array.
316 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
317 * @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.
318 */
319 private String get(String key) {
320 return get(key, null);
321 }
322 /** 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 * 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>.
324 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
325 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
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 * @see org.greenstone.gatherer.Gatherer
328 * @see org.greenstone.gatherer.Dictionary
329 */
330 private String get(String key, String args[]) {
331 if(key.indexOf('.') == -1) {
332 key = "CDM.CollectionMetaManager." + key;
333 }
334 return gatherer.dictionary.get(key, args);
335 }
336}
Note: See TracBrowser for help on using the repository browser.