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

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

Fixed tabbing.

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