Java Swing LineBorder changeBorder(JComponent field, Object fieldValue, PropertyChangeEvent e)

Here you can find the source of changeBorder(JComponent field, Object fieldValue, PropertyChangeEvent e)

Description

Creates a colored border around the given component in case of incoming change/conflict.

License

Open Source License

Parameter

Parameter Description
field Any JComponent
fieldValue The value in the field that is of the same type as the values in e
e The old and new values will be checked against the fieldValue

Return

True if the border was made due to an incoming change/conflict.

Declaration

public static boolean changeBorder(JComponent field, Object fieldValue, PropertyChangeEvent e) 

Method Source Code


//package com.java2s;
/*/*from   www  .  jav a2s  . c  o  m*/
 * Copyright (c) 2010, SQL Power Group Inc.
 *
 * This file is part of SQL Power Library.
 *
 * SQL Power Library 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.
 *
 * SQL Power Library 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/>. 
 */

import java.awt.Color;
import java.beans.PropertyChangeEvent;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;

import javax.swing.JComponent;

public class Main {
    public final static Color DARK_NONCONFLICTING_COLOR = new Color(255, 200, 0);
    public final static Color DARK_CONFLICTING_COLOR = new Color(255, 100, 100);

    /**
     * Creates a colored border around the given component in case of incoming change/conflict.
     * @param field Any JComponent
     * @param fieldValue The value in the field that is of the same type as the values in e
     * @param e The old and new values will be checked against the fieldValue
     * @return True if the border was made due to an incoming change/conflict.
     */
    public static boolean changeBorder(JComponent field, Object fieldValue, PropertyChangeEvent e) {
        Object oldValue = e.getOldValue();
        Object incomingValue = e.getNewValue();

        if (fieldValue instanceof String) {
            oldValue = (oldValue != null) ? ((String) oldValue).trim() : "";
            incomingValue = (incomingValue != null) ? ((String) incomingValue).trim() : "";
            fieldValue = (fieldValue != null) ? ((String) fieldValue).trim() : "";
        }

        if (incomingValue.equals(fieldValue))
            return false;

        if (oldValue.equals(fieldValue)) {
            field.setBorder(BorderFactory.createLineBorder(DARK_NONCONFLICTING_COLOR, 3));
        } else {
            field.setBorder(BorderFactory.createLineBorder(DARK_CONFLICTING_COLOR, 3));
        }
        if (field instanceof AbstractButton) {
            ((AbstractButton) field).setBorderPainted(true);
        }

        return true;
    }
}

Related

  1. addBordersToLine(Component content)
  2. addDebugBorders(JPanel panel)
  3. createDebugBorder()
  4. createLineBorder()
  5. createPanelBorder()
  6. createThickInsetBorder()