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

Last change on this file since 8243 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

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