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

Last change on this file since 10345 was 10345, checked in by mdewsnip, 19 years ago

Removed some more crap out of the Utility class.

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