net.anwiba.commons.swing.object.AbstractObjectFieldBuilder Maven / Gradle / Ivy
/*
* #%L
* anwiba commons swing
* %%
* Copyright (C) 2007 - 2016 Andreas Bartels
* %%
* 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 2.1 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
* .
* #L%
*/
package net.anwiba.commons.swing.object;
import java.awt.Color;
import net.anwiba.commons.lang.functional.IConverter;
import net.anwiba.commons.model.IChangeableObjectListener;
import net.anwiba.commons.model.IObjectDistributor;
import net.anwiba.commons.model.IObjectModel;
import net.anwiba.commons.swing.utilities.GuiUtilities;
import net.anwiba.commons.utilities.string.StringUtilities;
import net.anwiba.commons.utilities.validation.IValidationResult;
import net.anwiba.commons.utilities.validation.IValidator;
@SuppressWarnings("unchecked")
public abstract class AbstractObjectFieldBuilder, B extends AbstractObjectFieldBuilder> {
final private Color INVALID_COLOR = new Color(240, 240, 180);
final private Color VALID_COLOR = Color.WHITE;
C builder;
public AbstractObjectFieldBuilder(final C builder) {
this.builder = builder;
}
protected C getConfigurationBuilder() {
return this.builder;
}
public IObjectField build() {
final AbstractObjectTextField field = create(this.builder.build());
final IObjectDistributor validationResultDistributor = field.getValidationResultDistributor();
validationResultDistributor.addChangeListener(new IChangeableObjectListener() {
@Override
public void objectChanged() {
GuiUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final IValidationResult validationResult = validationResultDistributor.get();
field.getColorReciever().setBackground(
validationResult.isValid()
? AbstractObjectFieldBuilder.this.VALID_COLOR
: AbstractObjectFieldBuilder.this.INVALID_COLOR);
}
});
}
});
final IValidationResult validationResult = validationResultDistributor.get();
if (validationResult != null) {
field.getColorReciever().setBackground(validationResult.isValid() ? this.VALID_COLOR : this.INVALID_COLOR);
}
return field;
}
protected abstract AbstractObjectTextField create(IObjectFieldConfiguration configuration);
public B setModel(final IObjectModel model) {
this.builder.setModel(model);
return (B) this;
}
public B setColumns(final int columns) {
this.builder.setColumns(columns);
return (B) this;
}
public B setEditable(final boolean isEditable) {
this.builder.setEditable(isEditable);
return (B) this;
}
public B setToObjectConverter(final IConverter toObjectConverter) {
this.builder.setToObjectConverter(toObjectConverter);
return (B) this;
}
public B setToStringConverter(final IConverter toStringConverter) {
this.builder.setToStringConverter(toStringConverter);
return (B) this;
}
public B setValidator(final IValidator validator) {
this.builder.setValidator(validator);
return (B) this;
}
public B setNotEmptyValidator(final String message) {
getConfigurationBuilder().setValidator(new IValidator() {
@Override
public IValidationResult validate(final String value) {
if (StringUtilities.isNullOrTrimmedEmpty(value)) {
return IValidationResult.inValid(message);
}
return IValidationResult.valid();
}
});
return (B) this;
}
public B setValidStateModel(final IObjectModel validStateModel) {
this.builder.setValidStateModel(validStateModel);
return (B) this;
}
public B setToolTipFactory(final IToolTipFactory factory) {
this.builder.setToolTipFactory(factory);
return (B) this;
}
public B addClearAction(final String tooltip) {
this.builder.addClearAction(tooltip);
return (B) this;
}
public B addActionFactory(final IActionFactory tooltip) {
this.builder.addActionFactory(tooltip);
return (B) this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy