Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //----------------------------------------------------------------------------
  // Copyright (C) 2003  Rafael H. Bordini, Jomi F. Hubner, et al.
  //
  // 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; either
  // version 2.1 of the License, or (at your option) any later version.
  //
  // 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
 //
 // To contact the authors:
 // http://www.inf.ufrgs.br/~bordini
 // http://www.das.ufsc.br/~jomi
 //
 //----------------------------------------------------------------------------
 
 package jason.util;
 
 
 
 import java.io.File;
 
 
 public class ConfigGUI {
     
     protected JTextField jasonTF;
     protected JTextField javaTF;
     protected JTextField antTF;
     protected JTextArea infraTP;
     //JCheckBox  insideJIDECBox;
     protected JCheckBox  closeAllCBox;
     protected JCheckBox  checkVersionCBox;
     protected JCheckBox  warnSingVarsCBox;
 
     protected JTextField jadeJarTF;
     protected JTextField jadeArgsTF;
     protected JCheckBox  jadeSnifferCB;
     protected JCheckBox  jadeRmaCB;
 
     protected JCheckBox  shortUnnamedVarCB;
 
     protected static Config userProperties = Config.get();
 
     static {
         String currJasonVersion = .getJasonRunningVersion();
 
         // check new version
         //File jasonConfFile = getUserConfFile();
         if (.getProperty("version") != null) {
             //userProperties.load(new FileInputStream(jasonConfFile));
             if (!.getProperty("version").equals(currJasonVersion) && !currJasonVersion.equals("?")) { 
                 // new version, set all values to default
                 ..println("This is a new version of Jason, reseting configuration...");
                 //userProperties.clear();
                 .remove(.);
                 .remove(.);
                 .remove(.);
                 .remove(.);
             }
         } 
 
         .fix();
         .store();
     }
     
     public static void main(String[] args) {
         new ConfigGUI().run();
     }
     
    protected static ConfigGUI getNewInstance() {
        return new ConfigGUI();
    }
    
    public void run() {
        final ConfigGUI jid = getNewInstance();
        JFrame f = new JFrame(jid.getWindowTitle());
        
        JPanel pBt = new JPanel(new FlowLayout());
        JButton bQuit = new JButton("Exit without saving");
        bQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        pBt.add(bQuit);
        JButton bSave = new JButton("Save configuration and Exit");
        bSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jid.save();
                System.exit(0);
            }
        });
        pBt.add(bSave);
        JPanel p = new JPanel(new BorderLayout());
        p.add(.jid.getJasonConfigPanel());
        p.add(.pBt);
        
        f.getContentPane().add(p);
        f.pack();
        f.setVisible(true);
    }
    
    protected String getWindowTitle() {
        return "Jason Configuration -- "+.getProperty("version");
    }
    public JPanel getJasonConfigPanel() {    
        JPanel pop = new JPanel();
        pop.setLayout(new BoxLayout(pop.));
        // jason home
         = new JTextField(25);
        JPanel jasonHomePanel = new JPanel(new GridLayout(0,1));
        jasonHomePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), "Jason"..));
        JPanel jasonJarPanel = new JPanel(new FlowLayout(.));
        jasonJarPanel.add(new JLabel("jason.jar location"));
        jasonJarPanel.add();
        jasonJarPanel.add(createBrowseButton("jason.jar"));
        jasonHomePanel.add(jasonJarPanel);
        // jason check version
        JPanel checkVersionPanel = new JPanel(new FlowLayout(.));
        //checkVersionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Jason options", TitledBorder.LEFT, TitledBorder.TOP));
         = new JCheckBox("Check for new Jason versions on startup"false);
        checkVersionPanel.add();
        jasonHomePanel.add(checkVersionPanel);
        
        // warn sing vars 
        JPanel wsvPanel = new JPanel(new FlowLayout(.));
         = new JCheckBox("Print out warnings about singleton variables in plans and rules"false);
        wsvPanel.add();
        jasonHomePanel.add(wsvPanel);
        // unamed vars style
        JPanel unvPanel = new JPanel(new FlowLayout(.));
         = new JCheckBox("Use short names for unamed variables (those starting with _)"false);
        unvPanel.add();
        jasonHomePanel.add(unvPanel);
        pop.add(jasonHomePanel);
        
        // java home
        JPanel javaHomePanel = new JPanel();
        javaHomePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), "Java Home"..));
        javaHomePanel.setLayout(new FlowLayout(.));
        javaHomePanel.add(new JLabel("Directory"));
         = new JTextField(25);
        javaHomePanel.add();
        JButton setJava = new JButton("Browse");
        setJava.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
                     chooser.setDialogTitle("Select the Java JDK home directory");
                    chooser.setFileSelectionMode(.);
                    if (chooser.showOpenDialog(null) == .) {
                        String javaHome = (new File(chooser.getSelectedFile().getPath())).getCanonicalPath();
                        if (Config.checkJavaHomePath(javaHome)) {
                            .setText(javaHome);
                        } else {
                            JOptionPane.showMessageDialog(null"The selected JDK home directory doesn't have the bin/javac file!");
                        }
                    }
                } catch (Exception e) {}
            }
        });
        javaHomePanel.add(setJava);
        pop.add(javaHomePanel);
        
        // ant lib home
        JPanel antHomePanel = new JPanel();
        antHomePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), "Ant libs"..));
        antHomePanel.setLayout(new FlowLayout(.));
        antHomePanel.add(new JLabel("Directory"));
         = new JTextField(25);
        antHomePanel.add();
        JButton setAnt = new JButton("Browse");
        setAnt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
                    chooser.setDialogTitle("Select the directory with ant.jar and ant-launcher.jar files");
                    chooser.setFileSelectionMode(.);
                    if (chooser.showOpenDialog(null) == .) {
                        String antLib = (new File(chooser.getSelectedFile().getPath())).getCanonicalPath();
                        if (Config.checkAntLib(antLib)) {
                            .setText(antLib);
                        } else {
                            JOptionPane.showMessageDialog(null"The selected directory doesn't have the files ant.jar and ant-launcher.jar!");
                        }
                    }
                } catch (Exception e) {}
            }
        });
        antHomePanel.add(setAnt);
        pop.add(antHomePanel);
        // infras
        JPanel infraPanel = new JPanel();
        infraPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), "Available Insfrastructures"..));
        infraPanel.setLayout(new FlowLayout(.));
         = new JTextArea(5,45);
        infraPanel.add(new JScrollPane());
        pop.add(infraPanel);
        
        // jade home
          = new JTextField(25);
         = new JTextField(30);
        JPanel jadeHomePanel = new JPanel(new GridLayout(0,1));
        jadeHomePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), "JADE"..));
        JPanel jadeJarPanel = new JPanel(new FlowLayout(.));
        jadeJarPanel.add(new JLabel("jade.jar location"));
        jadeJarPanel.add();
        jadeJarPanel.add(createBrowseButton("jade.jar"));
        jadeHomePanel.add(jadeJarPanel);
        JPanel jadeArgsPanel = new JPanel(new FlowLayout(.));
        jadeArgsPanel.add(new JLabel("jade.Boot arguments"));
        jadeArgsPanel.add();
        jadeHomePanel.add(jadeArgsPanel);
        JPanel p = new JPanel(new FlowLayout(.));
         = new JCheckBox();
        .setToolTipText("Should the JADE management agent be run at startup?");
        p.add();
        p.add(new JLabel("Start management agent             "));
         = new JCheckBox();
        .setToolTipText("Should the JADE sniffer agent be run at startup?");
        p.add();
        p.add(new JLabel("Start Sniffer"));
        jadeHomePanel.add(p);
        
        pop.add(jadeHomePanel);
        // shell command
        /*
        JPanel shellPanel = new JPanel();
        shellPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), "Shell command", TitledBorder.LEFT, TitledBorder.TOP));
        shellPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        shellTF = new JTextField(30);
        shellTF.setToolTipText("This command will be used to run the scripts that run the MAS.");
        shellPanel.add(shellTF);
        pop.add(shellPanel);
        */
        
        // run centralised inside jIDE
        /*
        JPanel insideJIDEPanel = new JPanel();
        insideJIDEPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Centralised MAS execution mode", TitledBorder.LEFT, TitledBorder.TOP));
        insideJIDEPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        insideJIDECBox = new JCheckBox("Run MAS as a JasonIDE internal thread instead of another process.");
        insideJIDEPanel.add(insideJIDECBox);
        pop.add(insideJIDEPanel);
        */
        // close all before opening mas project
        JPanel closeAllPanel = new JPanel();
        closeAllPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "jEdit options"..));
        closeAllPanel.setLayout(new FlowLayout(.));
         = new JCheckBox("Close all files before opening a new Jason Project.");
        closeAllPanel.add();
        pop.add(closeAllPanel);
        //shellTF.setText(userProperties.getShellCommand());
        //insideJIDECBox.setSelected(userProperties.runAsInternalTread());
        
        for (String i.getAvailableInfrastructures()) {
            .append(i+"="+.getInfrastructureFactoryClass(i)+"\n");
        }
        
        return pop;
    }
    protected JButton createBrowseButton(final String jarfilefinal JTextField tf) {
        JButton bt = new JButton("Browse");
        bt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
                    chooser.setDialogTitle("Select the "+jarfile+" file");
                    chooser.setFileFilter(new JarFileFilter(jarfile"The "+jarfile+" file"));
                    //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    if (chooser.showOpenDialog(null) == .) {
                        String selJar = (new File(chooser.getSelectedFile().getPath())).getCanonicalPath();
                        if (Config.checkJar(selJar)) {
                            tf.setText(selJar);
                        } else {
                            JOptionPane.showMessageDialog(null"The selected "+jarfile+" file was not ok!");
                        }
                    }
                } catch (Exception e) {}
            }
        });
        return bt;
    }
    public void save() {
        if (Config.checkJar(.getText())) {
            .put(..getText().trim());
        }
        if (Config.checkJar(.getText())) {
            .put(..getText().trim());
        }
        if (Config.checkJavaHomePath(.getText()) || Config.checkJREHomePath(.getText())) {
            .setJavaHome(.getText().trim());
        }
        if (Config.checkAntLib(.getText())) {
            .setAntLib(.getText().trim());
        }
        //userProperties.put(Config.SHELL_CMD, shellTF.getText().trim());
        //userProperties.put(Config.RUN_AS_THREAD, insideJIDECBox.isSelected()+"");
        
        // infras
        BufferedReader in = new BufferedReader(new StringReader(.getText()));
        String i;
        try {
            for (String s.getAvailableInfrastructures()) {
                .removeInfrastructureFactoryClass(s);
            }
            while ( (i = in.readLine()) != null) {
                int pos = i.indexOf("=");
                if (pos > 0) {
                    String infra = i.substring(0,pos);
                    String factory = i.substring(pos+1);
                    .setInfrastructureFactoryClass(infrafactory);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        .store();
    }
    class JarFileFilter extends FileFilter {
        String jar,ds;
        public JarFileFilter(String jarString ds) {
            this. = jar;
            this.  = ds;
        }
        public boolean accept(File f) {
            if (f.getName().endsWith() || f.isDirectory()) {
                return true;
            } else {
                return false;
            }
        }
        
        public String getDescription() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X