use of com.codename1.ui.validation.Validator in project CodenameOne by codenameone.
the class Table method createCell.
/**
* Creates a cell based on the given value
*
* @param value the new value object
* @param row row number, -1 for the header rows
* @param column column number
* @param editable true if the cell is editable
* @return cell component instance
*/
protected Component createCell(Object value, int row, final int column, boolean editable) {
if (row == -1) {
Button header = new Button((String) value, getUIID() + "Header");
header.getAllStyles().setAlignment(titleAlignment);
header.setTextPosition(LEFT);
if (isSortSupported()) {
header.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Comparator cmp = createColumnSortComparator(column);
if (cmp == null) {
return;
}
if (column == sortedColumn) {
ascending = !ascending;
} else {
sortedColumn = column;
ascending = false;
}
if (model instanceof SortableTableModel) {
model = ((SortableTableModel) model).getUnderlying();
}
setModel(new SortableTableModel(sortedColumn, ascending, model, cmp));
}
});
if (sortedColumn == column) {
if (ascending) {
FontImage.setMaterialIcon(header, FontImage.MATERIAL_ARROW_DROP_UP);
} else {
FontImage.setMaterialIcon(header, FontImage.MATERIAL_ARROW_DROP_DOWN);
}
}
}
return header;
}
int constraint = TextArea.ANY;
Constraint validation = null;
if (isAbstractTableModel()) {
Class type = ((AbstractTableModel) model).getCellType(row, column);
if (type == Boolean.class) {
CheckBox cell = new CheckBox();
cell.setSelected(Util.toBooleanValue(value));
cell.setUIID(getUIID() + "Cell");
cell.setEnabled(editable);
return cell;
}
if (editable && (type == null || type == String.class)) {
String[] multiChoice = ((AbstractTableModel) model).getMultipleChoiceOptions(row, column);
if (multiChoice != null) {
Picker cell = new Picker();
cell.setStrings(multiChoice);
if (value != null) {
cell.setSelectedString((String) value);
}
cell.setUIID(getUIID() + "Cell");
return cell;
}
}
if (editable && type == Date.class) {
Picker cell = new Picker();
cell.setType(Display.PICKER_TYPE_DATE);
if (value != null) {
cell.setDate((Date) value);
}
cell.setUIID(getUIID() + "Cell");
return cell;
}
if (type == Integer.class || type == Long.class || type == Short.class || type == Byte.class) {
constraint = TextArea.NUMERIC;
} else {
if (type == Float.class || type == Double.class) {
constraint = TextArea.DECIMAL;
}
}
if (((AbstractTableModel) model).getValidator() != null) {
validation = ((AbstractTableModel) model).getValidationConstraint(row, column);
}
}
if (editable) {
TextField cell = new TextField(value == null ? "" : "" + value, -1);
cell.setConstraint(constraint);
cell.setLeftAndRightEditingTrigger(false);
cell.setUIID(getUIID() + "Cell");
if (validation != null) {
Validator v = ((AbstractTableModel) model).getValidator();
v.addConstraint(cell, validation);
}
return cell;
}
Label cell = new Label(value == null ? "" : "" + value);
cell.setUIID(getUIID() + "Cell");
cell.getUnselectedStyle().setAlignment(cellAlignment);
cell.getSelectedStyle().setAlignment(cellAlignment);
cell.setFocusable(true);
return cell;
}
use of com.codename1.ui.validation.Validator in project CodenameOne by codenameone.
the class Validator method addConstraint.
/**
* Places a constraint on the validator, returns this object so constraint
* additions can be chained. Shows validation errors messages even when the
* TextModeLayout is not {@code onTopMode} (it's possible to disable this
* functionality setting to false the theme constant
* {@code showValidationErrorsIfNotOnTopMode}: basically, the error
* message is shown for two second in place of the label on the left of the
* InputComponent (or on right of the InputComponent for RTL languages);
* this solution never breaks the layout, because the error message is
* trimmed to fit the available space. The error message UIID is
* "ErrorLabel" when it's not onTopMode.
*
* @param cmp the component to validate
* @param c the constraint or constraints
* @return this object so we can write code like v.addConstraint(cmp1,
* cons).addConstraint(cmp2, otherConstraint);
*/
public Validator addConstraint(Component cmp, Constraint... c) {
Constraint constraint = null;
if (c.length == 1) {
constraint = c[0];
constraintList.put(cmp, constraint);
} else if (c.length > 1) {
constraint = new GroupConstraint(c);
constraintList.put(cmp, constraint);
}
if (constraint == null) {
throw new IllegalArgumentException("addConstraint needs at least a Constraint, but the Constraint array in empty");
}
bindDataListener(cmp);
boolean isV = isValid();
for (Component btn : submitButtons) {
btn.setEnabled(isV);
}
// Show validation error on iPhone
if (UIManager.getInstance().isThemeConstant("showValidationErrorsIfNotOnTopMode", true) && cmp instanceof InputComponent) {
final InputComponent inputComponent = (InputComponent) cmp;
if (!inputComponent.isOnTopMode()) {
Label labelForComponent = null;
if (inputComponent instanceof TextComponent) {
labelForComponent = ((TextComponent) inputComponent).getField().getLabelForComponent();
} else if (inputComponent instanceof PickerComponent) {
labelForComponent = ((PickerComponent) inputComponent).getPicker().getLabelForComponent();
}
if (labelForComponent != null) {
final Label myLabel = labelForComponent;
final String originalText = myLabel.getText();
final String originalUIID = myLabel.getUIID();
final Constraint myConstraint = constraint;
final Runnable showError = new Runnable() {
@Override
public void run() {
boolean isValid = false;
if (inputComponent instanceof TextComponent) {
isValid = myConstraint.isValid(((TextComponent) inputComponent).getField().getText());
} else if (inputComponent instanceof PickerComponent) {
isValid = myConstraint.isValid(((PickerComponent) inputComponent).getPicker().getValue());
}
String errorMessage = trimLongString(UIManager.getInstance().localize(myConstraint.getDefaultFailMessage(), myConstraint.getDefaultFailMessage()), "ErrorLabel", myLabel.getWidth());
if (errorMessage != null && errorMessage.length() > 0 && !isValid) {
// show the error in place of the label for component
myLabel.setUIID("ErrorLabel");
myLabel.setText(errorMessage);
UITimer.timer(2000, false, Display.getInstance().getCurrent(), new Runnable() {
@Override
public void run() {
myLabel.setUIID(originalUIID);
myLabel.setText(originalText);
}
});
} else {
// show the label for component without the error
myLabel.setUIID(originalUIID);
myLabel.setText(originalText);
}
}
};
FocusListener myFocusListener = new FocusListener() {
@Override
public void focusLost(Component cmp) {
showError.run();
}
@Override
public void focusGained(Component cmp) {
// no code here
}
};
if (inputComponent instanceof TextComponent) {
((TextComponent) inputComponent).getField().addFocusListener(myFocusListener);
} else if (inputComponent instanceof PickerComponent) {
((PickerComponent) inputComponent).getPicker().addFocusListener(myFocusListener);
}
}
}
}
return this;
}
use of com.codename1.ui.validation.Validator in project CodenameOne by codenameone.
the class Validator method addSubmitButtons.
/**
* Submit buttons (or any other component type) can be disabled until all components contain a valid value.
* Notice that this method should be invoked after all the constraints are added so the initial state of the buttons
* will be correct.
*
* @param cmp set of buttons or components to disable until everything is valid
* @return the validator instance so this method can be chained
*/
public Validator addSubmitButtons(Component... cmp) {
boolean isV = isValid();
for (Component c : cmp) {
submitButtons.add(c);
c.setEnabled(isV);
}
return this;
}
Aggregations