fr.ird.observe.client.util.table.EditableTableWithCacheTableModelSupport Maven / Gradle / Ivy
package fr.ird.observe.client.util.table;
/*
* #%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 com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import fr.ird.observe.dto.IdDto;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.nuiton.util.beans.Binder;
import org.nuiton.util.beans.BinderFactory;
/**
* Created on 12/11/14.
*
* @author Tony Chemit - [email protected]
* @since 3.10
*/
public abstract class EditableTableWithCacheTableModelSupport extends EditableTableModelSupport {
private static final long serialVersionUID = 1L;
/**
* Cache de liste de données par numéro de ligne. Cela sert à pouvoir conserver plusieurs listes
* de données issues de ce table. Typiquement dans l'écran de capture ll ou on doit conserver pour une capture
* (donc une ligne du tableau principal) n mesures de taille et n meausre de poids.
*/
protected final Map> cacheByRow;
protected EditableTableWithCacheTableModelSupport() {
super(true);
this.cacheByRow = new TreeMap<>();
}
public List getCacheForRow(int rowIndex) {
EditableList measures = cacheByRow.get(rowIndex);
return measures == null ? null : measures.getData();
}
public void initCacheForRow(int editingRow, List data) {
EditableList editableList = cacheByRow.get(editingRow);
Preconditions.checkState(editableList == null, "Cant have a list for row: " + editingRow);
editableList = new EditableList<>();
// Get a copy of the list (to avoid to edit the content of the list)
List original = copyList(data);
editableList.setOriginal(original);
editableList.setData(data);
cacheByRow.put(editingRow, editableList);
}
public void storeInCacheForRow(int editingRow) {
List measures = getData();
EditableList editableList = cacheByRow.get(editingRow);
Preconditions.checkState(editableList != null, "No list found for row: " + editingRow);
editableList.setData(measures);
}
public void removeCacheForRow(int rowToDelete) {
cacheByRow.remove(rowToDelete);
List rows = Lists.newArrayList(getCacheRowsChanged());
Collections.sort(rows);
for (Integer row : rows) {
if (row > rowToDelete) {
// set now to row -1
EditableList remove = cacheByRow.remove(row);
cacheByRow.put(row - 1, remove);
}
}
}
public void resetCacheForRow(int rowtoReset) {
EditableList editableList = cacheByRow.get(rowtoReset);
Preconditions.checkState(editableList != null);
// remove data from cache
editableList.reset();
// restore a copy of original data
List newData = copyList(editableList.getOriginal());
editableList.setData(newData);
setData(newData);
validate();
}
public Set getCacheRowsChanged() {
return cacheByRow.keySet();
}
@Override
public void clear() {
cacheByRow.clear();
super.clear();
}
protected List copyList(List data) {
List copy = new ArrayList<>(data.size());
for (E measure : data) {
E originalMeasure = createNewRow();
Class sourceType = (Class) originalMeasure.getClass();
Binder binder = BinderFactory.newBinder(sourceType);
binder.copy(measure, originalMeasure);
copy.add(originalMeasure);
}
return copy;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy