source: trunk/gli/src/org/greenstone/gatherer/gui/MetadataSetDialog.java@ 13680

Last change on this file since 13680 was 12763, checked in by mdewsnip, 18 years ago

Now updates the metadata sets and metadata table when GEMS is closed after editing a metadata set.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1package org.greenstone.gatherer.gui;
2
3import java.awt.*;
4import java.awt.event.*;
5import javax.swing.*;
6import javax.swing.event.*;
7import java.util.ArrayList;
8import java.util.Vector;
9import java.io.File;
10import java.util.Observer;
11import java.util.Observable;
12
13import org.greenstone.gatherer.Configuration;
14import org.greenstone.gatherer.Dictionary;
15import org.greenstone.gatherer.Gatherer;
16import org.greenstone.gatherer.cdm.DynamicListModel;
17import org.greenstone.gatherer.metadata.MetadataSet;
18import org.greenstone.gatherer.metadata.MetadataSetManager;
19import org.greenstone.gatherer.gems.*;
20
21public class MetadataSetDialog
22 extends ModalDialog {
23
24 static private Dimension SIZE = new Dimension(600, 300);
25 static private Dimension ADD_SIZE = new Dimension(600, 500);
26
27 private ArrayList current_metadata_sets;
28 private DynamicListModel current_metadata_model;
29
30 private JButton add_button = null;
31 private JButton edit_button = null;
32 private JButton remove_button = null;
33
34 private JButton close_button = null;
35
36 private JList current_set_list = null;
37 private MetadataSetDialog set_dialog = null;
38 private boolean sets_changed = false;
39
40 private GEMS gems = null;
41
42 public MetadataSetDialog() {
43 super(Gatherer.g_man, true);
44 set_dialog = this;
45
46 setJMenuBar(new SimpleMenuBar("selectingmetadatasets"));
47 setSize(SIZE);
48 setTitle(Dictionary.get("MetadataSetDialog.Title"));
49
50 current_metadata_sets = MetadataSetManager.getMetadataSets();
51 current_metadata_model = new DynamicListModel();
52
53 int current_size = current_metadata_sets.size();
54 for (int i=0; i<current_size; i++) {
55 current_metadata_model.addElement(current_metadata_sets.get(i));
56 }
57 JPanel content_pane = (JPanel) getContentPane();
58 content_pane.setOpaque(true);
59
60 JLabel current_metadata_sets_label = new JLabel(Dictionary.get("MetadataSetDialog.Current_Sets"));
61 current_metadata_sets_label.setOpaque(true);
62 current_set_list = new JList(current_metadata_model);
63 current_set_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
64
65 JPanel button_pane = new JPanel();
66 add_button = new GLIButton(Dictionary.get("MetadataSetDialog.Add"), Dictionary.get("MetadataSetDialog.Add_Tooltip"));
67 add_button.setEnabled(true);
68
69 edit_button = new GLIButton(Dictionary.get("MetadataSetDialog.Edit"), Dictionary.get("MetadataSetDialog.Edit_Tooltip"));
70 edit_button.setEnabled(false);
71
72 remove_button = new GLIButton(Dictionary.get("MetadataSetDialog.Remove"), Dictionary.get("MetadataSetDialog.Remove_Tooltip"));
73 remove_button.setEnabled(false);
74
75 close_button = new GLIButton(Dictionary.get("General.Close"), Dictionary.get("General.Close_Tooltip"));
76 close_button.setEnabled(true);
77
78
79 // Add listeners
80 add_button.addActionListener(new AddButtonListener());
81 edit_button.addActionListener(new EditButtonListener());
82 remove_button.addActionListener(new RemoveButtonListener());
83 close_button.addActionListener(new ActionListener() {
84 public void actionPerformed(ActionEvent event) {
85 set_dialog.dispose();
86 }
87 });
88 current_set_list.addListSelectionListener(new MetadataSetListSelectionListener());
89
90 button_pane.setLayout(new GridLayout(2,3));
91 button_pane.add(add_button);
92 button_pane.add(edit_button);
93 button_pane.add(remove_button);
94 button_pane.add(new JPanel());
95 button_pane.add(new JPanel());
96 button_pane.add(close_button);
97
98 content_pane.setLayout(new BorderLayout());
99 content_pane.add(current_metadata_sets_label, BorderLayout.NORTH);
100 content_pane.add(new JScrollPane(current_set_list), BorderLayout.CENTER);
101 content_pane.add(button_pane, BorderLayout.SOUTH);
102
103 // Show
104 Dimension screen_size = Configuration.screen_size;
105 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
106 setVisible(true);
107
108 }
109
110 public boolean setsChanged() {
111 return sets_changed;
112 }
113
114 private class AddButtonListener
115 implements ActionListener {
116
117 public void actionPerformed(ActionEvent event) {
118 AddMetadataSetPrompt amsp = new AddMetadataSetPrompt();
119 if (!amsp.isCancelled()) {
120 sets_changed = true;
121 }
122 }
123 }
124
125 private class EditButtonListener
126 implements ActionListener, GEMSListener {
127
128 public void actionPerformed(ActionEvent event) {
129 // do a pop up message
130 MetadataSet metadata_set = (MetadataSet) current_set_list.getSelectedValue();
131 String metadata_path = metadata_set.getMetadataSetFile().toString();
132 if (gems == null){
133 gems = new GEMS(Configuration.gsdl_path, "", false, false);
134 gems.addGEMSListener(this);
135 }
136 gems.displayMetadataSet(metadata_path);
137 }
138
139 public void gemsIsClosed()
140 {
141 // We assume that the selected metadata was edited, so reload it and remember to update the table
142 MetadataSet edited_metadata_set = (MetadataSet) current_set_list.getSelectedValue();
143 MetadataSetManager.unloadMetadataSet(edited_metadata_set);
144 MetadataSetManager.loadMetadataSet(edited_metadata_set.getMetadataSetFile());
145 sets_changed = true;
146 }
147 }
148
149 private class RemoveButtonListener
150 implements ActionListener {
151
152 public void actionPerformed(ActionEvent event) {
153 MetadataSet metadata_set = (MetadataSet) current_set_list.getSelectedValue();
154 current_metadata_model.removeElement(metadata_set);
155 Gatherer.c_man.removeMetadataSet(metadata_set);
156 sets_changed = true;
157
158 }
159
160 }
161
162
163 private class MetadataSetListSelectionListener
164 implements ListSelectionListener {
165
166 public void valueChanged(ListSelectionEvent event)
167 {
168 // Wait until we get a stable event
169 if (event.getValueIsAdjusting()) {
170 return;
171 }
172
173 // Now we can process it
174 if (!current_set_list.isSelectionEmpty() && !((MetadataSet)current_set_list.getSelectedValue()).getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE)) {
175 remove_button.setEnabled(true);
176 edit_button.setEnabled(true);
177 }
178 else {
179 remove_button.setEnabled(false);
180 edit_button.setEnabled(false);
181 }
182
183 }
184
185 }
186
187 private class AddMetadataSetPrompt
188 extends ModalDialog implements GEMSListener{
189
190 private JDialog add_set_dialog;
191 private boolean cancelled = false;
192
193 private JList elements_list;
194 private JList available_sets_list;
195
196 private JButton add_button;
197
198 private JButton new_button;
199
200 private GEMSListener self;
201
202 public AddMetadataSetPrompt() {
203 super(Gatherer.g_man, true);
204 add_set_dialog = this;
205 setModal(true);
206 setJMenuBar(new SimpleMenuBar("choosingmetadatasets"));
207 setSize(ADD_SIZE);
208 setTitle(Dictionary.get("MetadataSetDialog.Add_Title"));
209 self = this;
210
211
212 JPanel center_pane = new JPanel();
213 JPanel sets_pane = new JPanel();
214 JLabel sets_label = new JLabel(Dictionary.get("MetadataSetDialog.Available_Sets"));
215 sets_label.setOpaque(false);
216
217 available_sets_list = new JList(getValidSetModel());
218 available_sets_list.addListSelectionListener(new AvailableSetListSelectionListener());
219 available_sets_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
220 JPanel elements_pane = new JPanel();
221 JLabel elements_label = new JLabel(Dictionary.get("MetadataSetDialog.Elements"));
222
223 elements_list = new JList();
224 elements_list.setCellRenderer(new MetadataElementListCellRenderer());
225 elements_list.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
226 elements_list.setForeground(Configuration.getColor("coloring.collection_tree_foreground", false));
227 elements_list.setSelectionBackground(Configuration.getColor("coloring.collection_tree_background", false));
228 elements_list.setSelectionForeground(Configuration.getColor("coloring.collection_tree_foreground", false));
229
230 JPanel button_pane = new JPanel();
231 add_button = new GLIButton(Dictionary.get("MetadataSetDialog.Add_Set"), Dictionary.get("MetadataSetDialog.Add_Set_Tooltip"));
232 add_button.setEnabled(false);
233
234 new_button = new GLIButton(Dictionary.get("MetadataSetDialog.New_Set"), Dictionary.get("MetadataSetDialog.New_Set_Tooltip"));
235
236 JButton browse_button = new GLIButton(Dictionary.get("MetadataSetDialog.Browse"), Dictionary.get("MetadataSetDialog.Browse_Tooltip"));
237 browse_button.setEnabled(true);
238 JButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
239
240 add_button.addActionListener(new AddSetActionListener());
241 browse_button.addActionListener(new BrowseActionListener());
242 cancel_button.addActionListener(new ActionListener() {
243 public void actionPerformed(ActionEvent event) {
244 cancelled = true;
245 if (gems != null) {
246 gems.removeGEMSListener(self);
247 }
248 add_set_dialog.dispose();
249 }
250 });
251
252 new_button.addActionListener(new ActionListener() {
253 public void actionPerformed(ActionEvent event) {
254 if (gems == null) {
255 gems = new GEMS(Configuration.gsdl_path, "", false, false);
256 }
257 gems.newMetadataSet();
258 gems.addGEMSListener(self);
259 }
260 });
261
262
263 // Layout
264 sets_pane.setLayout(new BorderLayout());
265 sets_pane.add(sets_label, BorderLayout.NORTH);
266 sets_pane.add(new JScrollPane(available_sets_list), BorderLayout.CENTER);
267
268 elements_pane.setLayout(new BorderLayout());
269 elements_pane.add(elements_label, BorderLayout.NORTH);
270 elements_pane.add(new JScrollPane(elements_list), BorderLayout.CENTER);
271
272 center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
273 center_pane.setLayout(new GridLayout(2,1,0,5));
274 center_pane.add(sets_pane);
275 center_pane.add(elements_pane);
276
277 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
278 button_pane.setLayout(new GridLayout(1,4,5,0));
279 button_pane.add(add_button);
280 button_pane.add(new_button);
281 button_pane.add(browse_button);
282 button_pane.add(cancel_button);
283
284 JPanel content_pane = (JPanel) getContentPane();
285 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
286 content_pane.setLayout(new BorderLayout());
287 content_pane.add(center_pane, BorderLayout.CENTER);
288 content_pane.add(button_pane, BorderLayout.SOUTH);
289
290 // Show
291 Dimension screen_size = Configuration.screen_size;
292 setLocation((screen_size.width - ADD_SIZE.width) / 2, (screen_size.height - ADD_SIZE.height) / 2);
293 setVisible(true);
294
295
296
297 }
298
299 public DynamicListModel getValidSetModel(){
300 // Show the metadata sets (except extracted, exploded, and currently assigned sets) in the GLI metadata folder
301 ArrayList all_metadata_sets = MetadataSetManager.listMetadataSets(new File(Gatherer.getGLIMetadataDirectoryPath()));
302 ArrayList current_metadata_sets = MetadataSetManager.getMetadataSets();
303 DynamicListModel valid_sets_model = new DynamicListModel();
304 for (int i=0; i<all_metadata_sets.size(); i++) {
305 MetadataSet set = (MetadataSet) all_metadata_sets.get(i);
306 if (set.getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE) || set.getNamespace().equals(MetadataSetManager.EXPLODED_METADATA_NAMESPACE)) {
307 continue;
308 }
309 // indexOf uses the equals() method, which for MetadataSets compares the toString() output, not the objects
310 if (current_metadata_sets.indexOf(set)!=-1) {
311 continue;
312 }
313 valid_sets_model.addElement(set);
314 }
315
316 return valid_sets_model;
317 }
318
319
320 public void gemsIsClosed(){
321 available_sets_list.setModel(getValidSetModel());
322 }
323
324 public boolean isCancelled() {
325 return cancelled;
326 }
327
328 private class AddSetActionListener
329 implements ActionListener {
330
331 public void actionPerformed(ActionEvent event) {
332 if (available_sets_list.isSelectionEmpty()) {
333 return;
334 }
335 MetadataSet metadata_set = (MetadataSet) available_sets_list.getSelectedValue();
336 String namespace = metadata_set.getNamespace();
337 // have we got a variant already in the collection??
338 MetadataSet existing_set = null;
339 if ((existing_set = MetadataSetManager.getMetadataSet(namespace)) != null) {
340 // warn that we are replacing
341 String [] args = new String [] {metadata_set.toString(), existing_set.toString()};
342 WarningDialog namespace_clash_dialog = new WarningDialog("warning.MetadataSetNamespaceClash", Dictionary.get("MetadataSetNamespaceClash.Title"), Dictionary.get("MetadataSetNamespaceClash.Message", args), null, true);
343 if (namespace_clash_dialog.display()==JOptionPane.CANCEL_OPTION) {
344 namespace_clash_dialog.dispose();
345 return;
346 }
347 // if we have got here, then we remove the old set
348 current_metadata_model.removeElement(existing_set);
349 Gatherer.c_man.removeMetadataSet(existing_set);
350 sets_changed = true;
351 namespace_clash_dialog.dispose();
352 }
353
354
355 Gatherer.c_man.importMetadataSet(metadata_set);
356 metadata_set = MetadataSetManager.getMetadataSet(namespace);
357 current_metadata_model.addElement(metadata_set);
358 sets_changed = true;
359 cancelled = false;
360 add_set_dialog.dispose();
361
362 }
363 }
364
365 private class BrowseActionListener
366 implements ActionListener {
367
368 public void actionPerformed(ActionEvent event) {
369 JFileChooser chooser = new JFileChooser(new File(Gatherer.getGLIMetadataDirectoryPath()));
370 chooser.setFileFilter(new MetadataSet.MetadataSetFileFilter());
371 chooser.setDialogTitle(Dictionary.get("MetadataSetDialog.Add_Title"));
372 int return_val = chooser.showDialog(Gatherer.g_man, Dictionary.get("MetadataSetDialog.Add_Set"));
373 if (return_val == JFileChooser.APPROVE_OPTION) {
374 MetadataSet meta_set = new MetadataSet(chooser.getSelectedFile());
375 Gatherer.c_man.importMetadataSet(meta_set);
376 current_metadata_model.addElement(meta_set);
377 cancelled = false;
378 add_set_dialog.dispose();
379 } else {
380 // we do nothing - user may want to add from the other dialog
381 }
382 }
383 }
384
385
386 private class AvailableSetListSelectionListener
387 implements ListSelectionListener
388 {
389 public void valueChanged(ListSelectionEvent event)
390 {
391 // Wait until we get a stable event
392 if (event.getValueIsAdjusting()) {
393 return;
394 }
395
396 if (!available_sets_list.isSelectionEmpty()) {
397 // Retrieve the selected set
398 MetadataSet metadata_set = (MetadataSet) available_sets_list.getSelectedValue();
399 elements_list.setListData(new Vector(metadata_set.getMetadataSetElements()));
400 add_button.setEnabled(true);
401 }
402 else {
403 elements_list.setListData(new String[0]);
404 add_button.setEnabled(false);
405 }
406 }
407 }
408
409
410 }
411}
Note: See TracBrowser for help on using the repository browser.