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 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.awt.*;
|
---|
30 | import java.awt.event.*;
|
---|
31 | import java.io.File;
|
---|
32 | import java.util.*;
|
---|
33 | import javax.swing.*;
|
---|
34 | import javax.swing.event.*;
|
---|
35 | import javax.swing.filechooser.*;
|
---|
36 | import org.greenstone.gatherer.Configuration;
|
---|
37 | import org.greenstone.gatherer.DebugStream;
|
---|
38 | import org.greenstone.gatherer.Dictionary;
|
---|
39 | import org.greenstone.gatherer.Gatherer;
|
---|
40 | import org.greenstone.gatherer.gui.DesignPaneHeader;
|
---|
41 | import org.greenstone.gatherer.gui.GLIButton;
|
---|
42 | import org.greenstone.gatherer.gui.MetadataElementListCellRenderer;
|
---|
43 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
44 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
45 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
46 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
47 | import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
|
---|
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 | */
|
---|
54 | public 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 | /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
|
---|
90 | * @param mode the new mode as an int
|
---|
91 | */
|
---|
92 | public void modeChanged(int mode) {
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void refreshModel()
|
---|
97 | {
|
---|
98 | // Remove any metadata sets from the model that are no longer loaded
|
---|
99 | ArrayList loaded_metadata_sets = MetadataSetManager.getMetadataSets();
|
---|
100 | for (int i = size() - 1; i >= 0; i--) {
|
---|
101 | MetadataSet metadata_set = (MetadataSet) get(i);
|
---|
102 | if (loaded_metadata_sets.contains(metadata_set) == false) {
|
---|
103 | // System.err.println("Metadata set in list no longer loaded...removing.");
|
---|
104 | remove(i);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | // Add any metadata sets that are loaded but not in the model
|
---|
109 | for (int i = 0; i < loaded_metadata_sets.size(); i++) {
|
---|
110 | MetadataSet loaded_metadata_set = (MetadataSet) loaded_metadata_sets.get(i);
|
---|
111 | boolean in_model = false;
|
---|
112 | for (int j = 0; j < size(); j++) {
|
---|
113 | MetadataSet metadata_set = (MetadataSet) get(j);
|
---|
114 | if (metadata_set.equals(loaded_metadata_set)) {
|
---|
115 | in_model = true;
|
---|
116 | break;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (in_model == false) {
|
---|
121 | // System.err.println("Metadata set loaded but not in list...adding.");
|
---|
122 | addElement(loaded_metadata_set);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /** 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. */
|
---|
129 | private class MetadataSetViewControls
|
---|
130 | extends JPanel
|
---|
131 | implements Control {
|
---|
132 | /** Adds a new set to the collection*/
|
---|
133 | private JButton add_button;
|
---|
134 | /** Will eventually launch GEMS for editing the selected set, for now just gives a message */
|
---|
135 | private JButton edit_button;
|
---|
136 | /** REmoves a set from the collection */
|
---|
137 | private JButton remove_button;
|
---|
138 | /** The label denoting the element list. */
|
---|
139 | private JLabel element_label = null;
|
---|
140 | /** The label denoting the set list. */
|
---|
141 | private JLabel set_label = null;
|
---|
142 | /** The list of elements for the choosen set. */
|
---|
143 | private JList element_list = null;
|
---|
144 | /** The list of sets in this collection. */
|
---|
145 | private JList set_list = null;
|
---|
146 | /** The panel onto which all other panels will be placed. */
|
---|
147 | private JPanel central_pane = null;
|
---|
148 | /** The panel onto which the element list will be placed. */
|
---|
149 | private JPanel element_pane = null;
|
---|
150 | /** The panel containing the set list. */
|
---|
151 | private JPanel set_pane = null;
|
---|
152 |
|
---|
153 | private JScrollPane element_list_scroll_pane;
|
---|
154 |
|
---|
155 | private MetadataElementListCellRenderer element_list_cell_renderer;
|
---|
156 | private MetadataSetListSelectionListener list_listener;
|
---|
157 |
|
---|
158 | /* Constructor.
|
---|
159 | * @see org.greenstone.gatherer.Dictionary
|
---|
160 | * @see org.greenstone.gatherer.cdm.MetadataSetView.ListListener
|
---|
161 | */
|
---|
162 | public MetadataSetViewControls()
|
---|
163 | {
|
---|
164 | // Create visual components
|
---|
165 | central_pane = new JPanel();
|
---|
166 | element_label = new JLabel();
|
---|
167 | //element_label.setHorizontalAlignment(JLabel.CENTER);
|
---|
168 | element_label.setOpaque(true);
|
---|
169 | Dictionary.registerText(element_label, "CDM.MetadataSetManager.Elements");
|
---|
170 | element_list = new JList();
|
---|
171 | element_list_scroll_pane = new JScrollPane();
|
---|
172 | element_list_scroll_pane.setViewportView(element_list);
|
---|
173 | element_list_cell_renderer = new MetadataElementListCellRenderer();
|
---|
174 | element_list.setCellRenderer(element_list_cell_renderer);
|
---|
175 | element_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
176 | element_pane = new JPanel();
|
---|
177 |
|
---|
178 | JPanel header_pane = new DesignPaneHeader("CDM.GUI.MetadataSets", "metadatasets");
|
---|
179 | set_label = new JLabel();
|
---|
180 | //set_label.setHorizontalAlignment(JLabel.CENTER);
|
---|
181 | set_label.setOpaque(true);
|
---|
182 | Dictionary.registerText(set_label, "CDM.MetadataSetManager.Sets");
|
---|
183 | set_list = new JList(model);
|
---|
184 | set_list.setCellRenderer(new MetadataSetListCellRenderer());
|
---|
185 | set_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
186 |
|
---|
187 | set_pane = new JPanel();
|
---|
188 |
|
---|
189 | JPanel button_pane = new JPanel();
|
---|
190 | add_button = new GLIButton();
|
---|
191 | add_button.setEnabled(true);
|
---|
192 | add_button.setMnemonic(KeyEvent.VK_A);
|
---|
193 | Dictionary.registerBoth(add_button, "CDM.MetadataSetManager.Add", "CDM.MetadataSetManager.Add_Tooltip");
|
---|
194 | edit_button = new GLIButton();
|
---|
195 | edit_button.setEnabled(false);
|
---|
196 | edit_button.setMnemonic(KeyEvent.VK_E);
|
---|
197 | Dictionary.registerBoth(edit_button, "CDM.MetadataSetManager.Edit", "CDM.MetadataSetManager.Edit_Tooltip");
|
---|
198 |
|
---|
199 | remove_button = new GLIButton();
|
---|
200 | remove_button.setEnabled(false);
|
---|
201 | remove_button.setMnemonic(KeyEvent.VK_R);
|
---|
202 | Dictionary.registerBoth(remove_button, "CDM.MetadataSetManager.Remove", "CDM.MetadataSetManager.Remove_Tooltip");
|
---|
203 | list_listener = new MetadataSetListSelectionListener();
|
---|
204 |
|
---|
205 | // Add listeners
|
---|
206 | add_button.addActionListener(new AddButtonListener());
|
---|
207 | edit_button.addActionListener(new EditButtonListener());
|
---|
208 | remove_button.addActionListener(new RemoveButtonListener());
|
---|
209 | set_list.addListSelectionListener(list_listener);
|
---|
210 | // Layout
|
---|
211 |
|
---|
212 | set_pane.setLayout(new BorderLayout());
|
---|
213 | set_pane.add(set_label, BorderLayout.NORTH);
|
---|
214 | set_pane.add(new JScrollPane(set_list), BorderLayout.CENTER);
|
---|
215 |
|
---|
216 | element_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
217 | element_pane.setLayout(new BorderLayout());
|
---|
218 | element_pane.add(element_label, BorderLayout.NORTH);
|
---|
219 | element_pane.add(element_list_scroll_pane, BorderLayout.CENTER);
|
---|
220 |
|
---|
221 | central_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
222 | central_pane.setLayout(new GridLayout(2,1));
|
---|
223 | central_pane.add(set_pane);
|
---|
224 | central_pane.add(element_pane);
|
---|
225 |
|
---|
226 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
227 | button_pane.setLayout(new GridLayout(1,3,0,0));
|
---|
228 | button_pane.add(add_button);
|
---|
229 | button_pane.add(edit_button);
|
---|
230 | button_pane.add(remove_button);
|
---|
231 |
|
---|
232 | setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
|
---|
233 | setLayout(new BorderLayout());
|
---|
234 | add(header_pane, BorderLayout.NORTH);
|
---|
235 | add(central_pane, BorderLayout.CENTER);
|
---|
236 | add(button_pane, BorderLayout.SOUTH);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | public void destroy() { }
|
---|
241 |
|
---|
242 |
|
---|
243 | public void gainFocus()
|
---|
244 | {
|
---|
245 |
|
---|
246 | // If no current selection, select first available set
|
---|
247 | if (set_list.isSelectionEmpty() && set_list.getModel().getSize() > 0) {
|
---|
248 | set_list.setSelectedIndex(0);
|
---|
249 | list_listener.valueChanged(new ListSelectionEvent(set_list, 0, 0, true));
|
---|
250 | }
|
---|
251 | if (!set_list.isSelectionEmpty()) {
|
---|
252 | edit_button.setEnabled(true);
|
---|
253 | remove_button.setEnabled(true);
|
---|
254 | } else {
|
---|
255 | edit_button.setEnabled(false);
|
---|
256 | remove_button.setEnabled(false);
|
---|
257 | }
|
---|
258 |
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | public void loseFocus() { }
|
---|
263 |
|
---|
264 |
|
---|
265 | /** Listens for clicks on the add button. */
|
---|
266 | private class AddButtonListener
|
---|
267 | implements ActionListener {
|
---|
268 |
|
---|
269 | /** Called when the add button is clicked.
|
---|
270 | * @param event an ActionEvent containing information about the mouse click
|
---|
271 | */
|
---|
272 | public void actionPerformed(ActionEvent event)
|
---|
273 | {
|
---|
274 | JFileChooser chooser = new JFileChooser(new File(Gatherer.getGLIMetadataDirectoryPath()));
|
---|
275 | chooser.setFileFilter(new MetadataSet.MetadataSetFileFilter());
|
---|
276 | chooser.setDialogTitle(Dictionary.get("CDM.MetadataSetManager.Add"));
|
---|
277 | int return_val = chooser.showDialog(Gatherer.g_man, Dictionary.get("CDM.MetadataSetManager.Chooser.Add"));
|
---|
278 | if (return_val == JFileChooser.APPROVE_OPTION) {
|
---|
279 | Gatherer.c_man.importMetadataSet(new MetadataSet(chooser.getSelectedFile()));
|
---|
280 | refreshModel();
|
---|
281 |
|
---|
282 | // The metadata.xml files may possibly contain metadata for this newly added metadata set
|
---|
283 | // Re-read all the metadata.xml files to account for this case
|
---|
284 | MetadataXMLFileManager.clearMetadataXMLFiles();
|
---|
285 | MetadataXMLFileManager.loadMetadataXMLFiles(new File(Gatherer.c_man.getCollectionImportDirectoryPath()));
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | /** Listens for clicks on the edit button. */
|
---|
292 | private class EditButtonListener
|
---|
293 | implements ActionListener {
|
---|
294 |
|
---|
295 | /** Called when the edit button is clicked.
|
---|
296 | * @param event an ActionEvent containing information about the mouse click
|
---|
297 | */
|
---|
298 | public void actionPerformed(ActionEvent event)
|
---|
299 | {
|
---|
300 | // do a pop up message
|
---|
301 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.MetadataSetManager.Edit_Message"), Dictionary.get("CDM.MetadataSetManager.Edit_Message_Title"), JOptionPane.INFORMATION_MESSAGE);
|
---|
302 |
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | private class MetadataSetListCellRenderer
|
---|
308 | implements ListCellRenderer
|
---|
309 | {
|
---|
310 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
|
---|
311 | {
|
---|
312 | DefaultListCellRenderer default_list_cell_renderer = new DefaultListCellRenderer();
|
---|
313 | JLabel list_cell_label = (JLabel) default_list_cell_renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
---|
314 |
|
---|
315 | if (value instanceof MetadataSet) {
|
---|
316 | MetadataSet metadata_set = (MetadataSet) value;
|
---|
317 | String metadata_set_name = MetadataTools.getMetadataSetAttribute(metadata_set, "Name", Configuration.getLanguage(), "en");
|
---|
318 | list_cell_label.setText("metadataset " + metadata_set.getNamespace() + " \"" + metadata_set_name + "\"");
|
---|
319 | }
|
---|
320 |
|
---|
321 | return list_cell_label;
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | private class MetadataSetListSelectionListener
|
---|
327 | implements ListSelectionListener {
|
---|
328 |
|
---|
329 | public void valueChanged(ListSelectionEvent event)
|
---|
330 | {
|
---|
331 | // Wait until we get a stable event
|
---|
332 | if (event.getValueIsAdjusting()) {
|
---|
333 | return;
|
---|
334 | }
|
---|
335 |
|
---|
336 | // Now we can process it
|
---|
337 | if (!set_list.isSelectionEmpty()) {
|
---|
338 | MetadataSet metadata_set = (MetadataSet) set_list.getSelectedValue();
|
---|
339 | element_list.setListData(new Vector(metadata_set.getMetadataSetElements()));
|
---|
340 | remove_button.setEnabled(true);
|
---|
341 | edit_button.setEnabled(true);
|
---|
342 |
|
---|
343 | // Finally check the directionality and scroll as necessary
|
---|
344 | JScrollBar scroll_bar = element_list_scroll_pane.getHorizontalScrollBar();
|
---|
345 | if (element_list_cell_renderer.getDirectionality() == Character.DIRECTIONALITY_RIGHT_TO_LEFT) {
|
---|
346 | scroll_bar.setValue(scroll_bar.getMaximum());
|
---|
347 | }
|
---|
348 | else {
|
---|
349 | scroll_bar.setValue(scroll_bar.getMinimum());
|
---|
350 | }
|
---|
351 | }
|
---|
352 | else {
|
---|
353 | remove_button.setEnabled(false);
|
---|
354 | edit_button.setEnabled(false);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | /** Listens for clicks on the remove button. */
|
---|
361 | private class RemoveButtonListener
|
---|
362 | implements ActionListener {
|
---|
363 |
|
---|
364 | /** Called when the remove button is clicked.
|
---|
365 | * @param event an ActionEvent containing information about the mouse click
|
---|
366 | */
|
---|
367 | public void actionPerformed(ActionEvent event)
|
---|
368 | {
|
---|
369 | MetadataSet metadata_set = (MetadataSet) set_list.getSelectedValue();
|
---|
370 | Gatherer.c_man.removeMetadataSet(metadata_set);
|
---|
371 | refreshModel();
|
---|
372 | element_list.setListData(new Vector());
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|