- Timestamp:
- 2003-05-27T15:58:15+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/gui/OptionsPane.java
r4346 r4367 88 88 89 89 /** The default constructor creates the few session length options, but either retrieves the rest from the current collection, or creates a default set of options. */ 90 91 92 93 94 95 90 public OptionsPane(JTextArea log, BuildOptions build_options) { 91 this.log_display = log; 92 this.build_options = build_options; 93 this.log_change_listener = new LogChangeListener(); 94 //this.target_pane = target_pane; 95 } 96 96 97 97 /** creates a new log from the text in log. The new file is named … … 126 126 } 127 127 128 128 /** This method creates the panel with all the build only options on it. 129 129 * @return A <strong>JPanel</strong> which can be used to display all the build only options. 130 130 */ 131 132 133 134 135 136 137 138 131 public JPanel buildBuild() { 132 JPanel pane = new JPanel(); 133 pane.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); 134 pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 135 int build_argument_count = build_options.getBuildArgumentCount(); 136 pane.setLayout(new GridLayout(build_argument_count, 1, 0, 0)); 137 int total_height = PANE_SIZE.height; 138 for(int i = 0; i < build_argument_count; i++) { 139 139 // Retrieve the argument so we know how to format the control. 140 140 Argument argument = build_options.getBuildArgument(i); 141 141 // Now attempt to retrieve any existing value for this argument. 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 142 boolean enabled = build_options.getBuildValueEnabled(argument.getName()); 143 String value = build_options.getBuildValue(argument.getName()); 144 ArgumentControl argument_control = new ArgumentControl(BUILD, argument, enabled, value); 145 total_height = total_height - argument_control.getPreferredSize().height; 146 pane.add(argument_control); 147 } 148 if(total_height > 0) { 149 JPanel filler = new JPanel(); 150 filler.setPreferredSize(new Dimension(100, total_height)); 151 filler.setSize(filler.getPreferredSize()); 152 pane.add(filler); 153 } 154 return pane; 155 } 156 /** This method creates the panel with all the import only options on it. 157 157 * @return A <strong>JPanel</strong> which can be used to display all the import only options. 158 158 */ 159 160 161 162 163 164 165 166 159 public JPanel buildImport() { 160 JPanel pane = new JPanel(); 161 pane.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); 162 pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 163 int import_argument_count = build_options.getImportArgumentCount(); 164 pane.setLayout(new GridLayout(import_argument_count, 1, 0, 0)); 165 int total_height = PANE_SIZE.height; 166 for(int i = 0; i < import_argument_count; i++) { 167 167 // Retrieve the argument so we know how to format the control. 168 168 Argument argument = build_options.getImportArgument(i); 169 169 // Now attempt to retrieve any existing value for this argument. 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 170 boolean enabled = build_options.getImportValueEnabled(argument.getName()); 171 String value = build_options.getImportValue(argument.getName()); 172 ArgumentControl argument_control = new ArgumentControl(IMPORT, argument, enabled, value); 173 total_height = total_height - argument_control.getPreferredSize().height; 174 pane.add(argument_control); 175 } 176 if(total_height > 0) { 177 JPanel filler = new JPanel(); 178 filler.setPreferredSize(new Dimension(100, total_height)); 179 filler.setSize(filler.getPreferredSize()); 180 pane.add(filler); 181 } 182 return pane; 183 } 184 /** This method is used to build a panel based on the message log, which is nothing like any of the other panels. 185 185 * @return A <strong>JPanel</strong> containing a scrollable text area which represents the shell process message log. 186 186 */ 187 188 189 190 187 public JPanel buildLog() { 188 // we now save the log pane 189 if (log_pane == null) { 190 log_pane = new JPanel(new BorderLayout()); 191 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 192 // Build a list of the log files available, ordering by last modified. Log files are like build_log.date.txt 193 DefaultListModel contents = new DefaultListModel(); 194 File log_directory = new File(Gatherer.c_man.getCollectionLog()); 195 File children[] = log_directory.listFiles(); 196 for(int i = 0; children != null && i < children.length; i++) { 197 if(children[i].getName().startsWith("build_log")&&children[i].getName().endsWith(".txt") ) { 198 FileEntry entry = new FileEntry(children[i]); 199 // We are about to insert it. But where. 200 boolean found = false; 201 for(int j = 0; j < contents.size(); j++) { 202 FileEntry sibling = (FileEntry) contents.getElementAt(j); 203 int order = entry.compareTo(sibling); 204 if(order > 0) { 205 contents.insertElementAt(entry, j); 206 found = true; 207 break; 208 } 209 } 210 if(!found) { 211 contents.addElement(entry); 212 } 213 } 214 } 215 215 216 217 218 219 220 216 log_list = new JList(contents); 217 log_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 218 log_list.setLayoutOrientation(JList.VERTICAL); 219 log_list.setVisibleRowCount(3); 220 log_list.addListSelectionListener(new LogListListener()); 221 221 222 222 JScrollPane list_scroller = new JScrollPane(log_list); 223 223 224 225 226 227 228 229 224 // If log is not empty and we have something in displayed_log - make that element selected 225 if (log_display.getText().length()>0 && displayed_log != null) { 226 int index = contents.indexOf(displayed_log); 227 if (index != -1) { 228 log_list.setSelectedIndex(index); 229 } 230 230 231 232 233 234 235 236 237 238 239 240 241 242 243 231 } 232 log_pane.add(new JScrollPane(log_display), BorderLayout.CENTER); 233 JLabel log_history_label = new JLabel(get("LogHistory")); 234 JPanel log_history_pane = new JPanel(); 235 log_history_pane.setLayout(new BorderLayout()); 236 log_history_pane.add(log_history_label, BorderLayout.NORTH); 237 log_history_pane.add(list_scroller, BorderLayout.SOUTH); 238 log_pane.add(log_history_pane, BorderLayout.SOUTH); 239 } 240 return log_pane; 241 } 242 243 /** This method retrieves a phrase from the dictionary based apon the key. 244 244 * @param key A <strong>String</strong> used as a reference to the correct phrase. 245 245 * @return A phrase, matching the key, as a <strong>String</strong>. 246 246 */ 247 248 249 250 247 private String get(String key) { 248 return get(key, null); 249 } 250 /** This method retrieves a phrase from the dictionary based apon the key and arguments. 251 251 * @param key A <strong>String</strong> used as a reference to the correct phrase. 252 252 * @param args A <strong>String[]</strong> whose contents are often substituted into the phrase before it is returned. … … 255 255 * @see org.greenstone.gatherer.Gatherer 256 256 */ 257 258 259 260 261 262 263 257 private String get(String key, String args[]) { 258 if(key.indexOf(".") == -1) { 259 key = "OptionsPane." + key; 260 } 261 return Gatherer.dictionary.get(key, args); 262 } 263 /** Attempts to discover the latest document count. 264 264 * @return An <strong>int</strong> detailing the number of documents in this collection. 265 265 */ 266 267 268 269 270 271 272 273 274 266 public int getDocumentCount() { 267 if(Gatherer.c_man.ready()) { 268 int count = Gatherer.c_man.getCollection().getDocumentCount(); 269 if(count != 0) { 270 return count; 271 } 272 } 273 return 1; 274 } 275 275 276 276 /** Loads a new log into the log text - saves the currently showing one, … … 348 348 349 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 350 /** Given a panel containing ArgumentControls, update the values associated with them. */ 351 public void update(JPanel panel) { 352 if(panel == log_pane) { 353 return; 354 } 355 356 for(int i = 0; i < panel.getComponentCount(); i++) { 357 Component component = panel.getComponent(i); 358 if(component instanceof ArgumentControl) { 359 ((ArgumentControl)component).update(); 360 } 361 } 362 } 363 364 private class ArgumentControl 365 extends JPanel { 366 private Argument argument; 367 private int type; 368 private JComponent value_control; 369 private JCheckBox enabled; 370 public ArgumentControl(int type, Argument argument, boolean enable, String value) { 371 super(); 372 this.argument = argument; 373 this.type = type; 374 String tooltip = Utility.formatHTMLWidth("<html>" + argument.getDesc() + "</html>", 60); 375 375 // Because of the dynamic order of component creation/connection/layout, we can't really follow that pattern here. 376 377 378 379 376 setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); 377 setBorder(BorderFactory.createEmptyBorder(2,0,2,0)); 378 setLayout(new BorderLayout()); 379 setPreferredSize(ROW_SIZE); 380 380 381 381 /* 382 JPanel inner_pane = new JPanel();383 inner_pane.setOpaque(false);382 JPanel inner_pane = new JPanel(); 383 inner_pane.setOpaque(false); 384 384 */ 385 385 386 386 // Try to determine what the value_controls value should be. 387 388 389 387 if(value == null) { 388 value = argument.getDefaultValue(); 389 } 390 390 // Create a correct value control based on the argument provided. 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 391 switch(argument.getType()) { 392 case Argument.ENUM: 393 JComboBox combobox = new JComboBox(); 394 combobox.setEnabled(enable); 395 combobox.setToolTipText(tooltip); 396 // Set enabled 397 if(enable) { 398 combobox.setBackground(Color.white); 399 } 400 else { 401 combobox.setBackground(Color.lightGray); 402 } 403 // Build an option model, wrapping each entry of the list table. 404 HashMap arg_list = argument.getList(); 405 Iterator it = arg_list.keySet().iterator(); 406 while(it.hasNext()) { 407 combobox.addItem((String) it.next()); 408 } 409 // Connect this up first, so that if a value is selected the tooltip updates accordingly. 410 combobox.addActionListener(new ToolTipUpdater(arg_list)); 411 if(value != null) { 412 // Set the selected string. However since they are all strings we had best iterate ourselves. 413 for(int i = 0; i < combobox.getItemCount(); i++) { 414 if(combobox.getItemAt(i).toString().equals(value)) { 415 combobox.setSelectedIndex(i); 416 } 417 } 418 } 419 // Layout 420 add(combobox, BorderLayout.CENTER); 421 // And remember 422 value_control = combobox; 423 break; 424 case Argument.FLAG: 425 // Only need the check box. 426 value_control = null; 427 break; 428 case Argument.INTEGER: 429 // Build a spinner 430 JSpinner spinner = new JSpinner(); 431 spinner.setEnabled(enable); 432 spinner.setPreferredSize(SPINNER_SIZE); 433 spinner.setToolTipText(tooltip); 434 // Set enabled 435 JComponent c = spinner.getEditor(); 436 if ( c instanceof JSpinner.DefaultEditor ) { 437 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 438 JFormattedTextField field = editor.getTextField(); 439 field.setEditable(enable); 440 if(enable) { 441 field.setBackground(Color.white); 442 } 443 else { 444 field.setBackground(Color.lightGray); 445 } 446 } 447 // If there was an original value, set it. 448 if(value != null) { 449 try { 450 spinner.setValue(new Integer(value)); 451 } 452 catch (Exception error) { 453 } 454 } 455 // Layout 456 add(new JLabel(), BorderLayout.CENTER); 457 add(spinner, BorderLayout.EAST); 458 // And remember it 459 value_control = spinner; 460 break; 461 case Argument.STRING: 462 // Use a standard text field 463 JTextField textfield = new JTextField(); 464 textfield.setEnabled(enable); 465 textfield.setToolTipText(tooltip); 466 // Set enabled 467 if(enable) { 468 textfield.setBackground(Color.white); 469 } 470 else { 471 textfield.setBackground(Color.lightGray); 472 } 473 // If there was an original value, set it. 474 if(value != null) { 475 textfield.setText(value); 476 } 477 // Layout 478 add(textfield, BorderLayout.CENTER); 479 // And remember it 480 value_control = textfield; 481 break; 482 } 483 483 484 484 // If the argument is required, then you don't get a choice of whether it is enabled. 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 485 if(argument.isRequired()) { 486 JLabel label = new JLabel(argument.getName()); 487 label.setOpaque(false); 488 label.setPreferredSize(LABEL_SIZE); 489 label.setToolTipText(tooltip); 490 add(label, BorderLayout.WEST); 491 } 492 else { 493 enabled = new JCheckBox(argument.getName(), enable); 494 enabled.setOpaque(false); 495 enabled.setPreferredSize(LABEL_SIZE); 496 enabled.setToolTipText(tooltip); 497 // Connect 498 enabled.addActionListener(new EnabledListener(value_control)); 499 // Layout 500 add(enabled, BorderLayout.WEST); 501 } 502 } 503 504 /** Update the values stored in the collection so as to rememebr the current state of this argument. */ 505 public void update() { 506 String name = argument.getName(); 507 boolean enable = true; 508 if(enabled != null) { 509 enable = enabled.isSelected(); 510 } 511 String value = null; 512 if(value_control == null) { 513 // Flag value, nothing to do. 514 } 515 else if(value_control instanceof JTextField) { 516 value = ((JTextField)value_control).getText(); 517 } 518 else if(value_control instanceof JSpinner) { 519 value = ((JSpinner)value_control).getValue().toString(); 520 } 521 else if(value_control instanceof JComboBox) { 522 value = (String) ((JComboBox)value_control).getSelectedItem(); 523 } 524 524 // If this argument was a flag, but is now disabled, remove from the build options altogether 525 526 527 528 529 530 531 532 525 if(!enable && value == null) { 526 if(type == BUILD) { 527 build_options.removeBuildValue(name); 528 } 529 else { 530 build_options.removeImportValue(name); 531 } 532 } 533 533 // Otherwise update the argument value 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 534 else { 535 if(type == BUILD) { 536 build_options.setBuildValue(name, enable, value); 537 } 538 else { 539 build_options.setImportValue(name, enable, value); 540 } 541 } 542 } 543 } 544 545 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */ 546 private class EnabledListener 547 implements ActionListener { 548 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */ 549 private JComponent target = null; 550 /** Constructor. */ 551 public EnabledListener(JComponent target) { 552 this.target = target; 553 } 554 /** Any implementation of ActionListener must include this method so that we can be informed when an action has been performed on or registered check box, prompting us to change the state of the other controls as per the users request. 555 555 * @param event An <strong>ActionEvent</strong> containing information about the click. 556 556 */ 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 557 public void actionPerformed(ActionEvent event) { 558 JCheckBox source = (JCheckBox)event.getSource(); 559 if(target != null) { 560 if(source.isSelected()) { 561 target.setBackground(Color.white); 562 target.setEnabled(true); 563 } 564 else { 565 target.setBackground(Color.lightGray); 566 target.setEnabled(false); 567 } 568 // Special case of stupid JSpinners who don't let their backgrounds change properly. 569 if(target instanceof JSpinner) { 570 JSpinner spinner = (JSpinner) target; 571 JComponent c = spinner.getEditor(); 572 if ( c instanceof JSpinner.DefaultEditor ) { 573 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; 574 JFormattedTextField field = editor.getTextField(); 575 field.setEditable(source.isSelected()); 576 if(source.isSelected()) { 577 field.setBackground(Color.white); 578 } 579 else { 580 field.setBackground(Color.lightGray); 581 } 582 } 583 } 584 } 585 } 586 } 587 587 588 588 /** holds a File which has a particular naming convention … … 708 708 } 709 709 710 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */ 711 private class ToolTipUpdater 712 implements ActionListener { 713 private HashMap arg_list; 714 public ToolTipUpdater(HashMap arg_list) { 715 this.arg_list = arg_list; 716 } 717 /** Any implementation of an ActionListener must include this method so that we can be informed when the selection in a combobox has changed and update the tooltip accordingly. 718 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call. 719 */ 720 public void actionPerformed(ActionEvent event) { 721 JComboBox source = (JComboBox)event.getSource(); 722 String key = (String) source.getSelectedItem(); 723 if(arg_list != null) { 724 String description = (String) arg_list.get(key); 725 if(description != null) { 726 description = Utility.formatHTMLWidth(DESCRIPTION_SEP + description, 60); 727 String original = source.getToolTipText(); 728 if(original == null) { 729 original = ""; 730 } 731 // Remove any existing extra description. 732 if(original.indexOf(DESCRIPTION_SEP) != -1) { 733 original = original.substring(0, original.indexOf(DESCRIPTION_SEP)); 734 } 735 source.setToolTipText(original + description); 736 } 737 } 738 } 739 } 740 741 710 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */ 711 private class ToolTipUpdater 712 implements ActionListener { 713 private HashMap arg_list; 714 public ToolTipUpdater(HashMap arg_list) { 715 this.arg_list = arg_list; 716 } 717 /** Any implementation of an ActionListener must include this method so that we can be informed when the selection in a combobox has changed and update the tooltip accordingly. 718 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call. 719 */ 720 public void actionPerformed(ActionEvent event) { 721 JComboBox source = (JComboBox)event.getSource(); 722 String key = (String) source.getSelectedItem(); 723 if(arg_list != null) { 724 String description = (String) arg_list.get(key); 725 if(description != null) { 726 description = Utility.formatHTMLWidth(DESCRIPTION_SEP + description, 60); 727 String original = source.getToolTipText(); 728 if(original == null) { 729 original = ""; 730 } 731 // Remove any existing extra description. 732 if(original.indexOf(DESCRIPTION_SEP) != -1) { 733 original = original.substring(0, original.indexOf(DESCRIPTION_SEP)); 734 } 735 source.setToolTipText(original + description); 736 } 737 } 738 } 739 } 742 740 }
Note:
See TracChangeset
for help on using the changeset viewer.