All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.client.util.ObserveValidatorMessageTableRenderer Maven / Gradle / Ivy

package fr.ird.observe.client.util;

/*
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program 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.
 * 
 * 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 Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import fr.ird.observe.dto.decoration.ObserveI18nLabelsBuilder;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JTable;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.jaxx.validator.swing.SwingValidatorMessage;
import org.nuiton.jaxx.validator.swing.SwingValidatorMessageTableModel;
import org.nuiton.jaxx.validator.swing.SwingValidatorMessageTableRenderer;
import org.nuiton.validator.NuitonValidatorScope;


import static org.nuiton.i18n.I18n.t;

/**
 * Created on 12/13/14.
 *
 * @author Tony Chemit - [email protected]
 * @since XXX
 */
public class ObserveValidatorMessageTableRenderer extends SwingValidatorMessageTableRenderer {

    private static final long serialVersionUID = 1L;

    /** Logger. */
    private static final Log log = LogFactory.getLog(ObserveValidatorMessageTableRenderer.class);

    private final ObserveI18nLabelsBuilder labelsBuilder = new ObserveI18nLabelsBuilder(null);

    @Override
    public String getFieldName(JTable table, String value, int row) {
        SwingValidatorMessageTableModel tableModel = (SwingValidatorMessageTableModel) table.getModel();
        SwingValidatorMessage model = tableModel.getRow(row);
        JComponent editor = model.getEditor();
        if (editor != null) {
            String validatorLabel = (String) editor.getClientProperty("validatorLabel");
            if (validatorLabel != null) {
                if (log.isWarnEnabled()) {
                    log.warn("Using deprecated validatorLabel : " + validatorLabel);
                }
                return validatorLabel;
            }

            Boolean doNotTranslateFieldName = (Boolean) editor.getClientProperty("doNotTranslateFieldName");
            if (BooleanUtils.isTrue(doNotTranslateFieldName)) {
                return value;
            }
        }

        if (value.startsWith("observe.") || model.getValidator() == null) {
            return t(value);
        }
        return t(labelsBuilder.getI18nKey(model.getValidator().getType(), ObserveI18nLabelsBuilder.OBSERVE_COMMON_PREFIX, value));
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component result = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        SwingValidatorMessageTableModel model = (SwingValidatorMessageTableModel) table.getModel();

        NuitonValidatorScope scope = (NuitonValidatorScope) (column == 0 ? value : model.getValueAt(row, 0));

        Color textColor = scope == NuitonValidatorScope.WARNING ? Color.RED : Color.BLACK;
        result.setForeground(textColor);
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy