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

Last change on this file since 34232 was 34232, checked in by ak19, 4 years ago

Bugfix. When using client-GLI noticed that creating or editing an existing metadataset (GEMS) caused an error. The problem was compounded by a deadlock situation in displaying the error message in a popup. That deadlock is not resolved here (see future commit for attempted fix of it). This commit resolves the root cause: which was that Configuration.site_name was suddenly set to null, thereby failing to upload the mds file to the remote GS3. The nulled site_name may however not be a problem that will only affect client-GLI, as the Preferences pane would not open and things froze in client-GLI because site_name was null, which I think can affect GLI too. Need to check this, albeit with the bug still intact, by using GEMS in GLI to create/edit an mds file, then going to Preferences (or for local GLI perhaps the active site_name should moreover be set to other than localsite first). The cause was that there was a single GEMS constructor, used both when GEMS is launched as a standalone app and when GEMS launched through GLI. However, in both cases, the GEMS constructor dangerously erased and replaced the static-looking Configuration object (and Dictionary) with a basic Configuration object, losing crucial information like site_name and who knows what else. The problem was duplicated in GEMS.java as this instantiated a MetadataSetManager object whose constructor did the same thing of erasing and replacing Configuration, where it may have been a copy-paste error from GEMS.java. Once the mysterious cause of this problem was finally tracked down, the solution was just to have additional constructors that assume Configuration and Dictionary exist (thus not overwriting them), to be called when GEMS is launched through GLI, and when MetadataSetManager is launched through GEMS as GEMS always ensures a Configuration object exists.

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