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

Last change on this file since 8853 was 8386, checked in by kjdon, 20 years ago

added a edit set button which just pops up a message teling the user about GEMS

  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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.io.File;
32import java.util.*;
33import javax.swing.*;
34import javax.swing.event.*;
35import javax.swing.filechooser.*;
36import org.greenstone.gatherer.Configuration;
37import org.greenstone.gatherer.DebugStream;
38import org.greenstone.gatherer.Dictionary;
39import org.greenstone.gatherer.Gatherer;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.gui.MetadataElementListCellRenderer;
42import org.greenstone.gatherer.metadata.MetadataElement;
43import org.greenstone.gatherer.metadata.MetadataSet;
44import org.greenstone.gatherer.metadata.MetadataSetManager;
45import org.greenstone.gatherer.metadata.MetadataTools;
46import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
47import org.greenstone.gatherer.util.Utility;
48
49
50/** This class 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.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3d
53 */
54public class MetadataSetView
55 extends DynamicListModel {
56
57 /** The visual contols used to review the metadata sets. */
58 private Control controls = null;
59 /** A reference to ourselves so our inner classes can refer to us. */
60 private DynamicListModel model = null;
61
62
63 public MetadataSetView()
64 {
65 model = this;
66
67 // Initialise the model
68 refreshModel();
69
70 // Build the controls
71 controls = new MetadataSetViewControls();
72 }
73
74
75 public void destroy()
76 {
77 controls.destroy();
78 controls = null;
79 model = null;
80 }
81
82
83 /** A method for retrieve the controls for this manager.
84 */
85 public Control getControls() {
86 return controls;
87 }
88
89
90 public void refreshModel()
91 {
92 // Remove any metadata sets from the model that are no longer loaded
93 ArrayList loaded_metadata_sets = MetadataSetManager.getMetadataSets();
94 for (int i = size() - 1; i >= 0; i--) {
95 MetadataSet metadata_set = (MetadataSet) get(i);
96 if (loaded_metadata_sets.contains(metadata_set) == false) {
97 // System.err.println("Metadata set in list no longer loaded...removing.");
98 remove(i);
99 }
100 }
101
102 // Add any metadata sets that are loaded but not in the model
103 for (int i = 0; i < loaded_metadata_sets.size(); i++) {
104 MetadataSet loaded_metadata_set = (MetadataSet) loaded_metadata_sets.get(i);
105 boolean in_model = false;
106 for (int j = 0; j < size(); j++) {
107 MetadataSet metadata_set = (MetadataSet) get(j);
108 if (metadata_set.equals(loaded_metadata_set)) {
109 in_model = true;
110 break;
111 }
112 }
113
114 if (in_model == false) {
115 // System.err.println("Metadata set loaded but not in list...adding.");
116 addElement(loaded_metadata_set);
117 }
118 }
119 }
120
121
122 /** 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. */
123 private class MetadataSetViewControls
124 extends JPanel
125 implements Control {
126 /** Adds a new set to the collection*/
127 private JButton add_button;
128 /** Will eventually launch GEMS for editing the selected set, for now just gives a message */
129 private JButton edit_button;
130 /** REmoves a set from the collection */
131 private JButton remove_button;
132 /** The label denoting the element list. */
133 private JLabel element_label = null;
134 /** The label denoting the set list. */
135 private JLabel set_label = null;
136 /** The title of these controls. */
137 private JLabel title = null;
138 /** The list of elements for the choosen set. */
139 private JList element_list = null;
140 /** The list of sets in this collection. */
141 private JList set_list = null;
142 /** The panel onto which all other panels will be placed. */
143 private JPanel central_pane = null;
144 /** The panel onto which the element list will be placed. */
145 private JPanel element_pane = null;
146 /** The panel containing the title and instructions. */
147 private JPanel header_pane = null;
148 /** The panel containing the set list. */
149 private JPanel set_pane = null;
150
151 private JScrollPane element_list_scroll_pane;
152 /** The text area of inline instructions. */
153 private JTextArea instructions = null;
154
155 private MetadataElementListCellRenderer element_list_cell_renderer;
156 private MetadataSetListSelectionListener list_listener;
157
158 /* Constructor.
159 * @see org.greenstone.gatherer.Coloring;
160 * @see org.greenstone.gatherer.Dictionary
161 * @see org.greenstone.gatherer.cdm.MetadataSetView.ListListener
162 */
163 public MetadataSetViewControls()
164 {
165 // Create visual components
166 central_pane = new JPanel();
167 element_label = new JLabel();
168 element_label.setHorizontalAlignment(JLabel.CENTER);
169 element_label.setOpaque(true);
170 Dictionary.registerText(element_label, "CDM.MetadataSetManager.Elements");
171 element_list = new JList();
172 element_list_scroll_pane = new JScrollPane();
173 element_list_scroll_pane.setViewportView(element_list);
174 element_list_cell_renderer = new MetadataElementListCellRenderer();
175 element_list.setCellRenderer(element_list_cell_renderer);
176 element_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
177 element_pane = new JPanel();
178 header_pane = new JPanel();
179 instructions = new JTextArea();
180 instructions.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
181 instructions.setEditable(false);
182 instructions.setLineWrap(true);
183 instructions.setRows(6);
184 instructions.setWrapStyleWord(true);
185 Dictionary.registerText(instructions, "CDM.MetadataSetManager.Instructions");
186 set_label = new JLabel();
187 set_label.setHorizontalAlignment(JLabel.CENTER);
188 set_label.setOpaque(true);
189 Dictionary.registerText(set_label, "CDM.MetadataSetManager.Sets");
190 set_list = new JList(model);
191 set_list.setCellRenderer(new MetadataSetListCellRenderer());
192 set_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
193
194 set_pane = new JPanel();
195 title = new JLabel();
196 title.setHorizontalAlignment(JLabel.CENTER);
197 title.setOpaque(true);
198 Dictionary.registerText(title, "CDM.MetadataSetManager.Title");
199
200 JPanel button_pane = new JPanel();
201 add_button = new GLIButton();
202 add_button.setEnabled(true);
203 add_button.setMnemonic(KeyEvent.VK_A);
204 Dictionary.registerBoth(add_button, "CDM.MetadataSetManager.Add", "CDM.MetadataSetManager.Add_Tooltip");
205 edit_button = new GLIButton();
206 edit_button.setEnabled(false);
207 edit_button.setMnemonic(KeyEvent.VK_E);
208 Dictionary.registerBoth(edit_button, "CDM.MetadataSetManager.Edit", "CDM.MetadataSetManager.Edit_Tooltip");
209
210 remove_button = new GLIButton();
211 remove_button.setEnabled(false);
212 remove_button.setMnemonic(KeyEvent.VK_R);
213 Dictionary.registerBoth(remove_button, "CDM.MetadataSetManager.Remove", "CDM.MetadataSetManager.Remove_Tooltip");
214 list_listener = new MetadataSetListSelectionListener();
215
216 // Add listeners
217 add_button.addActionListener(new AddButtonListener());
218 edit_button.addActionListener(new EditButtonListener());
219 remove_button.addActionListener(new RemoveButtonListener());
220 set_list.addListSelectionListener(list_listener);
221 // Layout
222 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
223
224 header_pane.setLayout(new BorderLayout());
225 header_pane.add(title, BorderLayout.NORTH);
226 header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
227
228 set_pane.setLayout(new BorderLayout());
229 set_pane.add(set_label, BorderLayout.NORTH);
230 set_pane.add(new JScrollPane(set_list), BorderLayout.CENTER);
231
232 element_pane.setLayout(new BorderLayout());
233 element_pane.add(element_label, BorderLayout.NORTH);
234 element_pane.add(element_list_scroll_pane, BorderLayout.CENTER);
235
236 central_pane.setLayout(new GridLayout(2,1));
237 central_pane.add(set_pane);
238 central_pane.add(element_pane);
239
240 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
241 button_pane.setLayout(new GridLayout(1,3,0,0));
242 button_pane.add(add_button);
243 button_pane.add(edit_button);
244 button_pane.add(remove_button);
245
246 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
247 setLayout(new BorderLayout());
248 add(header_pane, BorderLayout.NORTH);
249 add(central_pane, BorderLayout.CENTER);
250 add(button_pane, BorderLayout.SOUTH);
251 }
252
253
254 public void destroy() { }
255
256
257 public void gainFocus()
258 {
259 // Ensure the instructions area is scrolled to the top
260 if (instructions != null) {
261 instructions.setCaretPosition(0);
262 }
263
264 // If no current selection, select first available set
265 if (set_list.isSelectionEmpty() && set_list.getModel().getSize() > 0) {
266 set_list.setSelectedIndex(0);
267 list_listener.valueChanged(new ListSelectionEvent(set_list, 0, 0, true));
268 }
269 if (!set_list.isSelectionEmpty()) {
270 edit_button.setEnabled(true);
271 remove_button.setEnabled(true);
272 } else {
273 edit_button.setEnabled(false);
274 remove_button.setEnabled(false);
275 }
276
277 }
278
279
280 public void loseFocus() { }
281
282
283 /** Listens for clicks on the add button. */
284 private class AddButtonListener
285 implements ActionListener {
286
287 /** Called when the add button is clicked.
288 * @param event an ActionEvent containing information about the mouse click
289 */
290 public void actionPerformed(ActionEvent event)
291 {
292 JFileChooser chooser = new JFileChooser(new File(Utility.METADATA_DIR));
293 chooser.setFileFilter(new MetadataSet.MetadataSetFileFilter());
294 int return_val = chooser.showDialog(Gatherer.g_man, Dictionary.get("MSMPrompt.File_Import"));
295 if (return_val == JFileChooser.APPROVE_OPTION) {
296 Gatherer.c_man.importMetadataSet(new MetadataSet(chooser.getSelectedFile()));
297 refreshModel();
298
299 // The metadata.xml files may possibly contain metadata for this newly added metadata set
300 // Re-read all the metadata.xml files to account for this case
301 MetadataXMLFileManager.clearMetadataXMLFiles();
302 MetadataXMLFileManager.loadMetadataXMLFiles(new File(Gatherer.c_man.getCollectionImport()));
303 }
304 }
305 }
306
307
308 /** Listens for clicks on the edit button. */
309 private class EditButtonListener
310 implements ActionListener {
311
312 /** Called when the edit button is clicked.
313 * @param event an ActionEvent containing information about the mouse click
314 */
315 public void actionPerformed(ActionEvent event)
316 {
317 // do a pop up message
318 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.MetadataSetManager.Edit_Message"), Dictionary.get("CDM.MetadataSetManager.Edit_Message_Title"), JOptionPane.INFORMATION_MESSAGE);
319
320 }
321 }
322
323
324 private class MetadataSetListCellRenderer
325 implements ListCellRenderer
326 {
327 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
328 {
329 DefaultListCellRenderer default_list_cell_renderer = new DefaultListCellRenderer();
330 JLabel list_cell_label = (JLabel) default_list_cell_renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
331
332 if (value instanceof MetadataSet) {
333 MetadataSet metadata_set = (MetadataSet) value;
334 String metadata_set_name = MetadataTools.getMetadataSetAttribute(metadata_set, "Name", Configuration.getLanguage(), "en");
335 list_cell_label.setText("metadataset " + metadata_set.getNamespace() + " \"" + metadata_set_name + "\"");
336 }
337
338 return list_cell_label;
339 }
340 }
341
342
343 private class MetadataSetListSelectionListener
344 implements ListSelectionListener {
345
346 public void valueChanged(ListSelectionEvent event)
347 {
348 // Wait until we get a stable event
349 if (event.getValueIsAdjusting()) {
350 return;
351 }
352
353 // Now we can process it
354 if (!set_list.isSelectionEmpty()) {
355 MetadataSet metadata_set = (MetadataSet) set_list.getSelectedValue();
356 element_list.setListData(new Vector(metadata_set.getMetadataSetElements()));
357 remove_button.setEnabled(true);
358 edit_button.setEnabled(true);
359
360 // Finally check the directionality and scroll as necessary
361 JScrollBar scroll_bar = element_list_scroll_pane.getHorizontalScrollBar();
362 if (element_list_cell_renderer.getDirectionality() == Character.DIRECTIONALITY_RIGHT_TO_LEFT) {
363 scroll_bar.setValue(scroll_bar.getMaximum());
364 }
365 else {
366 scroll_bar.setValue(scroll_bar.getMinimum());
367 }
368 }
369 else {
370 remove_button.setEnabled(false);
371 edit_button.setEnabled(false);
372 }
373 }
374 }
375
376
377 /** Listens for clicks on the remove button. */
378 private class RemoveButtonListener
379 implements ActionListener {
380
381 /** Called when the remove button is clicked.
382 * @param event an ActionEvent containing information about the mouse click
383 */
384 public void actionPerformed(ActionEvent event)
385 {
386 MetadataSet metadata_set = (MetadataSet) set_list.getSelectedValue();
387 Gatherer.c_man.removeMetadataSet(metadata_set);
388 refreshModel();
389 element_list.setListData(new Vector());
390 }
391 }
392 }
393}
Note: See TracBrowser for help on using the repository browser.