source: trunk/gli/src/org/greenstone/gatherer/cdm/MetadataSetManager.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.7 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
43package 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: 16/05/02
50 * Revised: 28/05/02
51 * 17/11/02 - Commented
52 **************************************************************************************/
53import java.awt.BorderLayout;
54import java.awt.Dimension;
55import java.awt.GridLayout;
56import java.awt.Rectangle;
57import java.util.Collections;
58import java.util.Enumeration;
59import java.util.Vector;
60import javax.swing.BorderFactory;
61import javax.swing.JLabel;
62import javax.swing.JList;
63import javax.swing.JPanel;
64import javax.swing.JScrollPane;
65import javax.swing.JTextArea;
66import javax.swing.ListModel;
67import javax.swing.ListSelectionModel;
68import javax.swing.event.ListSelectionEvent;
69import javax.swing.event.ListSelectionListener;
70import org.greenstone.gatherer.Gatherer;
71import org.greenstone.gatherer.cdm.ElementWrapper;
72import org.greenstone.gatherer.msm.MetadataSet;
73import org.greenstone.gatherer.msm.MSMEvent;
74import org.greenstone.gatherer.msm.MSMUtils;
75import org.greenstone.gatherer.msm.MSMListener;
76import org.w3c.dom.Element;
77import org.w3c.dom.Node;
78import org.w3c.dom.NodeList;
79/** Unlike its namesake in the msm package, this class really only knows how to produce a simple visual representation of the currently imported metadata sets. It is also read-only, so should be fairly straight forward.
80 * @author John Thompson, Greenstone Digital Library, University of Waikato
81 * @version 2.3
82 * @see org.greenstone.gatherer.msm.MetadataSetManager
83 */
84public class MetadataSetManager
85 extends DynamicListModel
86 implements MSMListener {
87 /** The visual contols used to review the metadata sets. */
88 private Control controls = null;
89 /** A reference to the Gatherer. */
90 private Gatherer gatherer = null;
91 /** A reference to ourselves so our inner classes can refer to us. */
92 private DynamicListModel model = null;
93 /** Constructor.
94 * @param gatherer A reference to the <strong>Gatherer</strong>.
95 * @see org.greenstone.gatherer.collection.CollectionManager
96 * @see org.greenstone.gatherer.msm.MetadataSetManager
97 */
98 public MetadataSetManager(Gatherer gatherer) {
99 this.gatherer = gatherer;
100 this.model = this;
101 Gatherer.c_man.getCollection().msm.addMSMListener(this);
102 loadMetadataSets();
103 }
104 /** Destructor. Remove any references of this class from persistant objects.
105 * @see org.greenstone.gatherer.Gatherer
106 * @see org.greenstone.gatherer.collection.CollectionManager
107 * @see org.greenstone.gatherer.msm.MetadataSetManager
108 */
109 public void destroy() {
110 gatherer.c_man.msm.removeMSMListener(this);
111 }
112 /** Called when an element is changed within a set in the MSM, prompting us to refresh our list of elements being shown.
113 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change.
114 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
115 */
116 public void elementChanged(MSMEvent event) {
117 // Get the controls to refresh element list.
118 if(controls != null) {
119 controls.refreshElementList();
120 }
121 }
122 /** A method for retrieve the controls for this manager.
123 * @see org.greenstone.gatherer.Dictionary
124 * @see org.greenstone.gatherer.gui.Coloring
125 * @see org.greenstone.gatherer.msm.MetadataSetManager
126 */
127 public Control getControls() {
128 if(controls == null) {
129 controls = new Control();
130 }
131 return controls;
132 }
133 /** A method to invalidate the current set of controls, as they become obselete.
134 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
135 */
136 public void invalidateControls() {
137 if(controls != null) {
138 controls.destroy();
139 }
140 controls = null;
141 }
142 /** Called when a metadata value has undergone significant change.
143 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change.
144 */
145 public void metadataChanged(MSMEvent event) {
146 // Couldn't care less.
147 }
148 /** Called when a set is added or removed from the MSM.
149 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change.
150 */
151 public void setChanged(MSMEvent event) {
152 // Reload model.
153 clear();
154 loadMetadataSets();
155 }
156 /** Select the selected element, given its name, and return the bounds of the selection. Used during search and replace.
157 * @param element The elements fully qualified name as a <strong>String</strong>.
158 * @return The bounds of the selection as a <strong>Rectangle</strong>.
159 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
160 */
161 public Rectangle setSelectedElement(String element) {
162 if(controls != null) {
163 return controls.setSelectedElement(element);
164 }
165 return null;
166 }
167 /** Prints out the contents of this manager, as they would appear in the collection configuration file.
168 * @return A <strong>String</strong> containing a block of commands.
169 * @see org.greenstone.gatherer.cdm.MetadataSetManager.SetWrapper
170 */
171 public String toString() {
172 String text = "";
173 for(int i = 0; i < size(); i++) {
174 SetWrapper set = (SetWrapper)get(i);
175 text = text + set.toString() + "\n";
176 }
177 text = text + "\n";
178 return text;
179 }
180 /** Called when a significant change has occured to a value tree for a certain element, however we take no further action.
181 * @param event A <strong>MSMEvent</strong> containing information relevant to the event.
182 */
183 public void valueChanged(MSMEvent event) {
184 }
185 /** Method to retrieve a phrase from the dictionary based on a key.
186 * @param key A <strong>String</strong> used to find the correct phrase.
187 * @param args A <strong>String[]</strong> of arguments used in formatting and filling out the phrase.
188 * @return A <strong>String</strong> containing the correct phrase with the correct formatting.
189 */
190 private String get(String key) {
191 return get(key, null);
192 }
193 /** Method to retrieve a phrase from the dictionary based on a key.
194 * @param key A <strong>String</strong> used to find the correct phrase.
195 * @return A <strong>String</strong> containing the correct phrase with the correct formatting.
196 * @see org.greenstone.gatherer.Dictionary
197 * @see org.greenstone.gatherer.Gatherer
198 */
199 private String get(String key, String args[]) {
200 if(key.indexOf(".") == -1) {
201 key = "CDM.MetadataSetManager." + key;
202 }
203 return gatherer.dictionary.get(key, args);
204 }
205 /** Retrieves the current list of loaded metadata sets, and builds a list model around them.
206 * @see org.greenstone.gatherer.Gatherer
207 * @see org.greenstone.gatherer.cdm.MetadataSetManager.SetWrapper
208 * @see org.greenstone.gatherer.collection.CollectionManager
209 * @see org.greenstone.gatherer.msm.MetadataSet
210 * @see org.greenstone.gatherer.msm.MetadataSetManager
211 */
212 private void loadMetadataSets() {
213 // We initialize the set_model with wrapped metadata sets. Note that when we call getSets() we also end up adding ourselves as a listener to the metadata set manager.
214 Vector sets = gatherer.c_man.getCollection().msm.getSets();
215 for(int i = 0; i < sets.size(); i++) {
216 addElement(new SetWrapper((MetadataSet)sets.get(i)));
217 }
218 }
219 /** This class creates and lays-out the various controls for reviewing the metadata sets, and their commands as they would appear in the collection configuration file. */
220 private class Control
221 extends JPanel {
222 /** The label denoting the element list. */
223 private JLabel element_label = null;
224 /** The label denoting the set list. */
225 private JLabel set_label = null;
226 /** The title of these controls. */
227 private JLabel title = null;
228 /** The list of elements for the choosen set. */
229 private JList element_list = null;
230 /** The list of sets in this collection. */
231 private JList set_list = null;
232 /** The panel onto which all other panels will be placed. */
233 private JPanel central_pane = null;
234 /** The panel onto which the element list will be placed. */
235 private JPanel element_pane = null;
236 /** The panel containing the title and instructions. */
237 private JPanel header_pane = null;
238 /** The panel containing the set list. */
239 private JPanel set_pane = null;
240 /** The text area of inline instructions. */
241 private JTextArea instructions = null;
242 /** The element model for the currently selected set. */
243 private Vector element_model = null;
244 /* Constructor.
245 * @see org.greenstone.gatherer.Coloring;
246 * @see org.greenstone.gatherer.Dictionary
247 * @see org.greenstone.gatherer.cdm.MetadataSetManager.ListListener
248 * @see org.greenstone.gatherer.msm.MetadataSetManager
249 */
250 public Control() {
251 // Create visual components
252 central_pane = new JPanel();
253 element_label = new JLabel(get("Elements"));
254 element_label.setHorizontalAlignment(JLabel.CENTER);
255 element_label.setOpaque(true);
256 element_list = new JList();
257 element_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
258 element_pane = new JPanel();
259 header_pane = new JPanel();
260 instructions = new JTextArea(get("Instructions"));
261 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
262 instructions.setEditable(false);
263 instructions.setLineWrap(true);
264 instructions.setRows(5);
265 instructions.setWrapStyleWord(true);
266 set_label = new JLabel(get("Sets"));
267 set_label.setHorizontalAlignment(JLabel.CENTER);
268 set_label.setOpaque(true);
269 set_list = new JList(model);
270 set_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
271 set_pane = new JPanel();
272 title = new JLabel(get("Title"));
273 title.setHorizontalAlignment(JLabel.CENTER);
274 title.setOpaque(true);
275 // Add listeners
276 set_list.addListSelectionListener(new ListListener());
277 // Layout
278 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
279
280 header_pane.setLayout(new BorderLayout());
281 header_pane.add(title, BorderLayout.NORTH);
282 header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
283
284 set_pane.setLayout(new BorderLayout());
285 set_pane.add(set_label, BorderLayout.NORTH);
286 set_pane.add(new JScrollPane(set_list), BorderLayout.CENTER);
287
288 element_pane.setLayout(new BorderLayout());
289 element_pane.add(element_label, BorderLayout.NORTH);
290 element_pane.add(new JScrollPane(element_list), BorderLayout.CENTER);
291
292 central_pane.setLayout(new GridLayout(2,1));
293 central_pane.add(set_pane);
294 central_pane.add(element_pane);
295
296 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
297 setLayout(new BorderLayout());
298 add(header_pane, BorderLayout.NORTH);
299 add(central_pane, BorderLayout.CENTER);
300 }
301 /** Destructor.
302 */
303 public void destroy() {
304 }
305 /** Update the element list. */
306 public void refreshElementList() {
307 element_list.updateUI();
308 }
309 /** Select the selected element, given its name, and return the bounds of the selection. Used during search and replace.
310 * @param element The elements fully qualified name as a <strong>String</strong>.
311 * @return The bounds of the selection as a <strong>Rectangle</strong>.
312 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
313 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control.ElementWrapper
314 */
315 public Rectangle setSelectedElement(String element) {
316 Rectangle bounds = null;
317 // Parse off namespace
318 String namespace = element.substring(0, element.indexOf("."));
319 // Force the set list to show the correct entry.
320 ListModel s_model = set_list.getModel();
321 for(int i = 0; i < s_model.getSize(); i++) {
322 SetWrapper sw = (SetWrapper) s_model.getElementAt(i);
323 if(sw.getSet().getNamespace().equals(namespace)) {
324 set_list.setSelectedValue(sw, true);
325 }
326 }
327 // Force the element list to show that name.
328 ListModel e_model = element_list.getModel();
329 for(int i = 0; i < e_model.getSize(); i++) {
330 ElementWrapper ew = (ElementWrapper) e_model.getElementAt(i);
331 if(ew.name().equals(element)) {
332 element_list.setSelectedValue(ew, true);
333 bounds = element_list.getCellBounds(i, i);
334 }
335 }
336 // Return the bounds of the selected row.
337 return bounds;
338 }
339 /** Overriden to ensure the instruction area is scrolled to top.
340 */
341 public void updateUI() {
342 if(instructions != null) {
343 instructions.setCaretPosition(0);
344 }
345 super.updateUI();
346 }
347
348 private class ListListener
349 implements ListSelectionListener {
350 public void valueChanged(ListSelectionEvent event) {
351 if(!set_list.isSelectionEmpty()) {
352 MetadataSet set = ((SetWrapper)set_list.getSelectedValue()).getSet();
353 NodeList elements = set.getElements();
354 element_model = new Vector();
355 for(int i = 0; i < elements.getLength(); i++) {
356 element_model.add(new ElementWrapper(elements.item(i)));
357 }
358 Collections.sort(element_model);
359 element_list.setListData(element_model);
360 }
361 }
362 }
363 }
364 /** Provides a convience wrapper around a metadata set, that allows it to appear in the <strong>JList</strong> the same way it would in the collection configuration file. */
365 private class SetWrapper {
366 private MetadataSet set = null;
367 public SetWrapper(MetadataSet set) {
368 this.set = set;
369 }
370 public MetadataSet getSet() {
371 return set;
372 }
373 public String toString() {
374 String namespace = set.getNamespace();
375 // Watch out for the greenstone set, with its namespace of ""
376 if(namespace == null || namespace.length() == 0) {
377 namespace = "greenstone";
378 }
379 return "metadataset " + namespace + " \"" + set.getFile().getName() + "\"";
380 }
381 }
382}
Note: See TracBrowser for help on using the repository browser.