/*
 *  Copyright (c) 2002-2006, the pmd-netbeans team
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 *  DAMAGE.
 */

package pmd.config.ui;

import java.awt.Component;
import java.awt.Dialog;
import java.util.Properties;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;
import pmd.config.CustomRuleSetSettings;

/**
 *
 * @author  radim
 */
public class PmdOptionsComponent extends javax.swing.JPanel {
    
    PmdOptionsController controller;
    
    private RulesConfig rules;

    private CustomRuleSetSettings rulesets;
    
    /** Creates new form PmdOptionsComponent */
    public PmdOptionsComponent(PmdOptionsController c) {
        controller = c;
        initComponents();
    }
    
    void setEnableScan (boolean es) {
        jCheckBox1.setSelected(es);
        jTextField1.setEnabled(es);
    }
    
    boolean isEnableScan () {
        return jCheckBox1.isSelected();
    }
    
    void setScanInterval (int i) {
        jTextField1.setText(Integer.toString(i));
    }
    
    int getScanInterval () {
        return Integer.parseInt(jTextField1.getText());
    }
    
    void setRules(RulesConfig rules) {
        this.rules = rules;
    }
    
    RulesConfig getRules () {
        return rules;
    }
    
    void setRulesets(CustomRuleSetSettings customRuleSetSettings) {
        rulesets = customRuleSetSettings;
    }
    
    CustomRuleSetSettings getRulesets () {
        return rulesets;
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField1 = new javax.swing.JTextField();
        jPanelRules = new javax.swing.JPanel();
        jLblRules = new javax.swing.JLabel();
        jBtnRules = new javax.swing.JButton();
        jPanelRulesets = new javax.swing.JPanel();
        jLblRulesets = new javax.swing.JLabel();
        jBtnRulesets = new javax.swing.JButton();

        jLabel1.setLabelFor(jTextField1);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_ScanInterval"));

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBox1, org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_EnableScan"));
        jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jTextField1.setColumns(3);
        jTextField1.setText("0");

        jPanelRules.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_RulesPanel")));
        org.openide.awt.Mnemonics.setLocalizedText(jLblRules, org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_RulesText"));

        org.openide.awt.Mnemonics.setLocalizedText(jBtnRules, org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_RulesBtn"));
        jBtnRules.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnRulesActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelRulesLayout = new org.jdesktop.layout.GroupLayout(jPanelRules);
        jPanelRules.setLayout(jPanelRulesLayout);
        jPanelRulesLayout.setHorizontalGroup(
            jPanelRulesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelRulesLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLblRules)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 59, Short.MAX_VALUE)
                .add(jBtnRules)
                .addContainerGap())
        );
        jPanelRulesLayout.setVerticalGroup(
            jPanelRulesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelRulesLayout.createSequentialGroup()
                .add(jPanelRulesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jBtnRules)
                    .add(jLblRules))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelRulesets.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_RulesetsPanel")));
        org.openide.awt.Mnemonics.setLocalizedText(jLblRulesets, org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_RulesetsText"));

        org.openide.awt.Mnemonics.setLocalizedText(jBtnRulesets, org.openide.util.NbBundle.getMessage(PmdOptionsComponent.class, "LBL_RulesetBtn"));
        jBtnRulesets.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnRulesetsActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelRulesetsLayout = new org.jdesktop.layout.GroupLayout(jPanelRulesets);
        jPanelRulesets.setLayout(jPanelRulesetsLayout);
        jPanelRulesetsLayout.setHorizontalGroup(
            jPanelRulesetsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelRulesetsLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLblRulesets)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jBtnRulesets, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 146, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanelRulesetsLayout.setVerticalGroup(
            jPanelRulesetsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelRulesetsLayout.createSequentialGroup()
                .add(jPanelRulesetsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jBtnRulesets)
                    .add(jLblRulesets, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 42, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        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(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jCheckBox1)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(332, Short.MAX_VALUE))
            .add(jPanelRules, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanelRulesets, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jCheckBox1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelRules, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(8, 8, 8)
                .add(jPanelRulesets, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnRulesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnRulesActionPerformed
        RuleEditor editor = new RuleEditor();
        editor.setValue (rules);
        Component customEditor = editor.getCustomEditor ();
        DialogDescriptor dd = new DialogDescriptor (
                customEditor,
                NbBundle.getMessage(PmdOptionsComponent.class, "Rules_Editor_Title")
                );
        Dialog dialog = DialogDisplayer.getDefault ().createDialog (dd);
        dialog.setVisible (true);
        if (dd.getValue () == NotifyDescriptor.OK_OPTION) {
            rules = (RulesConfig) editor.getValue ();
            controller.dataChanged();
        }
    }//GEN-LAST:event_jBtnRulesActionPerformed

    private void jBtnRulesetsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnRulesetsActionPerformed
        RuleSetChooserEditor editor = new RuleSetChooserEditor();
        editor.setValue (rulesets);
        Component customEditor = editor.getCustomEditor ();
        DialogDescriptor dd = new DialogDescriptor (
                customEditor,
                NbBundle.getMessage(PmdOptionsComponent.class, "Rulesets_Editor_Title")
                );
        Dialog dialog = DialogDisplayer.getDefault ().createDialog (dd);
        dialog.setVisible (true);
        if (dd.getValue () == NotifyDescriptor.OK_OPTION) {
            rulesets = (CustomRuleSetSettings) editor.getValue ();
            controller.dataChanged();
        }
    }//GEN-LAST:event_jBtnRulesetsActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        jTextField1.setEnabled(jCheckBox1.isSelected());
        controller.dataChanged();
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnRules;
    private javax.swing.JButton jBtnRulesets;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLblRules;
    private javax.swing.JLabel jLblRulesets;
    private javax.swing.JPanel jPanelRules;
    private javax.swing.JPanel jPanelRulesets;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
