ca.sqlpower.matchmaker.swingui.munge.BooleanToStringMungeComponent.java Source code

Java tutorial

Introduction

Here is the source code for ca.sqlpower.matchmaker.swingui.munge.BooleanToStringMungeComponent.java

Source

/*
 * Copyright (c) 2008, SQL Power Group Inc.
 *
 * This file is part of DQguru
 *
 * DQguru 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.
 *
 * DQguru 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/>. 
 */

package ca.sqlpower.matchmaker.swingui.munge;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import ca.sqlpower.matchmaker.MatchMakerSession;
import ca.sqlpower.matchmaker.munge.BooleanToStringMungeStep;
import ca.sqlpower.matchmaker.munge.MungeStep;
import ca.sqlpower.validation.swingui.FormValidationHandler;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * This is the component for StringSubstitutionMungeStep. It has a 
 * JCheckBox for the USE_REGEX parameter and JTextFields for the 
 * FROM and TO parameter.
 */
public class BooleanToStringMungeComponent extends AbstractMungeComponent {

    private JTextField trueValue;
    private JTextField falseValue;

    public BooleanToStringMungeComponent(MungeStep step, FormValidationHandler handler, MatchMakerSession session) {
        super(step, handler, session);
    }

    @Override
    protected JPanel buildUI() {
        final BooleanToStringMungeStep step = (BooleanToStringMungeStep) getStep();
        trueValue = new JTextField(step.getTrueString());
        trueValue.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                doStuff();
            }

            public void removeUpdate(DocumentEvent e) {
                doStuff();
            }

            public void changedUpdate(DocumentEvent e) {
                doStuff();
            }

            private void doStuff() {
                step.setTrueString(trueValue.getText());
            }
        });

        falseValue = new JTextField(step.getFalseString());
        falseValue.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                doStuff();
            }

            public void removeUpdate(DocumentEvent e) {
                doStuff();
            }

            public void changedUpdate(DocumentEvent e) {
                doStuff();
            }

            private void doStuff() {
                step.setFalseString(falseValue.getText());
            }
        });

        FormLayout layout = new FormLayout("4dlu,pref,4dlu,fill:pref:grow,4dlu", // columns
                "4dlu,pref,4dlu,pref,4dlu"); // rows
        CellConstraints cc = new CellConstraints();

        JPanel content = new JPanel(layout);

        content.add(new JLabel("True:"), cc.xy(2, 2));
        content.add(trueValue, cc.xy(4, 2));
        content.add(new JLabel("False:"), cc.xy(2, 4));
        content.add(falseValue, cc.xy(4, 4));
        return content;
    }
}