⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cbautogui.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.ca.commons.cbutil;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Vector;

/**
 * This class creates a GUI interface to a command line tool.
 * It assumes that all the command line options are displayed as
 * is, or followed immediately by strings, and are presented in
 * the order they appear in a formatted file that describes them.<p>
 * <p/>
 * At the moment it will only wrap a single command line program.<p>
 * <p/>
 * The file format is: <br>
 * <pre>
 *    command string
 *    primary: {option name}                             (becomes a menu item)
 *    secondary: {option name} [nostring|ispassword]     (becomes a field)
 *    secondary: "your comment here"                     (becomes a helpfull label)
 *    secondary: ...
 *    ...
 *    primary:
 *    secondary:
 *    ...
 *    </pre>
 * <p/>
 * nb. this class tries to use an image: 'open.gif' to adorn the open file
 * file chooser, and expects this to be in the base directory.<p>
 * <p/>
 * Other options for secondary values include tip="..." for tooltips, and
 * default="..." for default values.
 *
 * @author Chris Betts
 */


public class CBAutoGUI extends JDialog implements ActionListener
{
    String commandName;     // the root OS utility name to run.
    Vector menuItems;       // primary options are menu items.
    Vector commonItems;     // some secondary options may be common to all Primary options

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

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

    boolean guiInitialised = false;

    boolean debug = true;

    static boolean standAlone = false;

    /**
     * An example of stand alone usage.
     */

    public static void main(String args[])
    {
        standAlone = true;

        String fileName = "keytool.txt";
        if (args.length >= 1)
            fileName = args[0];

        CBAutoGUI MrGui = new CBAutoGUI(null, fileName);
        MrGui.show(null);
    }

    /**
     * Constructs an AutoGUI showing a particular initial primary option.<p>
     * - requires a root GUI component
     * (may be null) and the fileName of a file containing a description
     * of the command line function to be called, and the parameters it
     * takes.
     *
     * @param owner    the root GUI (required for correct look and feel propagation
     *                 and repaint behaviour).
     * @param fileName the name of the file containing the command line function description.
     */

    public CBAutoGUI(Frame owner, String fileName)
    {
        super(owner);

        menuItems = new Vector(8);
        commonItems = new Vector(8);
        common = new PrimaryOption("common options");

        processFile(fileName);

        setSize(650, 550);
    }

    /**
     * Brute force search through vector for object by name.  yuck.
     * rewrite sometime to be prettier.  Kind of slow too - lots of string ops.
     * Oh well. CPU is cheap.
     */

    static Object getNamedObject(Vector v, String s)
    {
        s = s.trim();
        for (int i = 0; i < v.size(); i++)
        {
            if (v.get(i).toString().trim().equalsIgnoreCase(s))
                return v.get(i);
        }
        return null;
    }


    /**
     * Sets a particular primary option to display.
     *
     * @param optionName the primaryOption (and tab title) to set the tabbed display to.
     *                   a null or blank string sets the first option.
     */

    public void setOption(String optionName)
    {
        if (optionName == null || optionName.length() == 0)
        {
            if (tabPane.getTabCount() > 0)
                optionName = tabPane.getTitleAt(0).trim();
            else
            {
                error("no tabs set! - nothing to display");
                return;
            }
        }

        optionName = optionName.trim();

        if (optionName.length() == 0) return;
        if (optionName.charAt(0) == '-')
            optionName = optionName.substring(1);
        if (optionName.length() == 0) return;

        for (int i = 0; i < tabPane.getTabCount(); i++)
        {
            if (optionName.equalsIgnoreCase(tabPane.getTitleAt(i).trim()))
            {
                tabPane.setSelectedIndex(i);
                return;
            }
        }
    }

    /**
     * A bit of a hack - allows a program to set the default value
     * of an option at runtime.  Requires the primary and secondary
     * option names to be passed in (as they are in the file) as well
     * as the default value.
     *
     * @param primaryOptionName   the primary option command string, as in text file.
     * @param secondaryOptionName the secondary option command string, as in text file.
     * @param defaultValue        the new default value for the named secondary option.
     */

    public void setDefaultValue(String primaryOptionName, String secondaryOptionName, String defaultValue)
    {
        // XXX more of a hack - rig the names to be the same as the .toString() names of the Options...
        
        primaryOptionName = formatOptionName(primaryOptionName);
        secondaryOptionName = formatOptionName(secondaryOptionName);

        PrimaryOption primary = (PrimaryOption) getNamedObject(menuItems, primaryOptionName);
        if (primary == null)
            primary = common;

        SecondaryOption secondary = primary.get(secondaryOptionName);

        if (secondary != null)
            secondary.setDefaultValue(defaultValue);
    }

    /**
     * Sets the tab panel to the selected primary option,
     * and makes the component visible.
     *
     * @param option the command line primary option panel to make visible.
     */

    public void show(String option)
    {
        if (debug) System.out.println("showing GUI with option : " + ((option == null) ? " (no tab set) " : option));

        if (guiInitialised == false)
            initialiseGUI();
        else
            clearPasswords();

        setOption(option);
        setVisible(true);
    }

    /**
     * Create two panels.  In the top panel, place a tabbed set of panes
     * corresponding to each primary option, and populate that with all
     * the secondary options corresponding to the relevant primary option.
     * In the bottom panel, place the secondary options common to all primary options.
     */

    public void initialiseGUI()
    {
        if (debug) System.out.println("initialising GUI");

        setTitle("GUI interface to " + commandName);

        CBPanel display = new CBPanel();

        addTabbedPanes(display);

        addCommonOptions(display);


        JPanel buttons = new JPanel();
        JButton OK, Cancel, Help;

        display.makeWide();
        display.add(new JLabel(""));  // padding
        display.makeLight();
        display.add(OK = new JButton("Execute"));
        display.add(Cancel = new JButton("Cancel"));
        //display.add(Help = new JButton("Help"));
        display.makeWide();
        display.addLine(new JLabel(""));  // padding
        display.makeHeavy();

        OK.addActionListener(this);
        Cancel.addActionListener(this);
        //Help.addActionListener(this);

        //output = new JEditorPane();
        output.setBorder(new TitledBorder(new LineBorder(Color.black, 2), "output"));
        output.setPreferredSize(new Dimension(400, 150));

        display.addLine(new JScrollPane(output));

        getContentPane().add(display);
        setVisible(true);
        guiInitialised = true;
    }

    /**
     * This runs through all the groups of 'primary' options,
     * creating a new tabbed pane for each one, and populating
     * it with the associated secondary options.
     */

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

        tabPane = new JTabbedPane();

        for (int i = 0; i < menuItems.size(); i++)
        {
            PrimaryOption primary = (PrimaryOption) menuItems.get(i);
            CBPanel panel = new CBPanel();
            panel.makeLight();
            tabPane.addTab(primary.toString(), panel);

            boolean newLine = false;
            for (int j = 0; j < primary.size(); j++)
            {
                SecondaryOption secondary = primary.get(j);
                if (secondary.isLabel())
                {
                    addSecondaryToPanel(panel, secondary, newLine);
                    newLine = false;
                }
                else
                {
                    addSecondaryToPanel(panel, secondary, newLine);
                    newLine = !newLine;
                }
            }
            // Hack to get GridBagLayout to do what I want...
            
            panel.newLine();
            panel.add(new JLabel("                     ")); // spacer below...
            panel.makeHeavy();
            panel.addWide(new JLabel(" "), 5); // spacer below...
        }
        display.addLine(tabPane);
    }

    /**
     * Add common options to a separate, constant display area.
     * Sometimes commands are common to all options, for example a password
     * must be given before any action may be done. This method adds such
     * common options to a separate display area that remains constant
     * when the user moves between different tab panes.
     */

    public void addCommonOptions(CBPanel display)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -