source: trunk/gli/src/org/greenstone/gatherer/msm/MSMPrompt.java@ 5527

Last change on this file since 5527 was 5527, checked in by mdewsnip, 21 years ago

Partway through tooltip assignment. Made lots of little changes, registered a whole lot more components, removed dead code, fixed help links (with SimpleMenuBars). Lots more of the same to come.

  • Property svn:keywords set to Author Date Id Revision
File size: 40.2 KB
Line 
1package org.greenstone.gatherer.msm;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.awt.*;
39import java.awt.event.*;
40import java.io.*;
41import java.util.*;
42import javax.swing.*;
43import javax.swing.event.*;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.gui.ModalDialog;
46import org.greenstone.gatherer.gui.SimpleMenuBar;
47import org.greenstone.gatherer.msm.Declarations;
48import org.greenstone.gatherer.msm.MetadataSet;
49import org.greenstone.gatherer.msm.MetadataSetManager;
50import org.greenstone.gatherer.util.ArrayTools;
51import org.greenstone.gatherer.util.MED;
52import org.w3c.dom.*;
53/** A class dedicated to producing the visual components used by the MSM package.
54 * @author John Thompson, Greenstone Digital Library, University of Waikato
55 * @version 1.2
56 */
57public class MSMPrompt
58 implements ActionListener {
59
60 private boolean dialog_cancelled = false;
61 private Dimension screen_size = null;
62 private int action = Declarations.NO_ACTION;
63 private JButton add = null;
64 private JButton cancel = null;
65 private JButton merge = null;
66 private JButton rename = null;
67 private JButton replace = null;
68 private JButton skip = null;
69 private JDialog on_screen = null;
70 private JDialog dialog = null;
71 private JLabel current_details_label = null;
72 private JLabel new_details_label = null;
73 private JLabel progress_label = null;
74 private JProgressBar progress = null;
75 private JTextArea current_details = null;
76 private JTextArea new_details = null;
77 private MetadataSetManager manager = null;
78 private Object result = null;
79
80 final static private Dimension MDE_SIZE = new Dimension(500,250);
81 final static private Dimension MDS_SIZE = new Dimension(800,425);
82 final static private Dimension PROGRESS_SIZE = new Dimension(500,105);
83 final static private Dimension RENAME_LABEL_SIZE = new Dimension(100,25);
84 final static private Dimension RENAME_SIZE = new Dimension(300,145);
85 final static private Dimension SELECT_ELEMENT_SIZE = new Dimension(500,305);
86 final static private Dimension SELECT_LABEL_SIZE = new Dimension(175, 25);
87 final static private Dimension SELECT_SET_SIZE = new Dimension(600,210);
88 final static private Dimension SELECT_SIZE = new Dimension(200,225);
89 final static private int SELECT_LINE_COUNT = 8;
90
91 public MSMPrompt(MetadataSetManager manager) {
92 this.manager = manager;
93 // Create components
94
95 add = new JButton(get("MSMPrompt.Add"));
96 add.addActionListener(this);
97 add.setEnabled(false);
98 add.setMnemonic(KeyEvent.VK_A);
99
100
101 cancel = new JButton(get("MSMPrompt.Cancel"));
102 cancel.addActionListener(this);
103 cancel.setEnabled(true);
104 cancel.setMnemonic(KeyEvent.VK_C);
105
106 current_details = new JTextArea(get("MSMPrompt.No_Details"));
107
108 current_details_label = new JLabel(get("MSMPrompt.Current_Details"));
109
110 merge = new JButton(get("MSMPrompt.Merge"));
111 merge.addActionListener(this);
112 merge.setEnabled(true);
113 merge.setMnemonic(KeyEvent.VK_M);
114
115 new_details = new JTextArea(get("MSMPrompt.No_Details"));
116
117 new_details_label = new JLabel(get("MSMPrompt.New_Details"));
118
119 progress = new JProgressBar();
120 progress.setStringPainted(true);
121
122 progress_label = new JLabel(get("MSMPrompt.Progress"));
123
124 rename = new JButton(get("MSMPrompt.Rename"));
125 rename.addActionListener(this);
126 rename.setEnabled(false);
127 rename.setMnemonic(KeyEvent.VK_N);
128
129 replace = new JButton(get("MSMPrompt.Replace"));
130 replace.addActionListener(this);
131 replace.setEnabled(false);
132 replace.setMnemonic(KeyEvent.VK_R);
133
134 skip = new JButton(get("MSMPrompt.Skip"));
135 skip.addActionListener(this);
136 skip.setEnabled(true);
137 skip.setMnemonic(KeyEvent.VK_S);
138
139 screen_size = Gatherer.config.screen_size;
140 }
141
142 /** When called this method produces a nice error message on the screen, advising the user that the add action has failed.
143 * @param mde_new The <strong>Element</strong> that we were attempting to add.
144 * @param reason The phrase key for the reason the add failed, as a <strong>String</strong>.
145 */
146 public void addFailed(Element mde_new, String reason) {
147 String args[] = new String[2];
148 args[0] = mde_new.getAttribute("name");
149 args[1] = get(reason, null);
150 JOptionPane.showMessageDialog(Gatherer.g_man, get("MSMPrompt.Add_Failed", args), get("MSMPrompt.Add_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
151 }
152
153 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured.
154 * @param event An <strong>ActionEvent</strong> containing information gatherer when this event occured.
155 */
156 public void actionPerformed(ActionEvent event) {
157 Object source = event.getSource();
158 action = Declarations.NO_ACTION;
159 if(source == add) {
160 action = Declarations.ADD;
161 }
162 else if(source == cancel) {
163 action = Declarations.CANCEL;
164 }
165 else if(source == merge) {
166 action = Declarations.MERGE;
167 }
168 else if(source == rename) {
169 action = Declarations.RENAME;
170 }
171 else if(source == replace) {
172 action = Declarations.REPLACE;
173 }
174 else if(source == skip) {
175 action = Declarations.SKIP;
176 }
177 on_screen.setVisible(false);
178 }
179 /** Method called when the merging process is complete and the progress bar is no longer needed.
180 */
181 public void endMerge() {
182 }
183 /** Method to indicate that yet another element has been succesfully merged, so that progress bar should reflect this.
184 */
185 public void incrementMerge() {
186 progress.setValue(progress.getValue() + 1);
187 String percent = ((100 * progress.getValue()) / progress.getMaximum()) + "%";
188 progress.setString(percent);
189 }
190 /** Method to display the metadata element merging prompt, wherein the user determines how the attributes within an element should be merged.
191 * @param mde_cur The current <strong>Element</strong> we are merging against.
192 * @param atts_cur The list of matching attribute <strong>Elements</strong> of the current element in question.
193 * @param mde_new The <strong>Element</strong> which we have choosen to merge in, and whose attributes are being examined.
194 * @param att_new And the new elements attribute <strong>Element</strong>.
195 * @return An object, which is either an Integer specifying what further action should be undertaken, if any, or if the action was replace, returns an Element, which is the attribute Element to replace with the new one
196 */
197 public Object mDEPrompt(Element mde_cur, Element[] atts_cur, Element mde_new, Element att_new) {
198 action = Declarations.NO_ACTION;
199 // Construction and configuration
200 JDialog dialog = new ModalDialog(Gatherer.g_man);
201 dialog.setModal(true);
202 dialog.setSize(MDE_SIZE);
203 dialog.setTitle(get("MSMPrompt.Merge_MDE"));
204 dialog.setJMenuBar(new SimpleMenuBar("importingpreviouslyassignedmetadata"));
205 JPanel content_pane = (JPanel)dialog.getContentPane();
206
207 JLabel element_name_label = new JLabel(get("MSMPrompt.Element_Name"));
208 JLabel element_name = new JLabel(MSMUtils.getFullName(mde_cur));
209
210 JLabel attribute_name_label = new JLabel(get("MSMPrompt.Attribute"));
211 JLabel attribute_name = new JLabel(att_new.getAttribute("name"));
212
213 add.setEnabled(true);
214 replace.setEnabled(false);
215 skip.setEnabled(true);
216
217 JList cur_values = new JList(atts_cur);
218 cur_values.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
219 cur_values.setLayoutOrientation(JList.VERTICAL);
220 cur_values.setVisibleRowCount(3);
221 cur_values.setCellRenderer(new AttributeListCellRenderer());
222 cur_values.addListSelectionListener(new ElementListListener(cur_values, replace));
223 //Layout
224 JPanel title_pane = new JPanel();
225 title_pane.setLayout(new GridLayout(2,2));
226 title_pane.add(element_name_label);
227 title_pane.add(element_name);
228 title_pane.add(attribute_name_label);
229 title_pane.add(attribute_name);
230
231 JPanel current_pane = new JPanel();
232 current_pane.setLayout(new BorderLayout());
233 current_pane.setBorder(BorderFactory.createTitledBorder(get("MSMPrompt.Current_Values")));
234 JScrollPane scroll = new JScrollPane(cur_values);
235 current_pane.add(scroll, BorderLayout.CENTER);
236
237 JPanel new_pane = new JPanel();
238 new_pane.setBorder(BorderFactory.createTitledBorder(get("MSMPrompt.New_Value")));
239 new_pane.setLayout(new BorderLayout());
240 JLabel new_att = new JLabel(MSMUtils.getValue(att_new));
241 new_pane.add(new_att, BorderLayout.WEST);
242 JPanel central_pane = new JPanel();
243 central_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
244 central_pane.setLayout(new BoxLayout(central_pane, BoxLayout.Y_AXIS));
245 central_pane.add(title_pane);
246 central_pane.add(current_pane);
247 central_pane.add(new_pane);
248
249 JPanel button_pane = new JPanel();
250 button_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
251 button_pane.setLayout(new GridLayout(1,4));
252 button_pane.add(add);
253 button_pane.add(replace);
254 button_pane.add(skip);
255 button_pane.add(cancel);
256
257 content_pane.setLayout(new BorderLayout());
258 content_pane.add(central_pane, BorderLayout.CENTER);
259 content_pane.add(button_pane, BorderLayout.SOUTH);
260
261 // Display
262
263 dialog.setLocation((screen_size.width - MDE_SIZE.width) / 2, (screen_size.height - MDE_SIZE.height) / 2);
264 on_screen = dialog;
265 on_screen.setVisible(true); // blocks until hidden
266 on_screen.dispose();
267
268 // the return value - will either be an Integer containing the action,
269 // of the selected Element for a replace
270 Object result = new Integer(action);
271 // need to get the value of the list selection if replace was clicked
272 if (action == Declarations.REPLACE) {
273 result = cur_values.getSelectedValue();
274 }
275 content_pane = null;
276 element_name_label = null;
277 element_name = null;
278 attribute_name_label = null;
279 attribute_name=null;
280 cur_values = null;
281 title_pane=null;
282 current_pane = null;
283 scroll = null;
284 new_pane = null;
285 new_att = null;
286 central_pane = null;
287 button_pane = null;
288 on_screen = null;
289 dialog = null;
290
291 return result;
292 }
293 /** This method displays the metadata data set merging prompt, wherein the user determines how the elements within a set should be merged.
294 * @param mds_cur The current <strong>MetadataSet</strong> containing our document.
295 * @param mde_cur The current <strong>Element</strong> we are merging against.
296 * @param mds_new The <strong>MetadataSet</strong> we are merging in.
297 * @param mde_new The <strong>Element</strong> which we have choosen to merge in.
298 * @return An <i>int</i> specifying what further action should be undertaken, if any.
299 */
300 public int mDSPrompt(MetadataSet mds_cur, Element mde_cur, MetadataSet mds_new, Element mde_new) {
301 action = Declarations.NO_ACTION;
302 // Construction and configuration
303 JDialog dialog = new ModalDialog(Gatherer.g_man);
304 dialog.setModal(true);
305 dialog.setSize(MDS_SIZE);
306 dialog.setTitle(get("MSMPrompt.Merge_MDS"));
307 dialog.setJMenuBar(new SimpleMenuBar("importingpreviouslyassignedmetadata"));
308 JPanel content_pane = (JPanel)dialog.getContentPane();
309 if(mde_cur != null) {
310 add.setEnabled(false);
311 cancel.setEnabled(true);
312 merge.setEnabled(true);
313 rename.setEnabled(true);
314 replace.setEnabled(true);
315 skip.setEnabled(true);
316 // Current details.
317 String str_cur[] = MSMUtils.getStructuralDetails(mds_cur, mde_cur);
318 String opt_cur[] = MSMUtils.getOptionListDetails(mde_cur);
319 String ass_cur[] = MSMUtils.getAssignedValuesDetails(mds_cur, mde_cur);
320 String details_cur = get("MSMPrompt.Structural", str_cur);
321 if(opt_cur != null) {
322 details_cur = details_cur + "\n" + get("MSMPrompt.OptionList", opt_cur);
323 }
324 if(ass_cur != null) {
325 details_cur = details_cur + "\n" + get("MSMPrompt.AssignedValues", ass_cur);
326 }
327 current_details.setText(details_cur);
328 str_cur = null;
329 opt_cur = null;
330 ass_cur = null;
331 details_cur = null;
332 }
333 else {
334 add.setEnabled(true);
335 cancel.setEnabled(true);
336 merge.setEnabled(true);
337 rename.setEnabled(false);
338 replace.setEnabled(false);
339 skip.setEnabled(true);
340 current_details.setText(get("MSMPrompt.No_Element"));
341 }
342 // New details.
343 String str_new[] = MSMUtils.getStructuralDetails(mds_new, mde_new);
344 String opt_new[] = MSMUtils.getOptionListDetails(mde_new);
345 String ass_new[] = MSMUtils.getAssignedValuesDetails(mds_new, mde_new);
346 String details_new = get("MSMPrompt.Structural", str_new);
347 if(opt_new != null) {
348 details_new = details_new + "\n" + get("MSMPrompt.OptionList", opt_new);
349 }
350 if(ass_new != null) {
351 details_new = details_new + "\n" + get("MSMPrompt.AssignedValues", ass_new);
352 }
353 new_details.setText(details_new);
354 // Layout
355 JPanel current_details_pane = new JPanel();
356 current_details_pane.setLayout(new BorderLayout());
357 current_details_pane.add(current_details_label, BorderLayout.NORTH);
358 current_details_pane.add(new JScrollPane(current_details), BorderLayout.CENTER);
359
360 JPanel new_details_pane = new JPanel();
361 new_details_pane.setLayout(new BorderLayout());
362 new_details_pane.add(new_details_label, BorderLayout.NORTH);
363 new_details_pane.add(new JScrollPane(new_details), BorderLayout.CENTER);
364
365 JPanel details_pane = new JPanel();
366 details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
367 details_pane.setLayout(new GridLayout(1,2));
368 details_pane.add(current_details_pane);
369 details_pane.add(new_details_pane);
370
371 JPanel button_pane = new JPanel();
372 button_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
373 button_pane.setLayout(new GridLayout(1,6));
374 button_pane.add(add);
375 button_pane.add(merge);
376 button_pane.add(rename);
377 button_pane.add(replace);
378 button_pane.add(skip);
379 button_pane.add(cancel);
380
381 JPanel control_pane = new JPanel();
382 control_pane.setLayout(new BorderLayout());
383 control_pane.add(details_pane, BorderLayout.CENTER);
384 control_pane.add(button_pane, BorderLayout.SOUTH);
385
386 JPanel progress_pane = new JPanel();
387 progress_pane.setBorder(BorderFactory.createEmptyBorder(5,5,0,5));
388 progress_pane.setLayout(new BorderLayout());
389 progress_pane.add(progress_label, BorderLayout.NORTH);
390 progress_pane.add(progress, BorderLayout.CENTER);
391
392 content_pane.setLayout(new BorderLayout());
393 content_pane.add(progress_pane, BorderLayout.NORTH);
394 content_pane.add(control_pane, BorderLayout.CENTER);
395
396 // Display
397 dialog.setLocation((screen_size.width - MDS_SIZE.width) / 2, (screen_size.height - MDS_SIZE.height) / 2);
398 on_screen = dialog;
399 on_screen.setVisible(true); // Blocks until hidden.
400 on_screen.dispose();
401 on_screen = null;
402
403 content_pane = null;
404 str_new = null;
405 opt_new = null;
406 ass_new = null;
407 details_new = null;
408 current_details_pane = null;
409 new_details_pane = null;
410 details_pane = null;
411 button_pane = null;
412 control_pane = null;
413 progress_pane = null;
414 dialog = null;
415
416 if(mde_cur == null && action == Declarations.MERGE) {
417 action = Declarations.FORCE_MERGE;
418 }
419 return action;
420 }
421 /** This method initialises the progress bar.
422 * @param element_count An <i>int</i> specifying the total number of elements to be merged.
423 */
424 public void startMerge(int element_count) {
425 progress.setMaximum(element_count);
426 progress.setMinimum(0);
427 progress.setString("0%");
428 progress.setValue(0);
429 }
430
431 /** This method creates a prompt for renaming an Element.
432 * @param mde_new The <strong>Element</strong> we wish to rename.
433 * @return The new name as a <strong>String</strong>, <i>null</i> if cancelled.
434 */
435 public String rename(Element mde_new) {
436 action = Declarations.NO_ACTION;
437 // Create
438 JDialog dialog = new ModalDialog(Gatherer.g_man);
439 dialog.setModal(true);
440 dialog.setSize(RENAME_SIZE);
441 dialog.setTitle(get("MSMPrompt.Rename"));
442 dialog.setJMenuBar(new SimpleMenuBar("importingpreviouslyassignedmetadata"));
443 JLabel old_name_label = new JLabel(get("MSMPrompt.Old_Name"));
444 old_name_label.setPreferredSize(RENAME_LABEL_SIZE);
445
446 JLabel old_name = new JLabel(mde_new.getAttribute("name"));
447 old_name.setBackground(Color.white);
448 old_name.setOpaque(true);
449
450 JLabel new_name_label = new JLabel(get("MSMPrompt.New_Name"));
451 new_name_label.setPreferredSize(RENAME_LABEL_SIZE);
452
453 JTextField new_name = new JTextField("");
454 new_name.grabFocus();
455
456 JButton ok = new JButton(get("General.OK"));
457 ok.addActionListener(this);
458 ok.setMnemonic(KeyEvent.VK_O);
459
460 JPanel content_pane = (JPanel) dialog.getContentPane();
461
462 // Layout
463 JPanel old_name_pane = new JPanel();
464 old_name_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
465 old_name_pane.setLayout(new BorderLayout());
466 old_name_pane.add(old_name_label, BorderLayout.WEST);
467 old_name_pane.add(old_name, BorderLayout.CENTER);
468
469 JPanel new_name_pane = new JPanel();
470 new_name_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
471 new_name_pane.setLayout(new BorderLayout());
472 new_name_pane.add(new_name_label, BorderLayout.WEST);
473 new_name_pane.add(new_name, BorderLayout.CENTER);
474
475 JPanel control_pane = new JPanel();
476 control_pane.setLayout(new GridLayout(2,1));
477 control_pane.add(old_name_pane);
478 control_pane.add(new_name_pane);
479
480 JPanel button_pane = new JPanel();
481 button_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
482 button_pane.setLayout(new GridLayout(1,2));
483 button_pane.add(ok);
484 button_pane.add(cancel);
485
486 content_pane.setLayout(new BorderLayout());
487 content_pane.add(control_pane, BorderLayout.CENTER);
488 content_pane.add(button_pane, BorderLayout.SOUTH);
489 // Display
490 dialog.setLocation((screen_size.width - RENAME_SIZE.width) / 2, (screen_size.height - RENAME_SIZE.height) / 2);
491 on_screen = dialog;
492 on_screen.setVisible(true);
493 on_screen.dispose();
494 on_screen = null;
495
496 String result = new_name.getText();
497 button_pane = null;
498 control_pane = null;
499 new_name_pane = null;
500 old_name_pane = null;
501 content_pane = null;
502 ok.removeActionListener(this);
503 ok = null;
504 new_name = null;
505 new_name_label = null;
506 old_name = null;
507 old_name_label = null;
508 dialog = null;
509
510 if(action == Declarations.CANCEL) {
511 return null;
512 }
513 action = Declarations.NO_ACTION;
514 dialog = null;
515 return result;
516 }
517
518 /** When called this method produces a nice error message on the screen, advising the user that the remove action has failed.
519 * @param mde_cur The <strong>Element</strong> that we are attempting to remove.
520 * @param reason The phrase key for the reason the rename failed, also as a <strong>String</strong>.
521 */
522 public void removeFailed(Element mde_cur, String reason) {
523 String args[] = new String[2];
524 args[0] = mde_cur.getAttribute("name");
525 args[1] = get(reason, null);
526 JOptionPane.showMessageDialog(Gatherer.g_man, get("MSMPrompt.Remove_Failed", args), get("MSMPrompt.Remove_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
527 }
528
529 /** When called this method produces a nice error message on the screen, advising the user that the rename action has failed.
530 * @param mde_new The <strong>Element</strong> that we are attempting to add.
531 * @param new_name The name that we attempted to add it under, as a <strong>String</strong>.
532 * @param reason The phrase key for the reason the rename failed, also as a <strong>String</strong>.
533 */
534 public void renameFailed(Element mde_new, String new_name, String reason) {
535 String args[] = new String[3];
536 args[0] = mde_new.getAttribute("name");
537 args[1] = new_name;
538 args[2] = get(reason, null);
539 JOptionPane.showMessageDialog(Gatherer.g_man, get("MSMPrompt.Rename_Failed", args), get("MSMPrompt.Rename_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
540 }
541
542 /** Method to display a prompt asking the user to select a specific element to merge with.
543 * @param mds_cur The current <strong>MetadataSet</strong>.
544 * @return The selected <strong>Element</strong> or <i>null</i> if the user cancels the action.
545 */
546 public Element selectElement(MetadataSet mds_cur) {
547 action = Declarations.NO_ACTION;
548 // Create
549 JDialog dialog = new ModalDialog(Gatherer.g_man);
550 dialog.setModal(true);
551 dialog.setSize(SELECT_SIZE);
552 dialog.setTitle(get("MSMPrompt.Select"));
553 dialog.setJMenuBar(new SimpleMenuBar("importingpreviouslyassignedmetadata"));
554
555 JButton ok = new JButton(get("General.OK"));
556 ok.addActionListener(this);
557 ok.setMnemonic(KeyEvent.VK_O);
558 ok.setEnabled(false);
559
560 Node elements_raw[] = ArrayTools.nodeListToNodeArray(mds_cur.getElements());
561 ElementWrapper elements[] = new ElementWrapper[elements_raw.length];
562 for(int i = 0; i < elements_raw.length; i++) {
563 elements[i] = new ElementWrapper((Element)elements_raw[i]);
564 }
565 JList list = new JList(elements);
566 ElementListListener element_list_listener = new ElementListListener(list, ok);
567 list.addListSelectionListener(element_list_listener);
568 JPanel content_pane = (JPanel) dialog.getContentPane();
569 // Layout
570 JPanel list_pane = new JPanel();
571 list_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
572 list_pane.setLayout(new BorderLayout());
573 list_pane.add(new JScrollPane(list), BorderLayout.CENTER);
574
575 JPanel button_pane = new JPanel();
576 button_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
577 button_pane.setLayout(new GridLayout(1,2));
578 button_pane.add(ok);
579 button_pane.add(cancel);
580
581 content_pane.setLayout(new BorderLayout());
582 content_pane.add(list_pane, BorderLayout.CENTER);
583 content_pane.add(button_pane, BorderLayout.SOUTH);
584 // Display
585 dialog.setLocation((screen_size.width - SELECT_SIZE.width) / 2, (screen_size.height - SELECT_SIZE.height) / 2);
586 on_screen = dialog;
587 on_screen.setVisible(true);
588 on_screen.dispose();
589 on_screen = null;
590
591 // get the result
592 Element result = ((ElementWrapper)list.getSelectedValue()).getElement();
593 // Deallocate stuff since JDK1.4 won't.
594 ok.removeActionListener(this);
595 ok = null;
596 elements_raw = null;
597 elements = null;
598 list.removeListSelectionListener(element_list_listener);
599 list = null;
600 element_list_listener = null;
601 content_pane = null;
602 list_pane = null;
603 button_pane = null;
604 dialog = null;
605 // Return selected element.
606 if(action == Declarations.CANCEL) {
607 return null;
608 }
609 return result;
610 }
611
612 /** Prompts the user to choose how to import a metadata element. Gives the option of renaming the element to a certain value of a certain set, or adding to a selected set.
613 * @param name The name of the original metadata as a String.
614 * @param set The set previously choosen as the default set for this metadata (which it doesn't match somehow).
615 * @return An ElementWrapper around the element that it has been matched to.
616 * @see org.greenstone.gatherer.msm.MSMPrompt.MSMDialog
617 * @see org.greenstone.gatherer.util.MED
618 */
619 public ElementWrapper selectElement(String name) {
620 dialog_cancelled = false;
621 result = null;
622 String args[] = new String[1];
623 args[0] = name;
624 // Create
625 MSMDialog dialog = new MSMDialog();
626 dialog.setModal(true);
627 dialog.setSize(SELECT_ELEMENT_SIZE);
628 dialog.setTitle(get("MSMPrompt.Select_Element_Title"));
629 dialog.setJMenuBar(new SimpleMenuBar("importingpreviouslyassignedmetadata"));
630 JPanel content_pane = (JPanel) dialog.getContentPane();
631 JPanel control_pane = new JPanel();
632 JTextArea instructions = new JTextArea(get("MSMPrompt.Select_Element_Instructions", args));
633 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
634 instructions.setEditable(false);
635 instructions.setLineWrap(true);
636 instructions.setRows(SELECT_LINE_COUNT);
637 instructions.setWrapStyleWord(true);
638 JPanel original_pane = new JPanel();
639 JLabel original_label = new JLabel(get("MSMPrompt.Select_Element_Original"));
640 original_label.setPreferredSize(SELECT_LABEL_SIZE);
641 JTextField original = new JTextField(name);
642 original.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
643 original.setEditable(false);
644 JPanel element_pane = new JPanel();
645 JLabel element_label = new JLabel(get("MSMPrompt.Select_Element_Element"));
646 element_label.setPreferredSize(SELECT_LABEL_SIZE);
647 JComboBox element = new JComboBox();
648 JButton add_button = new JButton(get("MSMPrompt.Select_Element_Add"));
649 add_button.setEnabled(false);
650 add_button.setMnemonic(KeyEvent.VK_A);
651 JButton cancel_button = new JButton(get("General.Cancel"));
652 cancel_button.setMnemonic(KeyEvent.VK_C);
653 JButton merge_button = new JButton(get("MSMPrompt.Select_Element_Merge"));
654 merge_button.setEnabled(false);
655 merge_button.setMnemonic(KeyEvent.VK_M);
656 JButton ignore_button = new JButton(get("MSMPrompt.Select_Element_Ignore"));
657 ignore_button.setMnemonic(KeyEvent.VK_I);
658 JPanel set_pane = new JPanel();
659 JLabel set_label = new JLabel(get("MSMPrompt.Select_Element_Set"));
660 set_label.setPreferredSize(SELECT_LABEL_SIZE);
661 JComboBox set = new JComboBox(manager.getSets(false)); // Don't include the greenstone metadata set.
662 JPanel button_pane = new JPanel();
663 // Connect
664 AddListener add_listener = new AddListener(dialog, name, set);
665 CancelListener cancel_listener = new CancelListener(dialog);
666 IgnoreListener ignore_listener = new IgnoreListener(dialog);
667 MergeListener merge_listener = new MergeListener(dialog, element);
668 SetListener set_listener = new SetListener(set, element, name, add_button, merge_button);
669
670 add_button.addActionListener(add_listener);
671 cancel_button.addActionListener(cancel_listener);
672 merge_button.addActionListener(merge_listener);
673 ignore_button.addActionListener(ignore_listener);
674 set.addActionListener(set_listener);
675
676 // Init controls
677 if(set.getItemCount() > 0) {
678 // We now try to determine if there is any existing metadata
679 // element whose name is the same as the new one. we ignore
680 // any namespaces - if the name was a full match (namespace
681 // and element name) then we wouldn't be in here
682 MetadataSet closest_set = null;
683 ElementWrapper closest_element = null;
684 String base_name = name;
685 int ns_sep = base_name.indexOf(MSMUtils.NS_SEP);
686 if (ns_sep != -1) {
687 base_name = base_name.substring(ns_sep+1);
688 }
689 for(int i = 0; i < set.getItemCount(); i++) {
690 MetadataSet mds = (MetadataSet) set.getItemAt(i);
691 for(int j = 0; j < mds.size(); j++) {
692 Element mde = mds.getElement(j);
693 String new_name = mde.getAttribute("name");
694 if (base_name.equals(new_name)) {
695 closest_element = new ElementWrapper(mde);
696 closest_set = mds;
697 mde=null;
698 break;
699 }
700 mde = null;
701 } // for each element
702 if (closest_element != null) {
703 mds=null;
704 break;
705 }
706
707 mds = null;
708 } // for each set
709 if(closest_set != null && closest_element != null) {
710 set.setSelectedItem(closest_set);
711 set_listener.actionPerformed(closest_element);
712 }
713 else {
714 set_listener.actionPerformed((ElementWrapper)null);
715 }
716 closest_set = null;
717 closest_element = null;
718 }
719 merge_button.setEnabled(element.getItemCount() > 0);
720 // Layout
721 original_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
722 original_pane.setLayout(new BorderLayout());
723 original_pane.add(original_label, BorderLayout.WEST);
724 original_pane.add(original, BorderLayout.CENTER);
725 set_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
726 set_pane.setLayout(new BorderLayout());
727 set_pane.add(set_label, BorderLayout.WEST);
728 set_pane.add(set, BorderLayout.CENTER);
729 element_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
730 element_pane.setLayout(new BorderLayout());
731 element_pane.add(element_label, BorderLayout.WEST);
732 element_pane.add(element, BorderLayout.CENTER);
733 control_pane.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
734 control_pane.setLayout(new GridLayout(3,1));
735 control_pane.add(original_pane);
736 control_pane.add(set_pane);
737 control_pane.add(element_pane);
738 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
739 button_pane.setLayout(new GridLayout(1,4));
740 button_pane.add(add_button);
741 button_pane.add(merge_button);
742 button_pane.add(ignore_button);
743 button_pane.add(cancel_button);
744 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
745 content_pane.setLayout(new BorderLayout());
746 content_pane.add(new JScrollPane(instructions), BorderLayout.NORTH);
747 content_pane.add(control_pane, BorderLayout.CENTER);
748 content_pane.add(button_pane, BorderLayout.SOUTH);
749 // Display
750 dialog.setLocation((screen_size.width - SELECT_ELEMENT_SIZE.width) / 2, (screen_size.height - SELECT_ELEMENT_SIZE.height) / 2);
751 dialog.setVisible(true);
752 // Deallocate everything because JDK1.4 won't.
753 // Why, oh why did I do this?
754 add_button.removeActionListener(add_listener);
755 merge_button.removeActionListener(merge_listener);
756 ignore_button.removeActionListener(ignore_listener);
757 add_button = null;
758 merge_button = null;
759 ignore_button = null;
760 add_listener = null;
761 merge_listener = null;
762 ignore_listener = null;
763 // References
764 args = null;
765 content_pane = null;
766 control_pane = null;
767 instructions = null;
768 original_pane = null;
769 original_pane = null;
770 original = null;
771 element_pane = null;
772 element_label = null;
773 element = null;
774 set_pane = null;
775 set_label = null;
776 set.removeActionListener(set_listener);
777 set_listener = null;
778 set = null;
779 button_pane = null;
780 dialog.dispose();
781 dialog.destroy();
782 dialog = null;
783 // Dondage.
784 return (ElementWrapper)result;
785 }
786
787 public boolean wasDialogCancelled() {
788 return dialog_cancelled;
789 }
790
791 private String get(String key) {
792 return get(key, null);
793 }
794
795 private String get(String key, String args[]) {
796 if(key.indexOf(".") == -1) {
797 key = "MSMPrompt." + key;
798 }
799 return Gatherer.dictionary.get(key, args);
800 }
801
802 /** Prompts the user to select a metadata set from the given list. Uses the name parameter to attempt to automatically select the correct collection (ie the only collection that has an element with the same name).
803 * @param name The name of the metadata element whose set name is unknown.
804 * @return The metadata set the user has selected or null if no set selected.
805 */
806 final public MetadataSet selectSet(String name) {
807 String args[] = new String[1];
808 args[0] = name;
809 // Creation
810 MSMDialog dialog = new MSMDialog();
811 dialog.setModal(true);
812 dialog.setSize(SELECT_SET_SIZE);
813 dialog.setTitle(get("MSMPrompt.Select_Set_Title"));
814 dialog.setJMenuBar(new SimpleMenuBar("importingpreviouslyassignedmetadata"));
815 JPanel content_pane = (JPanel) dialog.getContentPane();
816 JPanel control_pane = new JPanel();
817 JTextArea instructions = new JTextArea(get("MSMPrompt.Select_Set_Instructions", args));
818 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
819 instructions.setEditable(false);
820 instructions.setLineWrap(true);
821 instructions.setRows(SELECT_LINE_COUNT);
822 instructions.setWrapStyleWord(true);
823 JComboBox set = new JComboBox();
824 set.setBackground(Color.white);
825 set.addItem(get("MSMPrompt.Select_Set_None"));
826 Vector sets = manager.getSets();
827 for(int i = sets.size() - 1; i >= 0; i--) {
828 set.addItem(sets.get(i));
829 }
830 JButton ok = new JButton(get("General.OK"));
831 ActionListener ok_listener = new IgnoreListener(dialog);
832 ok.addActionListener(ok_listener); // Doesn't really ignore. Just disposes()
833 // Select most likely set.
834 if(name.indexOf(".") != -1) {
835 String set_name = name.substring(0, name.indexOf("."));
836 MetadataSet metadata_set = manager.getSet(set_name);
837 if(metadata_set != null) {
838 set.setSelectedItem(metadata_set);
839 }
840 metadata_set = null;
841 set_name = null;
842 }
843 else {
844 Vector matches = manager.setsThatContain(name);
845 if(matches.size() == 1) {
846 set.setSelectedItem(matches.get(0));
847 }
848 matches = null;
849 }
850 // Layout
851 control_pane.setLayout(new GridLayout(2,1));
852 control_pane.add(set);
853 control_pane.add(ok);
854 content_pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
855 content_pane.setLayout(new BorderLayout());
856 content_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
857 content_pane.add(control_pane, BorderLayout.SOUTH);
858 // Display
859 dialog.setLocation((screen_size.width - SELECT_SET_SIZE.width) / 2, (screen_size.height - SELECT_SET_SIZE.height) / 2);
860 dialog.setVisible(true);
861 Object value = set.getSelectedItem();
862 if(value instanceof MetadataSet) {
863 return (MetadataSet) value;
864 }
865 value = null;
866 ok.removeActionListener(ok_listener);
867 ok_listener = null;
868 ok = null;
869 content_pane = null;
870 control_pane = null;
871 instructions = null;
872 set = null;
873 dialog.destroy();
874 dialog = null;
875 return null;
876 }
877
878 private class AddListener
879 implements ActionListener {
880 private JComboBox set = null;
881 private JDialog dialog = null;
882 private String name = null;
883 public AddListener(JDialog dialog, String name, JComboBox set) {
884 this.dialog = dialog;
885 this.name = name;
886 this.set = set;
887 }
888 public void actionPerformed(ActionEvent event) {
889 // Get the currently selected metadata set.
890 MetadataSet mds = (MetadataSet) set.getSelectedItem();
891 String n = name;
892 // If we've been forced to go this far then any given namespace is complete bollocks.
893 while(n.indexOf(".") != -1 && !n.equals(".")) {
894 n = n.substring(n.indexOf(".") + 1);
895 }
896 // However, before we attempt to add a new element to the set, we should check that none already exists.
897 Element element = mds.getElement(name);
898 if(element == null) {
899 result = mds.addElement(n);
900 }
901 else {
902 result = (ElementWrapper) element;
903 }
904 mds = null;
905 n = null;
906 element = null;
907 dialog.setVisible(false);
908 }
909 }
910
911 /** a ListCellRenderer for a list of Elements - the displayed value of the element is not Element.toString() */
912 private class AttributeListCellRenderer extends DefaultListCellRenderer {
913
914 public AttributeListCellRenderer() {
915 super();
916 }
917 public Component getListCellRendererComponent(JList list,
918 Object value,
919 int index,
920 boolean isSelected,
921 boolean cellHasFocus) {
922
923 setText(MSMUtils.getValue((Element)value));
924 if (isSelected) {
925 setBackground(list.getSelectionBackground());
926 setForeground(list.getSelectionForeground());
927 }
928 else {
929 setBackground(list.getBackground());
930 setForeground(list.getForeground());
931 }
932 setEnabled(list.isEnabled());
933 setFont(list.getFont());
934 setOpaque(true);
935 return this;
936 }
937
938
939 }
940
941 /** This listener listens for selections within the select element to merge prompt, and once one has been made enables the ok button.*/
942 private class ElementListListener
943 implements ListSelectionListener {
944 /** The button we either enable or disable depending on user selection. */
945 private JButton ok = null;
946 /** The list from whom we are listening for selection events. */
947 private JList list = null;
948 /** Constructor.
949 * @param list The <strong>JList</Strong> from whom we are listening for selection events.
950 * @param ok The <Strong>JButton</strong> we either enable or disable depending on user selection.
951 */
952 public ElementListListener(JList list, JButton ok) {
953 this.list = list;
954 this.ok = ok;
955 }
956 /** Any implementation of <i>ListSelectionListener</i> must include this method so we can be informed when a list selection event has occured.
957 * @param event A <strong>ListSelectionEvent</strong> generated by the event.
958 */
959 public void valueChanged(ListSelectionEvent event) {
960 if(list.getSelectedValue() != null) {
961 ok.setEnabled(true);
962 }
963 else {
964 ok.setEnabled(false);
965 }
966 }
967 }
968
969 private class CancelListener
970 implements ActionListener {
971 private JDialog dialog = null;
972 public CancelListener(JDialog dialog) {
973 this.dialog = dialog;
974 }
975 public void actionPerformed(ActionEvent event) {
976 dialog_cancelled = true;
977 dialog.setVisible(false);
978 }
979 }
980
981 private class IgnoreListener
982 implements ActionListener {
983 private JDialog dialog = null;
984 public IgnoreListener(JDialog dialog) {
985 this.dialog = dialog;
986 }
987 public void actionPerformed(ActionEvent event) {
988 dialog.setVisible(false);
989 }
990 }
991
992 private class MergeListener
993 implements ActionListener {
994 private JComboBox element = null;
995 private JDialog dialog = null;
996 public MergeListener(JDialog dialog, JComboBox element) {
997 this.dialog = dialog;
998 this.element = element;
999 }
1000 public void actionPerformed(ActionEvent event) {
1001 // Return the currently selected element
1002 result = (ElementWrapper)element.getSelectedItem();
1003 dialog.setVisible(false);
1004 }
1005 }
1006
1007 private class MSMDialog
1008 extends ModalDialog {
1009
1010 public MSMDialog() {
1011 super(Gatherer.g_man);
1012 }
1013 public void destroy() {
1014 rootPane = null;
1015 }
1016 }
1017
1018 private class SetListener
1019 implements ActionListener {
1020 private JButton add_button = null;
1021 private JButton merge_button = null;
1022 private JComboBox element = null;
1023 private JComboBox set = null;
1024 private String name = null;
1025 public SetListener(JComboBox set, JComboBox element, String name, JButton add_button, JButton merge_button) {
1026 this.add_button = add_button;
1027 this.element = element;
1028 this.merge_button = merge_button;
1029 this.name = name.toLowerCase();
1030 this.set = set;
1031 }
1032 /** because the dialog constructor is selecting an element before calling actionPerformed, and actionPerformed is where the list of elements is built, we need to tell this what element should be selected */
1033 public void actionPerformed(ElementWrapper selected_elem) {
1034 element.removeAllItems();
1035 MetadataSet mds = (MetadataSet) set.getSelectedItem();
1036 ///ystem.err.println("Set selected: " + mds);
1037 if(mds != null) {
1038 NodeList elements = mds.getElements();
1039 Vector temp = new Vector();
1040 for(int i = elements.getLength() - 1; i >= 0; i--) {
1041 temp.add(new ElementWrapper((Element)elements.item(i)));
1042 }
1043
1044 // The add button is only enabled if an element with the same name doesn't already exist in the destination set.
1045 boolean add_button_enabled = true;
1046 ///ystem.err.println("Checking for " + name);
1047 for(int j = 0; j < temp.size(); j++) {
1048 ElementWrapper an_element = (ElementWrapper)temp.get(j);
1049 // Can only add if no file with the same name exists.
1050 String check = an_element.toString().toLowerCase();
1051 int position = -1;
1052 if((position = check.indexOf(".")) != -1) {
1053 check = check.substring(position + 1);
1054 }
1055 ///ystem.err.println("does it match " + check + "? " + name.equals(check));
1056 add_button_enabled = add_button_enabled && !name.equals(check);
1057 check = null;
1058 element.addItem(an_element);
1059 an_element = null;
1060 }
1061 // if we have a pre selected element
1062 if (selected_elem != null) {
1063 // if this element is not in the set, the selection wont change
1064 element.setSelectedItem(selected_elem);
1065 }
1066 add_button.setEnabled(add_button_enabled);
1067
1068 // The merge button is only enabled if there is more than one element in the destination set.
1069 if(temp.size() > 0) {
1070 merge_button.setEnabled(true);
1071 }
1072 else {
1073 merge_button.setEnabled(false);
1074 }
1075 temp = null;
1076 elements = null;
1077 }
1078 else {
1079 add_button.setEnabled(false);
1080 merge_button.setEnabled(false);
1081 }
1082 mds = null;
1083 }
1084 public void actionPerformed(ActionEvent item) {
1085 actionPerformed((ElementWrapper)null);
1086 }
1087 }
1088}
Note: See TracBrowser for help on using the repository browser.