Package xplanetconfigurator.gui

Source Code of xplanetconfigurator.gui.JPanelControlParametersConfig

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* JPanelAll.java
*
* Created on 27.04.2009, 22:13:10
*/
package xplanetconfigurator.gui;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;
import xplanetconfigurator.util.XPlanetRessourceFinder;

/**
*
* @author tom
*/
public class JPanelControlParametersConfig extends ControlPanel implements IAskForUserTextLine {

    private Logger logger;
    private boolean blockSelection;
    private MainFrame mainFrame;

    /** Creates new form JPanelAll */
    public JPanelControlParametersConfig(MainFrame mainFrame) {
        super(mainFrame);
        this.mainFrame = mainFrame;
        initComponents();
        this.logger = Logger.getLogger(this.getClass().getName());
        this.loadConfigNames();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanelControls = new javax.swing.JPanel();
        jLabelParameter = new javax.swing.JLabel();
        jTextFieldParameterConfig = new javax.swing.JTextField();
        jComboBoxConfigNames = new javax.swing.JComboBox();
        jButtonRemove = new javax.swing.JButton();
        jLabelParameterSeachDir = new javax.swing.JLabel();
        jTextFieldParameterSearchDir = new javax.swing.JTextField();
        jLabelExplain1 = new javax.swing.JLabel();
        jButtonChooseSearchDir = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jPanelHelp = new javax.swing.JPanel();
        jLabelHelp = new javax.swing.JLabel();
        jScrollPane = new javax.swing.JScrollPane();
        jTextAreaHelp = new javax.swing.JTextArea();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(new java.awt.GridBagLayout());

        jPanelControls.setName("jPanelControls"); // NOI18N
        jPanelControls.setLayout(new java.awt.GridBagLayout());

        jLabelParameter.setText("<html><b>-config</b>"); // NOI18N
        jLabelParameter.setName("jLabelParameter"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(7, 12, 5, 0);
        jPanelControls.add(jLabelParameter, gridBagConstraints);

        jTextFieldParameterConfig.setToolTipText("<html>To add a configuration to the list\n<ul>\n<li>Execute XPlanet, or</li>\n<li>Press \"Save\"</li>\n</ul>"); // NOI18N
        jTextFieldParameterConfig.setMinimumSize(new java.awt.Dimension(300, 20));
        jTextFieldParameterConfig.setName("jTextFieldParameterConfig"); // NOI18N
        jTextFieldParameterConfig.setPreferredSize(new java.awt.Dimension(300, 20));
        jTextFieldParameterConfig.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextFieldParameterConfigCaretUpdate(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(7, 5, 5, 5);
        jPanelControls.add(jTextFieldParameterConfig, gridBagConstraints);

        jComboBoxConfigNames.setToolTipText("<html>To add a configuration to the list\n<ul>\n<li>Execute XPlanet, or</li>\n<li>Press \"Save\"</li>\n</ul>"); // NOI18N
        jComboBoxConfigNames.setName("jComboBoxConfigNames"); // NOI18N
        jComboBoxConfigNames.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxConfigNamesActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 5, 7);
        jPanelControls.add(jComboBoxConfigNames, gridBagConstraints);

        jButtonRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xplanetconfigurator/gui/resources/img/Remove24.gif"))); // NOI18N
        jButtonRemove.setToolTipText("Remove a Configuration");
        jButtonRemove.setMinimumSize(new java.awt.Dimension(25, 25));
        jButtonRemove.setName("jButtonRemove"); // NOI18N
        jButtonRemove.setPreferredSize(new java.awt.Dimension(25, 25));
        jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 5, 12);
        jPanelControls.add(jButtonRemove, gridBagConstraints);

        jLabelParameterSeachDir.setText("<html><b>-searchdir</b>"); // NOI18N
        jLabelParameterSeachDir.setName("jLabelParameterSeachDir"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0);
        jPanelControls.add(jLabelParameterSeachDir, gridBagConstraints);

        jTextFieldParameterSearchDir.setToolTipText("<html>To add a configuration to the list\n<ul>\n<li>Execute XPlanet, or</li>\n<li>Press \"Save\"</li>\n</ul>"); // NOI18N
        jTextFieldParameterSearchDir.setMinimumSize(new java.awt.Dimension(200, 20));
        jTextFieldParameterSearchDir.setName("jTextFieldParameterSearchDir"); // NOI18N
        jTextFieldParameterSearchDir.setPreferredSize(new java.awt.Dimension(200, 20));
        jTextFieldParameterSearchDir.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextFieldParameterSearchDirCaretUpdate(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        jPanelControls.add(jTextFieldParameterSearchDir, gridBagConstraints);

        jLabelExplain1.setText("<html>The GUI (the value you see here) allways overwrites the -searchdir in the config file. The character <font color='red'>'-' (minus) causes troubles</font> if contained in the directory.</html>"); // NOI18N
        jLabelExplain1.setName("jLabelExplain1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 12, 0);
        jPanelControls.add(jLabelExplain1, gridBagConstraints);
        jLabelExplain1.getAccessibleContext().setAccessibleName("<html>The GUI (the value you see here) allways overwrites the -searchdir in the config file. The character '-' (minus) in the path will cause problems.</html>");

        jButtonChooseSearchDir.setText("...");
        jButtonChooseSearchDir.setName("jButtonChooseSearchDir"); // NOI18N
        jButtonChooseSearchDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChooseSearchDirActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 12);
        jPanelControls.add(jButtonChooseSearchDir, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jPanelControls, gridBagConstraints);

        jSeparator1.setName("jSeparator1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(jSeparator1, gridBagConstraints);

        jPanelHelp.setName("jPanelHelp"); // NOI18N
        jPanelHelp.setLayout(new java.awt.GridBagLayout());

        jLabelHelp.setText("Help Text of XPlanet");
        jLabelHelp.setName("jLabelHelp"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanelHelp.add(jLabelHelp, gridBagConstraints);

        jScrollPane.setName("jScrollPane"); // NOI18N

        jTextAreaHelp.setColumns(20);
        jTextAreaHelp.setEditable(false);
        jTextAreaHelp.setRows(5);
        jTextAreaHelp.setText("-config config_file\nUse the configuration file config_file.  The format of config_file is\ndescribed in README.config.  See the description of -searchdir to see\nwhere xplanet looks in order to find the configuration file.\n\n-searchdir directory\nAny files used by xplanet should be placed in one of the following\ndirectories depending on its type: \"arcs\", \"config\", \"ephemeris\",\n\"fonts\", \"images\", \"markers\", \"origin\", \"satellites\", or \"stars\".  By\ndefault, xplanet will look for a file in the following order:\nThe current directory\nsearchdir\nsubdirectories of searchdir\nsubdirectories of xplanet (if it exists in the current directory)\nsubdirectories of ${HOME}/.xplanet on X11\nsubdirectories of ${HOME}/Library/Xplanet on Mac OS X\nsubdirectories of DATADIR/xplanet\nDATADIR is set at compile time and defaults to /usr/local/share.\n"); // NOI18N
        jTextAreaHelp.setName("jTextAreaHelp"); // NOI18N
        jScrollPane.setViewportView(jTextAreaHelp);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanelHelp.add(jScrollPane, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanelHelp, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldParameterConfigCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextFieldParameterConfigCaretUpdate
        this.sendParameter(this.jTextFieldParameterConfig, "-config", "(?i)(-config\\b)(\\s*)([^-]*)");
        if (this.jTextFieldParameterConfig.hasFocus()) {
            MainFrame.setCONFIG_NAME(this.jTextFieldParameterConfig.getText());
        }
    }//GEN-LAST:event_jTextFieldParameterConfigCaretUpdate

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        this.receiveCommandLineParameters();
    }//GEN-LAST:event_formComponentShown

    private void jComboBoxConfigNamesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxConfigNamesActionPerformed
        if (!this.jComboBoxConfigNames.hasFocus()) {
            return;
        }
        if (this.blockSelection) {
            return;
        }
        String name = (String) this.jComboBoxConfigNames.getSelectedItem();
        if (name != null) {
            this.jTextFieldParameterConfig.setText(name);
            this.sendParameter(this.jComboBoxConfigNames, "-config", "(?i)(-config\\b)(\\s*)([^-]*)");
            MainFrame.setCONFIG_NAME(name);
            this.mainFrame.loadConfigurationFromFiles();
        }
}//GEN-LAST:event_jComboBoxConfigNamesActionPerformed

    private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
        this.removeConfig();
    }//GEN-LAST:event_jButtonRemoveActionPerformed

    private void jTextFieldParameterSearchDirCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextFieldParameterSearchDirCaretUpdate
        if (this.jTextFieldParameterSearchDir.hasFocus()) {
            XPlanetRessourceFinder finder = new XPlanetRessourceFinder();
            String searchDir = this.jTextFieldParameterSearchDir.getText();
            if (searchDir.contains("\\")) {
                JOptionPane.showMessageDialog(this, "'\\' not allowed.", "XPlanet - Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            finder.setXPlanetSearchDir(searchDir);
            this.sendParameter(this.jTextFieldParameterSearchDir, "-searchdir", "(?i)(-searchdir\\b)(\\s*)([^-]*)");
        }

}//GEN-LAST:event_jTextFieldParameterSearchDirCaretUpdate

    private void jButtonChooseSearchDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChooseSearchDirActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        // The jar file must reside in the same directory as xplanet.exe
        String sDir = System.getProperty("user.dir");
        String fileName = this.jTextFieldParameterSearchDir.getText();
        String f = sDir + File.separator + fileName;
        File selectedFile = new File(f);
        File exe = new File(f);
        if (!exe.exists()) {
            selectedFile = new File(sDir);
        }
        fc.setSelectedFile(selectedFile);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String dir = file.getAbsolutePath();
            dir = dir.replaceAll("\\\\", "/");
            this.jTextFieldParameterSearchDir.setText(dir);
            XPlanetRessourceFinder rf = new XPlanetRessourceFinder();
            rf.setXPlanetSearchDir(dir);
        }
}//GEN-LAST:event_jButtonChooseSearchDirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonChooseSearchDir;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JComboBox jComboBoxConfigNames;
    private javax.swing.JLabel jLabelExplain1;
    private javax.swing.JLabel jLabelHelp;
    private javax.swing.JLabel jLabelParameter;
    private javax.swing.JLabel jLabelParameterSeachDir;
    private javax.swing.JPanel jPanelControls;
    private javax.swing.JPanel jPanelHelp;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextAreaHelp;
    private javax.swing.JTextField jTextFieldParameterConfig;
    private javax.swing.JTextField jTextFieldParameterSearchDir;
    // End of variables declaration//GEN-END:variables

    @Override
    boolean receiveCommandLineParameters(String parameters) throws XPlanetException {
        boolean hasFound = this.findParameterString(parameters);
        if (hasFound) {
            String regExpr = "(?i)(-config\\b)(\\s*)([^-]*)";
            this.receiveParameter(parameters, regExpr, this.jTextFieldParameterConfig);

            // Never read the -searchdir from file.
            // regExpr = "(?i)(-searchdir\\b)(\\s*)([^-]*)";
            // this.receiveParameter(parameters, regExpr, this.jTextFieldParameterSearchDir);

            // XPlanetRessourceFinder is allways used to get -searchdir
            XPlanetRessourceFinder finder = new XPlanetRessourceFinder();
            String searchdir = finder.getXPlanetSearchDir();
            this.jTextFieldParameterSearchDir.setText(searchdir);

            Pattern p = Pattern.compile("(?i)(-config\\b)(\\s*)([^-]*)");
            Matcher matcher = p.matcher(parameters);
            matcher.find();
            int groupCount = matcher.groupCount();
            String found = matcher.group();
            this.logger.finer("Found own parameter for -config in: " + found);
            found = matcher.group(groupCount);
            this.jComboBoxConfigNames.setSelectedItem(found.trim());
            // Set the config name because it is stored to user preferences
            // and used to read the command and config file.
            MainFrame.setCONFIG_NAME(found);
            return true;
        } else {
            // If no name of the configuration is found take
            // 'ownConfiguration' as name to avoid confusing new users
            this.logger.finest("Did not find own parameter. Therefor setting text field of control -config to default 'ownConfiguration'.");
            this.jTextFieldParameterConfig.setText("ownConfiguration");
            MainFrame.setCONFIG_NAME("ownConfiguration");
            return false;
        }
    }

    public String getText() {
        return this.jTextAreaHelp.getText();
    }

    public JTextComponent getTextComponent() {
        return this.jTextAreaHelp;
    }

    public void receiveUserTextLine(String textLine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void loadConfigNames() {
        // Store the selected item
        String selectedItem = (String) this.jComboBoxConfigNames.getSelectedItem();
        this.blockSelection = true;
        this.jComboBoxConfigNames.removeAllItems();

        XPlanetRessourceFinder rf = new XPlanetRessourceFinder();
        String confDir = rf.getRessourceDirectory("config");
        File dir = new File(confDir);
        if (dir.exists()) {
            File[] files = dir.listFiles();
            int length = files.length;
            for (int i = 0; i < length; i++) {
                File f = files[i];
                if (f.isFile()) {
                    String name = f.getName();
                    if (name.matches("(?i)[^\\.]+")) {
                        // Does not contain a ".", means no file extension
                        this.logger.finer("Found config file named: " + name);
                        this.jComboBoxConfigNames.addItem(name);
                    } else {
                        this.logger.finer("Ignoring file named: " + name);
                    }
                }
            }
            this.blockSelection = false;
            this.jComboBoxConfigNames.setSelectedItem(selectedItem);
        }
    }

    private void removeConfig() {
        String name = (String) this.jComboBoxConfigNames.getSelectedItem();
        this.jComboBoxConfigNames.removeAllItems();
        // Delete the config file
        XPlanetRessourceFinder rf = new XPlanetRessourceFinder();
        String pathXPlanet = rf.getRootDirectoryForDownloads();
        // Delete command file
        // At the moment windows is supported only
        String fileExtension = "cmd";
        File commandFile = new File(pathXPlanet + File.separator + name + "." + fileExtension);
        boolean deleted = commandFile.delete();
        if (!deleted) {
            this.logger.fine("Failed to delete file: " + commandFile);
        } else {
            this.logger.fine("Deleted config file: " + commandFile);
        }
        // Delete config file
        String configDir = rf.getRessourceDirectory("config");
        File f = new File(configDir + File.separator + name);
        boolean b = f.delete();
        if (!b) {
            this.logger.fine("Failed to delete file: " + f);
        } else {
            this.logger.fine("Deleted config file: " + f);
        }
        this.loadConfigNames();
        int count = this.jComboBoxConfigNames.getItemCount();
        if (count > 0) {
            this.jComboBoxConfigNames.setSelectedIndex(0);
        } else {
            this.jTextFieldParameterConfig.setText("own_config");
        }
        name = (String) this.jComboBoxConfigNames.getSelectedItem();
        this.jTextFieldParameterConfig.setText(name);
        MainFrame.setCONFIG_NAME(name);
        this.mainFrame.loadConfigurationFromFiles();
    }

    @Override
    void receiveCommandLineParameters() {
        String params = this.getMainFrame().getParameters();
        try {
            this.receiveCommandLineParameters(params);
        } catch (XPlanetException ex) {
            this.logger.log(Level.SEVERE, null, ex);
        }
    }
}
TOP

Related Classes of xplanetconfigurator.gui.JPanelControlParametersConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.