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

Last change on this file since 12238 was 12238, checked in by kjdon, 18 years ago

need to remove the set from the model before removing it from the collection. also, now we give a warning if adding a set to a collection with the same namespace as an exisitng set

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