source: trunk/gli/src/org/greenstone/gatherer/cdm/ClassifierManager.java@ 9640

Last change on this file since 9640 was 9126, checked in by kjdon, 19 years ago

moved the argument details xml parsing code to Argument.parseXML - the same code was duplicated in BuildOptions, PluginManager, ClassifierManager

  • Property svn:keywords set to Author Date Id Revision
File size: 33.7 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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.net.*;
33import java.util.*;
34import java.util.jar.*;
35import javax.swing.*;
36import javax.swing.event.*;
37import org.apache.xerces.parsers.*;
38import org.greenstone.gatherer.Configuration;
39import org.greenstone.gatherer.DebugStream;
40import org.greenstone.gatherer.Dictionary;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.gui.GComboBox;
43import org.greenstone.gatherer.gui.GLIButton;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.Utility;
46import org.greenstone.gatherer.util.XMLTools;
47import org.w3c.dom.*;
48import org.xml.sax.*;
49
50/** This class is responsible for keeping track of all the classifiers assigned to this collection, and providing methods for adding and removing them.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class ClassifierManager
55 extends DOMProxyListModel {
56
57 /** A list of known, but currently unassigned, classifiers. */
58 private ArrayList library = null;
59 /** The controls for editing the contents of this manager. */
60 private Control controls = null;
61
62 private DOMProxyListModel model;
63
64 /** Constructor.
65 * @see org.greenstone.gatherer.cdm.DynamicListModel
66 * @see org.greenstone.gatherer.collection.CollectionManager
67 */
68 public ClassifierManager() {
69 super(CollectionDesignManager.collect_config.getDocumentElement(), CollectionConfiguration.CLASSIFY_ELEMENT, new Classifier());
70 this.model = this;
71 DebugStream.println("ClassifierManager: " + getSize() + " classifiers parsed.");
72 // Reload/Create the library
73 loadClassifiers();
74 saveClassifiers();
75 }
76
77 /** Method to add a new classifier to library.
78 * @param classifier The new <strong>Classifier</strong>.
79 * @see org.greenstone.gatherer.cdm.DynamicListModel
80 */
81 private void addClassifier(Classifier classifier) {
82 if(!library.contains(classifier)) {
83 library.add(classifier);
84 }
85 }
86
87 /** Method to assign a classifier.
88 * @param classifier The base <strong>Classifier</strong> to assign.
89 * @see org.greenstone.gatherer.cdm.DynamicListModel
90 */
91 private void assignClassifier(Classifier classifier) {
92 if(!contains(classifier)) {
93 Element element = classifier.getElement();
94 // Locate where we should insert this new classifier.
95 Node target_node = CollectionConfiguration.findInsertionPoint(element);
96 add(root, classifier, target_node);
97 Gatherer.c_man.configurationChanged();
98 }
99 }
100
101 public static boolean clearClassifierCache() {
102
103 DebugStream.println("deleting classifiers.dat");
104 File class_file = new File(Utility.getGLIUserFolder(), "classifiers.dat");
105 if (class_file.exists()) {
106 return Utility.delete(class_file);
107 }
108 return true;
109 }
110 /** Destructor.
111 * @see org.greenstone.gatherer.Gatherer
112 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
113 * @see org.greenstone.gatherer.cdm.DynamicListModel
114 */
115 public void destroy() {
116 if(controls != null) {
117 controls.destroy();
118 controls = null;
119 }
120 library.clear();
121 library = null;
122 }
123
124 public Classifier getBaseClassifier(String name) {
125 int library_size = library.size();
126 for(int i = 0; i < library_size; i++) {
127 Classifier classifier = (Classifier) library.get(i);
128 if(classifier.getName().equals(name)) {
129 return classifier;
130 }
131 }
132 // No success.
133 return null;
134 }
135
136 /** Method to retrieve the classifier with the given index.
137 * @param index The index of the desired classifier as an <i>int</i>.
138 * @return The requested Classifier or <i>null</i> if no such classifier exists.
139 */
140 public Classifier getClassifier(int index) {
141 if(0 <= index && index < getSize()) {
142 return (Classifier) getElementAt(index);
143 }
144 return null;
145 }
146
147 /** Method to retrieve the control for this manager.
148 * @return the Control for editing classifiers
149 */
150 public Control getControls() {
151 if(controls == null) {
152 // Build controls
153 this.controls = new ClassifierControl();
154 }
155 return controls;
156 }
157
158 public ArrayList getHierarchyClassifiers() {
159 ArrayList result = new ArrayList();
160 for(int i = 0; i < getSize(); i++) {
161 Classifier classifier = (Classifier) getElementAt(i);
162 if(classifier.getName().equalsIgnoreCase(StaticStrings.HIERARCHY_CLASSIFIER)) {
163 result.add(classifier);
164 }
165 classifier = null;
166 }
167 return result;
168
169 }
170
171 /** Determine if the Phind classifier has been assigned.
172 * @return true if it has, false otherwise
173 */
174 public boolean isPhindClassifierAssigned() {
175 for(int i = 0; i < getSize(); i++) {
176 Classifier classifier = (Classifier) getElementAt(i);
177 if(classifier.getName().equalsIgnoreCase(StaticStrings.PHIND_CLASSIFIER)) {
178 return true;
179 }
180 classifier = null;
181 }
182 return false;
183 }
184
185 /** Method to move a classifier in the list order.
186 * @param classifier the Classifier you want to move.
187 * @param direction true to move the classifier up, false to move it down.
188 * @param all true to move to move all the way, false for a single step.
189 */
190 private void moveClassifier(Classifier classifier, boolean direction, boolean all) {
191 if(getSize() < 2) {
192 DebugStream.println("Not enough classifiers to allow moving.");
193 return;
194 }
195 if(all) {
196 // Move to top
197 if(direction) {
198 // Remove the moving classifier
199 remove(classifier);
200 // Retrieve the first classifier
201 Classifier first_classifier = (Classifier) getElementAt(0);
202 // Add the moving classifier before the first classifier
203 addBefore(classifier, first_classifier);
204 first_classifier = null;
205 Gatherer.c_man.configurationChanged();
206 }
207 else {
208 // Remove the moving classifier
209 remove(classifier);
210 // And add after last classifier
211 add(getSize(), classifier);
212 }
213 }
214 else {
215 // Try to move the classifier one step in the desired direction.
216 int index = indexOf(classifier);
217 ///ystem.err.println("Index of " + classifier + " = " + index);
218 if(direction) {
219 index--;
220 if(index < 0) {
221 String args[] = new String[2];
222 args[0] = Dictionary.get("CDM.ClassifierManager.Classifier");
223 args[1] = classifier.getName();
224 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.Move.At_Top", args), Dictionary.get("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE);
225 return;
226 }
227 remove(classifier);
228 add(index, classifier);
229 Gatherer.c_man.configurationChanged();
230 }
231 else {
232 index++;
233 if(index >= getSize()) {
234 String args[] = new String[2];
235 args[0] = Dictionary.get("CDM.ClassifierManager.Classifier_Str");
236 args[1] = classifier.getName();
237 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.Move.At_Bottom", args), Dictionary.get("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE);
238 return;
239 }
240 remove(classifier);
241 add(index, classifier);
242 Gatherer.c_man.configurationChanged();
243 }
244 }
245 }
246
247 /** This method removes an assigned classifier. I was tempted to call it unassign, but remove is more consistant. Note that there is no way to remove a classifier from the library.
248 * @param classifier The Classifier to remove
249 * @see org.greenstone.gatherer.cdm.DynamicListModel
250 */
251 private void removeClassifier(Classifier classifier) {
252 remove(classifier);
253 Gatherer.c_man.configurationChanged();
254 }
255
256 /** Method to cache the current contents of library (known classifiers) to file.
257 * @see org.greenstone.gatherer.util.Utility
258 */
259 private void saveClassifiers() {
260 try {
261 File classifiers_dat_file = new File(Utility.getGLIUserFolder(), "classifiers.dat");
262 FileOutputStream file = new FileOutputStream(classifiers_dat_file);
263 ObjectOutputStream out = new ObjectOutputStream(file);
264 out.writeObject(library);
265 out.close();
266 }
267 catch (Exception error) {
268 }
269 }
270
271 private Object[] getAvailable() {
272 ArrayList available = new ArrayList();
273 int library_size = library.size();
274 for(int i = 0; i < library_size; i++) {
275 Classifier classifier = (Classifier) library.get(i);
276 if(!classifier.isAbstract()) {
277 available.add(classifier);
278 }
279 classifier = null;
280 }
281 return available.toArray();
282 }
283
284 /** Method to extract just the classifiers name from a file object.
285 * @param classifier The <strong>File</strong> which references a certain classifier.
286 * @return A <strong>String</strong> containing just the classifiers name, without extension.
287 */
288 private String getClassifierName(String filename) {
289 String name = filename;
290 if(name.indexOf(".") != -1) {
291 name = name.substring(0, name.indexOf("."));
292 }
293 return name;
294 }
295
296 /** Method to load the details of a single plug-in.
297 * @param classifier The classifier <strong>File</strong> you wish to load.
298 */
299 private void loadClassifier(String classifier, String lang) {
300 ///ystem.err.println("Attempting to parse " + classifier);
301 Document document = null;
302 InputStream input_stream = null;
303
304 long start;
305 long end;
306 // Run classinfo on this classifier, and then send the results for parsing.
307 try {
308 if (Gatherer.isGsdlRemote) {
309 String launch = Gatherer.cgiBase + "launch";
310 launch += "?cmd=classinfo.pl";
311 launch += "&xml=&language="+lang;
312 launch += "&class=" + getClassifierName(classifier);
313
314 System.err.println("*** launch = " + launch);
315
316 URL launch_url = new URL(launch);
317 URLConnection launch_connection = launch_url.openConnection();
318 input_stream = launch_connection.getInputStream();
319 }
320 else {
321 String args[] = null;
322 if(Utility.isWindows()) {
323 args = new String[6];
324 if(Configuration.perl_path != null) {
325 args[0] = Configuration.perl_path;
326 }
327 else {
328 args[0] = "Perl.exe";
329 }
330 args[1] = Configuration.gsdl_path + "bin" + File.separator + "script" + File.separator + "classinfo.pl";
331 args[2] = "-xml";
332 args[3] = "-language";
333 args[4] = lang;
334 args[5] = getClassifierName(classifier);
335 }
336 else {
337 args = new String[5];
338 args[0] = "classinfo.pl";
339 args[1] = "-xml";
340 args[2] = "-language";
341 args[3] = lang;
342 args[4] = getClassifierName(classifier);
343 }
344
345 // Create the process.
346 Runtime runtime = Runtime.getRuntime();
347 Process process = runtime.exec(args);
348
349 input_stream = process.getErrorStream();
350 }
351
352 StringBuffer xml = Utility.readXMLStream(input_stream);
353 document = CollectionDesignManager.XMLStringToDOM(xml,classifier);
354 }
355 catch (Exception error) {
356 error.printStackTrace();
357 ///ystem.err.println("Error: Cannot parse " + getClassifierName(classifier));
358 }
359 if(document != null) {
360 parseXML(document.getDocumentElement());
361 }
362 }
363
364 /** Method to initially load information from the standard classifiers within the gsdl Perl library.
365 * @see org.greenstone.gatherer.util.Utility
366 */
367 private void loadClassifiers() {
368 // Attempt to restore the cached file.
369 File classifiers_dat_file = new File(Utility.getGLIUserFolder(), "classifiers.dat");
370 try {
371 FileInputStream file = new FileInputStream(classifiers_dat_file);
372 ObjectInputStream input = new ObjectInputStream(file);
373 library = (ArrayList) input.readObject();
374 }
375 catch (Exception error) {
376 DebugStream.println("Unable to open " + classifiers_dat_file);
377 }
378
379 if(library == null) {
380 library = new ArrayList();
381
382 if (Gatherer.isGsdlRemote) {
383
384 String lang = Configuration.getLanguage();
385
386 String launch = Gatherer.cgiBase + "launch";
387 launch += "?cmd=classinfo.pl";
388 launch += "&xml=&language="+lang;
389 launch += "&listall=";
390
391 System.err.println("*** launch = " + launch);
392
393 try {
394 URL launch_url = new URL(launch);
395 URLConnection launch_connection = launch_url.openConnection();
396 InputStream input_stream = launch_connection.getInputStream();
397 loadClassifiers(input_stream);
398 }
399 catch (Exception error) {
400 System.err.println("Failed when trying to connect to : " + launch);
401 error.printStackTrace();
402 }
403
404 }
405 else {
406
407 // Retrieve the gsdl home directory...
408 String directory = Configuration.gsdl_path;
409 directory = directory + "perllib" + File.separator + "classify" + File.separator;
410
411 String current_lang = Configuration.getLanguage();
412 File files[] = (new File(directory)).listFiles();
413 if(files != null) {
414 // Create a progress indicator.
415 ParsingProgress progress = new ParsingProgress(Dictionary.get("CDM.ClassifierManager.Parsing.Title"), Dictionary.get("CDM.ClassifierManager.Parsing.Message"), files.length);
416 for(int i = 0; i < files.length; i++) {
417 // We only want to check Perl Modules.
418 if(files[i].getName().endsWith(".pm")) {
419 loadClassifier(files[i].getName(), current_lang);
420 }
421 progress.inc();
422 }
423 progress.dispose();
424 progress.destroy();
425 progress = null;
426 }
427 }
428 }
429 }
430
431
432
433 /** Method to load classifier information from a specified input stream (could be local or through URL). Of course no classifiers may be found at this location.
434 * @param input_stream An <strong>InputStream</strong> indicating the where list of classifiers -- encoded in XML -- can be read from
435 */
436 private void loadClassifiers(InputStream input_stream)
437 {
438 StringBuffer xml = Utility.readXMLStream(input_stream);
439 Document document = CollectionDesignManager.XMLStringToDOM(xml, "-listall");
440
441 // Parse XML to build up list of classifier names
442 Node root = document.getDocumentElement();
443
444 NamedNodeMap attributes = root.getAttributes();
445 Node length_node = attributes.getNamedItem("length");
446 String num_classifiers_str = length_node.getNodeValue();
447 int num_classifiers = Integer.parseInt(num_classifiers_str);
448 String class_list[] = new String[num_classifiers];
449
450 Node node = root.getFirstChild();
451 int i = 0;
452 while (node != null) {
453 String node_name = node.getNodeName();
454 if (node_name.equalsIgnoreCase("ClassifyName")) {
455 String name = XMLTools.getValue(node);
456 class_list[i] = name;
457 i++;
458 }
459
460 node = node.getNextSibling();
461 }
462
463 String current_lang = Configuration.getLanguage();
464 if (num_classifiers>0) {
465 // Create a progress indicator.
466 ParsingProgress progress = new ParsingProgress(Dictionary.get("CDM.ClassifierManager.Parsing.Title"), Dictionary.get("CDM.ClassifierManager.Parsing.Message"), num_classifiers);
467
468 for (i=0; i<num_classifiers; i++) {
469 String classifier = class_list[i];
470 loadClassifier(classifier, current_lang);
471 progress.inc();
472 }
473 progress.dispose();
474 progress.destroy();
475 progress = null;
476 }
477 }
478
479
480 /** Parses a DOM tree model turning it into a Classifier and its associated arguments.
481 * @param root The <strong>Node</strong> at the root of the DOM model.
482 * @return A newly created <strong>Classifier</strong> based on the information parsed from the DOM model.
483 * @see org.greenstone.gatherer.cdm.Argument
484 */
485 private Classifier parseXML(Node root) {
486 Classifier classifier = new Classifier();
487 String node_name = null;
488 for(Node node = root.getFirstChild(); node != null;
489 node = node.getNextSibling()) {
490 node_name = node.getNodeName();
491 if(node_name.equals("Name")) {
492 String name = XMLTools.getValue(node);
493 // We can save ourselves some processing time if a classifier with this name already exists in our manager. If so retrieve it and return it.
494 Classifier existing = getBaseClassifier(name);
495 if(existing != null) {
496 return existing;
497 }
498 classifier.setName(name);
499 }
500 else if(node_name.equals("Desc")) {
501 classifier.setDescription(XMLTools.getValue(node));
502 }
503 else if(node_name.equals("Abstract")) {
504 classifier.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(CollectionConfiguration.YES_STR));
505 }
506 // Parse the multitude of arguments.
507 else if(node_name.equals("Arguments")) {
508 for(Node arg = node.getFirstChild(); arg != null; arg = arg.getNextSibling()) {
509 node_name = arg.getNodeName();
510 // An option.
511 if(node_name.equals("Option")) {
512 Argument argument = new Argument();
513 argument.parseXML((Element)arg);
514 classifier.addArgument(argument);
515 }
516 // A super classifier class.
517 else if(node_name.equals("ClassInfo")) {
518 Classifier super_classifier = parseXML(arg);
519 classifier.setSuper(super_classifier);
520 }
521 }
522 }
523 }
524 if(classifier.getName() != null) {
525 addClassifier(classifier);
526 return classifier;
527 }
528 return null;
529 }
530
531 /** A class which provides controls for assigned and editing classifiers. */
532 private class ClassifierControl
533 extends JPanel
534 implements Control {
535 /** A combobox containing all of the known classifiers, including those that may have already been assigned. */
536 private GComboBox classifier = null;
537 /** Button for adding classifiers. */
538 private JButton add = null;
539 /** Button for configuring the selected classifier. */
540 private JButton configure = null;
541
542 //private JButton move_bottom_button;
543
544 private JButton move_down_button;
545
546 //private JButton move_top_button;
547
548 private JButton move_up_button;
549
550 /** Button to remove the selected classifier. */
551 private JButton remove = null;
552
553 /** A list of assigned classifiers. */
554 private JList classifier_list = null;
555 /** The text area containing instructions on the use of this control. */
556 private JTextArea instructions = null;
557
558 /** Constructor.
559 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.AddListener
560 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.ConfigureListener
561 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.RemoveListener
562 */
563 public ClassifierControl() {
564 Collections.sort(library);
565 // Create
566 add = new GLIButton();
567 add.setMnemonic(KeyEvent.VK_A);
568 Dictionary.registerBoth(add, "CDM.ClassifierManager.Add", "CDM.ClassifierManager.Add_Tooltip");
569 JPanel button_pane = new JPanel();
570 JPanel central_pane = new JPanel();
571 configure = new GLIButton();
572 configure.setEnabled(false);
573 configure.setMnemonic(KeyEvent.VK_C);
574 Dictionary.registerBoth(configure, "CDM.ClassifierManager.Configure", "CDM.ClassifierManager.Configure_Tooltip");
575 JPanel header_pane = new JPanel();
576 instructions = new JTextArea();
577 instructions.setEditable(false);
578 instructions.setLineWrap(true);
579 instructions.setRows(6);
580 instructions.setWrapStyleWord(true);
581 Dictionary.registerText(instructions, "CDM.ClassifierManager.Instructions");
582
583 ClassifierComboboxListener ccl = new ClassifierComboboxListener();
584 classifier = new GComboBox(getAvailable());
585 classifier.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
586 classifier.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
587 classifier.setEditable(true);
588 classifier.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
589 classifier.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
590 if(classifier.getItemCount() > 0) {
591 classifier.setSelectedIndex(0);
592 ccl.itemStateChanged(new ItemEvent(classifier, 0, null, ItemEvent.SELECTED));
593 }
594
595 JLabel classifier_label = new JLabel();
596 Dictionary.registerText(classifier_label, "CDM.ClassifierManager.Classifier");
597 classifier_list = new JList(model);
598 classifier_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
599 JLabel classifier_list_label = new JLabel();
600 classifier_list_label.setHorizontalAlignment(JLabel.CENTER);
601 classifier_list_label.setOpaque(true);
602 Dictionary.registerText(classifier_list_label, "CDM.ClassifierManager.Assigned");
603 JPanel classifier_list_pane = new JPanel();
604 JPanel classifier_pane = new JPanel();
605 remove = new GLIButton();
606 remove.setEnabled(false);
607 remove.setMnemonic(KeyEvent.VK_R);
608 Dictionary.registerBoth(remove, "CDM.ClassifierManager.Remove", "CDM.ClassifierManager.Remove_Tooltip");
609
610 JLabel title = new JLabel();
611 title.setHorizontalAlignment(JLabel.CENTER);
612 title.setOpaque(true);
613 Dictionary.registerText(title, "CDM.ClassifierManager.Title");
614
615 JPanel temp = new JPanel(new BorderLayout());
616
617 JPanel move_button_pane = new JPanel();
618
619 move_up_button = new JButton("", Utility.getImage("arrow-up.gif"));
620 move_up_button.setEnabled(false);
621 move_up_button.setMnemonic(KeyEvent.VK_U);
622 //move_up_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
623 Dictionary.registerBoth(move_up_button, "CDM.Move.Move_Up", "CDM.Move.Move_Up_Tooltip");
624
625 move_down_button = new JButton("", Utility.getImage("arrow-down.gif"));
626 move_down_button.setEnabled(false);
627 move_down_button.setMnemonic(KeyEvent.VK_D);
628 //move_down_button.setPreferredSize(Utility.DOUBLE_IMAGE_BUTTON_SIZE);
629 Dictionary.registerBoth(move_down_button, "CDM.Move.Move_Down", "CDM.Move.Move_Down_Tooltip");
630
631 // Listeners
632 add.addActionListener(new AddListener());
633 classifier.addItemListener(ccl);
634 configure.addActionListener(new ConfigureListener());
635 remove.addActionListener(new RemoveListener());
636 classifier_list.addMouseListener(new ClickListener());
637 classifier_list.addListSelectionListener(new ListListener());
638 ccl = null;
639
640 MoveListener ml = new MoveListener();
641 //move_bottom_button.addActionListener(ml);
642 move_down_button.addActionListener(ml);
643 //move_top_button.addActionListener(ml);
644 move_up_button.addActionListener(ml);
645
646 // Layout
647 title.setBorder(BorderFactory.createEmptyBorder(0,0,2,0));
648
649 instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
650
651 header_pane.setLayout(new BorderLayout());
652 header_pane.add(title, BorderLayout.NORTH);
653 header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
654
655 move_button_pane.setLayout(new GridLayout(4,1));
656 move_button_pane.add(move_up_button);
657 move_button_pane.add(new JPanel());
658 move_button_pane.add(new JPanel());
659 move_button_pane.add(move_down_button);
660
661 classifier_list_label.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
662
663 classifier_list_pane.setLayout(new BorderLayout());
664 classifier_list_pane.add(classifier_list_label, BorderLayout.NORTH);
665 classifier_list_pane.add(new JScrollPane(classifier_list), BorderLayout.CENTER);
666 classifier_list_pane.add(move_button_pane, BorderLayout.EAST);
667
668 classifier_label.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
669
670 classifier_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
671 classifier_pane.setLayout(new BorderLayout(5,0));
672 classifier_pane.add(classifier_label, BorderLayout.WEST);
673 classifier_pane.add(classifier, BorderLayout.CENTER);
674
675 button_pane.setLayout(new GridLayout(1, 3));
676 button_pane.add(add);
677 button_pane.add(configure);
678 button_pane.add(remove);
679
680 // Scope these mad bordering skillz.
681 temp.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,0,5,0), BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(Dictionary.get("CDM.ClassifierManager.Controls")), BorderFactory.createEmptyBorder(2,2,2,2))));
682 temp.add(classifier_pane, BorderLayout.NORTH);
683 temp.add(button_pane, BorderLayout.SOUTH);
684
685 central_pane.setLayout(new BorderLayout());
686 central_pane.add(classifier_list_pane, BorderLayout.CENTER);
687 central_pane.add(temp, BorderLayout.SOUTH);
688
689 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
690 setLayout(new BorderLayout());
691 add(header_pane, BorderLayout.NORTH);
692 add(central_pane, BorderLayout.CENTER);
693 }
694
695 /** Method which acts like a destructor, tidying up references to persistant objects.
696 */
697 public void destroy() {
698 add = null;
699 classifier = null;
700 classifier_list = null;
701 configure = null;
702 instructions = null;
703 remove = null;
704 }
705
706 /** This method is overridden to ensure the instructions are scrolled to top, before the super classes updateUI() is called.
707 */
708 public void gainFocus() {
709 if(instructions != null) {
710 instructions.setCaretPosition(0);
711 }
712 }
713
714 public void loseFocus() {
715 }
716
717 /** This class listens for actions upon the add button in the controls, and if detected calls the assignClassifier() method.
718 */
719 private class AddListener
720 implements ActionListener {
721 /** Any implementation of ActionListener must include this method so that we can be informed when an action has occured on one of our target controls, so that we can add the selected Classifier.
722 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
723 * @see org.greenstone.gatherer.Gatherer
724 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration
725 * @see org.greenstone.gatherer.cdm.Classifier
726 */
727 public void actionPerformed(ActionEvent event) {
728 Object selected_object = classifier.getSelectedItem();
729 // If there is something in the combobox, but we haven't registered a selection, then add the object and select it!
730 if(selected_object != null) {
731 // Retrieve the base classifier
732 Classifier base_classifier = getBaseClassifier(selected_object.toString());
733
734 // Create a new element in the DOM
735 Element element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.CLASSIFY_ELEMENT);
736 Classifier new_classifier = null;
737 if(base_classifier != null) {
738 DebugStream.println("Creating Classifier based on existing Classifer.");
739 element.setAttribute(CollectionConfiguration.TYPE_ATTRIBUTE, base_classifier.getName());
740 new_classifier = new Classifier(element, base_classifier);
741 }
742 else {
743 DebugStream.println("Creating new custom Classifier.");
744 element.setAttribute(CollectionConfiguration.TYPE_ATTRIBUTE, selected_object.toString());
745 new_classifier = new Classifier(element, null);
746 // Also we add the custom classifier name to the combobox for convienence.
747 classifier.addItem(selected_object);
748 }
749 element = null;
750 // Automatically chain to configuration. This ensures required arguments are filled out.
751 ArgumentConfiguration ac = new ArgumentConfiguration(new_classifier);
752 if(ac.display()) {
753 if(!model.contains(new_classifier)) {
754 assignClassifier(new_classifier);
755 classifier_list.setSelectedValue(new_classifier, true);
756 }
757 else {
758 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_Exists"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
759 }
760 }
761 ac = null;
762 new_classifier = null;
763 }
764 }
765 }
766
767 /** This listener reacts to changes in the current selection of the classifier combobox. */
768 private class ClassifierComboboxListener
769 implements ItemListener {
770 /** When a user selects a certain plugin, update the tooltip to show the plugin description. */
771 public void itemStateChanged(ItemEvent event) {
772 if(event.getStateChange() == ItemEvent.SELECTED) {
773 // Retrieve the selected plugin
774 Object current_selection = classifier.getSelectedItem();
775 // And reset the tooltip. If the plugin is null or is a string, then go back to the default message
776 if(current_selection == null || current_selection instanceof String) {
777 Dictionary.registerTooltip(classifier, "CDM.ClassifierManager.Classifier_Tooltip");
778 }
779 else {
780 Classifier current_classifier = (Classifier) current_selection;
781 Dictionary.registerTooltipText(classifier, Utility.formatHTMLWidth(current_classifier.getDescription(), 40));
782 current_classifier = null;
783 }
784 current_selection = null;
785 }
786 }
787 }
788
789 /** Listens for double clicks apon the list and react as if the configure button was pushed. */
790 private class ClickListener
791 extends MouseAdapter {
792 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
793 * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
794 */
795 public void mouseClicked(MouseEvent event) {
796 if(event.getClickCount() == 2 ) {
797 if(!classifier_list.isSelectionEmpty()) {
798 Classifier classifier = (Classifier) classifier_list.getSelectedValue();
799 ArgumentConfiguration ac = new ArgumentConfiguration(classifier);
800 if(ac.display()) {
801 refresh(classifier);
802 }
803 ac.destroy();
804 ac = null;
805 }
806 }
807 }
808 }
809
810 /** This class listens for actions upon the configure button in the controls, and if detected creates a new ArgumentConfiguration dialog box to allow for configuration.
811 */
812 private class ConfigureListener
813 implements ActionListener {
814 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured on one of our target controls.
815 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
816 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration
817 * @see org.greenstone.gatherer.cdm.Classifier
818 */
819 public void actionPerformed(ActionEvent event) {
820 if(!classifier_list.isSelectionEmpty()) {
821 Classifier classifier = (Classifier) classifier_list.getSelectedValue();
822 ArgumentConfiguration ac = new ArgumentConfiguration(classifier);
823 if(ac.display()) {
824 refresh(classifier);
825 }
826 ac.destroy();
827 ac = null;
828 }
829 }
830 }
831
832 /** listens for changes in the list selection and enables the configure and remove buttons if there is a selection, disables them if there is no selection */
833 private class ListListener
834 implements ListSelectionListener {
835
836 public void valueChanged(ListSelectionEvent e) {
837 if (!e.getValueIsAdjusting()) { // we get two events for one change in list selection - use the false one ( the second one)
838 if (classifier_list.isSelectionEmpty()) {
839 //move_top_button.setEnabled(false);
840 move_up_button.setEnabled(false);
841 move_down_button.setEnabled(false);
842 //move_bottom_button.setEnabled(false);
843 configure.setEnabled(false);
844 remove.setEnabled(false);
845 }
846 else {
847 //move_top_button.setEnabled(true);
848 move_up_button.setEnabled(true);
849 move_down_button.setEnabled(true);
850 //move_bottom_button.setEnabled(true);
851 configure.setEnabled(true);
852 remove.setEnabled(true);
853 }
854 }
855 }
856 }
857
858 /** Listens for actions apon the move buttons in the manager controls, and if detected calls the <i>moveClassifier()</i> method of the manager with the appropriate details. */
859 private class MoveListener
860 implements ActionListener {
861 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured on one of our target controls.
862 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
863 */
864 public void actionPerformed(ActionEvent event) {
865 if(!classifier_list.isSelectionEmpty()) {
866 Object object = classifier_list.getSelectedValue();
867 if(object instanceof Classifier) {
868 Classifier classifier = (Classifier) object;
869 //if(event.getSource() == move_top_button) {
870 // moveClassifier(classifier, true, true);
871 //}
872 //else
873 if(event.getSource() == move_up_button) {
874 moveClassifier(classifier, true, false);
875 }
876 else if(event.getSource() == move_down_button) {
877 moveClassifier(classifier, false, false);
878 }
879 //else {
880 // moveClassifier(classifier, false, true);
881 //}
882 classifier_list.setSelectedValue(classifier, true);
883 }
884 }
885 }
886 }
887
888 /** This class listens for actions upon the remove button in the controls, and if detected calls the <i>removeClassifier()</i> method.
889 */
890 private class RemoveListener
891 implements ActionListener {
892 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured on one of our target controls.
893 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
894 */
895 public void actionPerformed(ActionEvent event) {
896 if(classifier_list.isSelectionEmpty()) {
897 remove.setEnabled(false);
898 return;
899 }
900 int selected_index = classifier_list.getSelectedIndex();
901 Object selected_classifier = classifier_list.getSelectedValue();
902 if (!(selected_classifier instanceof Classifier)) {
903 return; // what else could we have here???
904 }
905 removeClassifier((Classifier)selected_classifier);
906
907 if (selected_index >= classifier_list.getModel().getSize()) {
908 selected_index--;
909 }
910 if (selected_index >=0) {
911 classifier_list.setSelectedIndex(selected_index);
912 } else {
913 // no more classifiers in the list
914 remove.setEnabled(false);
915 }
916
917 }
918 }
919 }
920
921
922
923}
Note: See TracBrowser for help on using the repository browser.