Changeset 14036
- Timestamp:
- 2007-05-03T08:47:46+12:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/cdm/ClassifierManager.java
r13594 r14036 57 57 */ 58 58 public class ClassifierManager 59 extends DOMProxyListModel 60 { 59 extends DOMProxyListModel { 61 60 /** The controls for editing the contents of this manager. */ 62 61 private Control controls = null; 63 62 64 63 private DOMProxyListModel model; 65 64 66 65 /** Constructor. 67 66 * @see org.greenstone.gatherer.cdm.DynamicListModel 68 67 * @see org.greenstone.gatherer.collection.CollectionManager 69 68 */ 70 public ClassifierManager () {71 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.CLASSIFY_ELEMENT, new Classifier());72 73 DebugStream.println("ClassifierManager: " + getSize() + " classifiers parsed.");74 75 76 for (int i = 0; i < getSize(); i++) {77 getElementAt(i);78 79 } 80 81 69 public ClassifierManager () { 70 super (CollectionDesignManager.collect_config.getDocumentElement (), StaticStrings.CLASSIFY_ELEMENT, new Classifier ()); 71 this.model = this; 72 DebugStream.println ("ClassifierManager: " + getSize () + " classifiers parsed."); 73 74 // Force the assigned classifiers to be loaded and cached now 75 for (int i = 0; i < getSize (); i++) { 76 getElementAt (i); 77 } 78 } 79 80 82 81 /** Retrieve a list of the classifiers that are available to be added to the collection. */ 83 private Object[] getAvailableClassifiers() 84 { 85 ArrayList available = new ArrayList(); 86 87 // Add all the non-abstract core Greenstone classifiers 88 ArrayList classifiers_list = Classifiers.getClassifiersList(); 89 for (int i = 0; i < classifiers_list.size(); i++) { 90 Classifier classifier = (Classifier) classifiers_list.get(i); 91 if (!classifier.isAbstract()) { 92 available.add(classifier); 93 } 94 } 95 96 // Sort the available classifiers into alphabetical order 97 Collections.sort(available); 98 99 return available.toArray(); 100 } 101 102 103 /** Method to assign a classifier. 82 private Object[] getAvailableClassifiers () { 83 ArrayList available = new ArrayList (); 84 85 // Add all the non-abstract core Greenstone classifiers 86 ArrayList classifiers_list = Classifiers.getClassifiersList (); 87 for (int i = 0; i < classifiers_list.size (); i++) { 88 Classifier classifier = (Classifier) classifiers_list.get (i); 89 if (!classifier.isAbstract ()) { 90 available.add (classifier); 91 } 92 } 93 94 // Sort the available classifiers into alphabetical order 95 Collections.sort (available); 96 97 return available.toArray (); 98 } 99 100 101 /** Method to assign a classifier (i.e., add a new classifier onto the list). 104 102 * @param classifier The base <strong>Classifier</strong> to assign. 105 103 * @see org.greenstone.gatherer.cdm.DynamicListModel 106 104 */ 107 private void assignClassifier(Classifier classifier) { 108 if(!contains(classifier)) { 109 Element element = classifier.getElement(); 110 // Locate where we should insert this new classifier. 111 Node target_node = CollectionConfiguration.findInsertionPoint(element); 112 add(root, classifier, target_node); 113 } 114 } 105 private void assignClassifier (Classifier classifier) { 106 if(!contains (classifier)) { 107 Element element = classifier.getElement (); 108 // Locate where we should insert this new classifier. 109 Node target_node = CollectionConfiguration.findInsertionPoint (element); 110 add (root, classifier, target_node); 115 111 116 112 // tell the format manager to update the names of its format statements 113 Gatherer.c_man.getCollection ().cdm.format_manager.refresh (); 114 } 115 } 116 117 117 118 /** Destructor. */ 118 public void destroy() 119 { 120 if (controls != null) { 121 controls.destroy(); 122 controls = null; 123 } 124 } 125 126 119 public void destroy () { 120 if (controls != null) { 121 controls.destroy (); 122 controls = null; 123 } 124 } 125 126 127 127 /** Method to retrieve the classifier with the given index. 128 128 * @param index The index of the desired classifier as an <i>int</i>. 129 129 * @return The requested Classifier or <i>null</i> if no such classifier exists. 130 130 */ 131 public Classifier getClassifier (int index) {132 if(0 <= index && index < getSize()) {133 return (Classifier) getElementAt(index);134 135 136 } 137 131 public Classifier getClassifier (int index) { 132 if(0 <= index && index < getSize ()) { 133 return (Classifier) getElementAt (index); 134 } 135 return null; 136 } 137 138 138 /** Method to retrieve the control for this manager. 139 139 * @return the Control for editing classifiers 140 140 */ 141 public Control getControls () {142 143 144 this.controls = new ClassifierControl();145 146 147 } 148 141 public Control getControls () { 142 if(controls == null) { 143 // Build controls 144 this.controls = new ClassifierControl (); 145 } 146 return controls; 147 } 148 149 149 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden 150 150 * @param mode the new mode as an int 151 151 */ 152 public void modeChanged (int mode) {153 154 } 155 156 157 152 public void modeChanged (int mode) { 153 154 } 155 156 157 158 158 /** Determine if the Phind classifier has been assigned. 159 159 * @return true if it has, false otherwise 160 160 */ 161 public boolean isPhindClassifierAssigned () {162 for(int i = 0; i < getSize(); i++) {163 Classifier classifier = (Classifier) getElementAt(i);164 if(classifier.getName().equalsIgnoreCase(StaticStrings.PHIND_CLASSIFIER)) {165 166 167 168 169 170 } 171 161 public boolean isPhindClassifierAssigned () { 162 for(int i = 0; i < getSize (); i++) { 163 Classifier classifier = (Classifier) getElementAt (i); 164 if(classifier.getName ().equalsIgnoreCase (StaticStrings.PHIND_CLASSIFIER)) { 165 return true; 166 } 167 classifier = null; 168 } 169 return false; 170 } 171 172 172 /** Determine if a DateList classifier has been assigned 173 173 * @return true if it has, false otherwise 174 174 */ 175 public boolean isDateListClassifierAssigned () {176 for(int i = 0; i < getSize(); i++) {177 Classifier classifier = (Classifier) getElementAt(i);178 if(classifier.getName().equalsIgnoreCase(StaticStrings.DATELIST_CLASSIFIER)) {179 180 181 182 183 184 175 public boolean isDateListClassifierAssigned () { 176 for(int i = 0; i < getSize (); i++) { 177 Classifier classifier = (Classifier) getElementAt (i); 178 if(classifier.getName ().equalsIgnoreCase (StaticStrings.DATELIST_CLASSIFIER)) { 179 return true; 180 } 181 classifier = null; 182 } 183 return false; 184 185 185 } 186 186 /** Method to move a classifier in the list order. … … 189 189 * @param all true to move to move all the way, false for a single step. 190 190 */ 191 private void moveClassifier (Classifier classifier, boolean direction, boolean all) {192 if(getSize() < 2) {193 DebugStream.println("Not enough classifiers to allow moving.");194 195 196 197 198 199 200 remove(classifier);201 202 Classifier first_classifier = (Classifier) getElementAt(0);203 204 addBefore(classifier, first_classifier);205 206 207 208 209 remove(classifier);210 211 add(getSize(), classifier);212 213 214 215 216 int index = indexOf(classifier);217 218 219 220 221 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 226 227 remove(classifier);228 add(index, classifier);229 230 231 232 if(index >= getSize()) {233 234 args[0] = Dictionary.get("CDM.ClassifierManager.Classifier_Str");235 args[1] = classifier.getName();236 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.Move.At_Bottom", args), Dictionary.get("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE);237 238 239 remove(classifier);240 add(index, classifier);241 242 243 244 245 Gatherer.c_man.getCollection().cdm.format_manager.refresh();191 private void moveClassifier (Classifier classifier, boolean direction, boolean all) { 192 if(getSize () < 2) { 193 DebugStream.println ("Not enough classifiers to allow moving."); 194 return; 195 } 196 if(all) { 197 // Move to top 198 if(direction) { 199 // Remove the moving classifier 200 remove (classifier); 201 // Retrieve the first classifier 202 Classifier first_classifier = (Classifier) getElementAt (0); 203 // Add the moving classifier before the first classifier 204 addBefore (classifier, first_classifier); 205 first_classifier = null; 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 } 230 else { 231 index++; 232 if(index >= getSize ()) { 233 String args[] = new String[2]; 234 args[0] = Dictionary.get ("CDM.ClassifierManager.Classifier_Str"); 235 args[1] = classifier.getName (); 236 JOptionPane.showMessageDialog (Gatherer.g_man, Dictionary.get ("CDM.Move.At_Bottom", args), Dictionary.get ("CDM.Move.Title"), JOptionPane.ERROR_MESSAGE); 237 return; 238 } 239 remove (classifier); 240 add (index, classifier); 241 } 242 } 243 244 // tell the format manager to update the names of its format statements 245 Gatherer.c_man.getCollection ().cdm.format_manager.refresh (); 246 246 } 247 247 … … 250 250 * @see org.greenstone.gatherer.cdm.DynamicListModel 251 251 */ 252 private void removeClassifier(Classifier classifier) { 253 remove(classifier); 254 } 255 256 252 private void removeClassifier (Classifier classifier) { 253 remove (classifier); 254 // tell the format manager to update the names of its format statements 255 Gatherer.c_man.getCollection ().cdm.format_manager.refresh (); 256 257 } 258 // When remove, move, and add(assign) classifiers, this method has to be called to get the feature combobox 259 // on the 'format feature' panel of the 'format' panel up to date. 260 // private void informFormatManager() { 261 // //this is really to call the gainFocus() method 262 // CollectionDesignManager.format_manager.getControls(); 263 // } 264 257 265 /** A class which provides controls for assigned and editing classifiers. */ 258 266 private class ClassifierControl 259 extends JPanel 260 implements Control { 261 /** A combobox containing all of the known classifiers, including those that may have already been assigned. */ 262 private JComboBox classifier_combobox = null; 263 /** Button for adding classifiers. */ 264 private JButton add = null; 265 /** Button for configuring the selected classifier. */ 266 private JButton configure = null; 267 private JButton move_down_button; 268 private JButton move_up_button; 269 270 /** Button to remove the selected classifier. */ 271 private JButton remove = null; 272 273 /** A list of assigned classifiers. */ 274 private JList classifier_list = null; 275 276 /** Constructor. 277 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.AddListener 278 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.ConfigureListener 279 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.RemoveListener 280 */ 281 public ClassifierControl() 282 { 283 // Create 284 add = new GLIButton(Dictionary.get("CDM.ClassifierManager.Add"), Dictionary.get("CDM.ClassifierManager.Add_Tooltip")); 285 286 JPanel button_pane = new JPanel(); 287 JPanel central_pane = new JPanel(); 288 289 configure = new GLIButton(Dictionary.get("CDM.ClassifierManager.Configure"), Dictionary.get("CDM.ClassifierManager.Configure_Tooltip")); 290 configure.setEnabled(false); 291 292 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Classifiers", "classifiers"); 293 294 ClassifierComboboxListener ccl = new ClassifierComboboxListener(); 295 classifier_combobox = new JComboBox(getAvailableClassifiers()); 296 classifier_combobox.setOpaque(!Utility.isMac()); 297 classifier_combobox.setEditable(false); 298 if(classifier_combobox.getItemCount() > 0) { 299 classifier_combobox.setSelectedIndex(0); 300 ccl.itemStateChanged(new ItemEvent(classifier_combobox, 0, null, ItemEvent.SELECTED)); 301 } 302 303 JLabel classifier_label = new JLabel(Dictionary.get("CDM.ClassifierManager.Classifier")); 304 305 classifier_list = new JList(model); 306 classifier_list.setOpaque(true); 307 classifier_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 308 JLabel classifier_list_label = new JLabel(Dictionary.get("CDM.ClassifierManager.Assigned")); 309 310 classifier_list_label.setOpaque(true); 311 312 JPanel classifier_list_pane = new JPanel(); 313 JPanel classifier_pane = new JPanel(); 314 remove = new GLIButton(Dictionary.get("CDM.ClassifierManager.Remove"), Dictionary.get("CDM.ClassifierManager.Remove_Tooltip")); 315 remove.setEnabled(false); 316 317 JPanel temp = new JPanel(new BorderLayout()); 318 319 JPanel move_button_pane = new JPanel(); 320 321 move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip")); 322 move_up_button.setEnabled(false); 323 324 move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip")); 325 move_down_button.setEnabled(false); 326 327 // Listeners 328 add.addActionListener(new AddListener()); 329 classifier_combobox.addItemListener(ccl); 330 configure.addActionListener(new ConfigureListener()); 331 remove.addActionListener(new RemoveListener()); 332 remove.addActionListener(CollectionDesignManager.buildcol_change_listener); 333 classifier_list.addMouseListener(new ClickListener()); 334 classifier_list.addListSelectionListener(new ListListener()); 335 ccl = null; 336 337 MoveListener ml = new MoveListener(); 338 move_down_button.addActionListener(ml); 339 move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener); 340 move_up_button.addActionListener(ml); 341 move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener); 342 343 // Layout 344 move_button_pane.setLayout(new GridLayout(4,1)); 345 move_button_pane.add(move_up_button); 346 move_button_pane.add(new JPanel()); 347 move_button_pane.add(new JPanel()); 348 move_button_pane.add(move_down_button); 349 350 classifier_list_label.setBorder(BorderFactory.createEmptyBorder(0,2,0,2)); 351 352 classifier_list_pane.setLayout(new BorderLayout()); 353 classifier_list_pane.add(classifier_list_label, BorderLayout.NORTH); 354 classifier_list_pane.add(new JScrollPane(classifier_list), BorderLayout.CENTER); 355 classifier_list_pane.add(move_button_pane, BorderLayout.EAST); 356 357 classifier_label.setBorder(BorderFactory.createEmptyBorder(0,0,5,0)); 358 359 classifier_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0)); 360 classifier_pane.setLayout(new BorderLayout(5,0)); 361 classifier_pane.add(classifier_label, BorderLayout.WEST); 362 classifier_pane.add(classifier_combobox, BorderLayout.CENTER); 363 364 button_pane.setLayout(new GridLayout(1, 3)); 365 button_pane.add(add); 366 button_pane.add(configure); 367 button_pane.add(remove); 368 369 temp.add(classifier_pane, BorderLayout.NORTH); 370 temp.add(button_pane, BorderLayout.SOUTH); 371 372 central_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); 373 central_pane.setLayout(new BorderLayout()); 374 central_pane.add(classifier_list_pane, BorderLayout.CENTER); 375 central_pane.add(temp, BorderLayout.SOUTH); 376 377 setBorder(BorderFactory.createEmptyBorder(0,5,0,0)); 378 setLayout(new BorderLayout()); 379 add(header_pane, BorderLayout.NORTH); 380 add(central_pane, BorderLayout.CENTER); 381 } 382 383 /** Method which acts like a destructor, tidying up references to persistant objects. 384 */ 385 public void destroy() { 386 add = null; 387 classifier_combobox = null; 388 classifier_list = null; 389 configure = null; 390 //instructions = null; 391 remove = null; 392 } 393 394 public void gainFocus() { 395 } 396 397 public void loseFocus() { 398 } 399 400 401 private class AddListener 402 implements ActionListener 403 { 404 public void actionPerformed(ActionEvent event) 405 { 406 if (classifier_combobox.getSelectedItem() != null) { 407 // This must be done on a new thread for the remote building code 408 new AddClassifierTask(classifier_combobox.getSelectedItem().toString()).start(); 409 } 410 } 411 } 412 413 414 private class AddClassifierTask 415 extends Thread 416 { 417 private String classifier_name; 418 419 public AddClassifierTask(String classifier_name) 420 { 421 this.classifier_name = classifier_name; 422 } 423 424 public void run() 425 { 426 // Retrieve the classifier 427 Classifier classifier = Classifiers.getClassifier(classifier_name, true); 428 if (classifier == null) { 429 System.err.println("Error: getClassifier() returned null."); 430 return; 431 } 432 433 // Create a new element in the DOM 434 Element new_classifier_element = CollectionConfiguration.createElement(StaticStrings.CLASSIFY_ELEMENT); 435 new_classifier_element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, classifier.getName()); 436 Classifier new_classifier = new Classifier(new_classifier_element, classifier); 437 438 ArgumentConfiguration ac = new ArgumentConfiguration(new_classifier); 439 ac.addOKButtonActionListener(CollectionDesignManager.buildcol_change_listener); 440 if (ac.display()) { 441 assignClassifier(new_classifier); 442 classifier_list.setSelectedValue(new_classifier, true); 443 } 444 } 445 } 446 447 448 /** This listener reacts to changes in the current selection of the classifier combobox. */ 449 private class ClassifierComboboxListener 450 implements ItemListener { 451 /** When a user selects a certain classifier, update the tooltip to show the classifier description. */ 452 public void itemStateChanged(ItemEvent event) { 453 if(event.getStateChange() == ItemEvent.SELECTED) { 454 // Retrieve the selected classifier 455 Classifier current_selection = (Classifier) classifier_combobox.getSelectedItem(); 456 // And reset the tooltip. 457 classifier_combobox.setToolTipText(Utility.formatHTMLWidth(current_selection.getDescription(), 40)); 458 current_selection = null; 459 } 460 } 461 } 462 463 464 /** Listens for double clicks apon the list and react as if the configure button was pushed. */ 465 private class ClickListener 466 extends MouseAdapter { 467 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt. 468 * @param event A <strong>MouseEvent</strong> containing information about the mouse click. 469 */ 470 public void mouseClicked(MouseEvent event) { 471 if(event.getClickCount() == 2 ) { 472 if(!classifier_list.isSelectionEmpty()) { 473 Classifier classifier = (Classifier) classifier_list.getSelectedValue(); 474 ArgumentConfiguration ac = new ArgumentConfiguration(classifier); 475 ac.addOKButtonActionListener(CollectionDesignManager.buildcol_change_listener); 476 if (ac.display()) { 477 refresh(classifier); 478 } 479 ac.destroy(); 480 ac = null; 481 } 482 } 483 } 484 } 485 486 /** 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. 487 */ 488 private class ConfigureListener 489 implements ActionListener { 490 /** 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. 491 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 492 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration 493 * @see org.greenstone.gatherer.cdm.Classifier 494 */ 495 public void actionPerformed(ActionEvent event) { 496 if(!classifier_list.isSelectionEmpty()) { 497 Classifier classifier = (Classifier) classifier_list.getSelectedValue(); 498 ArgumentConfiguration ac = new ArgumentConfiguration(classifier); 499 ac.addOKButtonActionListener(CollectionDesignManager.buildcol_change_listener); 500 if (ac.display()) { 501 refresh(classifier); 502 } 503 ac.destroy(); 504 ac = null; 505 } 506 } 507 } 508 509 /** 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 */ 510 private class ListListener 511 implements ListSelectionListener { 512 513 public void valueChanged(ListSelectionEvent e) { 514 if (!e.getValueIsAdjusting()) { // we get two events for one change in list selection - use the false one ( the second one) 515 if (classifier_list.isSelectionEmpty()) { 516 move_up_button.setEnabled(false); 517 move_down_button.setEnabled(false); 518 configure.setEnabled(false); 519 remove.setEnabled(false); 520 } 521 else { 522 configure.setEnabled(true); 523 remove.setEnabled(true); 524 int selected_index = classifier_list.getSelectedIndex(); 525 move_up_button.setEnabled(selected_index !=0); 526 move_down_button.setEnabled(selected_index != model.getSize()-1); 527 } 528 } 529 } 530 } 531 532 /** 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. */ 533 private class MoveListener 534 implements ActionListener { 535 /** 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. 536 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 537 */ 538 public void actionPerformed(ActionEvent event) { 539 if(!classifier_list.isSelectionEmpty()) { 540 Object object = classifier_list.getSelectedValue(); 541 if(object instanceof Classifier) { 542 Classifier classifier = (Classifier) object; 543 if(event.getSource() == move_up_button) { 544 moveClassifier(classifier, true, false); 545 } 546 else if(event.getSource() == move_down_button) { 547 moveClassifier(classifier, false, false); 548 } 549 classifier_list.setSelectedValue(classifier, true); 550 } 551 } 552 } 553 } 554 555 /** This class listens for actions upon the remove button in the controls, and if detected calls the <i>removeClassifier()</i> method. 556 */ 557 private class RemoveListener 558 implements ActionListener { 559 /** 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. 560 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 561 */ 562 public void actionPerformed(ActionEvent event) { 563 if(classifier_list.isSelectionEmpty()) { 564 remove.setEnabled(false); 565 return; 566 } 567 int selected_index = classifier_list.getSelectedIndex(); 568 Object selected_classifier = classifier_list.getSelectedValue(); 569 if (!(selected_classifier instanceof Classifier)) { 570 return; // what else could we have here??? 571 } 572 removeClassifier((Classifier)selected_classifier); 573 574 if (selected_index >= classifier_list.getModel().getSize()) { 575 selected_index--; 576 } 577 if (selected_index >=0) { 578 classifier_list.setSelectedIndex(selected_index); 579 } else { 580 // no more classifiers in the list 581 remove.setEnabled(false); 582 } 583 } 584 } 267 extends JPanel 268 implements Control { 269 /** A combobox containing all of the known classifiers, including those that may have already been assigned. */ 270 private JComboBox classifier_combobox = null; 271 /** Button for adding classifiers. */ 272 private JButton add = null; 273 /** Button for configuring the selected classifier. */ 274 private JButton configure = null; 275 private JButton move_down_button; 276 private JButton move_up_button; 277 278 /** Button to remove the selected classifier. */ 279 private JButton remove = null; 280 281 /** A list of assigned classifiers. */ 282 private JList classifier_list = null; 283 284 /** Constructor. 285 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.AddListener 286 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.ConfigureListener 287 * @see org.greenstone.gatherer.cdm.ClassifierManager.ClassifierControl.RemoveListener 288 */ 289 public ClassifierControl () { 290 // Create 291 add = new GLIButton (Dictionary.get ("CDM.ClassifierManager.Add"), Dictionary.get ("CDM.ClassifierManager.Add_Tooltip")); 292 293 JPanel button_pane = new JPanel (); 294 JPanel central_pane = new JPanel (); 295 296 configure = new GLIButton (Dictionary.get ("CDM.ClassifierManager.Configure"), Dictionary.get ("CDM.ClassifierManager.Configure_Tooltip")); 297 configure.setEnabled (false); 298 299 JPanel header_pane = new DesignPaneHeader ("CDM.GUI.Classifiers", "classifiers"); 300 301 ClassifierComboboxListener ccl = new ClassifierComboboxListener (); 302 classifier_combobox = new JComboBox (getAvailableClassifiers ()); 303 classifier_combobox.setOpaque (!Utility.isMac ()); 304 classifier_combobox.setEditable (false); 305 if(classifier_combobox.getItemCount () > 0) { 306 classifier_combobox.setSelectedIndex (0); 307 ccl.itemStateChanged (new ItemEvent (classifier_combobox, 0, null, ItemEvent.SELECTED)); 308 } 309 310 JLabel classifier_label = new JLabel (Dictionary.get ("CDM.ClassifierManager.Classifier")); 311 312 classifier_list = new JList (model); 313 classifier_list.setOpaque (true); 314 classifier_list.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 315 JLabel classifier_list_label = new JLabel (Dictionary.get ("CDM.ClassifierManager.Assigned")); 316 317 classifier_list_label.setOpaque (true); 318 319 JPanel classifier_list_pane = new JPanel (); 320 JPanel classifier_pane = new JPanel (); 321 remove = new GLIButton (Dictionary.get ("CDM.ClassifierManager.Remove"), Dictionary.get ("CDM.ClassifierManager.Remove_Tooltip")); 322 remove.setEnabled (false); 323 324 JPanel temp = new JPanel (new BorderLayout ()); 325 326 JPanel move_button_pane = new JPanel (); 327 328 move_up_button = new GLIButton (Dictionary.get ("CDM.Move.Move_Up"), JarTools.getImage ("arrow-up.gif"), Dictionary.get ("CDM.Move.Move_Up_Tooltip")); 329 move_up_button.setEnabled (false); 330 331 move_down_button = new GLIButton (Dictionary.get ("CDM.Move.Move_Down"), JarTools.getImage ("arrow-down.gif"), Dictionary.get ("CDM.Move.Move_Down_Tooltip")); 332 move_down_button.setEnabled (false); 333 334 // Listeners 335 add.addActionListener (new AddListener ()); 336 classifier_combobox.addItemListener (ccl); 337 configure.addActionListener (new ConfigureListener ()); 338 remove.addActionListener (new RemoveListener ()); 339 remove.addActionListener (CollectionDesignManager.buildcol_change_listener); 340 classifier_list.addMouseListener (new ClickListener ()); 341 classifier_list.addListSelectionListener (new ListListener ()); 342 ccl = null; 343 344 MoveListener ml = new MoveListener (); 345 move_down_button.addActionListener (ml); 346 move_down_button.addActionListener (CollectionDesignManager.buildcol_change_listener); 347 move_up_button.addActionListener (ml); 348 move_up_button.addActionListener (CollectionDesignManager.buildcol_change_listener); 349 350 // Layout 351 move_button_pane.setLayout (new GridLayout (4,1)); 352 move_button_pane.add (move_up_button); 353 move_button_pane.add (new JPanel ()); 354 move_button_pane.add (new JPanel ()); 355 move_button_pane.add (move_down_button); 356 357 classifier_list_label.setBorder (BorderFactory.createEmptyBorder (0,2,0,2)); 358 359 classifier_list_pane.setLayout (new BorderLayout ()); 360 classifier_list_pane.add (classifier_list_label, BorderLayout.NORTH); 361 classifier_list_pane.add (new JScrollPane (classifier_list), BorderLayout.CENTER); 362 classifier_list_pane.add (move_button_pane, BorderLayout.EAST); 363 364 classifier_label.setBorder (BorderFactory.createEmptyBorder (0,0,5,0)); 365 366 classifier_pane.setBorder (BorderFactory.createEmptyBorder (5,0,5,0)); 367 classifier_pane.setLayout (new BorderLayout (5,0)); 368 classifier_pane.add (classifier_label, BorderLayout.WEST); 369 classifier_pane.add (classifier_combobox, BorderLayout.CENTER); 370 371 button_pane.setLayout (new GridLayout (1, 3)); 372 button_pane.add (add); 373 button_pane.add (configure); 374 button_pane.add (remove); 375 376 temp.add (classifier_pane, BorderLayout.NORTH); 377 temp.add (button_pane, BorderLayout.SOUTH); 378 379 central_pane.setBorder (BorderFactory.createEmptyBorder (5,0,0,0)); 380 central_pane.setLayout (new BorderLayout ()); 381 central_pane.add (classifier_list_pane, BorderLayout.CENTER); 382 central_pane.add (temp, BorderLayout.SOUTH); 383 384 setBorder (BorderFactory.createEmptyBorder (0,5,0,0)); 385 setLayout (new BorderLayout ()); 386 add (header_pane, BorderLayout.NORTH); 387 add (central_pane, BorderLayout.CENTER); 388 } 389 390 /** Method which acts like a destructor, tidying up references to persistant objects. 391 */ 392 public void destroy () { 393 add = null; 394 classifier_combobox = null; 395 classifier_list = null; 396 configure = null; 397 //instructions = null; 398 remove = null; 399 } 400 401 public void gainFocus () { 402 } 403 404 public void loseFocus () { 405 } 406 407 408 private class AddListener 409 implements ActionListener { 410 public void actionPerformed (ActionEvent event) { 411 if (classifier_combobox.getSelectedItem () != null) { 412 // This must be done on a new thread for the remote building code 413 new AddClassifierTask (classifier_combobox.getSelectedItem ().toString ()).start (); 414 } 415 } 416 } 417 418 419 private class AddClassifierTask 420 extends Thread { 421 private String classifier_name; 422 423 public AddClassifierTask (String classifier_name) { 424 this.classifier_name = classifier_name; 425 } 426 427 public void run () { 428 // Retrieve the classifier 429 Classifier classifier = Classifiers.getClassifier (classifier_name, true); 430 if (classifier == null) { 431 System.err.println ("Error: getClassifier() returned null."); 432 return; 433 } 434 435 // Create a new element in the DOM 436 Element new_classifier_element = CollectionConfiguration.createElement (StaticStrings.CLASSIFY_ELEMENT); 437 new_classifier_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, classifier.getName ()); 438 439 Classifier new_classifier = new Classifier (new_classifier_element, classifier); 440 441 ArgumentConfiguration ac = new ArgumentConfiguration (new_classifier); 442 ac.addOKButtonActionListener (CollectionDesignManager.buildcol_change_listener); 443 if (ac.display ()) { 444 assignClassifier (new_classifier); 445 classifier_list.setSelectedValue (new_classifier, true); 446 } 447 } 448 } 449 450 451 /** This listener reacts to changes in the current selection of the classifier combobox. */ 452 private class ClassifierComboboxListener 453 implements ItemListener { 454 /** When a user selects a certain classifier, update the tooltip to show the classifier description. */ 455 public void itemStateChanged (ItemEvent event) { 456 if(event.getStateChange () == ItemEvent.SELECTED) { 457 // Retrieve the selected classifier 458 Classifier current_selection = (Classifier) classifier_combobox.getSelectedItem (); 459 // And reset the tooltip. 460 classifier_combobox.setToolTipText (Utility.formatHTMLWidth (current_selection.getDescription (), 40)); 461 current_selection = null; 462 } 463 } 464 } 465 466 467 /** Listens for double clicks apon the list and react as if the configure button was pushed. */ 468 private class ClickListener 469 extends MouseAdapter { 470 /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt. 471 * @param event A <strong>MouseEvent</strong> containing information about the mouse click. 472 */ 473 public void mouseClicked (MouseEvent event) { 474 if(event.getClickCount () == 2 ) { 475 if(!classifier_list.isSelectionEmpty ()) { 476 Classifier classifier = (Classifier) classifier_list.getSelectedValue (); 477 ArgumentConfiguration ac = new ArgumentConfiguration (classifier); 478 ac.addOKButtonActionListener (CollectionDesignManager.buildcol_change_listener); 479 if (ac.display ()) { 480 refresh (classifier); 481 } 482 ac.destroy (); 483 ac = null; 484 } 485 } 486 } 487 } 488 489 /** 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. 490 */ 491 private class ConfigureListener 492 implements ActionListener { 493 /** 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. 494 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 495 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration 496 * @see org.greenstone.gatherer.cdm.Classifier 497 */ 498 public void actionPerformed (ActionEvent event) { 499 if(!classifier_list.isSelectionEmpty ()) { 500 Classifier classifier = (Classifier) classifier_list.getSelectedValue (); 501 ArgumentConfiguration ac = new ArgumentConfiguration (classifier); 502 ac.addOKButtonActionListener (CollectionDesignManager.buildcol_change_listener); 503 if (ac.display ()) { 504 refresh (classifier); 505 } 506 ac.destroy (); 507 ac = null; 508 } 509 } 510 } 511 512 /** 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 */ 513 private class ListListener 514 implements ListSelectionListener { 515 516 public void valueChanged (ListSelectionEvent e) { 517 if (!e.getValueIsAdjusting ()) { // we get two events for one change in list selection - use the false one ( the second one) 518 if (classifier_list.isSelectionEmpty ()) { 519 move_up_button.setEnabled (false); 520 move_down_button.setEnabled (false); 521 configure.setEnabled (false); 522 remove.setEnabled (false); 523 } 524 else { 525 configure.setEnabled (true); 526 remove.setEnabled (true); 527 int selected_index = classifier_list.getSelectedIndex (); 528 move_up_button.setEnabled (selected_index !=0); 529 move_down_button.setEnabled (selected_index != model.getSize ()-1); 530 } 531 } 532 } 533 } 534 535 /** 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. */ 536 private class MoveListener 537 implements ActionListener { 538 /** 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. 539 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 540 */ 541 public void actionPerformed (ActionEvent event) { 542 if(!classifier_list.isSelectionEmpty ()) { 543 Object object = classifier_list.getSelectedValue (); 544 if(object instanceof Classifier) { 545 Classifier classifier = (Classifier) object; 546 if(event.getSource () == move_up_button) { 547 moveClassifier (classifier, true, false); 548 } 549 else if(event.getSource () == move_down_button) { 550 moveClassifier (classifier, false, false); 551 } 552 classifier_list.setSelectedValue (classifier, true); 553 } 554 } 555 } 556 } 557 558 /** This class listens for actions upon the remove button in the controls, and if detected calls the <i>removeClassifier()</i> method. 559 */ 560 private class RemoveListener 561 implements ActionListener { 562 /** 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. 563 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 564 */ 565 public void actionPerformed (ActionEvent event) { 566 if(classifier_list.isSelectionEmpty ()) { 567 remove.setEnabled (false); 568 return; 569 } 570 int selected_index = classifier_list.getSelectedIndex (); 571 Object selected_classifier = classifier_list.getSelectedValue (); 572 if (!(selected_classifier instanceof Classifier)) { 573 return; // what else could we have here??? 574 } 575 removeClassifier ((Classifier)selected_classifier); 576 577 if (selected_index >= classifier_list.getModel ().getSize ()) { 578 selected_index--; 579 } 580 if (selected_index >=0) { 581 classifier_list.setSelectedIndex (selected_index); 582 } else { 583 // no more classifiers in the list 584 remove.setEnabled (false); 585 } 586 } 587 } 585 588 } 586 589 }
Note:
See TracChangeset
for help on using the changeset viewer.