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

Last change on this file since 18370 was 18370, checked in by kjdon, 15 years ago

committed code submitted by Amin Hedjazi for making the GLI right to left. I worked on this code on the rtl-gli branch, then merged the branch back to the trunk at revision 18368. The branch code was slightly different in a couple of places where it shouldn't have been. So don't use the branch code next time. Start a new branch.

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