/*
 * RuleSetChooser.java
 *
 * Created on 20. februar 2003, 21:16
 */

package pmd.config.ui;

import java.beans.PropertyEditorSupport;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

/**
 * UI for configuring custom rulesets.
 */
public class RuleSetChooser extends JPanel {
	private final PropertyEditorSupport editor;
	private final JFileChooser ruleSetChooser = new JFileChooser();
	private final JFileChooser jarChooser = new JFileChooser();
	/** Creates new form RuleSetChooser */
	public RuleSetChooser(PropertyEditorSupport editor) {
		this.editor = editor;
		RuleSetFilter filter = new RuleSetFilter( RuleSetFilter.RULESETS);
		ruleSetChooser.setFileFilter( filter );
		RuleSetFilter jarFilter = new RuleSetFilter( RuleSetFilter.JARS);
		jarChooser.setFileFilter( jarFilter );
		
		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.
	 */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        upperPanel = new javax.swing.JPanel();
        rulelListPane = new javax.swing.JScrollPane();
        rulesetList = new javax.swing.JList();
        uperRightPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        removeRuleSet = new javax.swing.JButton();
        addRuleSet = new javax.swing.JButton();
        lblRuleSets = new javax.swing.JLabel();
        stdRules = new javax.swing.JCheckBox();
        lowerPanel = new javax.swing.JPanel();
        jarListPane = new javax.swing.JScrollPane();
        jarList = new javax.swing.JList();
        lowerRightPanel = new javax.swing.JPanel();
        buttonPanel2 = new javax.swing.JPanel();
        removejar = new javax.swing.JButton();
        addjar = new javax.swing.JButton();
        lblRules = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        setPreferredSize(new java.awt.Dimension(600, 400));
        setMinimumSize(new java.awt.Dimension(500, 500));
        upperPanel.setLayout(new java.awt.GridBagLayout());

        upperPanel.setBorder(new javax.swing.border.EtchedBorder());
        upperPanel.setMinimumSize(new java.awt.Dimension(200, 200));
        rulelListPane.setPreferredSize(new java.awt.Dimension(300, 131));
        rulesetList.setModel(new RuleSetListModel());
        rulesetList.setMinimumSize(new java.awt.Dimension(200, 200));
        rulelListPane.setViewportView(rulesetList);

        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 = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 1, 3);
        upperPanel.add(rulelListPane, gridBagConstraints);

        uperRightPanel.setLayout(new java.awt.BorderLayout());

        buttonPanel.setLayout(new java.awt.BorderLayout(0, 5));

        removeRuleSet.setText("Remove RuleSets");
        removeRuleSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeRuleSetActionPerformed(evt);
            }
        });

        buttonPanel.add(removeRuleSet, java.awt.BorderLayout.CENTER);

        addRuleSet.setText("Add RuleSet");
        addRuleSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRuleSetActionPerformed(evt);
            }
        });

        buttonPanel.add(addRuleSet, java.awt.BorderLayout.NORTH);

        uperRightPanel.add(buttonPanel, java.awt.BorderLayout.NORTH);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        upperPanel.add(uperRightPanel, gridBagConstraints);

        lblRuleSets.setLabelFor(rulelListPane);
        lblRuleSets.setText("Choose the rulesets to use");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
        upperPanel.add(lblRuleSets, gridBagConstraints);

        stdRules.setText("Include PMD standard rules.");
        stdRules.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stdRulesActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        upperPanel.add(stdRules, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(upperPanel, gridBagConstraints);

        lowerPanel.setLayout(new java.awt.GridBagLayout());

        lowerPanel.setBorder(new javax.swing.border.EtchedBorder());
        lowerPanel.setMinimumSize(new java.awt.Dimension(200, 200));
        jarListPane.setPreferredSize(new java.awt.Dimension(300, 131));
        jarList.setModel(new RuleSetListModel());
        jarList.setMinimumSize(new java.awt.Dimension(200, 300));
        jarListPane.setViewportView(jarList);

        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 = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 1, 3);
        lowerPanel.add(jarListPane, gridBagConstraints);

        lowerRightPanel.setLayout(new java.awt.BorderLayout());

        buttonPanel2.setLayout(new java.awt.BorderLayout(0, 5));

        removejar.setText("Remove Rule jar");
        removejar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removejarActionPerformed(evt);
            }
        });

        buttonPanel2.add(removejar, java.awt.BorderLayout.CENTER);

        addjar.setText("Add Rule jar");
        addjar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addjarActionPerformed(evt);
            }
        });

        buttonPanel2.add(addjar, java.awt.BorderLayout.NORTH);

        lowerRightPanel.add(buttonPanel2, java.awt.BorderLayout.NORTH);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        lowerPanel.add(lowerRightPanel, gridBagConstraints);

        lblRules.setLabelFor(jarListPane);
        lblRules.setText("Choose rule jars");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
        lowerPanel.add(lblRules, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(lowerPanel, gridBagConstraints);

    }//GEN-END:initComponents

	private void removejarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removejarActionPerformed
		int index = jarList.getSelectedIndex();
		if( index >= 0 ) {
			getJarListModel().removeElementAt( index );
			editor.firePropertyChange();
		}
	}//GEN-LAST:event_removejarActionPerformed

	private void addjarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addjarActionPerformed
		int returnValue = jarChooser.showOpenDialog( this );
		if(returnValue == JFileChooser.APPROVE_OPTION) {
			if( !getJarListModel().contains( jarChooser.getSelectedFile().getAbsolutePath() ) ) {
				getJarListModel().addElement( 
					jarChooser.getSelectedFile().getAbsolutePath() );
			}
		}
		jarChooser.cancelSelection();
		editor.firePropertyChange();
	}//GEN-LAST:event_addjarActionPerformed

	private void stdRulesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stdRulesActionPerformed
		editor.firePropertyChange();
	}//GEN-LAST:event_stdRulesActionPerformed

	private void removeRuleSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeRuleSetActionPerformed
		int index = rulesetList.getSelectedIndex();
		if( index >= 0 ) {
			getRuleSetListModel().removeElementAt( index );
			editor.firePropertyChange();
		}
	}//GEN-LAST:event_removeRuleSetActionPerformed

	private void addRuleSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRuleSetActionPerformed
		int returnValue = ruleSetChooser.showOpenDialog( this );
		if(returnValue == JFileChooser.APPROVE_OPTION) {
			if( !getRuleSetListModel().contains( ruleSetChooser.getSelectedFile().getAbsolutePath() ) ) {
				getRuleSetListModel().addElement( 
					ruleSetChooser.getSelectedFile().getAbsolutePath() );
			}
		}
		ruleSetChooser.cancelSelection();
		editor.firePropertyChange();
	}//GEN-LAST:event_addRuleSetActionPerformed
	
	public RuleSetListModel getRuleSetListModel() {
		return (RuleSetListModel)rulesetList.getModel();
	}
	
	public RuleSetListModel getJarListModel() {
		return (RuleSetListModel)jarList.getModel();
	}
	
	
	public boolean includeStandardRules() {
		return stdRules.isSelected();
	}
	
	public void setIncludeStandardRules( boolean selected ) {
		stdRules.setSelected( selected );
	}
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addRuleSet;
    private javax.swing.JButton addjar;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel buttonPanel2;
    private javax.swing.JList jarList;
    private javax.swing.JScrollPane jarListPane;
    private javax.swing.JLabel lblRuleSets;
    private javax.swing.JLabel lblRules;
    private javax.swing.JPanel lowerPanel;
    private javax.swing.JPanel lowerRightPanel;
    private javax.swing.JButton removeRuleSet;
    private javax.swing.JButton removejar;
    private javax.swing.JScrollPane rulelListPane;
    private javax.swing.JList rulesetList;
    private javax.swing.JCheckBox stdRules;
    private javax.swing.JPanel uperRightPanel;
    private javax.swing.JPanel upperPanel;
    // End of variables declaration//GEN-END:variables
	
}
