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

📄 bootgui.java

📁 JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简化了多Agent系统的实现。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * JADE - Java Agent DEvelopment Framework is a framework to develop
 * multi-agent systems in compliance with the FIPA specifications.
 * Copyright (C) 2000 CSELT S.p.A.
 *
 * GNU Lesser General Public License
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation,
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */
package jade;

//#ALL_EXCLUDE_FILE

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.filechooser.*;

import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

//import jade.core.BootProfileImpl;
import jade.util.BasicProperties;
import jade.util.ExpandedProperties;


import jade.gui.TreeHelp;

/**
 * This class create the gui for the jade configuration
 * @author Tiziana Trucco - CSELT S.p.A.
 * @author Dick Cowan - HP Labs
 * @author Dominic Greenwood - Whitestein Technologies AG
 * @version $Date: 2006-06-05 16:00:30 +0200 (lun, 05 giu 2006) $ $Revision: 5887 $
 */
public class BootGUI extends JDialog {
    static String EXTENSION = "conf";
    static String TITLE = "--JADE Properties--";

    Vector propertiesVector = null;
    File currentDir = null;
    JTextField statusField = new JTextField();
    JPanel topPanel = new JPanel();
    JPanel propertyPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    String propertyFileName = null;
    BasicProperties outProp = null;
    BootGUI thisBootGui;
    Boot booter;

    /**
     * Constructor - launches the GUI configurator.
     * @param theBooter The Boot class.
     */
    public BootGUI(Boot theBooter) {
        super();
        thisBootGui = this;
        
        booter = theBooter;
        propertiesVector = createPropertyVector(booter.getProperties());

        setTitle("JADE Configurator");

        Border raisedbevel = BorderFactory.createRaisedBevelBorder();
        JPanel mainPanel = new JPanel();

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(raisedbevel);
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
        propertyPanel.setLayout(new BoxLayout(propertyPanel,
                                              BoxLayout.Y_AXIS));

        for (Enumeration it = propertiesVector.elements();
                it.hasMoreElements(); ) {
            singlePanel propPanel = new singlePanel();
            PropertyType p = (PropertyType) it.nextElement();
            JPanel panel = propPanel.newSinglePanel(p);

            propertyPanel.add(panel);
        }

        ////////////////////////
        // Status message
        ////////////////////////
        JPanel statusPanel = new JPanel();

        statusPanel.setLayout(new BorderLayout());
        statusField.setEditable(false);
        statusField.setFont(new Font("Monospaced", Font.PLAIN, 12));


        statusField.setPreferredSize(new Dimension(600, 50));

        //statusField.setMaximumSize(new Dimension(200,50));
        statusField.setMinimumSize(new Dimension(50, 50));

        //if(modified)
        // statusField.setText("Warning: default parameter overriden by \ncommand line ones");
        statusPanel.add(statusField, BorderLayout.CENTER);
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));

        JButton openB = new JButton("Open File");

        openB.setToolTipText("Read configuration from file");
        openB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String param = (String) e.getActionCommand();

                if (param.equals("Open File")) {
                    JFileChooser chooser = new JFileChooser();

                    chooser.setFileFilter(new myFileFilter());

                    if (currentDir != null) {
                        chooser.setCurrentDirectory(currentDir);
                    }

                    int returnVal = chooser.showOpenDialog(null);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        currentDir = chooser.getCurrentDirectory();

                        String fileName =
                            chooser.getSelectedFile().getAbsolutePath();

                        try {
                            loadPropertiesFromFile(fileName);
                            propertyFileName = fileName;
                            updateProperties();
                        } catch (FileNotFoundException fe) {
                            System.out.println("File not found Exception");
                        } catch (IOException ioe) {
                            System.out.println("IO Exception");
                        }
                    }
                }
            }
        });
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        buttonPanel.add(openB);

        JButton saveB = new JButton("Save File");

        saveB.setToolTipText("Save configuration into a file");
        saveB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String param = (String) e.getActionCommand();

                if (param.equals("Save File")) {
                    BasicProperties propToSave = extractPropertiesFromGui();

                    //propToSave.list(System.out);
                    try {
                        booter.setProperties(propToSave);

                        propToSave = booter.getProperties();

                        //propToSave.list(System.out);
                        JFileChooser chooser = new JFileChooser();

                        chooser.setFileFilter(new myFileFilter());

                        if (currentDir != null) {
                            chooser.setCurrentDirectory(currentDir);
                        }

                        int returnVal = chooser.showSaveDialog(null);

                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            currentDir = chooser.getCurrentDirectory();

                            String fileName =
                                chooser.getSelectedFile().getAbsolutePath();
                            boolean ext = hasExtension(fileName);

                            if (ext == false) {
                                fileName = fileName.concat(".conf");
                            }

                            propertyFileName = fileName;

                            try {
                                FileOutputStream out =
                                    new FileOutputStream(fileName);
				// do not save -conf=true otherwise 
				// -conf <fileName starts the GUI again
				propToSave.put(BootProfileImpl.CONF_KEY,"false");
                                propToSave.store(out, TITLE);
                                out.close();

                                outProp = propToSave;

                                //dispose();
                            } catch (FileNotFoundException e1) {
                                System.out
                                    .println("File not found Exception");
                            } catch (IOException e2) {
                                System.out.println("IO exception");
                            }
                        }
                    } catch (BootException be) {
                        statusField.setText(be.getMessage());
                    }
                }
            }
        });
        buttonPanel.add(saveB);

        JButton runB = new JButton("Run");

        runB.setToolTipText("Launch the system");
        runB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String param = (String) e.getActionCommand();

                if (param.equals("Run")) {
                    BasicProperties propToSave = extractPropertiesFromGui();

                    try {
                        booter.setProperties(propToSave);

                        propToSave = booter.getProperties();

                        boolean different = false;

                        if (propertyFileName != null) {

                            // compares the properties from gui with those in the file
                            BasicProperties p =
                                readPropertiesFromFile(propertyFileName);

                            //p.list(System.out);
                            different = compareProperties(propToSave, p);
                        }

                        if (different || (propertyFileName == null)) {
                            int val = JOptionPane.showConfirmDialog(topPanel, "Save this configuration?",
                                                 "JADE Configurator", JOptionPane.YES_NO_CANCEL_OPTION);
                            if (val == JOptionPane.CANCEL_OPTION) {
                                return;
                            } else if (val == JOptionPane.YES_OPTION) {
                                //Save file
                                JFileChooser chooser = new JFileChooser();

                                chooser.setFileFilter(new myFileFilter());

                                if (currentDir != null) {
                                    chooser.setCurrentDirectory(currentDir);
                                }

                                int returnVal = chooser.showSaveDialog(null);

                                if (returnVal
                                        == JFileChooser.APPROVE_OPTION) {
                                    currentDir =
                                        chooser.getCurrentDirectory();

                                    String fileName =
                                        chooser.getSelectedFile()
                                            .getAbsolutePath();
                                    boolean ext = hasExtension(fileName);

                                    if (ext == false) {
                                        fileName = fileName.concat(".conf");
                                    }

                                    try {
                                        FileOutputStream out =
                                            new FileOutputStream(fileName);

                                        propToSave.store(out, TITLE);
                                        out.close();
                                    } catch (FileNotFoundException e1) {
                                        System.out.println(
                                            "File not found exception");
                                    } catch (IOException e2) {
                                        System.out.println("IO exception");
                                    }
                                }
                            }
                        }

                        outProp = propToSave;

                        dispose();
                    } catch (BootException be) {
                        statusField.setText(be.getMessage());
                    } catch (FileNotFoundException e1) {
                        System.out.println("File not found");
                    } catch (IOException e2) {
                        System.out.println("Io Exception");
                    }
                }
            }
        });
        buttonPanel.add(runB);

        JButton exitB = new JButton("Exit");

        exitB.setToolTipText("Exit without executing");
        exitB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String param = (String) e.getActionCommand();

                if (param.equals("Exit")) {
                    System.exit(0);
                }
            }
        });
        buttonPanel.add(exitB);

        JButton helpB = new JButton("Help");

        helpB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String param = (String) e.getActionCommand();

                if (param.equals("Help")) {
                    TreeHelp help = new TreeHelp(thisBootGui, "Boot Help",
                                                 "help/BOOTGUI.html");

                    // must insert the listener for the close action
                    help.setVisible(true);
                    help.requestFocus();
                }
            }
        });
        buttonPanel.add(helpB);
        topPanel.add(buttonPanel);
        topPanel.add(propertyPanel);
        mainPanel.add(topPanel);
        mainPanel.add(statusPanel);
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        setResizable(false);
        setModal(true);
        ShowCorrect();
    }

    /**
     * Extract the values of the configuration properties from the GUI.
     */
    BasicProperties extractPropertiesFromGui() {
        BasicProperties out = new BasicProperties();
        int size = propertyPanel.getComponentCount();

        for (Enumeration it = propertiesVector.elements();

⌨️ 快捷键说明

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