pl.fhframework.model.forms.TableRowOptimized Maven / Gradle / Ivy
package pl.fhframework.model.forms;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import pl.fhframework.events.IEventSource;
import pl.fhframework.model.dto.ElementChanges;
import pl.fhframework.model.forms.optimized.ColumnOptimized;
import pl.fhframework.model.forms.optimized.TableCellOptimized;
import pl.fhframework.model.forms.optimized.TableOptimized;
import pl.fhframework.model.forms.table.LowLevelRowMetadata;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class TableRowOptimized {
@JsonIgnore
private TableOptimized table;
/**
* Table cells holds data stored/wrapped in visual components Structure acts as an indirect
* layer between model and presentation layer
*/
@Getter
private List tableCells = new ArrayList<>();
public TableRowOptimized(TableOptimized table, LowLevelRowMetadata lowLevelRowMetadata) {
this.table = table;
for (ColumnOptimized column : table.getColumns()) {
tableCells.addAll(column.createTableCells(lowLevelRowMetadata));
}
}
public void updateFormComponents(Set formComponentsChanges) {
for (FormElement tableCell : tableCells) {
if (tableCell instanceof TableCellOptimized) {
((TableCellOptimized) tableCell).doActionForEverySubcomponent((FormElement component) -> {
updateFromComponent(component, formComponentsChanges);
});
} else {
updateFromComponent(tableCell, formComponentsChanges);
}
}
}
// probably never used method - in future it should be removed
public IEventSource getEventSource(String elementId) {
final IEventSource[] result = new IEventSource[1];
for (FormElement tableCell : tableCells) {
if (tableCell instanceof TableCellOptimized) {
((TableCellOptimized) tableCell).doActionForEverySubcomponent((FormElement component) -> {
if (component.getId().equals(elementId))
result[0] = (IEventSource) component;
});
} else {
if (tableCell.getId().equals(elementId)) result[0] = (IEventSource) tableCell;
}
if (result[0] != null) break;
}
return result[0];
}
private void updateFromComponent(FormElement formElement, Set formComponentsChanges) {
ElementChanges elementChanges = formElement.updateView();
if (!elementChanges.getChangedAttributes().isEmpty()) {
formComponentsChanges.add(elementChanges);
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TableRowOptimized other = (TableRowOptimized) obj;
if (table == null) {
if (other.table != null) {
return false;
}
} else if (!table.equals(other.table)) {
return false;
}
if (tableCells == null) {
if (other.tableCells != null) {
return false;
}
} else if (!tableCells.equals(other.tableCells)) {
return false;
}
return true;
}
}