/* * ==========================================================================%%# * 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.regexes; import info.gianlucacosta.easypmd.ide.DialogService; import info.gianlucacosta.easypmd.ide.Injector; import info.gianlucacosta.helios.swing.jlist.AdvancedSelectionListModel; import javax.swing.*; import java.util.Collection; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; /** * Panel for managing regular expressions */ public class RegexesPanel extends JPanel { private final AdvancedSelectionListModel<String> regexesModel = new AdvancedSelectionListModel<>(); private final DialogService dialogService; private final RegexTemplateSelectionDialog regexTemplateSelectionDialog; /** * Creates new form RegexPanel */ public RegexesPanel() { initComponents(); dialogService = Injector.lookup(DialogService.class); regexTemplateSelectionDialog = Injector.lookup(RegexTemplateSelectionDialog.class); regexesList.setModel(regexesModel); } public Collection<String> getRegexes() { return regexesModel.getItems(); } public void setRegexes(Collection<String> regexes) { regexesModel.setItems(regexes); } private String processInputRegex(String regex) { regex = regex.trim(); try { Pattern.compile(regex); } catch (PatternSyntaxException ex) { dialogService.showWarning("Invalid regular expression"); return null; } return regex; } /** * 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; regexesScrollPane = new javax.swing.JScrollPane(); regexesList = new info.gianlucacosta.helios.swing.jlist.AdvancedSelectionJList(); includedPathsButtonsPanel = new javax.swing.JPanel(); addPredefinedPathButton = new javax.swing.JButton(); addCustomPathButton = new javax.swing.JButton(); moveUpPathButton = new javax.swing.JButton(); moveDownPathButton = new javax.swing.JButton(); removePathButton = new javax.swing.JButton(); setLayout(new java.awt.BorderLayout()); regexesScrollPane.setViewportView(regexesList); add(regexesScrollPane, java.awt.BorderLayout.CENTER); includedPathsButtonsPanel.setLayout(new java.awt.GridBagLayout()); addPredefinedPathButton.setText(org.openide.util.NbBundle.getMessage(RegexesPanel.class, "RegexesPanel.addPredefinedPathButton.text")); // NOI18N addPredefinedPathButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addPredefinedPathButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); includedPathsButtonsPanel.add(addPredefinedPathButton, gridBagConstraints); addCustomPathButton.setText(org.openide.util.NbBundle.getMessage(RegexesPanel.class, "RegexesPanel.addCustomPathButton.text")); // NOI18N addCustomPathButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addCustomPathButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); includedPathsButtonsPanel.add(addCustomPathButton, gridBagConstraints); moveUpPathButton.setText(org.openide.util.NbBundle.getMessage(RegexesPanel.class, "RegexesPanel.moveUpPathButton.text")); // NOI18N moveUpPathButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { moveUpPathButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); includedPathsButtonsPanel.add(moveUpPathButton, gridBagConstraints); moveDownPathButton.setText(org.openide.util.NbBundle.getMessage(RegexesPanel.class, "RegexesPanel.moveDownPathButton.text")); // NOI18N moveDownPathButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { moveDownPathButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); includedPathsButtonsPanel.add(moveDownPathButton, gridBagConstraints); removePathButton.setText(org.openide.util.NbBundle.getMessage(RegexesPanel.class, "RegexesPanel.removePathButton.text")); // NOI18N removePathButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { removePathButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); includedPathsButtonsPanel.add(removePathButton, gridBagConstraints); add(includedPathsButtonsPanel, java.awt.BorderLayout.LINE_END); }// </editor-fold>//GEN-END:initComponents private void addPredefinedPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPredefinedPathButtonActionPerformed while (true) { RegexTemplate regexTemplate = regexTemplateSelectionDialog.askForRegexTemplate(); if (regexTemplate == null) { return; } String newPathRegex = regexTemplate.getRegex(); if (newPathRegex == null) { return; } newPathRegex = processInputRegex(newPathRegex); if (newPathRegex == null || newPathRegex.isEmpty()) { continue; } regexesModel.addElement(newPathRegex); break; } }//GEN-LAST:event_addPredefinedPathButtonActionPerformed private void addCustomPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCustomPathButtonActionPerformed while (true) { String newPathRegex = dialogService.askForString("Regular expression:"); if (newPathRegex == null) { return; } newPathRegex = processInputRegex(newPathRegex); if (newPathRegex == null || newPathRegex.isEmpty()) { continue; } regexesModel.addElement(newPathRegex); break; } }//GEN-LAST:event_addCustomPathButtonActionPerformed private void moveUpPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpPathButtonActionPerformed regexesList.moveUpSelection(); }//GEN-LAST:event_moveUpPathButtonActionPerformed private void moveDownPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownPathButtonActionPerformed regexesList.moveDownSelection(); }//GEN-LAST:event_moveDownPathButtonActionPerformed private void removePathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePathButtonActionPerformed regexesList.removeSelection(); }//GEN-LAST:event_removePathButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addCustomPathButton; private javax.swing.JButton addPredefinedPathButton; private javax.swing.JPanel includedPathsButtonsPanel; private javax.swing.JButton moveDownPathButton; private javax.swing.JButton moveUpPathButton; private info.gianlucacosta.helios.swing.jlist.AdvancedSelectionJList regexesList; private javax.swing.JScrollPane regexesScrollPane; private javax.swing.JButton removePathButton; // End of variables declaration//GEN-END:variables }