KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBAutoGUI


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import javax.swing.border.LineBorder JavaDoc;
5 import javax.swing.border.TitledBorder JavaDoc;
6 import java.awt.*;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.io.*;
10 import java.util.Vector JavaDoc;
11
12 /**
13  * This class creates a GUI interface to a command line tool.
14  * It assumes that all the command line options are displayed as
15  * is, or followed immediately by strings, and are presented in
16  * the order they appear in a formatted file that describes them.<p>
17  * <p/>
18  * At the moment it will only wrap a single command line program.<p>
19  * <p/>
20  * The file format is: <br>
21  * <pre>
22  * command string
23  * primary: {option name} (becomes a menu item)
24  * secondary: {option name} [nostring|ispassword] (becomes a field)
25  * secondary: "your comment here" (becomes a helpfull label)
26  * secondary: ...
27  * ...
28  * primary:
29  * secondary:
30  * ...
31  * </pre>
32  * <p/>
33  * nb. this class tries to use an image: 'open.gif' to adorn the open file
34  * file chooser, and expects this to be in the base directory.<p>
35  * <p/>
36  * Other options for secondary values include tip="..." for tooltips, and
37  * default="..." for default values.
38  *
39  * @author Chris Betts
40  */

41
42
43 public class CBAutoGUI extends JDialog implements ActionListener JavaDoc
44 {
45     String JavaDoc commandName; // the root OS utility name to run.
46
Vector JavaDoc menuItems; // primary options are menu items.
47
Vector JavaDoc commonItems; // some secondary options may be common to all Primary options
48

49     final JEditorPane output = new JEditorPane(); // where the command output goes...
50
JTabbedPane tabPane; // the top level GUI component containing the option panes.
51
CBPanel commonOptions; // the top level GUI component containing shared options
52
PrimaryOption common; // a 'fake' primary option containing all shared secondary options
53

54     static String JavaDoc lastDirectory = ""; // the last directory used by the user, used in the file chooser.
55

56     boolean guiInitialised = false;
57
58     boolean debug = true;
59
60     static boolean standAlone = false;
61
62     /**
63      * An example of stand alone usage.
64      */

65
66     public static void main(String JavaDoc args[])
67     {
68         standAlone = true;
69
70         String JavaDoc fileName = "keytool.txt";
71         if (args.length >= 1)
72             fileName = args[0];
73
74         CBAutoGUI MrGui = new CBAutoGUI(null, fileName);
75         MrGui.show(null);
76     }
77
78     /**
79      * Constructs an AutoGUI showing a particular initial primary option.<p>
80      * - requires a root GUI component
81      * (may be null) and the fileName of a file containing a description
82      * of the command line function to be called, and the parameters it
83      * takes.
84      *
85      * @param owner the root GUI (required for correct look and feel propagation
86      * and repaint behaviour).
87      * @param fileName the name of the file containing the command line function description.
88      */

89
90     public CBAutoGUI(Frame owner, String JavaDoc fileName)
91     {
92         super(owner);
93
94         menuItems = new Vector JavaDoc(8);
95         commonItems = new Vector JavaDoc(8);
96         common = new PrimaryOption("common options");
97
98         processFile(fileName);
99
100         setSize(650, 550);
101     }
102
103     /**
104      * Brute force search through vector for object by name. yuck.
105      * rewrite sometime to be prettier. Kind of slow too - lots of string ops.
106      * Oh well. CPU is cheap.
107      */

108
109     static Object JavaDoc getNamedObject(Vector JavaDoc v, String JavaDoc s)
110     {
111         s = s.trim();
112         for (int i = 0; i < v.size(); i++)
113         {
114             if (v.get(i).toString().trim().equalsIgnoreCase(s))
115                 return v.get(i);
116         }
117         return null;
118     }
119
120
121     /**
122      * Sets a particular primary option to display.
123      *
124      * @param optionName the primaryOption (and tab title) to set the tabbed display to.
125      * a null or blank string sets the first option.
126      */

127
128     public void setOption(String JavaDoc optionName)
129     {
130         if (optionName == null || optionName.length() == 0)
131         {
132             if (tabPane.getTabCount() > 0)
133                 optionName = tabPane.getTitleAt(0).trim();
134             else
135             {
136                 error("no tabs set! - nothing to display");
137                 return;
138             }
139         }
140
141         optionName = optionName.trim();
142
143         if (optionName.length() == 0) return;
144         if (optionName.charAt(0) == '-')
145             optionName = optionName.substring(1);
146         if (optionName.length() == 0) return;
147
148         for (int i = 0; i < tabPane.getTabCount(); i++)
149         {
150             if (optionName.equalsIgnoreCase(tabPane.getTitleAt(i).trim()))
151             {
152                 tabPane.setSelectedIndex(i);
153                 return;
154             }
155         }
156     }
157
158     /**
159      * A bit of a hack - allows a program to set the default value
160      * of an option at runtime. Requires the primary and secondary
161      * option names to be passed in (as they are in the file) as well
162      * as the default value.
163      *
164      * @param primaryOptionName the primary option command string, as in text file.
165      * @param secondaryOptionName the secondary option command string, as in text file.
166      * @param defaultValue the new default value for the named secondary option.
167      */

168
169     public void setDefaultValue(String JavaDoc primaryOptionName, String JavaDoc secondaryOptionName, String JavaDoc defaultValue)
170     {
171         // XXX more of a hack - rig the names to be the same as the .toString() names of the Options...
172

173         primaryOptionName = formatOptionName(primaryOptionName);
174         secondaryOptionName = formatOptionName(secondaryOptionName);
175
176         PrimaryOption primary = (PrimaryOption) getNamedObject(menuItems, primaryOptionName);
177         if (primary == null)
178             primary = common;
179
180         SecondaryOption secondary = primary.get(secondaryOptionName);
181
182         if (secondary != null)
183             secondary.setDefaultValue(defaultValue);
184     }
185
186     /**
187      * Sets the tab panel to the selected primary option,
188      * and makes the component visible.
189      *
190      * @param option the command line primary option panel to make visible.
191      */

192
193     public void show(String JavaDoc option)
194     {
195         if (debug) System.out.println("showing GUI with option : " + ((option == null) ? " (no tab set) " : option));
196
197         if (guiInitialised == false)
198             initialiseGUI();
199         else
200             clearPasswords();
201
202         setOption(option);
203         setVisible(true);
204     }
205
206     /**
207      * Create two panels. In the top panel, place a tabbed set of panes
208      * corresponding to each primary option, and populate that with all
209      * the secondary options corresponding to the relevant primary option.
210      * In the bottom panel, place the secondary options common to all primary options.
211      */

212
213     public void initialiseGUI()
214     {
215         if (debug) System.out.println("initialising GUI");
216
217         setTitle("GUI interface to " + commandName);
218
219         CBPanel display = new CBPanel();
220
221         addTabbedPanes(display);
222
223         addCommonOptions(display);
224
225
226         JPanel buttons = new JPanel();
227         JButton OK, Cancel, Help;
228
229         display.makeWide();
230         display.add(new JLabel("")); // padding
231
display.makeLight();
232         display.add(OK = new JButton("Execute"));
233         display.add(Cancel = new JButton("Cancel"));
234         //display.add(Help = new JButton("Help"));
235
display.makeWide();
236         display.addLine(new JLabel("")); // padding
237
display.makeHeavy();
238
239         OK.addActionListener(this);
240         Cancel.addActionListener(this);
241         //Help.addActionListener(this);
242

243         //output = new JEditorPane();
244
output.setBorder(new TitledBorder JavaDoc(new LineBorder JavaDoc(Color.black, 2), "output"));
245         output.setPreferredSize(new Dimension(400, 150));
246
247         display.addLine(new JScrollPane(output));
248
249         getContentPane().add(display);
250         setVisible(true);
251         guiInitialised = true;
252     }
253
254     /**
255      * This runs through all the groups of 'primary' options,
256      * creating a new tabbed pane for each one, and populating
257      * it with the associated secondary options.
258      */

259
260     public void addTabbedPanes(CBPanel display)
261     {
262         if (menuItems.size() == 0) return; // don't add anything if there aren't any...
263

264         tabPane = new JTabbedPane();
265
266         for (int i = 0; i < menuItems.size(); i++)
267         {
268             PrimaryOption primary = (PrimaryOption) menuItems.get(i);
269             CBPanel panel = new CBPanel();
270             panel.makeLight();
271             tabPane.addTab(primary.toString(), panel);
272
273             boolean newLine = false;
274             for (int j = 0; j < primary.size(); j++)
275             {
276                 SecondaryOption secondary = primary.get(j);
277                 if (secondary.isLabel())
278                 {
279                     addSecondaryToPanel(panel, secondary, newLine);
280                     newLine = false;
281                 }
282                 else
283                 {
284                     addSecondaryToPanel(panel, secondary, newLine);
285                     newLine = !newLine;
286                 }
287             }
288             // Hack to get GridBagLayout to do what I want...
289

290             panel.newLine();
291             panel.add(new JLabel(" ")); // spacer below...
292
panel.makeHeavy();
293             panel.addWide(new JLabel(" "), 5); // spacer below...
294
}
295         display.addLine(tabPane);
296     }
297
298     /**
299      * Add common options to a separate, constant display area.
300      * Sometimes commands are common to all options, for example a password
301      * must be given before any action may be done. This method adds such
302      * common options to a separate display area that remains constant
303      * when the user moves between different tab panes.
304      */

305
306     public void addCommonOptions(CBPanel display)
307     {
308         if (commonItems.size() == 0) return; // if there are no common options, don't do anything.
309

310         commonOptions = new CBPanel();
311         for (int i = 0; i < commonItems.size(); i++)
312         {
313             SecondaryOption secondary = (SecondaryOption) commonItems.get(i);
314             addSecondaryToPanel(commonOptions, secondary, ((i % 2) != 0));
315         }
316         // Hack to get GridBagLayout to do what I want...
317

318         commonOptions.newLine();
319         commonOptions.add(new JLabel(" ")); // spacer below...
320
commonOptions.makeHeavy();
321         commonOptions.addWide(new JLabel(" "), 5); // spacer below...
322

323         display.addLine(commonOptions);
324     }
325
326     void clearPasswords()
327     {
328         for (int i = 0; i < tabPane.getTabCount(); i++)
329         {
330             Component c = tabPane.getComponent(i);
331             clearPasswords(c);
332         }
333         clearPasswords(commonOptions);
334     }
335
336     void clearPasswords(Component c)
337     {
338         if (c instanceof Container)
339         {
340             Container con = (Container) c;
341             for (int i = 0; i < con.getComponentCount(); i++)
342             {
343                 Component comp = con.getComponent(i);
344                 if (comp instanceof JPasswordField)
345                 {
346                     ((JPasswordField) comp).setText("");
347                 }
348             }
349         }
350     }
351
352     void addSecondaryToPanel(CBPanel panel, SecondaryOption secondary, boolean newLine)
353     {
354         if (secondary.isHidden()) return; // don't add hidden fields.
355

356         JComponent comp1 = null;
357         JComponent comp2 = null;
358         CBFileChooserButton fileChooser = null;
359
360         String JavaDoc defaultValue = secondary.getDefaultValue(); // most don't have defaults...
361

362         if (secondary.isLabel())
363         {
364             JLabel label = new JLabel(secondary.toString());
365             Font current = label.getFont();
366             label.setFont(new Font(current.getName(), Font.BOLD, current.getSize() + 2));
367             panel.addLine(label);
368 /* if (newLine)
369                 panel.addWide( label, 2);
370             else
371                 panel.addWide( label, 4);
372 */

373             newLine = true; // force new line...
374
return;
375         }
376         else if (secondary.isPassword())
377         {
378             panel.add(comp1 = new JLabel(secondary.toString()));
379             panel.addGreedyWide(comp2 = new JPasswordField(), 2);
380         }
381         else if (secondary.isCheckBox())
382         {
383             panel.add(comp1 = new JLabel(secondary.toString()));
384             panel.add(comp2 = new JCheckBox());
385             if ("".equals(defaultValue) == false)
386                 ((JCheckBox) comp2).setSelected(true);
387             panel.add(new JLabel(""));
388         }
389         else if (secondary.isFile())
390         {
391             panel.add(comp1 = new JLabel(secondary.toString()));
392             panel.addGreedyWide(comp2 = new JTextField(defaultValue, 30));
393             panel.add(fileChooser = new CBFileChooserButton((JTextField) comp2, this, "File"));
394             if (defaultValue.length() > 0)
395             {
396                 fileChooser.setLocalDirectoryUse(true);
397                 fileChooser.setStartingDirectory(defaultValue);
398             }
399         }
400         else
401         {
402             panel.add(comp1 = new JLabel(secondary.toString()));
403             panel.addGreedyWide(comp2 = new JTextField(defaultValue, 40), 2);
404         }
405
406         if (comp1 != null) comp1.setToolTipText(secondary.getTip());
407         //if (comp2 != null) comp2.setToolTipText(secondary.getTip());
408

409         if (newLine)
410             panel.newLine();
411     }
412
413
414     void processFile(String JavaDoc fileName)
415     {
416         if (debug) System.out.println("processing file '" + fileName + "'");
417
418         PrimaryOption currentItem = null;
419         try
420         {
421             BufferedReader readText = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
422
423             String JavaDoc line = ""; // the first line read from the ldif file
424
int count = 0;
425             while ((line = readText.readLine()) != null)
426             {
427                 line = line.trim();
428                 if (line.length() == 0 || line.charAt(0) == '#')
429                 {
430                     // do nothing - comment line.
431
}
432                 else if (commandName == null) // get command line utility name (first real line in file)
433
{
434                     if (debug) System.out.println("read commandName as '" + line + "'");
435                     commandName = line;
436                 }
437                 else if (line.toLowerCase().startsWith("primary: "))
438                 {
439                     line = line.substring(9).trim();
440                     if (debug) System.out.println("read primary option as '" + line + "'");
441                     menuItems.add((currentItem = new PrimaryOption(line)));
442                 }
443                 else if (line.toLowerCase().startsWith("common: "))
444                 {
445                     line = line.substring(8).trim();
446                     if (debug) System.out.println("read common option as '" + line + "'");
447                     SecondaryOption op = getSecondaryOption(line);
448                     common.add(op);
449                     commonItems.add(op);
450                 }
451                 else if (line.toLowerCase().startsWith("secondary: ") && currentItem != null)
452                 {
453                     line = line.substring(11).trim();
454                     if (debug) System.out.println("read secondary option as '" + line + "'");
455                     currentItem.add(getSecondaryOption(line));
456                 }
457                 else
458                     error("WARNING: ignoring line '" + line + "'");
459             }
460         }
461         catch (Exception JavaDoc e)
462         {
463             error("unable to open file:\n" + e.toString());
464             e.printStackTrace();
465         }
466     }
467
468     SecondaryOption getSecondaryOption(String JavaDoc line)
469     {
470         int pos = line.indexOf(' ');
471         if (pos < 0) pos = line.length();
472
473         String JavaDoc name = line.substring(0, pos);
474
475         boolean isPwd = false;
476         boolean hasArg = true;
477         boolean isLabel = false;
478         boolean isHidden = false;
479         boolean isFile = false;
480
481         String JavaDoc tooltip = null;
482         String JavaDoc defaultValue = null;
483
484         if (line.charAt(0) == '\"')
485         {
486             isLabel = true;
487             name = line.substring(1, line.length() - 1); // the entire line is one comment.
488
}
489         else if (pos != line.length())
490         {
491             String JavaDoc lowC = line.substring(pos).toLowerCase();
492             
493             // This is damn ugly, but I'm in a hurry...
494
if (lowC.indexOf("tip=") != -1) // get tool tip
495
{
496                 pos = lowC.indexOf("tip=");
497                 pos = line.indexOf("\"", pos);
498                 int pos2 = line.indexOf("\"", pos + 1);
499                 if (pos2 == -1) pos2 = line.length(); // no closing quote...
500
tooltip = line.substring(pos + 1, pos2);
501                 lowC = line.substring(0, pos).toLowerCase() + line.substring(pos2); // trim out comment so as not to confuse keyword search below.
502
}
503             if (lowC.indexOf("default=") != -1) // get default value
504
{
505                 pos = lowC.indexOf("default=");
506                 pos = line.indexOf("\"", pos);
507                 int pos2 = line.indexOf("\"", pos + 1);
508                 if (pos2 == -1) pos2 = line.length(); // no closing quote...
509
defaultValue = line.substring(pos + 1, pos2);
510                 lowC = line.substring(0, pos).toLowerCase() + line.substring(pos2); // trim out default values so as not to confuse keyword search below.
511
}
512             if (lowC.indexOf("nostring") != -1) hasArg = false;
513             if (lowC.indexOf("ispassword") != -1) isPwd = true;
514             if (lowC.indexOf("hidden") != -1) isHidden = true;
515             if (lowC.indexOf("file") != -1) isFile = true;
516         }
517
518         return (new SecondaryOption(name, isPwd, hasArg, isLabel, isHidden, isFile, tooltip, defaultValue));
519     }
520
521
522     /**
523      * Simple error printing routine - over-ride this for app specific functionality.
524      */

525
526     public void error(String JavaDoc msg)
527     {
528         System.err.println(msg);
529     }
530
531     public void actionPerformed(ActionEvent JavaDoc e)
532     {
533         String JavaDoc cmd = e.getActionCommand().trim();
534         if (cmd.equalsIgnoreCase("Execute"))
535         {
536             execute();
537         }
538         else if (cmd.equalsIgnoreCase("Cancel"))
539         {
540             cancel();
541         }
542         else if (cmd.equalsIgnoreCase("Help"))
543         {
544             help();
545         }
546         else
547             error("Unknown command in OpenConWin\n" + cmd);
548     }
549
550     public void execute()
551     {
552         execute(2000); // two second default timeout
553
}
554
555     public void execute(int millisecTimeout)
556     {
557         final int timeout = millisecTimeout;
558
559         CBPanel current = (CBPanel) tabPane.getSelectedComponent();
560         Vector JavaDoc args = new Vector JavaDoc();
561         String JavaDoc title = tabPane.getTitleAt(tabPane.getSelectedIndex()); // name of the primary option...
562

563         PrimaryOption command = (PrimaryOption) getNamedObject(menuItems, title);
564         args.add(command.toCommandString());
565
566         Component[] comps = current.getComponents();
567
568         setArguments(args, comps, command);
569
570         comps = commonOptions.getComponents();
571
572         setArguments(args, comps, common);
573
574         String JavaDoc commandString = commandName;
575
576         if (commandString.startsWith("%JAVA%"))
577         {
578             commandString = System.getProperty("java.home") + System.getProperty("file.separator") +
579                     "bin" + System.getProperty("file.separator") + commandString.substring(6);
580         }
581
582         final String JavaDoc finalName = commandString + " " + command.toCommandString();
583
584         for (int i = 0; i < args.size(); i++)
585             commandString += " " + args.get(i).toString();
586
587         final String JavaDoc finalCommand = commandString;
588
589         output.setText("running command:\n " + finalName);
590
591         final Thread JavaDoc worker = new Thread JavaDoc("Execute " + finalName) // run command in a separate worker thread
592
{
593             public void run()
594             {
595                 try
596                 {
597                     Runtime JavaDoc runMe = Runtime.getRuntime();
598                     Process JavaDoc goBoy = runMe.exec(finalCommand);
599                     //BufferedReader readText = new BufferedReader(new InputStreamReader(goBoy.getInputStream()));
600
output.read(goBoy.getInputStream(), "text");
601                     output.setText(output.getText() + "\n\nCommand " + finalName + " Executed.\n");
602                 }
603                 catch (IOException e)
604                 {
605                     output.setText(output.getText() + "\n\nIOException reading command Output\n" + e.toString());
606                 }
607             }
608         };
609         
610         // fire up another thread to time out the first, if necessary...
611

612         Thread JavaDoc waiter = new Thread JavaDoc("Execute Watcher for " + finalCommand)
613         {
614             public void run()
615             {
616                 try
617                 {
618                     (Thread.currentThread()).sleep(timeout);
619
620                     if (worker != null && worker.isAlive())
621                     {
622                         worker.interrupt();
623                         try
624                         {
625                             (Thread.currentThread()).sleep(10);
626                         } // pause 10 ms
627
catch (InterruptedException JavaDoc e)
628                         {
629                         }
630                     
631                         /* apparently unnecessary - if code above failed, stop() wouldn't work anyway?
632                         if (worker.isInterrupted()==false) // we tried to be nice but...
633                         {
634                             System.out.println("attempting to force stop...");
635                             worker.stop();
636                         }
637                         */

638                         output.setText(output.getText() + "\n\nError - unable to complete command " + finalName + "\n - request timed out in " + timeout + " milliseconds.\n");
639                     }
640                 }
641                 catch (InterruptedException JavaDoc intEx)
642                 {
643                 }
644             }
645         };
646
647         worker.start();
648         waiter.start();
649     }
650
651     void setArguments(Vector JavaDoc args, Component[] comps, PrimaryOption command)
652     {
653         Component oldC = comps[0];
654
655         for (int i = 1; i < comps.length; i++)
656         {
657             Component newC = comps[i];
658             if (oldC instanceof JLabel)
659             {
660                 String JavaDoc newArg = null;
661
662                 if (newC instanceof JPasswordField)
663                 {
664                     JPasswordField temp = (JPasswordField) newC;
665                     char[] pass = temp.getPassword();
666                     if (pass.length > 0)
667                     {
668                         String JavaDoc hack = new String JavaDoc(pass); // destroy all that carefull security 'cause we can't be bothered trying
669
if (hack.trim().length() > 0) // to do magic when we echo the command line args...
670
newArg = hack.trim();
671                     }
672                 }
673                 else if (newC instanceof JTextField)
674                 {
675                     JTextField temp = (JTextField) newC;
676                     if (temp.getText().trim().length() > 0)
677                         newArg = temp.getText().trim();
678
679                 }
680                 else if (newC instanceof JCheckBox)
681                 {
682                     JCheckBox temp = (JCheckBox) newC;
683                     if (temp.isSelected())
684                         newArg = ""; // el hack - set to blank, so that base arg gets included (but no argument text :-) )
685
}
686
687                 if (newArg != null)
688                 {
689                     String JavaDoc secondaryOptionName = ((JLabel) oldC).getText().trim();
690                     SecondaryOption opt = command.get(secondaryOptionName);
691                     if (opt == null)
692                         error("Error - unknown option " + secondaryOptionName);
693                     else
694                     {
695                         args.add(opt.toCommandString());
696                         args.add(newArg);
697                     }
698                 }
699             }
700             oldC = newC;
701         }
702
703         Vector JavaDoc hiddenOps = command.getHiddenOptions();
704         for (int i = 0; i < hiddenOps.size(); i++)
705         {
706             SecondaryOption opt = (SecondaryOption) hiddenOps.get(i);
707             args.add(opt.toCommandString());
708         }
709     }
710
711     public void cancel()
712     {
713         setVisible(false);
714         dispose();
715         if (standAlone) System.exit(0);
716     }
717
718     // your help code here (you'll need to add a 'Help' button called 'help'....)
719
public void help()
720     {
721
722     }
723
724     public static String JavaDoc formatOptionName(String JavaDoc name)
725     {
726         if (name == null || name.length() == 0)
727             return "";
728         else
729             return (" " + ((name.charAt(0) == '-') ? name.substring(1) : name));
730     }
731
732     /**
733      * A generic Abstract Option (simply a name);
734      */

735     abstract class Option
736     {
737         String JavaDoc name; // the name of the option, i.e. '-verbose'
738
String JavaDoc tip = ""; // optional tool tip
739

740         public Option(String JavaDoc optionString)
741         {
742             name = optionString;
743         }
744         
745 // public String toString() { return name; }
746

747         public String JavaDoc toCommandString()
748         {
749             return name;
750         }
751
752         public String JavaDoc getName()
753         {
754             return name;
755         }
756
757         public void setTip(String JavaDoc t)
758         {
759             if (t != null) tip = t;
760         }
761
762         public String JavaDoc getTip()
763         {
764             return tip;
765         }
766
767         public String JavaDoc toString()
768         {
769             return formatOptionName(name);
770         }
771     }
772     
773     /*
774     * A 'Primary Option' is the first argument to the command line.
775     * it never has an argument.
776     */

777     
778     class PrimaryOption extends Option
779     {
780         Vector JavaDoc options;
781
782         public PrimaryOption(String JavaDoc optionName)
783         {
784             super(optionName);
785             options = new Vector JavaDoc(8);
786         }
787
788         public void add(SecondaryOption option)
789         {
790             options.add(option);
791         }
792
793         public int size()
794         {
795             return options.size();
796         }
797
798         public SecondaryOption get(int i)
799         {
800             return (SecondaryOption) options.get(i);
801         }
802
803         /**
804          * looks for a named option. Tries original string, if that files tries again
805          * with a preceeding dash.
806          */

807         public SecondaryOption get(String JavaDoc s)
808         {
809             SecondaryOption op = (SecondaryOption) CBAutoGUI.getNamedObject(options, s);
810             if (op == null && s.charAt(0) != '-')
811                 op = (SecondaryOption) CBAutoGUI.getNamedObject(options, "-" + s);
812             return op;
813         }
814
815         public Vector JavaDoc getHiddenOptions()
816         {
817             Vector JavaDoc ret = new Vector JavaDoc();
818             for (int i = 0; i < options.size(); i++)
819             {
820                 SecondaryOption test = (SecondaryOption) options.get(i);
821                 if (test.isHidden())
822                     ret.add(test);
823             }
824             return ret;
825         }
826
827         public String JavaDoc toCommandString()
828         {
829             return name;
830         }
831
832     }
833     
834     /*
835     * A secondary option appears after the primary option, may
836     * have an argument, and if it does that argument (which is
837     * entered using a text field) may be a hidden password field.
838     */

839     
840     class SecondaryOption extends Option
841     {
842         boolean password;
843         boolean label;
844         boolean hasArgument;
845         boolean hidden;
846         boolean file;
847         String JavaDoc defaultValue = "";
848
849         /**
850          * Construct a new option that will appear on a tabbed pane.
851          *
852          * @param optionString the name of the option, as it appears on the command line, but without the dash
853          * @param pwd whether the option is a password field
854          * @param arg whether the option has an argument (if not, treat it as a check box)
855          * @param lbl whether the 'option' is in fact simply a label (a comment, or extra directions or something)
856          * @param hide whether the option is hidden from the user, and always present.
857          * @param whether the option represents a file, and hence requires a file picker menu.
858          * @param tt the tooltip (may be null) to show the user when the mouse passes over this component.
859          * @param an optional default value for the component (nb *any* value corresponds to 'on' for a checkbox...)
860          */

861         public SecondaryOption(String JavaDoc optionString, boolean pwd, boolean arg, boolean lbl, boolean hide, boolean isFile, String JavaDoc tooltip, String JavaDoc defVal)
862         {
863             super(optionString);
864             password = pwd;
865             hasArgument = arg;
866             label = lbl;
867             hidden = hide;
868             file = isFile;
869             if (tooltip != null)
870                 tip = tooltip;
871             if (defVal != null)
872                 defaultValue = defVal;
873         }
874
875         // handle default values...
876
public void setDefaultValue(String JavaDoc s)
877         {
878             if (defaultValue != null) defaultValue = s;
879         }
880
881         public String JavaDoc getDefaultValue()
882         {
883             return (defaultValue == null) ? "" : defaultValue;
884         }
885
886         public String JavaDoc toCommandString()
887         {
888             return name;
889         }
890
891         public boolean isPassword()
892         {
893             return password;
894         }
895
896         public boolean isLabel()
897         {
898             return label;
899         }
900
901         public boolean isCheckBox()
902         {
903             return !hasArgument;
904         }
905
906         public boolean isHidden()
907         {
908             return hidden;
909         }
910
911         public boolean isFile()
912         {
913             return file;
914         }
915
916     }
917 }
Popular Tags