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

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

Changed text handling to use Dictionary.get rather than Dictionary.setText or Dictionary.registerBoth etc. also removed mnemonics cos they suck for other languages.

  • Property svn:keywords set to Author Date Id Revision
File size: 32.0 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.LocalGreenstone;
43import org.greenstone.gatherer.gui.DesignPaneHeader;
44import org.greenstone.gatherer.gui.GComboBox;
45import org.greenstone.gatherer.gui.GLIButton;
46import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
47import org.greenstone.gatherer.util.JarTools;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.greenstone.gatherer.util.Utility;
50import org.greenstone.gatherer.util.XMLTools;
51import org.w3c.dom.*;
52import org.xml.sax.*;
53
54/** This class is responsible for keeping track of all the classifiers assigned to this collection, and providing methods for adding and removing them.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.3
57 */
58public class ClassifierManager
59 extends DOMProxyListModel {
60
61 /** A list of known, but currently unassigned, classifiers. */
62 private ArrayList library = null;
63 /** The controls for editing the contents of this manager. */
64 private Control controls = null;
65
66 private DOMProxyListModel model;
67
68 /** Constructor.
69 * @see org.greenstone.gatherer.cdm.DynamicListModel
70 * @see org.greenstone.gatherer.collection.CollectionManager
71 */
72 public ClassifierManager() {
73 super(CollectionDesignManager.collect_config.getDocumentElement(), CollectionConfiguration.CLASSIFY_ELEMENT, new Classifier());
74 this.model = this;
75 DebugStream.println("ClassifierManager: " + getSize() + " classifiers parsed.");
76 // Reload/Create the library
77 loadClassifiers();
78 saveClassifiers();
79 }
80
81 /** Method to add a new classifier to library.
82 * @param classifier The new <strong>Classifier</strong>.
83 * @see org.greenstone.gatherer.cdm.DynamicListModel
84 */
85 private void addClassifier(Classifier classifier) {
86 if(!library.contains(classifier)) {
87 library.add(classifier);
88 }
89 }
90
91 /** Method to assign a classifier.
92 * @param classifier The base <strong>Classifier</strong> to assign.
93 * @see org.greenstone.gatherer.cdm.DynamicListModel
94 */
95 private void assignClassifier(Classifier classifier) {
96 if(!contains(classifier)) {
97 Element element = classifier.getElement();
98 // Locate where we should insert this new classifier.
99 Node target_node = CollectionConfiguration.findInsertionPoint(element);
100 add(root, classifier, target_node);
101 Gatherer.c_man.configurationChanged();
102 }
103 }
104
105 public static boolean clearClassifierCache() {
106
107 DebugStream.println("deleting classifiers.dat");
108 File class_file = new File(Gatherer.getGLIUserDirectoryPath() + "classifiers.dat");
109 if (class_file.exists()) {
110 return Utility.delete(class_file);
111 }
112 return true;
113 }
114 /** Destructor.
115 * @see org.greenstone.gatherer.Gatherer
116 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
117 * @see org.greenstone.gatherer.cdm.DynamicListModel
118 */
119 public void destroy() {
120 if(controls != null) {
121 controls.destroy();
122 controls = null;
123 }
124 library.clear();
125 library = null;
126 }
127
128 public Classifier getBaseClassifier(String name) {
129 int library_size = library.size();
130 for(int i = 0; i < library_size; i++) {
131 Classifier classifier = (Classifier) library.get(i);
132 if(classifier.getName().equals(name)) {
133 return classifier;
134 }
135 }
136 // No success.
137 return null;
138 }
139
140 /** Method to retrieve the classifier with the given index.
141 * @param index The index of the desired classifier as an <i>int</i>.
142 * @return The requested Classifier or <i>null</i> if no such classifier exists.
143 */
144 public Classifier getClassifier(int index) {
145 if(0 <= index && index < getSize()) {
146 return (Classifier) getElementAt(index);
147 }
148 return null;
149 }
150
151 /** Method to retrieve the control for this manager.
152 * @return the Control for editing classifiers
153 */
154 public Control getControls() {
155 if(controls == null) {
156 // Build controls
157 this.controls = new ClassifierControl();
158 }
159 return controls;
160 }
161
162 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
163 * @param mode the new mode as an int
164 */
165 public void modeChanged(int mode) {
166
167 }
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 }
206 else {
207 // Remove the moving classifier
208 remove(classifier);
209 // And add after last classifier
210 add(getSize(), classifier);
211 }
212 }
213 else {
214 // Try to move the classifier one step in the desired direction.
215 int index = indexOf(classifier);
216 ///ystem.err.println("Index of " + classifier + " = " + index);
217 if(direction) {
218 index--;
219 if(index < 0) {
220 String args[] = new String[2];
221 args[0] = Dictionary.get("CDM.ClassifierManager.Classifier");
222 args[1] = classifier.getName();
223 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.Move.At_Top", args), Dictionary.get("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE);
224 return;
225 }
226 remove(classifier);
227 add(index, classifier);
228 }
229 else {
230 index++;
231 if(index >= getSize()) {
232 String args[] = new String[2];
233 args[0] = Dictionary.get("CDM.ClassifierManager.Classifier_Str");
234 args[1] = classifier.getName();
235 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.Move.At_Bottom", args), Dictionary.get("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE);
236 return;
237 }
238 remove(classifier);
239 add(index, classifier);
240 }
241 }
242
243 Gatherer.c_man.configurationChanged();
244 // tell the format manager to update the names of its format statements
245 Gatherer.c_man.getCollection().cdm.format_manager.refresh();
246 }
247
248 /** 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.
249 * @param classifier The Classifier to remove
250 * @see org.greenstone.gatherer.cdm.DynamicListModel
251 */
252 private void removeClassifier(Classifier classifier) {
253 remove(classifier);
254 Gatherer.c_man.configurationChanged();
255 }
256
257 /** Method to cache the current contents of library (known classifiers) to file.
258 * @see org.greenstone.gatherer.util.Utility
259 */
260 private void saveClassifiers() {
261 try {
262 File classifiers_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "classifiers.dat");
263 FileOutputStream file = new FileOutputStream(classifiers_dat_file);
264 ObjectOutputStream out = new ObjectOutputStream(file);
265 out.writeObject(library);
266 out.close();
267 }
268 catch (Exception error) {
269 }
270 }
271
272 private Object[] getAvailable() {
273 ArrayList available = new ArrayList();
274 int library_size = library.size();
275 for(int i = 0; i < library_size; i++) {
276 Classifier classifier = (Classifier) library.get(i);
277 if(!classifier.isAbstract()) {
278 available.add(classifier);
279 }
280 classifier = null;
281 }
282 return available.toArray();
283 }
284
285 /** Method to extract just the classifiers name from a file object.
286 * @param classifier The <strong>File</strong> which references a certain classifier.
287 * @return A <strong>String</strong> containing just the classifiers name, without extension.
288 */
289 private String getClassifierName(String filename) {
290 String name = filename;
291 if(name.indexOf(".") != -1) {
292 name = name.substring(0, name.indexOf("."));
293 }
294 return name;
295 }
296
297 /** Method to load the details of a single plug-in.
298 * @param classifier The classifier <strong>File</strong> you wish to load.
299 */
300 private void loadClassifier(String classifier, String lang) {
301 ///ystem.err.println("Attempting to parse " + classifier);
302 Document document = null;
303 InputStream input_stream = null;
304
305 long start;
306 long end;
307 // Run classinfo on this classifier, and then send the results for parsing.
308 try {
309 StringBuffer xml = null;
310 if (Gatherer.isGsdlRemote) {
311 String classinfo_output = RemoteGreenstoneServer.getScriptOptions("classinfo.pl", "&classifier=" + classifier);
312 xml = new StringBuffer(classinfo_output);
313 }
314 else {
315 String args[] = null;
316 if(Utility.isWindows()) {
317 args = new String[6];
318 if(Configuration.perl_path != null) {
319 args[0] = Configuration.perl_path;
320 }
321 else {
322 args[0] = "Perl.exe";
323 }
324 args[1] = LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.pl";
325 args[2] = "-xml";
326 args[3] = "-language";
327 args[4] = lang;
328 args[5] = getClassifierName(classifier);
329 }
330 else {
331 args = new String[5];
332 args[0] = "classinfo.pl";
333 args[1] = "-xml";
334 args[2] = "-language";
335 args[3] = lang;
336 args[4] = getClassifierName(classifier);
337 }
338
339 // Create the process.
340 Runtime runtime = Runtime.getRuntime();
341 Process process = runtime.exec(args);
342
343 input_stream = process.getErrorStream();
344 xml = XMLTools.readXMLStream(input_stream);
345 }
346
347 if (xml.length() > 0) {
348 document = CollectionDesignManager.XMLStringToDOM(xml, classifier);
349 }
350 else {
351 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_XML_Parse_Failed", classifier), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
352 }
353 }
354 catch (Exception exception) {
355 DebugStream.println("Failed when trying to parse: " + classifier);
356 DebugStream.printStackTrace(exception);
357 }
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(Gatherer.getGLIUserDirectoryPath() + "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 String classinfo_output = RemoteGreenstoneServer.getScriptOptions("classinfo.pl", "&listall=");
384 loadClassifiers(new StringBuffer(classinfo_output));
385 }
386
387 else {
388 // Retrieve the gsdl home directory...
389 String directory = LocalGreenstone.getDirectoryPath();
390 directory = directory + "perllib" + File.separator + "classify" + File.separator;
391
392 String current_lang = Configuration.getLanguage();
393 File files[] = (new File(directory)).listFiles();
394 if(files != null) {
395 // Create a progress indicator.
396 ParsingProgress progress = new ParsingProgress(Dictionary.get("CDM.ClassifierManager.Parsing.Title"), Dictionary.get("CDM.ClassifierManager.Parsing.Message"), files.length);
397 for(int i = 0; i < files.length; i++) {
398 // We only want to check Perl Modules.
399 if(files[i].getName().endsWith(".pm")) {
400 loadClassifier(files[i].getName(), current_lang);
401 }
402 progress.inc();
403 }
404 progress.dispose();
405 progress.destroy();
406 progress = null;
407 }
408 }
409 }
410 }
411
412
413
414 /** 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.
415 * @param input_stream An <strong>InputStream</strong> indicating the where list of classifiers -- encoded in XML -- can be read from
416 */
417 private void loadClassifiers(StringBuffer xml)
418 {
419 Document document = CollectionDesignManager.XMLStringToDOM(xml, "-listall");
420
421 // Parse XML to build up list of classifier names
422 Node root = document.getDocumentElement();
423
424 NamedNodeMap attributes = root.getAttributes();
425 Node length_node = attributes.getNamedItem("length");
426 String num_classifiers_str = length_node.getNodeValue();
427 int num_classifiers = Integer.parseInt(num_classifiers_str);
428 String class_list[] = new String[num_classifiers];
429
430 Node node = root.getFirstChild();
431 int i = 0;
432 while (node != null) {
433 String node_name = node.getNodeName();
434 if (node_name.equalsIgnoreCase("ClassifyName")) {
435 String name = XMLTools.getValue(node);
436 class_list[i] = name;
437 i++;
438 }
439
440 node = node.getNextSibling();
441 }
442
443 String current_lang = Configuration.getLanguage();
444 if (num_classifiers>0) {
445 // Create a progress indicator.
446 ParsingProgress progress = new ParsingProgress(Dictionary.get("CDM.ClassifierManager.Parsing.Title"), Dictionary.get("CDM.ClassifierManager.Parsing.Message"), num_classifiers);
447
448 for (i=0; i<num_classifiers; i++) {
449 String classifier = class_list[i];
450 loadClassifier(classifier, current_lang);
451 progress.inc();
452 }
453 progress.dispose();
454 progress.destroy();
455 progress = null;
456 }
457 }
458
459
460 /** Parses a DOM tree model turning it into a Classifier and its associated arguments.
461 * @param root The <strong>Node</strong> at the root of the DOM model.
462 * @return A newly created <strong>Classifier</strong> based on the information parsed from the DOM model.
463 * @see org.greenstone.gatherer.cdm.Argument
464 */
465 private Classifier parseXML(Node root) {
466 Classifier classifier = new Classifier();
467 String node_name = null;
468 for(Node node = root.getFirstChild(); node != null;
469 node = node.getNextSibling()) {
470 node_name = node.getNodeName();
471 if(node_name.equals("Name")) {
472 String name = XMLTools.getValue(node);
473 // 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.
474 Classifier existing = getBaseClassifier(name);
475 if(existing != null) {
476 return existing;
477 }
478 classifier.setName(name);
479 }
480 else if(node_name.equals("Desc")) {
481 classifier.setDescription(XMLTools.getValue(node));
482 }
483 else if(node_name.equals("Abstract")) {
484 classifier.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(CollectionConfiguration.YES_STR));
485 }
486 // Parse the multitude of arguments.
487 else if(node_name.equals("Arguments")) {
488 for(Node arg = node.getFirstChild(); arg != null; arg = arg.getNextSibling()) {
489 node_name = arg.getNodeName();
490 // An option.
491 if(node_name.equals("Option")) {
492 Argument argument = new Argument();
493 argument.parseXML((Element)arg);
494 classifier.addArgument(argument);
495 }
496 }
497 }
498 // A super classifier class.
499 else if(node_name.equals("ClassInfo")) {
500 Classifier super_classifier = parseXML(node);
501 classifier.setSuper(super_classifier);
502 }
503
504 }
505 if(classifier.getName() != null) {
506 addClassifier(classifier);
507 return classifier;
508 }
509 return null;
510 }
511
512 /** A class which provides controls for assigned and editing classifiers. */
513 private class ClassifierControl
514 extends JPanel
515 implements Control {
516 /** A combobox containing all of the known classifiers, including those that may have already been assigned. */
517 private GComboBox classifier = null;
518 /** Button for adding classifiers. */
519 private JButton add = null;
520 /** Button for configuring the selected classifier. */
521 private JButton configure = null;
522 private JButton move_down_button;
523 private JButton move_up_button;
524
525 /** Button to remove the selected classifier. */
526 private JButton remove = null;
527
528 /** A list of assigned classifiers. */
529 private JList classifier_list = null;
530
531 /** Constructor.
532 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.AddListener
533 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.ConfigureListener
534 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.RemoveListener
535 */
536 public ClassifierControl() {
537 Collections.sort(library);
538 // Create
539 add = new GLIButton(Dictionary.get("CDM.ClassifierManager.Add"), Dictionary.get("CDM.ClassifierManager.Add_Tooltip"));
540
541 JPanel button_pane = new JPanel();
542 JPanel central_pane = new JPanel();
543
544configure = new GLIButton(Dictionary.get("CDM.ClassifierManager.Configure"), Dictionary.get("CDM.ClassifierManager.Configure_Tooltip"));
545 configure.setEnabled(false);
546
547 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Classifiers", "classifiers");
548
549 ClassifierComboboxListener ccl = new ClassifierComboboxListener();
550 classifier = new GComboBox(getAvailable());
551 classifier.setBackgroundNonSelectionColor(Configuration.getColor("coloring.editable_background", false));
552 classifier.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
553 classifier.setEditable(true);
554 classifier.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
555 classifier.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
556 if(classifier.getItemCount() > 0) {
557 classifier.setSelectedIndex(0);
558 ccl.itemStateChanged(new ItemEvent(classifier, 0, null, ItemEvent.SELECTED));
559 }
560
561 JLabel classifier_label = new JLabel(Dictionary.get("CDM.ClassifierManager.Classifier"));
562
563 classifier_list = new JList(model);
564 classifier_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
565 JLabel classifier_list_label = new JLabel(Dictionary.get("CDM.ClassifierManager.Assigned"));
566
567 classifier_list_label.setOpaque(true);
568
569 JPanel classifier_list_pane = new JPanel();
570 JPanel classifier_pane = new JPanel();
571 remove = new GLIButton(Dictionary.get("CDM.ClassifierManager.Remove"), Dictionary.get("CDM.ClassifierManager.Remove_Tooltip"));
572 remove.setEnabled(false);
573
574 JPanel temp = new JPanel(new BorderLayout());
575
576 JPanel move_button_pane = new JPanel();
577
578 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
579 move_up_button.setEnabled(false);
580
581 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
582 move_down_button.setEnabled(false);
583
584 // Listeners
585 add.addActionListener(new AddListener());
586 add.addActionListener(CollectionDesignManager.buildcol_change_listener);
587 classifier.addItemListener(ccl);
588 configure.addActionListener(new ConfigureListener());
589 configure.addActionListener(CollectionDesignManager.buildcol_change_listener);
590 remove.addActionListener(new RemoveListener());
591 remove.addActionListener(CollectionDesignManager.buildcol_change_listener);
592 classifier_list.addMouseListener(new ClickListener());
593 classifier_list.addListSelectionListener(new ListListener());
594 ccl = null;
595
596 MoveListener ml = new MoveListener();
597 move_down_button.addActionListener(ml);
598 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
599 move_up_button.addActionListener(ml);
600 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
601
602 // Layout
603 move_button_pane.setLayout(new GridLayout(4,1));
604 move_button_pane.add(move_up_button);
605 move_button_pane.add(new JPanel());
606 move_button_pane.add(new JPanel());
607 move_button_pane.add(move_down_button);
608
609 classifier_list_label.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
610
611 classifier_list_pane.setLayout(new BorderLayout());
612 classifier_list_pane.add(classifier_list_label, BorderLayout.NORTH);
613 classifier_list_pane.add(new JScrollPane(classifier_list), BorderLayout.CENTER);
614 classifier_list_pane.add(move_button_pane, BorderLayout.EAST);
615
616 classifier_label.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
617
618 classifier_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
619 classifier_pane.setLayout(new BorderLayout(5,0));
620 classifier_pane.add(classifier_label, BorderLayout.WEST);
621 classifier_pane.add(classifier, BorderLayout.CENTER);
622
623 button_pane.setLayout(new GridLayout(1, 3));
624 button_pane.add(add);
625 button_pane.add(configure);
626 button_pane.add(remove);
627
628 temp.add(classifier_pane, BorderLayout.NORTH);
629 temp.add(button_pane, BorderLayout.SOUTH);
630
631 central_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
632 central_pane.setLayout(new BorderLayout());
633 central_pane.add(classifier_list_pane, BorderLayout.CENTER);
634 central_pane.add(temp, BorderLayout.SOUTH);
635
636 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
637 setLayout(new BorderLayout());
638 add(header_pane, BorderLayout.NORTH);
639 add(central_pane, BorderLayout.CENTER);
640 }
641
642 /** Method which acts like a destructor, tidying up references to persistant objects.
643 */
644 public void destroy() {
645 add = null;
646 classifier = null;
647 classifier_list = null;
648 configure = null;
649 //instructions = null;
650 remove = null;
651 }
652
653 public void gainFocus() {
654 }
655
656 public void loseFocus() {
657 }
658
659 /** This class listens for actions upon the add button in the controls, and if detected calls the assignClassifier() method.
660 */
661 private class AddListener
662 implements ActionListener {
663 /** 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.
664 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
665 * @see org.greenstone.gatherer.Gatherer
666 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration
667 * @see org.greenstone.gatherer.cdm.Classifier
668 */
669 public void actionPerformed(ActionEvent event) {
670 Object selected_object = classifier.getSelectedItem();
671 // If there is something in the combobox, but we haven't registered a selection, then add the object and select it!
672 if(selected_object != null) {
673 // Retrieve the base classifier
674 Classifier base_classifier = getBaseClassifier(selected_object.toString());
675
676 // Create a new element in the DOM
677 Element element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.CLASSIFY_ELEMENT);
678 Classifier new_classifier = null;
679 if(base_classifier != null) {
680 DebugStream.println("Creating Classifier based on existing Classifer.");
681 element.setAttribute(CollectionConfiguration.TYPE_ATTRIBUTE, base_classifier.getName());
682 new_classifier = new Classifier(element, base_classifier);
683 }
684 else {
685 DebugStream.println("Creating new custom Classifier.");
686 element.setAttribute(CollectionConfiguration.TYPE_ATTRIBUTE, selected_object.toString());
687 new_classifier = new Classifier(element, null);
688 // Also we add the custom classifier name to the combobox for convienence.
689 classifier.addItem(selected_object);
690 }
691 element = null;
692 // Automatically chain to configuration. This ensures required arguments are filled out.
693 ArgumentConfiguration ac = new ArgumentConfiguration(new_classifier);
694 if(ac.display()) {
695 if(!model.contains(new_classifier)) {
696 assignClassifier(new_classifier);
697 classifier_list.setSelectedValue(new_classifier, true);
698 }
699 else {
700 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_Exists"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
701 }
702 }
703 ac = null;
704 new_classifier = null;
705 }
706 }
707 }
708
709 /** This listener reacts to changes in the current selection of the classifier combobox. */
710 private class ClassifierComboboxListener
711 implements ItemListener {
712 /** When a user selects a certain plugin, update the tooltip to show the plugin description. */
713 public void itemStateChanged(ItemEvent event) {
714 if(event.getStateChange() == ItemEvent.SELECTED) {
715 // Retrieve the selected plugin
716 Object current_selection = classifier.getSelectedItem();
717 // And reset the tooltip. If the plugin is null or is a string, then go back to the default message
718 if(current_selection == null || current_selection instanceof String) {
719 classifier.setToolTipText(Dictionary.get("CDM.ClassifierManager.Classifier_Tooltip"));
720 }
721 else {
722 Classifier current_classifier = (Classifier) current_selection;
723 classifier.setToolTipText(Utility.formatHTMLWidth(current_classifier.getDescription(), 40));
724 current_classifier = null;
725 }
726 current_selection = null;
727 }
728 }
729 }
730
731 /** Listens for double clicks apon the list and react as if the configure button was pushed. */
732 private class ClickListener
733 extends MouseAdapter {
734 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
735 * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
736 */
737 public void mouseClicked(MouseEvent event) {
738 if(event.getClickCount() == 2 ) {
739 if(!classifier_list.isSelectionEmpty()) {
740 Classifier classifier = (Classifier) classifier_list.getSelectedValue();
741 ArgumentConfiguration ac = new ArgumentConfiguration(classifier);
742 if(ac.display()) {
743 refresh(classifier);
744 }
745 ac.destroy();
746 ac = null;
747 // cos I can't be bothered checking every argument to see if it has changed or not, we'll asasume that the configuration has changed if someone has clicked configure
748 Gatherer.c_man.configurationChanged();
749 }
750 }
751 }
752 }
753
754 /** 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.
755 */
756 private class ConfigureListener
757 implements ActionListener {
758 /** 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.
759 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
760 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration
761 * @see org.greenstone.gatherer.cdm.Classifier
762 */
763 public void actionPerformed(ActionEvent event) {
764 if(!classifier_list.isSelectionEmpty()) {
765 Classifier classifier = (Classifier) classifier_list.getSelectedValue();
766 ArgumentConfiguration ac = new ArgumentConfiguration(classifier);
767 if(ac.display()) {
768 refresh(classifier);
769 }
770 ac.destroy();
771 ac = null;
772 // cos I can't be bothered checking every argument to see if it has changed or not, we'll asasume that the configuration has changed if someone has clicked configure
773 Gatherer.c_man.configurationChanged();
774 }
775 }
776 }
777
778 /** 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 */
779 private class ListListener
780 implements ListSelectionListener {
781
782 public void valueChanged(ListSelectionEvent e) {
783 if (!e.getValueIsAdjusting()) { // we get two events for one change in list selection - use the false one ( the second one)
784 if (classifier_list.isSelectionEmpty()) {
785 move_up_button.setEnabled(false);
786 move_down_button.setEnabled(false);
787 configure.setEnabled(false);
788 remove.setEnabled(false);
789 }
790 else {
791 configure.setEnabled(true);
792 remove.setEnabled(true);
793 int selected_index = classifier_list.getSelectedIndex();
794 move_up_button.setEnabled(selected_index !=0);
795 move_down_button.setEnabled(selected_index != model.getSize()-1);
796 }
797 }
798 }
799 }
800
801 /** 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. */
802 private class MoveListener
803 implements ActionListener {
804 /** 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.
805 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
806 */
807 public void actionPerformed(ActionEvent event) {
808 if(!classifier_list.isSelectionEmpty()) {
809 Object object = classifier_list.getSelectedValue();
810 if(object instanceof Classifier) {
811 Classifier classifier = (Classifier) object;
812 if(event.getSource() == move_up_button) {
813 moveClassifier(classifier, true, false);
814 }
815 else if(event.getSource() == move_down_button) {
816 moveClassifier(classifier, false, false);
817 }
818 classifier_list.setSelectedValue(classifier, true);
819 }
820 }
821 }
822 }
823
824 /** This class listens for actions upon the remove button in the controls, and if detected calls the <i>removeClassifier()</i> method.
825 */
826 private class RemoveListener
827 implements ActionListener {
828 /** 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.
829 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action.
830 */
831 public void actionPerformed(ActionEvent event) {
832 if(classifier_list.isSelectionEmpty()) {
833 remove.setEnabled(false);
834 return;
835 }
836 int selected_index = classifier_list.getSelectedIndex();
837 Object selected_classifier = classifier_list.getSelectedValue();
838 if (!(selected_classifier instanceof Classifier)) {
839 return; // what else could we have here???
840 }
841 removeClassifier((Classifier)selected_classifier);
842
843 if (selected_index >= classifier_list.getModel().getSize()) {
844 selected_index--;
845 }
846 if (selected_index >=0) {
847 classifier_list.setSelectedIndex(selected_index);
848 } else {
849 // no more classifiers in the list
850 remove.setEnabled(false);
851 }
852 }
853 }
854 }
855}
Note: See TracBrowser for help on using the repository browser.