jaxx.runtime.validator.swing.SwingValidatorMessageTableModel Maven / Gradle / Ivy
/*
* *##%
* JAXX Runtime
* Copyright (C) 2008 - 2009 CodeLutin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* ##%*
*/
package jaxx.runtime.validator.swing;
import jaxx.runtime.validator.BeanValidatorEvent;
import javax.swing.JComponent;
import java.util.ArrayList;
import java.util.List;
import jaxx.runtime.validator.BeanValidatorField;
import jaxx.runtime.validator.BeanValidatorScope;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* The model of the table of errors.
*
* The model listens validators messages and update his internal model from it.
*
* @author chemit
* @since 1.3
*/
public class SwingValidatorMessageTableModel
extends javax.swing.table.AbstractTableModel
implements jaxx.runtime.validator.BeanValidatorListener {
private static final long serialVersionUID = 1L;
/**
* to use log facility, just put in your code: log.info(\"...\");
*/
private static Log log = LogFactory.getLog(SwingValidatorMessageTableMouseListener.class);
public static final String[] columnNames = {"validator.scope", "validator.field", "validator.message"};
public static final Class>[] columnClasses = {BeanValidatorScope.class, String.class, String.class};
/**
* list of registred validators
*/
protected transient List> validators;
/**
* list of messages actual displayed
*/
protected List data;
public SwingValidatorMessageTableModel() {
super();
validators = new ArrayList>();
data = new java.util.ArrayList();
}
/**
* Register a validator for this model.
*
*
* Note: a validator can not be register twice in the same model.
*
* @param validator the validator to register
*/
public void registerValidator(SwingValidator> validator) {
if (validators.contains(validator)) {
throw new IllegalArgumentException("the validator " + validator + " is already registred in " + this);
}
validators.add(validator);
validator.addBeanValidatorListener(this);
}
public void addMessages(SwingValidator> validator, String fieldName, BeanValidatorScope scope, String... messages) {
addMessages(validator, fieldName, scope, true, messages);
}
public void addMessages(JComponent editor, String fieldName, BeanValidatorScope scope, String... messages) {
addMessages(editor, fieldName, scope, true, messages);
}
public void addMessages(SwingValidator> validator, BeanValidatorField> field, BeanValidatorScope scope, String... messages) {
addMessages(validator, field, scope, true, messages);
}
public void removeMessages(JComponent editor, BeanValidatorScope scope) {
// do it in reverse mode (only one pass in that way since index
// will stay coherent while removing them)
for (int i = getRowCount() - 1; i > -1; i--) {
SwingValidatorMessage error = data.get(i);
if (error.getEditor() == editor && (scope == null || error.getScope() == scope)) {
// remove the message
data.remove(i);
fireTableRowsDeleted(i, i);
}
}
}
public void removeMessages(SwingValidator> validator, String fieldName, BeanValidatorScope scope, String... messages) {
removeMessages(validator, fieldName, scope, true, messages);
}
public void removeMessages(JComponent editor, String fieldName, BeanValidatorScope scope) {
removeMessages(editor, fieldName, scope, true);
}
public void removeMessages(SwingValidator> validator, BeanValidatorField> field, BeanValidatorScope scope, String... messages) {
removeMessages(validator, field, scope, true, messages);
}
public void clear() {
int i = data.size();
if (i > 0) {
data.clear();
fireTableRowsDeleted(0, i - 1);
}
}
/**
* Obtain the message for a given row.
*
* @param rowIndex the row index
* @return the message for the given row index
*/
public SwingValidatorMessage getRow(int rowIndex) {
ensureRowIndex(rowIndex);
return data.get(rowIndex);
}
@Override
public boolean isCellEditable(int row, int column) {
// cells are never editable in this model
return false;
}
@Override
public Class> getColumnClass(int columnIndex) {
ensureColumnIndex(columnIndex);
return columnClasses[columnIndex];
}
@Override
public String getColumnName(int column) {
ensureColumnIndex(column);
return columnNames[column];
}
@Override
public void onFieldChanged(BeanValidatorEvent event) {
String[] toDelete = event.getMessagesToDelete();
String[] toAdd = event.getMessagesToAdd();
BeanValidatorField> field = event.getField();
BeanValidatorScope scope = event.getScope();
boolean mustAdd = toAdd != null && toAdd.length > 0;
boolean mustDel = toDelete != null && toDelete.length > 0;
if (log.isTraceEnabled()) {
log.trace("----------------------------------------------------------");
log.trace(field + " - (" + getRowCount() + ") toAdd " + mustAdd);
log.trace(field + " - (" + getRowCount() + ") toDelete " + mustDel);
}
SwingValidator> validator = (SwingValidator>) event.getSource();
if (mustDel) {
// removes datas and notify if no messages to add
removeMessages(validator, field, scope, !mustAdd, toDelete);
}
if (mustAdd) {
// add new messages, sort datas and notify
addMessages(validator, field, scope, true, toAdd);
}
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ensureColumnIndex(columnIndex);
ensureRowIndex(rowIndex);
SwingValidatorMessage row = data.get(rowIndex);
if (columnIndex == 0) {
// the icon
return row.getScope();
}
if (columnIndex == 1) {
// the field
return row.getFieldName();
}
if (columnIndex == 2) {
// the message
return row.getMessage();
}
// should never come here
return null;
}
protected void ensureRowIndex(int rowIndex) throws ArrayIndexOutOfBoundsException {
if (rowIndex < -1 || rowIndex >= getRowCount()) {
throw new ArrayIndexOutOfBoundsException("the rowIndex was " + rowIndex + ", but should be int [0," + (getRowCount() - 1) + "]");
}
}
protected void ensureColumnIndex(int index) throws ArrayIndexOutOfBoundsException {
if (index < -1 || index >= getColumnCount()) {
throw new ArrayIndexOutOfBoundsException("the columnIndex was " + index + ", but should be int [0," + (getColumnCount() - 1) + "]");
}
}
protected void addMessages(SwingValidator> validator, BeanValidatorField> field, BeanValidatorScope scope, boolean sort, String... messages) {
JComponent editor = validator == null ? null : validator.getFieldRepresentation(field.getName());
// add new errors
for (String error : messages) {
SwingValidatorMessage row = new SwingValidatorMessage(validator, field, error, scope, editor);
data.add(row);
if (!sort) {
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
}
if (sort) {
// resort datas
java.util.Collections.sort(data);
// notify
fireTableDataChanged();
}
}
protected void addMessages(SwingValidator> validator, String fieldName, BeanValidatorScope scope, boolean sort, String... messages) {
JComponent editor = validator == null ? null : validator.getFieldRepresentation(fieldName);
// add new errors
for (String error : messages) {
SwingValidatorMessage row = new SwingValidatorMessage(validator, fieldName, error, scope, editor);
data.add(row);
if (!sort) {
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
}
if (sort) {
// resort datas
java.util.Collections.sort(data);
// notify
fireTableDataChanged();
}
}
protected void addMessages(JComponent editor, String fieldName, BeanValidatorScope scope, boolean sort, String... messages) {
// add new errors
for (String error : messages) {
SwingValidatorMessage row = new SwingValidatorMessage(null, fieldName, error, scope, editor);
data.add(row);
if (!sort) {
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
}
if (sort) {
// resort datas
java.util.Collections.sort(data);
// notify
fireTableDataChanged();
}
}
protected void removeMessages(SwingValidator> validator, BeanValidatorField> field, BeanValidatorScope scope, boolean notify, String... messages) {
List messagesToDel = new java.util.ArrayList(java.util.Arrays.asList(messages));
// do it in reverse mode (only one pass in that way since index
// will stay coherent while removing them)
for (int i = getRowCount() - 1; i > -1; i--) {
SwingValidatorMessage error = data.get(i);
if (error.getValidator() == validator && error.getScope() == scope && error.getFieldName().equals(field.getName()) && messagesToDel.contains(error.getMessage())) {
// remove the message
data.remove(i);
if (notify) {
fireTableRowsDeleted(i, i);
}
}
}
}
protected void removeMessages(SwingValidator> validator, String fieldName, BeanValidatorScope scope, boolean notify, String... messages) {
List messagesToDel = new java.util.ArrayList(java.util.Arrays.asList(messages));
// do it in reverse mode (only one pass in that way since index
// will stay coherent while removing them)
for (int i = getRowCount() - 1; i > -1; i--) {
SwingValidatorMessage error = data.get(i);
if (error.getValidator() == validator && error.getScope() == scope && error.getFieldName().equals(fieldName) && messagesToDel.contains(error.getMessage())) {
// remove the message
data.remove(i);
if (notify) {
fireTableRowsDeleted(i, i);
}
}
}
}
protected void removeMessages(JComponent editor, String fieldName, BeanValidatorScope scope, boolean notify) {
// do it in reverse mode (only one pass in that way since index
// will stay coherent while removing them)
for (int i = getRowCount() - 1; i > -1; i--) {
SwingValidatorMessage error = data.get(i);
if (error.getEditor() == editor && (scope == null || error.getScope() == scope) && error.getFieldName().equals(fieldName)) {
// remove the message
data.remove(i);
if (notify) {
fireTableRowsDeleted(i, i);
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy