📄 cbautogui.java
字号:
{
if (commonItems.size() == 0) return; // if there are no common options, don't do anything.
commonOptions = new CBPanel();
for (int i = 0; i < commonItems.size(); i++)
{
SecondaryOption secondary = (SecondaryOption) commonItems.get(i);
addSecondaryToPanel(commonOptions, secondary, ((i % 2) != 0));
}
// Hack to get GridBagLayout to do what I want...
commonOptions.newLine();
commonOptions.add(new JLabel(" ")); // spacer below...
commonOptions.makeHeavy();
commonOptions.addWide(new JLabel(" "), 5); // spacer below...
display.addLine(commonOptions);
}
void clearPasswords()
{
for (int i = 0; i < tabPane.getTabCount(); i++)
{
Component c = tabPane.getComponent(i);
clearPasswords(c);
}
clearPasswords(commonOptions);
}
void clearPasswords(Component c)
{
if (c instanceof Container)
{
Container con = (Container) c;
for (int i = 0; i < con.getComponentCount(); i++)
{
Component comp = con.getComponent(i);
if (comp instanceof JPasswordField)
{
((JPasswordField) comp).setText("");
}
}
}
}
void addSecondaryToPanel(CBPanel panel, SecondaryOption secondary, boolean newLine)
{
if (secondary.isHidden()) return; // don't add hidden fields.
JComponent comp1 = null;
JComponent comp2 = null;
CBFileChooserButton fileChooser = null;
String defaultValue = secondary.getDefaultValue(); // most don't have defaults...
if (secondary.isLabel())
{
JLabel label = new JLabel(secondary.toString());
Font current = label.getFont();
label.setFont(new Font(current.getName(), Font.BOLD, current.getSize() + 2));
panel.addLine(label);
/* if (newLine)
panel.addWide( label, 2);
else
panel.addWide( label, 4);
*/
newLine = true; // force new line...
return;
}
else if (secondary.isPassword())
{
panel.add(comp1 = new JLabel(secondary.toString()));
panel.addGreedyWide(comp2 = new JPasswordField(), 2);
}
else if (secondary.isCheckBox())
{
panel.add(comp1 = new JLabel(secondary.toString()));
panel.add(comp2 = new JCheckBox());
if ("".equals(defaultValue) == false)
((JCheckBox) comp2).setSelected(true);
panel.add(new JLabel(""));
}
else if (secondary.isFile())
{
panel.add(comp1 = new JLabel(secondary.toString()));
panel.addGreedyWide(comp2 = new JTextField(defaultValue, 30));
panel.add(fileChooser = new CBFileChooserButton((JTextField) comp2, this, "File"));
if (defaultValue.length() > 0)
{
fileChooser.setLocalDirectoryUse(true);
fileChooser.setStartingDirectory(defaultValue);
}
}
else
{
panel.add(comp1 = new JLabel(secondary.toString()));
panel.addGreedyWide(comp2 = new JTextField(defaultValue, 40), 2);
}
if (comp1 != null) comp1.setToolTipText(secondary.getTip());
//if (comp2 != null) comp2.setToolTipText(secondary.getTip());
if (newLine)
panel.newLine();
}
void processFile(String fileName)
{
if (debug) System.out.println("processing file '" + fileName + "'");
PrimaryOption currentItem = null;
try
{
BufferedReader readText = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line = ""; // the first line read from the ldif file
int count = 0;
while ((line = readText.readLine()) != null)
{
line = line.trim();
if (line.length() == 0 || line.charAt(0) == '#')
{
// do nothing - comment line.
}
else if (commandName == null) // get command line utility name (first real line in file)
{
if (debug) System.out.println("read commandName as '" + line + "'");
commandName = line;
}
else if (line.toLowerCase().startsWith("primary: "))
{
line = line.substring(9).trim();
if (debug) System.out.println("read primary option as '" + line + "'");
menuItems.add((currentItem = new PrimaryOption(line)));
}
else if (line.toLowerCase().startsWith("common: "))
{
line = line.substring(8).trim();
if (debug) System.out.println("read common option as '" + line + "'");
SecondaryOption op = getSecondaryOption(line);
common.add(op);
commonItems.add(op);
}
else if (line.toLowerCase().startsWith("secondary: ") && currentItem != null)
{
line = line.substring(11).trim();
if (debug) System.out.println("read secondary option as '" + line + "'");
currentItem.add(getSecondaryOption(line));
}
else
error("WARNING: ignoring line '" + line + "'");
}
}
catch (Exception e)
{
error("unable to open file:\n" + e.toString());
e.printStackTrace();
}
}
SecondaryOption getSecondaryOption(String line)
{
int pos = line.indexOf(' ');
if (pos < 0) pos = line.length();
String name = line.substring(0, pos);
boolean isPwd = false;
boolean hasArg = true;
boolean isLabel = false;
boolean isHidden = false;
boolean isFile = false;
String tooltip = null;
String defaultValue = null;
if (line.charAt(0) == '\"')
{
isLabel = true;
name = line.substring(1, line.length() - 1); // the entire line is one comment.
}
else if (pos != line.length())
{
String lowC = line.substring(pos).toLowerCase();
// This is damn ugly, but I'm in a hurry...
if (lowC.indexOf("tip=") != -1) // get tool tip
{
pos = lowC.indexOf("tip=");
pos = line.indexOf("\"", pos);
int pos2 = line.indexOf("\"", pos + 1);
if (pos2 == -1) pos2 = line.length(); // no closing quote...
tooltip = line.substring(pos + 1, pos2);
lowC = line.substring(0, pos).toLowerCase() + line.substring(pos2); // trim out comment so as not to confuse keyword search below.
}
if (lowC.indexOf("default=") != -1) // get default value
{
pos = lowC.indexOf("default=");
pos = line.indexOf("\"", pos);
int pos2 = line.indexOf("\"", pos + 1);
if (pos2 == -1) pos2 = line.length(); // no closing quote...
defaultValue = line.substring(pos + 1, pos2);
lowC = line.substring(0, pos).toLowerCase() + line.substring(pos2); // trim out default values so as not to confuse keyword search below.
}
if (lowC.indexOf("nostring") != -1) hasArg = false;
if (lowC.indexOf("ispassword") != -1) isPwd = true;
if (lowC.indexOf("hidden") != -1) isHidden = true;
if (lowC.indexOf("file") != -1) isFile = true;
}
return (new SecondaryOption(name, isPwd, hasArg, isLabel, isHidden, isFile, tooltip, defaultValue));
}
/**
* Simple error printing routine - over-ride this for app specific functionality.
*/
public void error(String msg)
{
System.err.println(msg);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand().trim();
if (cmd.equalsIgnoreCase("Execute"))
{
execute();
}
else if (cmd.equalsIgnoreCase("Cancel"))
{
cancel();
}
else if (cmd.equalsIgnoreCase("Help"))
{
help();
}
else
error("Unknown command in OpenConWin\n" + cmd);
}
public void execute()
{
execute(2000); // two second default timeout
}
public void execute(int millisecTimeout)
{
final int timeout = millisecTimeout;
CBPanel current = (CBPanel) tabPane.getSelectedComponent();
Vector args = new Vector();
String title = tabPane.getTitleAt(tabPane.getSelectedIndex()); // name of the primary option...
PrimaryOption command = (PrimaryOption) getNamedObject(menuItems, title);
args.add(command.toCommandString());
Component[] comps = current.getComponents();
setArguments(args, comps, command);
comps = commonOptions.getComponents();
setArguments(args, comps, common);
String commandString = commandName;
if (commandString.startsWith("%JAVA%"))
{
commandString = System.getProperty("java.home") + System.getProperty("file.separator") +
"bin" + System.getProperty("file.separator") + commandString.substring(6);
}
final String finalName = commandString + " " + command.toCommandString();
for (int i = 0; i < args.size(); i++)
commandString += " " + args.get(i).toString();
final String finalCommand = commandString;
output.setText("running command:\n " + finalName);
final Thread worker = new Thread("Execute " + finalName) // run command in a separate worker thread
{
public void run()
{
try
{
Runtime runMe = Runtime.getRuntime();
Process goBoy = runMe.exec(finalCommand);
//BufferedReader readText = new BufferedReader(new InputStreamReader(goBoy.getInputStream()));
output.read(goBoy.getInputStream(), "text");
output.setText(output.getText() + "\n\nCommand " + finalName + " Executed.\n");
}
catch (IOException e)
{
output.setText(output.getText() + "\n\nIOException reading command Output\n" + e.toString());
}
}
};
// fire up another thread to time out the first, if necessary...
Thread waiter = new Thread("Execute Watcher for " + finalCommand)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -