r2689 - in trunk: jaxx-compiler/src/main/java/jaxx/compiler/tags jaxx-validator/src/main/java/jaxx/runtime/validator/swing jaxx-widgets/src/main/java/jaxx/runtime/swing/session
Author: tchemit Date: 2013-07-03 12:13:24 +0200 (Wed, 03 Jul 2013) New Revision: 2689 Url: http://nuiton.org/projects/jaxx/repository/revisions/2689 Log: fixes #2730: Introduce a SimpleBeanValidator table model Added: trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableModel.java trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableRenderer.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/DefaultObjectHandler.java trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanDoubleListState.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanFilterableComboBoxState.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/DefaultObjectHandler.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/DefaultObjectHandler.java 2013-06-04 13:09:25 UTC (rev 2688) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/DefaultObjectHandler.java 2013-07-03 10:13:24 UTC (rev 2689) @@ -732,11 +732,20 @@ } } if (ClassDescriptorHelper.getClassDescriptor(BeanTypeAware.class).isAssignableFrom(object.getObjectClass())) { - // add beanType from genericType - if (log.isDebugEnabled()) { - log.debug("Add beanType property: " + value + " to " + object); + + try { + ClassDescriptorHelper.getClassDescriptor(value); + // add beanType from genericType + if (log.isDebugEnabled()) { + log.debug("Add beanType property: " + value + " to " + object); + } + setAttribute(object, BeanTypeAware.PROPERTY_BEAN_TYPE, "{" + value + ".class}", true, compiler); + } catch (ClassNotFoundException e) { + // not a class, must be a reference to another generic type + if (log.isDebugEnabled()) { + log.debug("Could not find beanType from value "+value); + } } - setAttribute(object, BeanTypeAware.PROPERTY_BEAN_TYPE, "{" + value + ".class}", true, compiler); } } Added: trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableModel.java =================================================================== --- trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableModel.java (rev 0) +++ trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableModel.java 2013-07-03 10:13:24 UTC (rev 2689) @@ -0,0 +1,271 @@ +package jaxx.runtime.validator.swing; + +/* + * #%L + * JAXX :: Validator + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import jaxx.runtime.SwingUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.validator.NuitonValidatorScope; +import org.nuiton.validator.bean.simple.SimpleBeanValidator; +import org.nuiton.validator.bean.simple.SimpleBeanValidatorEvent; +import org.nuiton.validator.bean.simple.SimpleBeanValidatorListener; +import org.nuiton.validator.bean.simple.SimpleBeanValidatorMessage; + +import javax.swing.table.AbstractTableModel; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * A model of the table of errors based on {@link SimpleBeanValidatorMessage}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.6.23 + */ +public class SimpleBeanValidatorMessageTableModel extends AbstractTableModel + implements SimpleBeanValidatorListener { + + private static final long serialVersionUID = 1L; + + /** Logger */ + private static Log log = + LogFactory.getLog(SimpleBeanValidatorMessageTableModel.class); + + public static final String[] columnNames = + {"validator.scope", "validator.field", "validator.message"}; + + public static final Class<?>[] columnClasses = + {NuitonValidatorScope.class, String.class, String.class}; + + /** list of registred validators */ + protected transient List<SimpleBeanValidator<?>> validators; + + /** list of messages actual displayed */ + protected List<SimpleBeanValidatorMessage> data; + + public SimpleBeanValidatorMessageTableModel() { + validators = new ArrayList<SimpleBeanValidator<?>>(); + data = new ArrayList<SimpleBeanValidatorMessage>(); + } + + /** + * Register a validator for this model. + * <p/> + * <p/> + * Note: a validator can not be register twice in the same model. + * + * @param validator the validator to register + */ + public void registerValidator(SimpleBeanValidator<?> validator) { + if (validators.contains(validator)) { + throw new IllegalArgumentException( + "the validator " + validator + " is already registred in " + + this); + } + validators.add(validator); + validator.addSimpleBeanValidatorListener(this); + } + + public void addMessages(SimpleBeanValidator<?> validator, + String fieldName, + NuitonValidatorScope scope, + String... messages) { + addMessages(validator, fieldName, scope, true, messages); + } + + public void removeMessages(SimpleBeanValidator<?> validator, + String fieldName, + NuitonValidatorScope scope, + String... messages) { + removeMessages(validator, fieldName, scope, true, messages); + } + + public void clear() { + int i = data.size(); + if (i > 0) { + data.clear(); + fireTableRowsDeleted(0, i - 1); + } + } + + public void clearValidators() { + for (SimpleBeanValidator<?> v : validators) { + v.removeSimpleBeanValidatorListener(this); + } + validators.clear(); + } + + /** + * Obtain the message for a given row. + * + * @param rowIndex the row index + * @return the message for the given row index + */ + public SimpleBeanValidatorMessage getRow(int rowIndex) { + SwingUtil.ensureRowIndex(this, 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) { + SwingUtil.ensureColumnIndex(this, columnIndex); + return columnClasses[columnIndex]; + } + + @Override + public String getColumnName(int column) { + SwingUtil.ensureColumnIndex(this, column); + return columnNames[column]; + } + + @Override + public void onFieldChanged(SimpleBeanValidatorEvent event) { + String[] toDelete = event.getMessagesToDelete(); + String[] toAdd = event.getMessagesToAdd(); + String field = event.getField(); + NuitonValidatorScope 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); + } + + SimpleBeanValidator<?> validator = 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) { + SwingUtil.ensureColumnIndex(this, columnIndex); + SwingUtil.ensureRowIndex(this, rowIndex); + + SimpleBeanValidatorMessage row = data.get(rowIndex); + if (columnIndex == 0) { + // the icon + return row.getScope(); + } + if (columnIndex == 1) { + // the field + return row.getField(); + } + if (columnIndex == 2) { + // the message + return row.getMessage(); + } + + // should never come here + return null; + } + + protected void addMessages(SimpleBeanValidator<?> validator, + String fieldName, + NuitonValidatorScope scope, + boolean sort, + String... messages) { + + + // add new errors + for (String error : messages) { + SimpleBeanValidatorMessage row = + new SimpleBeanValidatorMessage<SimpleBeanValidatorMessage<?>>( + validator, + fieldName, + error, + scope + ); + data.add(row); + if (!sort) { + fireTableRowsInserted(data.size() - 1, data.size() - 1); + } + } + + if (sort) { + + // resort datas + Collections.sort(data); + + // notify + fireTableDataChanged(); + } + } + + protected void removeMessages(SimpleBeanValidator<?> validator, + String fieldName, + NuitonValidatorScope scope, + boolean notify, + String... messages) { + + List<String> messagesToDel = + new ArrayList<String>(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--) { + SimpleBeanValidatorMessage error = data.get(i); + if (validator.equals(error.getValidator()) && + error.getScope() == scope && + error.getField().equals(fieldName) && + messagesToDel.contains(error.getMessage())) { + // remove the message + data.remove(i); + if (notify) { + fireTableRowsDeleted(i, i); + } + } + } + } +} \ No newline at end of file Property changes on: trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableRenderer.java (from rev 2688, trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableRenderer.java) =================================================================== --- trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableRenderer.java (rev 0) +++ trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SimpleBeanValidatorMessageTableRenderer.java 2013-07-03 10:13:24 UTC (rev 2689) @@ -0,0 +1,123 @@ +/* + * #%L + * JAXX :: Validator + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2010 CodeLutin, Tony Chemit + * %% + * 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package jaxx.runtime.validator.swing; + +import org.nuiton.validator.NuitonValidatorScope; +import org.nuiton.validator.bean.simple.SimpleBeanValidatorMessage; + +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.JTable; +import javax.swing.table.DefaultTableCellRenderer; +import java.awt.Component; + +import static org.nuiton.i18n.I18n._; + +/** + * A simple render of a table of validator's messages, says a table that use + * a {@link SimpleBeanValidatorMessageTableModel} model. + * + * @author tchemit <chemit@codelutin.com> + * @see SimpleBeanValidatorMessageTableModel + * @since 2.6.23 + */ +public class SimpleBeanValidatorMessageTableRenderer extends DefaultTableCellRenderer { + + private static final long serialVersionUID = 1L; + + @Override + public Component getTableCellRendererComponent(JTable table, + Object value, + boolean isSelected, + boolean hasFocus, + int row, + int column) { + JLabel rendererComponent = (JLabel) + super.getTableCellRendererComponent( + table, + value, + isSelected, + hasFocus, + row, + column + ); + + ImageIcon icon = null; + String text = null; + String toolTipText = null; + + column = table.convertColumnIndexToModel(column); + if (table.getRowSorter() != null) { + row = table.getRowSorter().convertRowIndexToModel(row); + } + + switch (column) { + case 0: + // scope + NuitonValidatorScope scope = (NuitonValidatorScope) value; + icon = SwingValidatorUtil.getIcon(scope); + String label = _(scope.getLabel()); + toolTipText = _("validator.scope.tip", label); + break; + + case 1: + // field name + text = getFieldName(table, (String) value, row); + toolTipText = _("validator.field.tip", text); + break; + + case 2: + // message + text = getMessage(table, (String) value, row); + toolTipText = _("validator.message.tip", text); + break; + } + + rendererComponent.setText(text); + rendererComponent.setToolTipText(toolTipText); + rendererComponent.setIcon(icon); + return rendererComponent; + } + + public ImageIcon getIcon(NuitonValidatorScope scope) { + ImageIcon icon = SwingValidatorUtil.getIcon(scope); + return icon; + } + + public String getMessage(JTable table, String value, int row) { + SimpleBeanValidatorMessageTableModel tableModel = + (SimpleBeanValidatorMessageTableModel) table.getModel(); + SimpleBeanValidatorMessage model = tableModel.getRow(row); + String text = SwingValidatorUtil.getMessage(model); + return text; + } + + public String getFieldName(JTable table, String value, int row) { + SimpleBeanValidatorMessageTableModel tableModel = + (SimpleBeanValidatorMessageTableModel) table.getModel(); + String fieldName = value; + return fieldName; + } +} Modified: trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java =================================================================== --- trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java 2013-06-04 13:09:25 UTC (rev 2688) +++ trunk/jaxx-validator/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java 2013-07-03 10:13:24 UTC (rev 2689) @@ -36,6 +36,7 @@ import org.nuiton.util.ReflectUtil; import org.nuiton.validator.NuitonValidatorScope; import org.nuiton.validator.bean.list.BeanListValidator; +import org.nuiton.validator.bean.simple.SimpleBeanValidatorMessage; import org.nuiton.validator.bean.simple.SimpleBeanValidators; import javax.swing.ImageIcon; @@ -288,6 +289,29 @@ /** * Prepare the ui where to display the validators messages. * + * @param errorTable the table where to display simpleBean validators messages + * @param render renderer to use + * @since 2.6.23 + */ + public static void installUI(JTable errorTable, + SimpleBeanValidatorMessageTableRenderer render) { + errorTable.setDefaultRenderer(Object.class, render); + errorTable.getRowSorter().setSortKeys( + Arrays.asList(new RowSorter.SortKey(0, SortOrder.ASCENDING))); + SwingUtil.setI18nTableHeaderRenderer( + errorTable, + n_("validator.scope.header"), + n_("validator.scope.header.tip"), + n_("validator.field.header"), + n_("validator.field.header.tip"), + n_("validator.message.header"), + n_("validator.message.header.tip")); + SwingUtil.fixTableColumnWidth(errorTable, 0, 25); + } + + /** + * Prepare the ui where to display the validators messages. + * * @param errorTable the table where to display validators messages * @param render renderer to use * @since 2.5.3 @@ -477,6 +501,14 @@ return text; } + public static String getMessage(SimpleBeanValidatorMessage model) { + String text = model.getMessage(); + if (model.getField() != null) { + text = model.getI18nError(text); + } + return text; + } + public static String getMessage(SwingListValidatorMessage model) { String text = model.getMessage(); if (model.getField() != null) { Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanDoubleListState.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanDoubleListState.java 2013-06-04 13:09:25 UTC (rev 2688) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanDoubleListState.java 2013-07-03 10:13:24 UTC (rev 2689) @@ -1,5 +1,29 @@ package jaxx.runtime.swing.session; +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + import jaxx.runtime.swing.editor.bean.BeanDoubleList; import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox; Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanDoubleListState.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanFilterableComboBoxState.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanFilterableComboBoxState.java 2013-06-04 13:09:25 UTC (rev 2688) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanFilterableComboBoxState.java 2013-07-03 10:13:24 UTC (rev 2689) @@ -1,5 +1,29 @@ package jaxx.runtime.swing.session; +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2013 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox; /** Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/session/BeanFilterableComboBoxState.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.nuiton.org