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

Last change on this file since 12730 was 12561, checked in by shaoqun, 18 years ago

added code to use gems

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