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

📄 startit.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999-2003.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
 */

package com.sri.oaa2.agt.startit;

import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class Startit extends JFrame {

    JLabel title;
    FacPanel facPanel;
    StartButton startBtn;
    StartitInfo sinfo;
    AppBox apps;
    StartitMenuBar menuBar;
    OaaConnection oaa;
    boolean autostart = false;
    boolean nogui = false;
    private boolean showDevOptions = false;
    private String configFilename;



    private boolean quiet = false;

    static public OutputWindow outwin;

    private static final int MARGIN = 3;
    private static final int MARGIN2 = MARGIN * 2;

    public boolean isQuiet() {
        return quiet;
    }

    public Startit(String[] args) {
        Container top = getContentPane();

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints gc = new GridBagConstraints();
        top.setLayout(gridbag);

        processCommandLineArgs(args);

        StartitColors.initialize(this);

        sinfo = new StartitInfo(configFilename, args);
        sinfo.showDevOptions = showDevOptions;

        if (!quiet){
            outwin = new OutputWindow(sinfo);
            outwin.setAutoWarn(true);
            sinfo.setOutputter(outwin);
        }else{
            sinfo.setOutputter(new Outputter() {
                public void outputLine(AppInfo app, String line, int type) {/* be quiet */}
            });
        }


        menuBar = new StartitMenuBar(this);
        setJMenuBar(menuBar);

        apps = new AppBox(sinfo, menuBar.hidden);
        apps.setBackground(StartitColors.gray2);

        String proj = sinfo.getActiveProject();
        title = new JLabel(proj == null ? "StartIt" : proj);
        title.setFont(new Font("SansSerif", Font.PLAIN, 32));
        title.setForeground(startButtonColor);
        gc.insets = new Insets(0, MARGIN2, 0, MARGIN2);
        gc.gridy = 0;
        gc.weightx = 1;
        gc.gridwidth = 2;
        gridbag.setConstraints(title, gc);
        top.add(title);

        facPanel = new FacPanel();
        gc.insets = new Insets(MARGIN2, MARGIN2, 0, MARGIN);
        gc.gridy = 1;
        gc.weightx = 1.0;
        gc.gridwidth = 1;
        gc.fill = gc.BOTH;
        gridbag.setConstraints(facPanel, gc);
        top.add(facPanel);

        startBtn = new StartButton();
        gc.insets = new Insets(MARGIN2, MARGIN, 0, MARGIN2);
        gc.weightx = 0.0;
        gc.fill = gc.NONE;
        gridbag.setConstraints(startBtn, gc);
        top.add(startBtn);

        gc.insets = new Insets(MARGIN2, MARGIN2, MARGIN2, MARGIN2);
        gc.gridy = 2;
        gc.weightx = 1.0;
        gc.weighty = 1.0;
        gc.gridwidth = 2;
        gc.fill = gc.BOTH;
        JScrollPane scrollApps = new JScrollPane(apps,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollApps.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        gridbag.setConstraints(scrollApps, gc);
        top.add(scrollApps);

        // The quit() function will handle closing the window - don't do it automatically!
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                quit();
            }
        });

        if (sinfo.managedFacilitator()) {
            sinfo.facilitator.getProcess().addStatusChangeListener(facPanel);
        } else {
            oaa = sinfo.getOaaConnection();
            //      oaa.addStatusListener(new OaaConnection.StatusListener() {
            //	public void agentStatusChanged(String agentName, String status, String oaaID) {
            //	  facPanel.update();
            //	}
            //      }, "root");
            updateFacilitatorDisplay();
        }

        setTitle("StartIt");
        pack();
        if (!nogui) show();

        if (autostart) sinfo.startShownApps();
    }

    static public void main(String args[]) {
        new Startit(args);
    }

    public void updateFacilitatorDisplay() {
        facPanel.update();
        menuBar.facConnected((oaa != null) && oaa.isConnected());
    }

    private void processCommandLineArgs(String args[]) {
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-help")) {
                printUsage();
                System.exit(0);
            } else if (args[i].equals("-delay")) {
                i++;
                int d = new Integer(args[i]).intValue();
                StartitProcess.setStartTimerDelay(d);
            } else if (args[i].equals("-autostart")) {
                autostart = true;
            } else if (args[i].equals("-nogui")) {
                nogui = true;
            } else if (args[i].equals("-dev")) {
                showDevOptions = true;
            } else if (args[i].equals("-props")) {
                String propsFile = args[++i];
                try {
                    Properties p = new Properties();
                    p.load(new FileInputStream(new File(propsFile)));
                    Environment.merge(p, true);
                } catch (Exception e) {
                    System.err.println("ERROR getting properties from file " + propsFile + ":" + e);
                }
            } else if (args[i].equals("-quiet")) {
                quiet = true;
            }
        }
        if (args.length < 1) {
            System.err.println("ERROR: no .config file specified");
            printUsage();
            System.exit(-1);
        } else {
            configFilename = args[args.length - 1];
        }
    }

    private void printUsage() {
        System.out.println("StartIt java version 2.0");
        System.out.println("Usage: java Startit [-help] [-props <.props file>]* [-autostart] [-nogui] [-quiet] [-delay ms] [<oaa-args>] <config-file>");
    }

    static final String[] quitOptions = {"Quit Now", "Cancel"};

    public void quit() {
        int reply;

        if (sinfo.areAnyAppsRunning()) {
            reply = JOptionPane.showOptionDialog(this,
                    "<html>Some applications are still running.<br>" +
                    "If you quit now they won't be killed.<br>" +
                    "<br>" +
                    "Do you want to quit now and leave the applications running?",
                    "Really quit?",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
                    quitOptions, quitOptions[1]);
            if (reply == 1) return;
        }

        System.exit(0);
    }

    /**
     * WARNING: this doesn't change the selected menu item - it assumes it was
     * the menu item that called it.   :-(
     */
    public void setActiveProject(String project) {
        if (sinfo.setActiveProject(project)) {
            apps.showApps();
            title.setText(project == null ? "Global" : project);
        }
    }

    protected void popupConnectDialog() {

    }

    static public Color startButtonColor = new Color(0, 0, 190);

    private class StartButton extends JPanel {
        public JButton btn;

        public StartButton() {
            setBackground(startButtonColor);
            setBorder(BorderFactory.createCompoundBorder
                    (BorderFactory.createRaisedBevelBorder(),
                            BorderFactory.createRaisedBevelBorder()));

            btn = new JButton("Start");
            btn.setMargin(new Insets(0, 50, 0, 50));
            btn.setBackground(startButtonColor);
            btn.setForeground(Color.white);
            btn.setFont(new Font("SansSerif", Font.PLAIN, 24));
            btn.setFocusable(false);

            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    apps.updateStartitInfo();
                    sinfo.startShownApps();
                }
            });

            add(btn);
        }
    }

    public static final String NO_FAC_LABEL = "(No facilitator specified)";

    private class FacPanel extends JPanel implements StatusChangeListener {

        public StatusLight light;
        public JLabel label;

        public FacPanel() {
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints gc = new GridBagConstraints();
            setLayout(gridbag);

            gc.insets = new Insets(MARGIN2, MARGIN2, MARGIN2, MARGIN2);
            light = new StatusLight(15);
            light.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
            gridbag.setConstraints(light, gc);
            add(light);

            gc.insets = new Insets(MARGIN2, 0, MARGIN2, MARGIN2);
            label = new JLabel();
            gc.fill = gc.HORIZONTAL;
            gc.weightx = 1.0;
            gridbag.setConstraints(label, gc);
            add(label);

            setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
            //setBackground(StartitColors.gray2);
            update();
        }

        public void update() {
            int status;
            if (sinfo.managedFacilitator()) {
                status = sinfo.facilitator.getProcess().getState();
            } else {
                if (oaa == null || !oaa.isConnected())
                    status = StartitProcess.DIED;
                else
                    status = StartitProcess.READY;
            }
            update(status);
        }

        public void update(int status) {
            light.setStatus(status);
            if (sinfo.managedFacilitator()) {
                if (status == StartitProcess.READY) {
                    label.setText("<html>" + sinfo.facilitator.appname + " <i>(" +
                            sinfo.facilitator.resolveVariable(AppInfo.VAR_OAACONNECT, false) +
                            ")</i></html>");
                } else {
                    label.setText(sinfo.facilitator.appname);
                }
            } else {
                label.setText((oaa == null || oaa.getOAAConnectString() == null) ?
                        NO_FAC_LABEL :
                        "Facilitator: " + oaa.getOAAConnectString());
            }
        }

        public void statusChanged(AppInfo info, int status) {
            update(status);
        }
    }

}

⌨️ 快捷键说明

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