Package de.whitewashing.php.cs.ui.options

Source Code of de.whitewashing.php.cs.ui.options.CodeSnifferOptionsPanel

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

/*
* CodeSnifferPanel2.java
*
* Created on Nov 17, 2009, 1:45:25 PM
*/
package de.whitewashing.php.cs.ui.options;

import de.whitewashing.php.cs.command.CodeSniffer;
import de.whitewashing.php.cs.command.CodeSnifferBuilder;
import java.awt.Color;
import java.io.File;
import java.util.List;
import org.openide.filesystems.FileChooserBuilder;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

/**
*
* @author petr
*/
public class CodeSnifferOptionsPanel extends javax.swing.JPanel {

    /**
     * Suffix used for the last selected codesniffer directory.
     */
    private static final String CODE_SNIFFER_LAST_FOLDER_SUFFIX = ".codeSniffer";

    private CodeSniffer command = null;
    private CodeSnifferOptions options = null;

    public CodeSnifferOptionsPanel() {
        initComponents();
    }

    /** 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() {

        labelStandard = new javax.swing.JLabel();
        inputBoxStandard = new javax.swing.JComboBox();
        checkBoxShowWarnings = new javax.swing.JCheckBox();
        codeSnifferLabel = new javax.swing.JLabel();
        codeSnifferTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        statusTextLabel = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        versionLabel = new javax.swing.JLabel();
        versionTextLabel = new javax.swing.JLabel();

        setNextFocusableComponent(this);
        setPreferredSize(new java.awt.Dimension(612, 322));

        labelStandard.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "LBL_STANDARD")); // NOI18N

        inputBoxStandard.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Zend", "PEAR", "PHPCS", "Squiz", "MySource" }));

        checkBoxShowWarnings.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "LBL_SHOW_WORNINGS")); // NOI18N

        codeSnifferLabel.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.codeSnifferLabel.text")); // NOI18N

        codeSnifferTextField.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.codeSnifferTextField.text")); // NOI18N

        browseButton.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.browseButton.text")); // NOI18N
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        refreshButton.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.refreshButton.text")); // NOI18N
        refreshButton.setToolTipText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.refreshButton.toolTipText")); // NOI18N
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        statusTextLabel.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.statusTextLabel.text")); // NOI18N

        statusLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        statusLabel.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.statusLabel.text")); // NOI18N

        versionLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        versionLabel.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.versionLabel.text")); // NOI18N

        versionTextLabel.setText(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferOptionsPanel.versionTextLabel.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(labelStandard)
                    .add(codeSnifferLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(statusLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(versionLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(versionTextLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(statusTextLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(codeSnifferTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 350, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(checkBoxShowWarnings, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 230, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(inputBoxStandard, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 350, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(browseButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(refreshButton)))
                .add(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(codeSnifferTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(browseButton)
                    .add(refreshButton)
                    .add(codeSnifferLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusLabel)
                    .add(statusTextLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(versionLabel)
                    .add(versionTextLabel))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(inputBoxStandard, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(labelStandard))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(checkBoxShowWarnings)
                .addContainerGap(185, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CodeSnifferOptionsPanel.class, "CodeSnifferPanel.AccessibleContext.accessibleDescription")); // NOI18N
    }// </editor-fold>//GEN-END:initComponents

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        File codeSnifferScript = new FileChooserBuilder(CodeSnifferOptionsPanel.class.getName() + CODE_SNIFFER_LAST_FOLDER_SUFFIX)
                .setTitle(NbBundle.getMessage(CodeSnifferOptionsPanel.class, "LBL_CodeSnifferSelect"))
                .setFilesOnly(true)
                .showOpenDialog();
        if (codeSnifferScript != null) {
            codeSnifferScript = FileUtil.normalizeFile(codeSnifferScript);
            codeSnifferTextField.setText(codeSnifferScript.getAbsolutePath());
            refresh();
        }
    }//GEN-LAST:event_browseButtonActionPerformed

private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
    refresh();
}//GEN-LAST:event_refreshButtonActionPerformed

    /**
     * Loads everything that were previously set (if any) and refresh
     */
    void load() {
        command = CodeSnifferBuilder.createOrReturn();
       
        // Gets available standards
        options = new CodeSnifferOptions(command.getAvailableStandards());
       
        // Fill inputs with previously saved preferences
        fillForm();
       
        //
        refresh();
    }
   
    /**
     * Refresh current form
     * Validates if PHPCS is installed and loads latest code standards
     */
    void refresh() {
        String csPath = codeSnifferTextField.getText();
        command.setShellScript(csPath);
       
        if(valid()) {
            store(true);
           
            statusTextLabel.setText("Activated");
            statusTextLabel.setForeground(new Color(16, 110, 0));

            inputBoxStandard.setEnabled(true);
            checkBoxShowWarnings.setEnabled(true);

            // Updates inputs with new provided data (if any)
            fillForm();
        } else {
            inputBoxStandard.setEnabled(false);
            checkBoxShowWarnings.setEnabled(false);
            checkBoxShowWarnings.setSelected(false);
            statusTextLabel.setForeground(Color.RED);
        }
       
        versionTextLabel.setText(command.getVersion());
    }
   
    /**
     * Validates form, PHPCS script and standards
     * @return boolean
     */
    boolean valid() {
        boolean isValid;
       
        // Gets PHPCS version, if "?" then, valid PHPCS were not found
        if(command.getVersion().equals("?")) {
            isValid = false;
            statusTextLabel.setText("Invalid CodeSniffer script");
        } else {
            // Gets available standards
            List<String> availableStandards = command.getAvailableStandards();
            options = new CodeSnifferOptions(availableStandards);

            if(availableStandards.isEmpty()) {
                isValid = false;
                statusTextLabel.setText("No code standards found");
            } else {
                isValid = true;
            }
        }
       
        return isValid;
    }

    /**
     * Store method as NB need it (without arguments)
     */
    void store() {
        store(false);
    }
   
    /**
     * Store method, will persist set NBPreferences
     * @param assumeValid If validation should be run again. If true, avoids unnecessary validations.
     */
    void store(boolean assumeValid) {
        if(assumeValid || valid()) {
            // Only add when a standard exists
            String selectedItem = (String) inputBoxStandard.getSelectedItem();
            if(selectedItem != null) {
                options.setCodingStandard((String) inputBoxStandard.getSelectedItem());
            }

            options.setShowWarnings(checkBoxShowWarnings.isSelected());
            options.setShellScript(codeSnifferTextField.getText());
           
            // or:
            // SomeSystemOption.getDefault().setSomeStringProperty(someTextField.getText());
        }
    }
   
    /**
     * Fill form input's and checkboxes with nbpreferences data
     */
    void fillForm() {
        // Remove all standards
        inputBoxStandard.removeAllItems();
       
        for(String standard : options.getCodingStandards()) {
            inputBoxStandard.addItem(standard);
        }
       
        inputBoxStandard.setSelectedItem(options.getCodingStandard());
        checkBoxShowWarnings.setSelected(options.hasShowWarnings());
        codeSnifferTextField.setText(options.getShellScript());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.JCheckBox checkBoxShowWarnings;
    private javax.swing.JLabel codeSnifferLabel;
    private javax.swing.JTextField codeSnifferTextField;
    private javax.swing.JComboBox inputBoxStandard;
    private javax.swing.JLabel labelStandard;
    private javax.swing.JButton refreshButton;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JLabel statusTextLabel;
    private javax.swing.JLabel versionLabel;
    private javax.swing.JLabel versionTextLabel;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of de.whitewashing.php.cs.ui.options.CodeSnifferOptionsPanel

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.