org.nekorp.workflow.desktop.view.binding.JTextFieldWithValidation.java Source code

Java tutorial

Introduction

Here is the source code for org.nekorp.workflow.desktop.view.binding.JTextFieldWithValidation.java

Source

/**
 *   Copyright 2015 TIKAL-TECHNOLOGY
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
package org.nekorp.workflow.desktop.view.binding;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.LinkedList;
import javax.swing.text.DocumentFilter;
import org.apache.commons.lang.StringUtils;
import org.nekorp.workflow.desktop.view.model.validacion.EstatusValidacion;

/**
 *
 * @author Nekorp
 */
public class JTextFieldWithValidation extends javax.swing.JPanel implements Bindable {

    private final LinkedList<Object> ignore = new LinkedList<>();

    /**
     * Creates new form JTextFieldWithValidation
     */
    public JTextFieldWithValidation() {
        this.initComponents();
        this.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                textField.requestFocus();
            }

            @Override
            public void focusLost(FocusEvent e) {
                //nada
            }
        });
    }

    public SimpleBindableJTextField getTextField() {
        return (SimpleBindableJTextField) this.textField;
    }

    public void setMargenColor(Color color) {
        this.up.setBackground(color);
        this.down.setBackground(color);
        this.left.setBackground(color);
        this.right.setBackground(color);
    }

    public void setDocumentFilter(DocumentFilter filter) {
        ((javax.swing.text.AbstractDocument) textField.getDocument()).setDocumentFilter(filter);
    }

    public void setEditable(boolean editable) {
        this.textField.setEditable(editable);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.textField.setEnabled(enabled);
    }

    @Override
    public void setFocusable(boolean value) {
        super.setFocusable(value);
        this.textField.setFocusable(value);
    }

    /**
     * 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() {

        textField = new SimpleBindableJTextField();
        up = new javax.swing.JPanel();
        down = new javax.swing.JPanel();
        left = new javax.swing.JPanel();
        right = new javax.swing.JPanel();

        setMinimumSize(new java.awt.Dimension(10, 24));
        setPreferredSize(new java.awt.Dimension(10, 24));
        setLayout(new java.awt.BorderLayout());

        textField.setBackground(new java.awt.Color(224, 230, 230));
        textField.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        textField.setText("Text");
        textField.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        textField.setDisabledTextColor(new java.awt.Color(100, 100, 100));
        add(textField, java.awt.BorderLayout.CENTER);

        up.setBackground(new java.awt.Color(224, 230, 230));
        up.setPreferredSize(new java.awt.Dimension(28, 2));

        javax.swing.GroupLayout upLayout = new javax.swing.GroupLayout(up);
        up.setLayout(upLayout);
        upLayout.setHorizontalGroup(upLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 182, Short.MAX_VALUE));
        upLayout.setVerticalGroup(upLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,
                100, Short.MAX_VALUE));

        add(up, java.awt.BorderLayout.NORTH);

        down.setBackground(new java.awt.Color(224, 230, 230));
        down.setPreferredSize(new java.awt.Dimension(28, 2));

        javax.swing.GroupLayout downLayout = new javax.swing.GroupLayout(down);
        down.setLayout(downLayout);
        downLayout.setHorizontalGroup(downLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 182, Short.MAX_VALUE));
        downLayout.setVerticalGroup(downLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 2, Short.MAX_VALUE));

        add(down, java.awt.BorderLayout.SOUTH);

        left.setBackground(new java.awt.Color(224, 230, 230));
        left.setMinimumSize(new java.awt.Dimension(2, 20));
        left.setPreferredSize(new java.awt.Dimension(2, 20));

        javax.swing.GroupLayout leftLayout = new javax.swing.GroupLayout(left);
        left.setLayout(leftLayout);
        leftLayout.setHorizontalGroup(leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 2, Short.MAX_VALUE));
        leftLayout.setVerticalGroup(leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE));

        add(left, java.awt.BorderLayout.WEST);

        right.setBackground(new java.awt.Color(224, 230, 230));
        right.setMinimumSize(new java.awt.Dimension(2, 20));
        right.setPreferredSize(new java.awt.Dimension(2, 20));

        javax.swing.GroupLayout rightLayout = new javax.swing.GroupLayout(right);
        right.setLayout(rightLayout);
        rightLayout.setHorizontalGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 2, Short.MAX_VALUE));
        rightLayout.setVerticalGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE));

        add(right, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel down;
    private javax.swing.JPanel left;
    private javax.swing.JPanel right;
    private javax.swing.JTextField textField;
    private javax.swing.JPanel up;
    // End of variables declaration//GEN-END:variables

    @Override
    public void updateModel(Object origen, String property, Object value) {
        if (!ignore.remove(value)) {
            if (value instanceof EstatusValidacion) {
                EstatusValidacion statusValidacion = (EstatusValidacion) value;
                if (statusValidacion.isValido()) {
                    this.setMargenColor(new java.awt.Color(224, 230, 230));
                } else {
                    this.setMargenColor(Color.RED);
                }
                if (StringUtils.isEmpty(statusValidacion.getDetalle())) {
                    this.textField.setToolTipText(null);
                } else {
                    this.textField.setToolTipText(statusValidacion.getDetalle());
                }
            } else {
                throw new IllegalArgumentException("solo sabe usar EstatusValidacion");
            }
        }
    }

    @Override
    public void ignoreUpdate(Object value) {
        this.ignore.add(value);
    }

    @Override
    public Object getModelValue() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void bindListener(Object target, String property) {
        //read only no actualiza el modelo.
    }
}