source: trunk/gli/src/org/greenstone/gatherer/cdm/MetadataSetView.java@ 4932

Last change on this file since 4932 was 4932, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 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;
28import java.awt.*;
29import java.util.*;
30import javax.swing.*;
31import javax.swing.event.*;
32import org.greenstone.gatherer.Gatherer;
33import org.greenstone.gatherer.cdm.CollectionDesignManager;
34import org.greenstone.gatherer.cdm.Control;
35import org.greenstone.gatherer.cdm.ElementWrapper;
36import org.greenstone.gatherer.msm.MetadataSet;
37import org.greenstone.gatherer.msm.MSMEvent;
38import org.greenstone.gatherer.msm.MSMUtils;
39import org.greenstone.gatherer.msm.MSMListener;
40import org.w3c.dom.*;
41/** 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. (It has no namesake any longer as my co-programmers found this far too confusing - however I have now added a CollectionConfiguration to the cdm package just to keep them on their toes!)
42 * @author John Thompson, Greenstone Digital Library, University of Waikato
43 * @version 2.3d
44 * @see org.greenstone.gatherer.msm.MetadataSetManager
45 */
46public class MetadataSetView
47 extends DynamicListModel
48 implements MSMListener {
49 /** The visual contols used to review the metadata sets. */
50 private Control controls = null;
51 /** A reference to ourselves so our inner classes can refer to us. */
52 private DynamicListModel model = null;
53 /** Constructor.
54 */
55 public MetadataSetView() {
56 Gatherer.println("MetadataSetView: Initialized.");
57 model = this;
58 Gatherer.c_man.getCollection().msm.addMSMListener(this);
59 loadMetadataSets();
60 // Build the controls
61 controls = new MetadataSetControl();
62 }
63 /** Destructor. Remove any references of this class from persistant objects.
64 * @see org.greenstone.gatherer.Gatherer
65 * @see org.greenstone.gatherer.collection.CollectionManager
66 * @see org.greenstone.gatherer.msm.MetadataSetManager
67 */
68 public void destroy() {
69 controls.destroy();
70 controls = null;
71 Gatherer.c_man.msm.removeMSMListener(this);
72 model = null;
73 }
74 /** Called when an element is changed within a set in the MSM, prompting us to refresh our list of elements being shown.
75 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change.
76 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
77 */
78 public void elementChanged(MSMEvent event) {
79 // Get the controls to refresh element list.
80 ((MetadataSetControl)controls).refreshElementList();
81 }
82 /** A method for retrieve the controls for this manager.
83 * @see org.greenstone.gatherer.Dictionary
84 * @see org.greenstone.gatherer.gui.Coloring
85 * @see org.greenstone.gatherer.msm.MetadataSetManager
86 */
87 public Control getControls() {
88 return controls;
89 }
90 /** Called when a metadata value has undergone significant change.
91 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change.
92 */
93 public void metadataChanged(MSMEvent event) {
94 // Couldn't care less.
95 }
96 /** Called when a set is added or removed from the MSM.
97 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change.
98 */
99 public void setChanged(MSMEvent event) {
100 // Reload model.
101 clear();
102 loadMetadataSets();
103 }
104 /** Select the selected element, given its name, and return the bounds of the selection. Used during search and replace.
105 * @param element The elements fully qualified name as a <strong>String</strong>.
106 * @return The bounds of the selection as a <strong>Rectangle</strong>.
107 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
108 */
109 public Rectangle setSelectedElement(String element) {
110 return ((MetadataSetControl)controls).setSelectedElement(element);
111 }
112 /** Prints out the contents of this manager, as they would appear in the collection configuration file.
113 * @return A <strong>String</strong> containing a block of commands.
114 * @see org.greenstone.gatherer.cdm.MetadataSetManager.SetWrapper
115 */
116 public String toString() {
117 String text = "";
118 for(int i = 0; i < size(); i++) {
119 SetWrapper set = (SetWrapper)get(i);
120 text = text + set.toString() + "\n";
121 }
122 text = text + "\n";
123 return text;
124 }
125 /** Called when a significant change has occured to a value tree for a certain element, however we take no further action.
126 * @param event A <strong>MSMEvent</strong> containing information relevant to the event.
127 */
128 public void valueChanged(MSMEvent event) {
129 }
130 /** Method to retrieve a phrase from the dictionary based on a key.
131 * @param key A <strong>String</strong> used to find the correct phrase.
132 * @param args A <strong>String[]</strong> of arguments used in formatting and filling out the phrase.
133 * @return A <strong>String</strong> containing the correct phrase with the correct formatting.
134 */
135 private String get(String key) {
136 return get(key, null);
137 }
138 /** Method to retrieve a phrase from the dictionary based on a key.
139 * @param key A <strong>String</strong> used to find the correct phrase.
140 * @return A <strong>String</strong> containing the correct phrase with the correct formatting.
141 * @see org.greenstone.gatherer.Dictionary
142 * @see org.greenstone.gatherer.Gatherer
143 */
144 private String get(String key, String args[]) {
145 if(key.indexOf(".") == -1) {
146 key = "CDM.MetadataSetManager." + key;
147 }
148 return Gatherer.dictionary.get(key, args);
149 }
150 /** Retrieves the current list of loaded metadata sets, and builds a list model around them.
151 * @see org.greenstone.gatherer.Gatherer
152 * @see org.greenstone.gatherer.cdm.MetadataSetManager.SetWrapper
153 * @see org.greenstone.gatherer.collection.CollectionManager
154 * @see org.greenstone.gatherer.msm.MetadataSet
155 * @see org.greenstone.gatherer.msm.MetadataSetManager
156 */
157 private void loadMetadataSets() {
158 // 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.
159 Vector sets = Gatherer.c_man.getCollection().msm.getSets();
160 for(int i = 0; i < sets.size(); i++) {
161 addElement(new SetWrapper((MetadataSet)sets.get(i)));
162 }
163 }
164 /** 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. */
165 private class MetadataSetControl
166 extends JPanel
167 implements Control {
168 /** The label denoting the element list. */
169 private JLabel element_label = null;
170 /** The label denoting the set list. */
171 private JLabel set_label = null;
172 /** The title of these controls. */
173 private JLabel title = null;
174 /** The list of elements for the choosen set. */
175 private JList element_list = null;
176 /** The list of sets in this collection. */
177 private JList set_list = null;
178 /** The panel onto which all other panels will be placed. */
179 private JPanel central_pane = null;
180 /** The panel onto which the element list will be placed. */
181 private JPanel element_pane = null;
182 /** The panel containing the title and instructions. */
183 private JPanel header_pane = null;
184 /** The panel containing the set list. */
185 private JPanel set_pane = null;
186 /** The text area of inline instructions. */
187 private JTextArea instructions = null;
188 /** The element model for the currently selected set. */
189 private Vector element_model = null;
190 /* Constructor.
191 * @see org.greenstone.gatherer.Coloring;
192 * @see org.greenstone.gatherer.Dictionary
193 * @see org.greenstone.gatherer.cdm.MetadataSetManager.ListListener
194 * @see org.greenstone.gatherer.msm.MetadataSetManager
195 */
196 public MetadataSetControl() {
197 // Create visual components
198 central_pane = new JPanel();
199 element_label = new JLabel(get("Elements"));
200 element_label.setHorizontalAlignment(JLabel.CENTER);
201 element_label.setOpaque(true);
202 element_list = new JList();
203 element_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
204 element_pane = new JPanel();
205 header_pane = new JPanel();
206 instructions = new JTextArea(get("Instructions"));
207 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
208 instructions.setEditable(false);
209 instructions.setLineWrap(true);
210 instructions.setRows(5);
211 instructions.setWrapStyleWord(true);
212 set_label = new JLabel(get("Sets"));
213 set_label.setHorizontalAlignment(JLabel.CENTER);
214 set_label.setOpaque(true);
215 set_list = new JList(model);
216 set_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
217 set_pane = new JPanel();
218 title = new JLabel(get("Title"));
219 title.setHorizontalAlignment(JLabel.CENTER);
220 title.setOpaque(true);
221 // Add listeners
222 set_list.addListSelectionListener(new ListListener());
223 // Layout
224 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
225
226 header_pane.setLayout(new BorderLayout());
227 header_pane.add(title, BorderLayout.NORTH);
228 header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
229
230 set_pane.setLayout(new BorderLayout());
231 set_pane.add(set_label, BorderLayout.NORTH);
232 set_pane.add(new JScrollPane(set_list), BorderLayout.CENTER);
233
234 element_pane.setLayout(new BorderLayout());
235 element_pane.add(element_label, BorderLayout.NORTH);
236 element_pane.add(new JScrollPane(element_list), BorderLayout.CENTER);
237
238 central_pane.setLayout(new GridLayout(2,1));
239 central_pane.add(set_pane);
240 central_pane.add(element_pane);
241
242 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
243 setLayout(new BorderLayout());
244 add(header_pane, BorderLayout.NORTH);
245 add(central_pane, BorderLayout.CENTER);
246 }
247 /** Destructor. */
248 public void destroy() {
249 }
250 /** Update the element list. */
251 public void refreshElementList() {
252 element_list.updateUI();
253 }
254 /** Select the selected element, given its name, and return the bounds of the selection. Used during search and replace.
255 * @param element The elements fully qualified name as a <strong>String</strong>.
256 * @return The bounds of the selection as a <strong>Rectangle</strong>.
257 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control
258 * @see org.greenstone.gatherer.cdm.MetadataSetManager.Control.ElementWrapper
259 */
260 public Rectangle setSelectedElement(String element) {
261 Rectangle bounds = null;
262 // Parse off namespace
263 String namespace = element.substring(0, element.indexOf("."));
264 // Force the set list to show the correct entry.
265 ListModel s_model = set_list.getModel();
266 for(int i = 0; i < s_model.getSize(); i++) {
267 SetWrapper sw = (SetWrapper) s_model.getElementAt(i);
268 if(sw.getSet().getNamespace().equals(namespace)) {
269 set_list.setSelectedValue(sw, true);
270 }
271 }
272 // Force the element list to show that name.
273 ListModel e_model = element_list.getModel();
274 for(int i = 0; i < e_model.getSize(); i++) {
275 ElementWrapper ew = (ElementWrapper) e_model.getElementAt(i);
276 if(ew.name().equals(element)) {
277 element_list.setSelectedValue(ew, true);
278 bounds = element_list.getCellBounds(i, i);
279 }
280 }
281 // Return the bounds of the selected row.
282 return bounds;
283 }
284 /** Overriden to ensure the instruction area is scrolled to top.
285 */
286 public void gainFocus() {
287 if(instructions != null) {
288 instructions.setCaretPosition(0);
289 }
290 }
291
292 public void loseFocus() {
293 }
294
295 private class ListListener
296 implements ListSelectionListener {
297 public void valueChanged(ListSelectionEvent event) {
298 if(!set_list.isSelectionEmpty()) {
299 MetadataSet set = ((SetWrapper)set_list.getSelectedValue()).getSet();
300 NodeList elements = set.getElements();
301 element_model = new Vector();
302 for(int i = 0; i < elements.getLength(); i++) {
303 element_model.add(new ElementWrapper(elements.item(i)));
304 }
305 Collections.sort(element_model);
306 element_list.setListData(element_model);
307 }
308 }
309 }
310 }
311 /** 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. */
312 private class SetWrapper {
313 private MetadataSet set = null;
314 public SetWrapper(MetadataSet set) {
315 this.set = set;
316 }
317 public MetadataSet getSet() {
318 return set;
319 }
320 public String toString() {
321 String namespace = set.getNamespace();
322 // Watch out for the greenstone set, with its namespace of ""
323 if(namespace == null || namespace.length() == 0) {
324 namespace = "greenstone";
325 }
326 return "metadataset " + namespace + " \"" + set.getFile().getName() + "\"";
327 }
328 }
329}
Note: See TracBrowser for help on using the repository browser.