📄 cbautogui.java
字号:
{
public void run()
{
try
{
(Thread.currentThread()).sleep(timeout);
if (worker != null && worker.isAlive())
{
worker.interrupt();
try
{
(Thread.currentThread()).sleep(10);
} // pause 10 ms
catch (InterruptedException e)
{
}
/* apparently unnecessary - if code above failed, stop() wouldn't work anyway?
if (worker.isInterrupted()==false) // we tried to be nice but...
{
System.out.println("attempting to force stop...");
worker.stop();
}
*/
output.setText(output.getText() + "\n\nError - unable to complete command " + finalName + "\n - request timed out in " + timeout + " milliseconds.\n");
}
}
catch (InterruptedException intEx)
{
}
}
};
worker.start();
waiter.start();
}
void setArguments(Vector args, Component[] comps, PrimaryOption command)
{
Component oldC = comps[0];
for (int i = 1; i < comps.length; i++)
{
Component newC = comps[i];
if (oldC instanceof JLabel)
{
String newArg = null;
if (newC instanceof JPasswordField)
{
JPasswordField temp = (JPasswordField) newC;
char[] pass = temp.getPassword();
if (pass.length > 0)
{
String hack = new String(pass); // destroy all that carefull security 'cause we can't be bothered trying
if (hack.trim().length() > 0) // to do magic when we echo the command line args...
newArg = hack.trim();
}
}
else if (newC instanceof JTextField)
{
JTextField temp = (JTextField) newC;
if (temp.getText().trim().length() > 0)
newArg = temp.getText().trim();
}
else if (newC instanceof JCheckBox)
{
JCheckBox temp = (JCheckBox) newC;
if (temp.isSelected())
newArg = ""; // el hack - set to blank, so that base arg gets included (but no argument text :-) )
}
if (newArg != null)
{
String secondaryOptionName = ((JLabel) oldC).getText().trim();
SecondaryOption opt = command.get(secondaryOptionName);
if (opt == null)
error("Error - unknown option " + secondaryOptionName);
else
{
args.add(opt.toCommandString());
args.add(newArg);
}
}
}
oldC = newC;
}
Vector hiddenOps = command.getHiddenOptions();
for (int i = 0; i < hiddenOps.size(); i++)
{
SecondaryOption opt = (SecondaryOption) hiddenOps.get(i);
args.add(opt.toCommandString());
}
}
public void cancel()
{
setVisible(false);
dispose();
if (standAlone) System.exit(0);
}
// your help code here (you'll need to add a 'Help' button called 'help'....)
public void help()
{
}
public static String formatOptionName(String name)
{
if (name == null || name.length() == 0)
return "";
else
return (" " + ((name.charAt(0) == '-') ? name.substring(1) : name));
}
/**
* A generic Abstract Option (simply a name);
*/
abstract class Option
{
String name; // the name of the option, i.e. '-verbose'
String tip = ""; // optional tool tip
public Option(String optionString)
{
name = optionString;
}
// public String toString() { return name; }
public String toCommandString()
{
return name;
}
public String getName()
{
return name;
}
public void setTip(String t)
{
if (t != null) tip = t;
}
public String getTip()
{
return tip;
}
public String toString()
{
return formatOptionName(name);
}
}
/*
* A 'Primary Option' is the first argument to the command line.
* it never has an argument.
*/
class PrimaryOption extends Option
{
Vector options;
public PrimaryOption(String optionName)
{
super(optionName);
options = new Vector(8);
}
public void add(SecondaryOption option)
{
options.add(option);
}
public int size()
{
return options.size();
}
public SecondaryOption get(int i)
{
return (SecondaryOption) options.get(i);
}
/**
* looks for a named option. Tries original string, if that files tries again
* with a preceeding dash.
*/
public SecondaryOption get(String s)
{
SecondaryOption op = (SecondaryOption) CBAutoGUI.getNamedObject(options, s);
if (op == null && s.charAt(0) != '-')
op = (SecondaryOption) CBAutoGUI.getNamedObject(options, "-" + s);
return op;
}
public Vector getHiddenOptions()
{
Vector ret = new Vector();
for (int i = 0; i < options.size(); i++)
{
SecondaryOption test = (SecondaryOption) options.get(i);
if (test.isHidden())
ret.add(test);
}
return ret;
}
public String toCommandString()
{
return name;
}
}
/*
* A secondary option appears after the primary option, may
* have an argument, and if it does that argument (which is
* entered using a text field) may be a hidden password field.
*/
class SecondaryOption extends Option
{
boolean password;
boolean label;
boolean hasArgument;
boolean hidden;
boolean file;
String defaultValue = "";
/**
* Construct a new option that will appear on a tabbed pane.
*
* @param optionString the name of the option, as it appears on the command line, but without the dash
* @param pwd whether the option is a password field
* @param arg whether the option has an argument (if not, treat it as a check box)
* @param lbl whether the 'option' is in fact simply a label (a comment, or extra directions or something)
* @param hide whether the option is hidden from the user, and always present.
* @param whether the option represents a file, and hence requires a file picker menu.
* @param tt the tooltip (may be null) to show the user when the mouse passes over this component.
* @param an optional default value for the component (nb *any* value corresponds to 'on' for a checkbox...)
*/
public SecondaryOption(String optionString, boolean pwd, boolean arg, boolean lbl, boolean hide, boolean isFile, String tooltip, String defVal)
{
super(optionString);
password = pwd;
hasArgument = arg;
label = lbl;
hidden = hide;
file = isFile;
if (tooltip != null)
tip = tooltip;
if (defVal != null)
defaultValue = defVal;
}
// handle default values...
public void setDefaultValue(String s)
{
if (defaultValue != null) defaultValue = s;
}
public String getDefaultValue()
{
return (defaultValue == null) ? "" : defaultValue;
}
public String toCommandString()
{
return name;
}
public boolean isPassword()
{
return password;
}
public boolean isLabel()
{
return label;
}
public boolean isCheckBox()
{
return !hasArgument;
}
public boolean isHidden()
{
return hidden;
}
public boolean isFile()
{
return file;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -