source: trunk/gli/src/org/greenstone/gatherer/gui/OptionsPane.java@ 5873

Last change on this file since 5873 was 5873, checked in by jmt12, 20 years ago

Restored import and build panes to use grid layout - but without the two column problem of before

  • Property svn:keywords set to Author Date Id Revision
File size: 22.2 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.io.*;
42import java.util.*;
43import javax.swing.*;
44import javax.swing.event.*;
45import javax.swing.text.*;
46import org.greenstone.gatherer.Dictionary;
47import org.greenstone.gatherer.Gatherer;
48import org.greenstone.gatherer.cdm.Argument;
49import org.greenstone.gatherer.checklist.CheckList;
50import org.greenstone.gatherer.collection.BuildOptions;
51import org.greenstone.gatherer.collection.Collection;
52import org.greenstone.gatherer.collection.CollectionManager;
53import org.greenstone.gatherer.msm.ElementWrapper;
54import org.greenstone.gatherer.util.AppendLineOnlyFileDocument;
55import org.greenstone.gatherer.util.AppendLineOnlyFileDocumentOwner;
56import org.greenstone.gatherer.util.Utility;
57
58/** This class serves as the data holder for all subclasses of option panes, such as Import options or All options. It also contains methods for creating each of the option lines as they would appear in the subpane. Futhermore it has a method for considering all the arguments and generating a <strong>String[]</strong> to allow you to pass them to the <strong>GShell</strong>.
59 * @author John Thompson, Greenstone Digital Library, University of Waikato
60 * @version 2.2
61 */
62public class OptionsPane
63 extends JPanel
64 implements AppendLineOnlyFileDocumentOwner {
65
66 static final public char SUCCESSFUL = 's';
67 static final public char UNSUCCESSFUL = 'u';
68 static final public char CANCELLED = 'c';
69 static final public char UNKNOWN = 'x';
70
71 /** All process messages are written to this log text area. */
72 public JTextArea log_textarea = null;
73
74 /** The <strong>BuildOptions</strong> data object contains all the option settings we wish to persist between Gatherer sessions (and thus is stored in <strong>Collection</strong>). */
75 private BuildOptions build_options = null;
76
77 private FileEntry file_entry = null;
78 /** the log pane - we only create it once now, not each time */
79 private JPanel log_pane = null;
80 /** the list of previous log messages */
81 private JList log_list = null;
82 private Vector writing_documents;
83
84 static private int BUILD = 0;
85 static private int IMPORT = 1;
86 static private Dimension LABEL_SIZE = new Dimension(180, 25);
87 static private Dimension ROW_SIZE = new Dimension(610, 25);
88 static private Dimension SPINNER_SIZE = new Dimension(100, 25);
89 static private String DESCRIPTION_SEP = " + ";
90
91
92 /** 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. */
93 public OptionsPane(BuildOptions build_options) {
94 this.build_options = build_options;
95 this.writing_documents = new Vector();
96
97 // Have to do this here, not in display, as the message log view may not have been displayed yet.
98 log_textarea = new JTextArea();
99 log_textarea.setEditable(false);
100 }
101
102 /** This method creates the panel with all the build only options on it.
103 * @return A <strong>JPanel</strong> which can be used to display all the build only options.
104 */
105 public JPanel buildBuild() {
106 JPanel pane = new JPanel();
107 pane.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
108 pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
109 int build_argument_count = build_options.getBuildArgumentCount();
110 pane.setLayout(new GridLayout(build_argument_count, 1, 5, 5));
111 for(int i = 0; i < build_argument_count; i++) {
112 // Retrieve the argument so we know how to format the control.
113 Argument argument = build_options.getBuildArgument(i);
114 // Now attempt to retrieve any existing value for this argument.
115 boolean enabled = build_options.getBuildValueEnabled(argument.getName());
116 String value = build_options.getBuildValue(argument.getName());
117 ArgumentControl argument_control = new ArgumentControl(BUILD, argument, enabled, value);
118 pane.add(argument_control);
119 }
120 return pane;
121 }
122 /** This method creates the panel with all the import only options on it.
123 * @return A <strong>JPanel</strong> which can be used to display all the import only options.
124 */
125 public JPanel buildImport() {
126 JPanel pane = new JPanel();
127 pane.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
128 pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
129 int import_argument_count = build_options.getImportArgumentCount();
130 pane.setLayout(new GridLayout(import_argument_count, 1, 5, 5));
131 for(int i = 0; i < import_argument_count; i++) {
132 // Retrieve the argument so we know how to format the control.
133 Argument argument = build_options.getImportArgument(i);
134 // Now attempt to retrieve any existing value for this argument.
135 boolean enabled = build_options.getImportValueEnabled(argument.getName());
136 String value = build_options.getImportValue(argument.getName());
137 ArgumentControl argument_control = new ArgumentControl(IMPORT, argument, enabled, value);
138 pane.add(argument_control);
139 }
140 return pane;
141 }
142 /** This method is used to build a panel based on the message log, which is nothing like any of the other panels.
143 * @return A <strong>JPanel</strong> containing a scrollable text area which represents the shell process message log.
144 */
145 public JPanel buildLog() {
146 // we now save the log pane
147 if (log_pane == null) {
148 log_pane = new JPanel(new BorderLayout());
149
150 // Build a list of the log files available, ordering by last modified. Log files are like build_log.date.txt
151 DefaultListModel contents = new DefaultListModel();
152 File log_directory = new File(Gatherer.c_man.getCollectionLog());
153 File children[] = log_directory.listFiles();
154 for(int i = 0; children != null && i < children.length; i++) {
155 if(children[i].getName().startsWith("build_log") && children[i].getName().endsWith(".txt") ) {
156 FileEntry entry = new FileEntry(children[i].getName(), children[i].getAbsolutePath());
157 // We are about to insert it. But where.
158 boolean found = false;
159 for(int j = 0; !found && j < contents.size(); j++) {
160 FileEntry sibling = (FileEntry) contents.getElementAt(j);
161 int order = entry.compareTo(sibling);
162 if(order > 0) {
163 contents.insertElementAt(entry, j);
164 found = true;
165 }
166 }
167 if(!found) {
168 contents.addElement(entry);
169 }
170 }
171 }
172
173 log_list = new JList(contents);
174 log_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
175 log_list.setLayoutOrientation(JList.VERTICAL);
176 log_list.setPreferredSize(new Dimension(600, 100));
177 log_list.setVisibleRowCount(3);
178 log_list.addListSelectionListener(new LogListListener());
179
180 JLabel log_history_label = new JLabel();
181 Dictionary.registerText(log_history_label, "OptionsPane.LogHistory");
182 JPanel log_history_pane = new JPanel();
183 log_history_pane.setPreferredSize(new Dimension(600, 100));
184 log_history_pane.setLayout(new BorderLayout());
185 log_history_pane.add(log_history_label, BorderLayout.NORTH);
186 log_history_pane.add(new JScrollPane(log_list), BorderLayout.CENTER);
187
188 log_pane.add(new JScrollPane(log_textarea), BorderLayout.CENTER);
189 log_pane.add(log_history_pane, BorderLayout.SOUTH);
190 }
191 return log_pane;
192 }
193
194 public AppendLineOnlyFileDocument createNewLogDocument() {
195 long time = System.currentTimeMillis();
196 StringBuffer name = new StringBuffer();
197 name.append("build_log.");
198 name.append(time);
199 name.append(".txt");
200 // just in case there is no log directory
201 File file = new File(Gatherer.c_man.getCollectionLog() + name.toString());
202 File parent_file = file.getParentFile();
203 parent_file.mkdirs();
204 parent_file = null;
205 // create the file entry and add it to the list at pos 0 - it will always be the newest one created
206 file_entry = new FileEntry(name.toString(), file.getAbsolutePath());
207 ((DefaultListModel)log_list.getModel()).add(0, file_entry);
208 log_list.setSelectedIndex(0);
209 // Finally retrieve and return the document associated with this file entry
210 return file_entry.getDocument();
211 }
212
213
214 /** Attempts to discover the latest document count.
215 * @return An <strong>int</strong> detailing the number of documents in this collection.
216 */
217 public int getDocumentCount() {
218 if(Gatherer.c_man.ready()) {
219 int count = Gatherer.c_man.getCollection().getDocumentCount();
220 if(count != 0) {
221 return count;
222 }
223 }
224 return 1;
225 }
226
227 /** Called by our magic log documents after they have finished writing themselves to file, whereapon it is no longer necessary to hold a reference to them. */
228 public void remove(AppendLineOnlyFileDocument document) {
229 writing_documents.remove(document);
230 }
231
232 public void resetFileEntry() {
233 if(file_entry != null) {
234 file_entry.reset();
235 }
236 }
237
238 /** Given a panel containing ArgumentControls, update the values associated with them. */
239 public void update(JPanel panel) {
240 if(panel == log_pane) {
241 return;
242 }
243
244 for(int i = 0; i < panel.getComponentCount(); i++) {
245 Component component = panel.getComponent(i);
246 if(component instanceof ArgumentControl) {
247 ((ArgumentControl)component).update();
248 }
249 }
250 }
251
252 private class ArgumentControl
253 extends JPanel {
254 private Argument argument;
255 private int type;
256 private JComponent value_control;
257 private JCheckBox enabled;
258 public ArgumentControl(int type, Argument argument, boolean enable, String value) {
259 super();
260 this.argument = argument;
261 this.type = type;
262 String tooltip = Utility.formatHTMLWidth("<html>" + argument.getDescription() + "</html>", 60);
263 // Because of the dynamic order of component creation/connection/layout, we can't really follow that pattern here.
264 setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
265 setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
266 setLayout(new BorderLayout());
267 setPreferredSize(ROW_SIZE);
268
269 // Try to determine what the value_controls value should be.
270 if(value == null) {
271 value = argument.getDefaultValue();
272 }
273 // Create a correct value control based on the argument provided.
274 switch(argument.getType()) {
275 case Argument.ENUM:
276 JComboBox combobox = new JComboBox();
277 combobox.setEnabled(enable);
278 combobox.setToolTipText(tooltip);
279 // Set enabled
280 if(enable) {
281 combobox.setBackground(Color.white);
282 }
283 else {
284 combobox.setBackground(Color.lightGray);
285 }
286 // Build an option model, wrapping each entry of the list table.
287 HashMap arg_list = argument.getOptions();
288 Iterator it = arg_list.keySet().iterator();
289 while(it.hasNext()) {
290 combobox.addItem((String) it.next());
291 }
292 // Connect this up first, so that if a value is selected the tooltip updates accordingly.
293 combobox.addActionListener(new ToolTipUpdater(arg_list));
294 if(value != null) {
295 // Set the selected string. However since they are all strings we had best iterate ourselves.
296 for(int i = 0; i < combobox.getItemCount(); i++) {
297 if(combobox.getItemAt(i).toString().equals(value)) {
298 combobox.setSelectedIndex(i);
299 }
300 }
301 }
302 // Layout
303 add(combobox, BorderLayout.CENTER);
304 // And remember
305 value_control = combobox;
306 break;
307 case Argument.FLAG:
308 // Only need the check box.
309 value_control = null;
310 break;
311 case Argument.INTEGER:
312 // Build a spinner
313 JSpinner spinner = new JSpinner();
314 spinner.setEnabled(enable);
315 spinner.setPreferredSize(SPINNER_SIZE);
316 spinner.setToolTipText(tooltip);
317 // Set enabled
318 JComponent c = spinner.getEditor();
319 if ( c instanceof JSpinner.DefaultEditor ) {
320 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
321 JFormattedTextField field = editor.getTextField();
322 field.setEditable(enable);
323 if(enable) {
324 field.setBackground(Color.white);
325 }
326 else {
327 field.setBackground(Color.lightGray);
328 }
329 }
330 // If there was an original value, set it.
331 if(value != null) {
332 try {
333 spinner.setValue(new Integer(value));
334 }
335 catch (Exception error) {
336 }
337 }
338 // Layout
339 add(new JLabel(), BorderLayout.CENTER);
340 add(spinner, BorderLayout.EAST);
341 // And remember it
342 value_control = spinner;
343 break;
344 case Argument.STRING:
345 // Use a standard text field
346 JTextField textfield = new JTextField();
347 textfield.setEnabled(enable);
348 textfield.setToolTipText(tooltip);
349 // Set enabled
350 if(enable) {
351 textfield.setBackground(Color.white);
352 }
353 else {
354 textfield.setBackground(Color.lightGray);
355 }
356 // If there was an original value, set it.
357 if(value != null) {
358 textfield.setText(value);
359 }
360 // Layout
361 add(textfield, BorderLayout.CENTER);
362 // And remember it
363 value_control = textfield;
364 break;
365 }
366
367 // If the argument is required, then you don't get a choice of whether it is enabled.
368 if(argument.isRequired()) {
369 JLabel label = new JLabel(argument.getName());
370 label.setOpaque(false);
371 label.setPreferredSize(LABEL_SIZE);
372 label.setToolTipText(tooltip);
373 add(label, BorderLayout.WEST);
374 }
375 else {
376 enabled = new JCheckBox(argument.getName(), enable);
377 enabled.setOpaque(false);
378 enabled.setPreferredSize(LABEL_SIZE);
379 enabled.setToolTipText(tooltip);
380 // Connect
381 enabled.addActionListener(new EnabledListener(value_control));
382 // Layout
383 add(enabled, BorderLayout.WEST);
384 }
385 }
386
387 /** Update the values stored in the collection so as to rememebr the current state of this argument. */
388 public void update() {
389 String name = argument.getName();
390 boolean enable = true;
391 if(enabled != null) {
392 enable = enabled.isSelected();
393 }
394 String value = null;
395 if(value_control == null) {
396 // Flag value, nothing to do.
397 }
398 else if(value_control instanceof JTextField) {
399 value = ((JTextField)value_control).getText();
400 }
401 else if(value_control instanceof JSpinner) {
402 value = ((JSpinner)value_control).getValue().toString();
403 }
404 else if(value_control instanceof JComboBox) {
405 value = (String) ((JComboBox)value_control).getSelectedItem();
406 }
407 // If this argument was a flag, but is now disabled, remove from the build options altogether
408 if(!enable && value == null) {
409 if(type == BUILD) {
410 build_options.removeBuildValue(name);
411 }
412 else {
413 build_options.removeImportValue(name);
414 }
415 }
416 // Otherwise update the argument value
417 else {
418 if(type == BUILD) {
419 build_options.setBuildValue(name, enable, value);
420 }
421 else {
422 build_options.setImportValue(name, enable, value);
423 }
424 }
425 }
426 }
427
428 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
429 private class EnabledListener
430 implements ActionListener {
431 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
432 private JComponent target = null;
433 /** Constructor. */
434 public EnabledListener(JComponent target) {
435 this.target = target;
436 }
437 /** 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.
438 * @param event An <strong>ActionEvent</strong> containing information about the click.
439 */
440 public void actionPerformed(ActionEvent event) {
441 JCheckBox source = (JCheckBox)event.getSource();
442 if(target != null) {
443 if(source.isSelected()) {
444 target.setBackground(Color.white);
445 target.setEnabled(true);
446 }
447 else {
448 target.setBackground(Color.lightGray);
449 target.setEnabled(false);
450 }
451 // Special case of stupid JSpinners who don't let their backgrounds change properly.
452 if(target instanceof JSpinner) {
453 JSpinner spinner = (JSpinner) target;
454 JComponent c = spinner.getEditor();
455 if ( c instanceof JSpinner.DefaultEditor ) {
456 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
457 JFormattedTextField field = editor.getTextField();
458 field.setEditable(source.isSelected());
459 if(source.isSelected()) {
460 field.setBackground(Color.white);
461 }
462 else {
463 field.setBackground(Color.lightGray);
464 }
465 }
466 }
467 }
468 }
469 }
470
471 /** Holds a File which has a particular naming convention build_log.date.txt also keeps a Date corresponding to the date in its name*/
472 private class FileEntry {
473
474 private AppendLineOnlyFileDocument current_document;
475 private Date date;
476 private long last_modified;
477 private String display;
478 private String filename;
479 private String filepath;
480
481 public FileEntry(String filename, String filepath) {
482 this.date = null;
483 this.display = null;
484 this.filename = filename;
485 this.filepath = filepath;
486 this.last_modified = 0L;
487 }
488
489 /** returns 0 if the dates are the same, -ve number if the current FileEntry is earlier than the fe FileEntry ...*/
490 public int compareTo(FileEntry file_entry) {
491 Date our_date = getDate();
492 Date other_date = file_entry.getDate();
493 return our_date.compareTo(other_date);
494 }
495
496 public Date getDate() {
497 if(date == null) {
498 // Need to exclude first '.'
499 int first_index = filename.indexOf(".") + 1;
500 // Need to exclude the last '.'
501 int last_index = filename.lastIndexOf(".");
502 if(first_index > 0 && last_index > 0 && first_index < last_index) {
503 String date_string = filename.substring(first_index, last_index);
504 date = new Date(Long.parseLong(date_string));
505 }
506 else {
507 date = new Date(); // Current date
508 }
509 }
510 return date;
511 }
512
513 public AppendLineOnlyFileDocument getDocument() {
514 if(current_document == null) {
515 current_document = new AppendLineOnlyFileDocument(filepath);
516 }
517 return current_document;
518 }
519
520 public void reset() {
521 display = null;
522 }
523
524 /** we only want the date out of the file name, not the whole path */
525 public String toString() {
526 File file = new File(filename);
527 if(display == null) {
528 last_modified = file.lastModified();
529 StringBuffer d = new StringBuffer();
530 Date date = getDate();
531 d.append(date.toString());
532 char success = UNKNOWN;
533 File the_file = new File(filepath);
534 if(the_file.exists()) {
535 try {
536 FileInputStream in = new FileInputStream(the_file);
537 success = (char) in.read();
538 in.close();
539 in = null;
540 }
541 catch(Exception error) {
542 ///ystem.err.println("Log '" + filepath + "' not found!");
543 ///atherer.printStackTrace(error);
544 }
545 }
546 the_file = null;
547 switch (success) {
548 case SUCCESSFUL:
549 d.append(Dictionary.get("OptionsPane.Successful"));
550 break;
551 case UNSUCCESSFUL:
552 d.append(Dictionary.get("OptionsPane.Unsuccessful"));
553 break;
554 case CANCELLED:
555 d.append(Dictionary.get("OptionsPane.Cancelled"));
556 break;
557 default:
558 d.append(Dictionary.get("OptionsPane.Unknown"));
559 }
560 display = d.toString();
561 }
562 return display;
563 }
564 }
565
566 /** a ListSelectionListener that triggers the load of a newly selected log */
567 private class LogListListener implements ListSelectionListener {
568
569 public void valueChanged(ListSelectionEvent e) {
570 if (!e.getValueIsAdjusting()) { // we get two events for one change in list selection - use the false one ( the second one)
571 JList source = (JList)e.getSource();
572 file_entry = (FileEntry) source.getSelectedValue();
573 // First we determine if the old log has been completely written to file
574 Document document = log_textarea.getDocument();
575 if(document instanceof AppendLineOnlyFileDocument) {
576 AppendLineOnlyFileDocument append_line_only_file_document = (AppendLineOnlyFileDocument) document;
577 if(append_line_only_file_document.isStillWriting()) {
578 writing_documents.add(append_line_only_file_document); // We have to maintain a reference until they are all done.
579 append_line_only_file_document.setOwner(OptionsPane.this);
580 append_line_only_file_document.setExit();
581 }
582 }
583 // Load the new log
584 log_textarea.setDocument(file_entry.getDocument());
585 }
586 }
587 }
588
589 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
590 private class ToolTipUpdater
591 implements ActionListener {
592 private HashMap arg_list;
593 public ToolTipUpdater(HashMap arg_list) {
594 this.arg_list = arg_list;
595 }
596 /** 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.
597 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
598 */
599 public void actionPerformed(ActionEvent event) {
600 JComboBox source = (JComboBox)event.getSource();
601 String key = (String) source.getSelectedItem();
602 if(arg_list != null) {
603 String description = (String) arg_list.get(key);
604 if(description != null) {
605 description = Utility.formatHTMLWidth(DESCRIPTION_SEP + description, 60);
606 String original = source.getToolTipText();
607 if(original == null) {
608 original = "";
609 }
610 // Remove any existing extra description.
611 if(original.indexOf(DESCRIPTION_SEP) != -1) {
612 original = original.substring(0, original.indexOf(DESCRIPTION_SEP));
613 }
614 source.setToolTipText(original + description);
615 }
616 }
617 }
618 }
619}
Note: See TracBrowser for help on using the repository browser.