Package org.gephi.ui.filters.plugin.attribute

Source Code of org.gephi.ui.filters.plugin.attribute.EqualStringPanel$RegexValidator

/*
Copyright 2008-2010 Gephi
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
Website : http://www.gephi.org

This file is part of Gephi.

Gephi is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

Gephi 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.gephi.ui.filters.plugin.attribute;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.gephi.filters.plugin.attribute.AttributeEqualBuilder;
import org.gephi.filters.spi.FilterProperty;
import org.netbeans.validation.api.Problems;
import org.netbeans.validation.api.Validator;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.ValidationPanel;

/**
*
* @author Mathieu Bastian
*/
public class EqualStringPanel extends javax.swing.JPanel implements ActionListener {

    private AttributeEqualBuilder.EqualStringFilter filter;

    public EqualStringPanel() {
        initComponents();

        okButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent evt) {
        FilterProperty pattern = filter.getProperties()[1];
        FilterProperty useRegex = filter.getProperties()[2];
        try {
            if (pattern.getValue() == null || !pattern.getValue().equals(textField.getText())) {
                pattern.setValue(textField.getText());
            }
            if (useRegex.getValue() == null || !useRegex.getValue().equals(regexCheckbox.isSelected())) {
                useRegex.setValue(regexCheckbox.isSelected());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setup(AttributeEqualBuilder.EqualStringFilter filter) {
        this.filter = filter;
        this.setToolTipText(filter.getName() + " '" + filter.getColumn().getTitle() + "'");
        FilterProperty pattern = filter.getProperties()[1];
        FilterProperty useRegex = filter.getProperties()[2];
        try {
            textField.setText((String) pattern.getValue());
            regexCheckbox.setSelected((Boolean) useRegex.getValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ValidationPanel createValidationPanel(final EqualStringPanel innerPanel) {
        final ValidationPanel validationPanel = new ValidationPanel();
        validationPanel.setInnerComponent(innerPanel);

        ValidationGroup group = validationPanel.getValidationGroup();
        validationPanel.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                innerPanel.okButton.setEnabled(!validationPanel.isProblem());
            }
        });
        //Node field
        group.add(innerPanel.textField, new RegexValidator(innerPanel));

        return validationPanel;
    }

    /** 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;

        labelPattern = new javax.swing.JLabel();
        textField = new javax.swing.JTextField();
        regexCheckbox = new javax.swing.JCheckBox();
        okButton = new javax.swing.JButton();

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

        labelPattern.setText(org.openide.util.NbBundle.getMessage(EqualStringPanel.class, "EqualStringPanel.labelPattern.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
        add(labelPattern, gridBagConstraints);

        textField.setText(org.openide.util.NbBundle.getMessage(EqualStringPanel.class, "EqualStringPanel.textField.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(textField, gridBagConstraints);

        regexCheckbox.setText(org.openide.util.NbBundle.getMessage(EqualStringPanel.class, "EqualStringPanel.regexCheckbox.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(regexCheckbox, gridBagConstraints);

        okButton.setText(org.openide.util.NbBundle.getMessage(EqualStringPanel.class, "EqualStringPanel.okButton.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
        add(okButton, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel labelPattern;
    private javax.swing.JButton okButton;
    private javax.swing.JCheckBox regexCheckbox;
    private javax.swing.JTextField textField;
    // End of variables declaration//GEN-END:variables

    private static class RegexValidator implements Validator<String> {

        private EqualStringPanel panel;

        public RegexValidator(EqualStringPanel panel) {
            this.panel = panel;
        }

        @Override
        public boolean validate(Problems problems, String compName, String model) {
            boolean result = true;
            if (panel.regexCheckbox.isSelected()) {
                try {
                    Pattern p = Pattern.compile(model);
                } catch (Exception e) {
                    result = false;
                }
                if (!result) {
                    String message = "Invalid regex";
                    problems.add(message);
                }
            }
            return result;
        }
    }
}
TOP

Related Classes of org.gephi.ui.filters.plugin.attribute.EqualStringPanel$RegexValidator

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.