/*
* ==========================================================================%%#
* EasyPmd
* ===========================================================================%%
* Copyright (C) 2009 - 2016 Gianluca Costa
* ===========================================================================%%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* ==========================================================================%##
*/
package info.gianlucacosta.easypmd.ide.options;
import info.gianlucacosta.easypmd.ide.DialogService;
import info.gianlucacosta.easypmd.ide.Injector;
import info.gianlucacosta.easypmd.pmdscanner.RuleSetWrapper;
import info.gianlucacosta.easypmd.pmdscanner.StandardRuleSetsCatalog;
import info.gianlucacosta.helios.conversions.CollectionToArrayConverter;
import info.gianlucacosta.helios.product.ProductInfoService;
import info.gianlucacosta.helios.swing.jlist.AdvancedSelectionListModel;
import java.io.File;
import java.util.Collection;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
/**
* Panel dedicated to editing rulesets
*/
public class RuleSetsPanel extends JPanel {
private static final CollectionToArrayConverter<RuleSetWrapper> ruleSetsArrayConverter = new CollectionToArrayConverter<>(RuleSetWrapper.class);
private final AdvancedSelectionListModel<String> ruleSetsModel = new AdvancedSelectionListModel<>();
private final DialogService dialogService;
private final ProductInfoService pluginInfoService;
private final StandardRuleSetsCatalog standardRulesetsCatalog;
private final JFileChooser ruleSetsFileChooser = new JFileChooser();
public RuleSetsPanel() {
initComponents();
dialogService = Injector.lookup(DialogService.class);
pluginInfoService = Injector.lookup(ProductInfoService.class);
standardRulesetsCatalog = Injector.lookup(StandardRuleSetsCatalog.class);
ruleSetsList.setModel(ruleSetsModel);
ruleSetsFileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory()
|| (f.isFile()
&& f.getName().endsWith(".xml"));
}
@Override
public String getDescription() {
return "Ruleset descriptor (*.xml)";
}
});
ruleSetsFileChooser.setMultiSelectionEnabled(true);
ruleSetsFileChooser.setDialogTitle("Add 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
ruleSetsScrollPane = new javax.swing.JScrollPane();
ruleSetsList = new info.gianlucacosta.helios.swing.jlist.AdvancedSelectionJList();
buttonsPanel = new javax.swing.JPanel();
addStandardRuleSetButton = new javax.swing.JButton();
addFileRuleSetsButton = new javax.swing.JButton();
addCustomRuleSetButton = new javax.swing.JButton();
moveUpButton = new javax.swing.JButton();
moveDownButton = new javax.swing.JButton();
removeButton = new javax.swing.JButton();
setLayout(new java.awt.GridBagLayout());
ruleSetsScrollPane.setViewportView(ruleSetsList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(ruleSetsScrollPane, gridBagConstraints);
buttonsPanel.setLayout(new java.awt.GridBagLayout());
addStandardRuleSetButton.setText(org.openide.util.NbBundle.getMessage(RuleSetsPanel.class, "RuleSetsPanel.addStandardRuleSetButton.text")); // NOI18N
addStandardRuleSetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addStandardRuleSetButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
buttonsPanel.add(addStandardRuleSetButton, gridBagConstraints);
addFileRuleSetsButton.setText(org.openide.util.NbBundle.getMessage(RuleSetsPanel.class, "RuleSetsPanel.addFileRuleSetsButton.text")); // NOI18N
addFileRuleSetsButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addFileRuleSetsButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
buttonsPanel.add(addFileRuleSetsButton, gridBagConstraints);
addCustomRuleSetButton.setText(org.openide.util.NbBundle.getMessage(RuleSetsPanel.class, "RuleSetsPanel.addCustomRuleSetButton.text")); // NOI18N
addCustomRuleSetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addCustomRuleSetButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
buttonsPanel.add(addCustomRuleSetButton, gridBagConstraints);
moveUpButton.setText(org.openide.util.NbBundle.getMessage(RuleSetsPanel.class, "RuleSetsPanel.moveUpButton.text")); // NOI18N
moveUpButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
moveUpButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
buttonsPanel.add(moveUpButton, gridBagConstraints);
moveDownButton.setText(org.openide.util.NbBundle.getMessage(RuleSetsPanel.class, "RuleSetsPanel.moveDownButton.text")); // NOI18N
moveDownButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
moveDownButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
buttonsPanel.add(moveDownButton, gridBagConstraints);
removeButton.setText(org.openide.util.NbBundle.getMessage(RuleSetsPanel.class, "RuleSetsPanel.removeButton.text")); // NOI18N
removeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
buttonsPanel.add(removeButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
add(buttonsPanel, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void addStandardRuleSetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addStandardRuleSetButtonActionPerformed
StandardRuleSetsInputPanel inputPanel = new StandardRuleSetsInputPanel(standardRulesetsCatalog);
int userChoice = JOptionPane.showConfirmDialog(
null,
inputPanel,
pluginInfoService.getName(),
JOptionPane.OK_CANCEL_OPTION
);
if (userChoice == JOptionPane.CANCEL_OPTION) {
return;
}
inputPanel.getSelectedRuleSets().forEach(ruleSet
-> ruleSetsModel.addElement(ruleSet.getFileName())
);
}//GEN-LAST:event_addStandardRuleSetButtonActionPerformed
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
ruleSetsList.removeSelection();
}//GEN-LAST:event_removeButtonActionPerformed
private void addCustomRuleSetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCustomRuleSetButtonActionPerformed
String newRuleSet = dialogService.askForString("Rule set:");
if (newRuleSet == null) {
return;
}
newRuleSet = newRuleSet.trim();
if (newRuleSet.isEmpty()) {
dialogService.showWarning("Invalid rule set");
}
ruleSetsModel.addElement(newRuleSet);
}//GEN-LAST:event_addCustomRuleSetButtonActionPerformed
private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
ruleSetsList.moveUpSelection();
}//GEN-LAST:event_moveUpButtonActionPerformed
private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
ruleSetsList.moveDownSelection();
}//GEN-LAST:event_moveDownButtonActionPerformed
private void addFileRuleSetsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFileRuleSetsButtonActionPerformed
ruleSetsFileChooser.setSelectedFiles(null);
if (ruleSetsFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
for (File ruleSetFile : ruleSetsFileChooser.getSelectedFiles()) {
ruleSetsModel.addElement(ruleSetFile.getAbsolutePath());
}
}
}//GEN-LAST:event_addFileRuleSetsButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addCustomRuleSetButton;
private javax.swing.JButton addFileRuleSetsButton;
private javax.swing.JButton addStandardRuleSetButton;
private javax.swing.JPanel buttonsPanel;
private javax.swing.JButton moveDownButton;
private javax.swing.JButton moveUpButton;
private javax.swing.JButton removeButton;
private info.gianlucacosta.helios.swing.jlist.AdvancedSelectionJList ruleSetsList;
private javax.swing.JScrollPane ruleSetsScrollPane;
// End of variables declaration//GEN-END:variables
public Collection<String> getRuleSets() {
return ruleSetsModel.getItems();
}
public void setRuleSets(Collection<String> ruleSets) {
ruleSetsModel.setItems(ruleSets);
}
}