Author: tchemit Date: 2012-12-12 23:50:50 +0100 (Wed, 12 Dec 2012) New Revision: 54 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/54 Log: continue species batch screen Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchRowModel.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTableModel.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeModel.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeNode.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyTableModel.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyUIHandler.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesTabUIHandler.java Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchRowModel.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchRowModel.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchRowModel.java 2012-12-12 22:50:50 UTC (rev 54) @@ -366,12 +366,6 @@ computedNumber); } - //TODO Use validator - @Override - public boolean isValid() { - return species != null && weight != null; - } - public int getRowCount() { return frequency == null ? 0 : frequency.size(); } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTableModel.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTableModel.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTableModel.java 2012-12-12 22:50:50 UTC (rev 54) @@ -134,7 +134,11 @@ @Override protected SpeciesBatchRowModel createNewRow() { - return new SpeciesBatchRowModel(); + SpeciesBatchRowModel result = new SpeciesBatchRowModel(); + + // by default empty row is not valid + result.setValid(false); + return result; } @Override Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeModel.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeModel.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeModel.java 2012-12-12 22:50:50 UTC (rev 54) @@ -24,11 +24,16 @@ * #L% */ +import com.google.common.collect.HashMultimap; import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; import com.google.common.collect.Sets; +import fr.ifremer.tutti.persistence.entities.data.VracHorsVracEnum; import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; +import org.apache.commons.collections.CollectionUtils; import java.io.Serializable; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -67,14 +72,13 @@ * * @since 0.2 */ - protected final Map<SpeciesBatchTreeNode, SpeciesBatchRowModel> nodeToRow; + protected final Multimap<SpeciesBatchTreeNode, SpeciesBatchRowModel> nodeToRow; - public SpeciesBatchTreeModel(String... samplingOrder) { this.samplingOrder = samplingOrder; root = new SpeciesBatchTreeNode(); rowToNode = Maps.newHashMap(); - nodeToRow = Maps.newHashMap(); + nodeToRow = HashMultimap.create(); } public void populate(List<SpeciesBatchRowModel> rows) { @@ -85,7 +89,11 @@ SpeciesBatchTreeNode node = getSamplingNode(row); - nodeToRow.put(node, row); + // check if row is valid + boolean rowIsValid = isValid(row, node); + + // set it in row + row.setValid(rowIsValid); } } @@ -107,12 +115,11 @@ nodeToRow.clear(); } - public SpeciesBatchTreeNode removeNodeFromCache(SpeciesBatchRowModel row) { SpeciesBatchTreeNode result = rowToNode.remove(row); if (result != null) { - nodeToRow.remove(result); + nodeToRow.remove(result, row); } return result; } @@ -127,8 +134,17 @@ // not in cache must build the path result = getSamplingNode(root, 0, row); - // and add it in cache - rowToNode.put(row, result); + if (result == root) { + + // this means an empty row, so no node for it + result = null; + + } else { + + // found a row with a sampling, add it in cache + rowToNode.put(row, result); + nodeToRow.put(result, row); + } } return result; } @@ -173,16 +189,26 @@ Set<SpeciesBatchRowModel> childRows = Sets.newHashSet(); - SpeciesBatchRowModel superSamplingRow = nodeToRow.get(rootNode); + SpeciesBatchRowModel superSamplingRow = getNodeToRow(rootNode); + float totalWeight = 0f; for (int i = 0, nbChildren = rootNode.getChildCount(); i < nbChildren; i++) { SpeciesBatchTreeNode node = rootNode.getChildAt(i); - SpeciesBatchRowModel row = nodeToRow.get(node); + + SpeciesBatchRowModel row = getNodeToRow(node); if (row != null) { - childRows.add(row); - Float weight = row.getWeight(); - if (weight != null) { - totalWeight += weight; + VracHorsVracEnum vracHorsVrac = row.getVracHorsVrac(); + if (VracHorsVracEnum.HORS_VRAC.equals(vracHorsVrac)) { + + // never take account of a such child + + } else { + + childRows.add(row); + Float weight = row.getWeight(); + if (weight != null) { + totalWeight += weight; + } } } } @@ -193,6 +219,34 @@ return result; } + public SpeciesBatchRowModel getNodeToRow(SpeciesBatchTreeNode node) { + Collection<SpeciesBatchRowModel> rows = nodeToRow.get(node); + + SpeciesBatchRowModel result = null; + if (CollectionUtils.isNotEmpty(rows)) { + for (SpeciesBatchRowModel next : rows) { + if (next.isValid()) { + result = next; + break; + } + } + } + return result; + } + + public boolean isValid(SpeciesBatchRowModel row, SpeciesBatchTreeNode newNode) { + + // row must be on a sampling and having a weight + boolean result = row.getWeight() != null && newNode != null; + + // make sure there is not already another row for this node + if (result && CollectionUtils.size(nodeToRow.get(newNode)) > 1) { + result = false; + } + + return result; + } + public static class SamplingContext { private final SpeciesBatchTreeNode rootNode; @@ -228,5 +282,14 @@ public float getTotalWeight() { return totalWeight; } + + public void applyNewSampleValues(float samplingWeight, + Float samplingRatio) { + + for (SpeciesBatchRowModel row : samplingRows) { + row.setSampleWeight(samplingWeight); + row.setSamplingRatio(samplingRatio); + } + } } } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeNode.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeNode.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesBatchTreeNode.java 2012-12-12 22:50:50 UTC (rev 54) @@ -29,6 +29,7 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeNode; import java.io.Serializable; +import java.util.Arrays; /** * Defines a node of a species catches sampling. @@ -159,4 +160,16 @@ return (SpeciesBatchTreeNode) super.getPreviousLeaf(); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SpeciesBatchTreeNode)) return false; + + SpeciesBatchTreeNode that = (SpeciesBatchTreeNode) o; + + Object[] path = getUserObjectPath(); + Object[] path2 = that.getUserObjectPath(); + return Arrays.equals(path, path2); + } + } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyTableModel.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyTableModel.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyTableModel.java 2012-12-12 22:50:50 UTC (rev 54) @@ -93,6 +93,7 @@ } SpeciesFrequencyRowModel result = new SpeciesFrequencyRowModel(); result.setLengthStep(defaultStep); + result.setValid(false); return result; } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyUIHandler.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesFrequencyUIHandler.java 2012-12-12 22:50:50 UTC (rev 54) @@ -134,6 +134,18 @@ } } + @Override + protected void onRowValidStateChanged(SpeciesFrequencyRowModel row, + Boolean oldValue, + Boolean newValue) { + } + + @Override + protected void onRowModifyStateChanged(SpeciesFrequencyRowModel row, + Boolean oldValue, + Boolean newValue) { + } + //------------------------------------------------------------------------// //-- AbstractTuttiUIHandler methods --// //------------------------------------------------------------------------// Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesTabUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesTabUIHandler.java 2012-12-12 22:50:16 UTC (rev 53) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/species/SpeciesTabUIHandler.java 2012-12-12 22:50:50 UTC (rev 54) @@ -47,7 +47,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdesktop.swingx.JXTable; +import org.jdesktop.swingx.decorator.ComponentAdapter; import org.jdesktop.swingx.decorator.HighlightPredicate; +import org.jdesktop.swingx.decorator.Highlighter; import org.jdesktop.swingx.table.DefaultTableColumnModelExt; import org.nuiton.util.beans.BeanMonitor; import org.nuiton.util.decorator.Decorator; @@ -114,8 +116,8 @@ SpeciesBatchRowModel.PROPERTY_MATURITY, SpeciesBatchRowModel.PROPERTY_AGE, SpeciesBatchRowModel.PROPERTY_WEIGHT, - SpeciesBatchRowModel.PROPERTY_SAMPLE_WEIGHT, - SpeciesBatchRowModel.PROPERTY_SAMPLING_RATIO, +// SpeciesBatchRowModel.PROPERTY_SAMPLE_WEIGHT, +// SpeciesBatchRowModel.PROPERTY_SAMPLING_RATIO, SpeciesBatchRowModel.PROPERTY_COMMENT, SpeciesBatchRowModel.PROPERTY_FREQUENCY); this.ui = ui; @@ -156,6 +158,15 @@ } @Override + protected String[] getRowPropertiesToIgnore() { + return new String[]{ + SpeciesBatchRowModel.PROPERTY_SAMPLE_WEIGHT, + SpeciesBatchRowModel.PROPERTY_SAMPLING_RATIO, + SpeciesBatchRowModel.PROPERTY_FREQUENCY + }; + } + + @Override protected TableColumnModel createTableColumnModel() { List<String> samplingOrder = getModel().getSamplingOrder(); @@ -318,9 +329,8 @@ SpeciesBatchTreeModel samplingTreeModel = model.getSamplingTreeModel(); - if (SAMPLING_PROPERTIES.contains(propertyName) && row.isValid()) { + if (SAMPLING_PROPERTIES.contains(propertyName)) { - // Need to rebuilt this row sampling tree path (and then recompute // old super - samplingRatio and new super - samplingRatio) @@ -328,23 +338,86 @@ // and remove it from any cache SpeciesBatchTreeNode oldNode = samplingTreeModel.removeNodeFromCache(row); + boolean rowWasValid = row.isValid(); + if (oldNode != null && rowWasValid) { + + // remove this row from his super sampling + recomputeSuperSamplingRatio(samplingTreeModel, oldNode); + } + // get new sampling node SpeciesBatchTreeNode newNode = samplingTreeModel.getSamplingNode(row); - moveSamplingNode(samplingTreeModel, row, oldNode, newNode); + // check this row is valid + boolean rowValid = samplingTreeModel.isValid(row, newNode); + + // push this state back to the row + row.setValid(rowValid); + + if (rowValid) { + + // can add it to his super-sampling + recomputeSuperSamplingRatio(samplingTreeModel, newNode); + } else { + + if (rowWasValid) { + + row.setSampleWeight(null); + row.setSamplingRatio(null); + + getTableModel().updateSamplingRatio(Sets.newHashSet(row)); + } + } } - if (SpeciesBatchRowModel.PROPERTY_WEIGHT.equals(propertyName) && - row.isValid()) { + if (SpeciesBatchRowModel.PROPERTY_WEIGHT.equals(propertyName)) { // Need to recompute the super - samplingRatio SpeciesBatchTreeNode node = samplingTreeModel.getSamplingNode(row); - recomputeSuperSamplingRatio(samplingTreeModel, row, node); + boolean rowWasValid = row.isValid(); + + // check this row is valid + boolean rowValid = samplingTreeModel.isValid(row, node); + + // push this state back to the row + row.setValid(rowValid); + + if (rowValid) { + recomputeSuperSamplingRatio(samplingTreeModel, node); + } else { + if (rowWasValid) { + + // must remove this row from his super-sampling + recomputeSuperSamplingRatio(samplingTreeModel, node); + + row.setSampleWeight(null); + row.setSamplingRatio(null); + + getTableModel().updateSamplingRatio(Sets.newHashSet(row)); + } + } } } + @Override + protected void onRowValidStateChanged(SpeciesBatchRowModel row, + Boolean oldValue, + Boolean newValue) { + int rowIndex = getTableModel().getRowIndex(row); + + if (rowIndex > -1) { + getTableModel().fireTableRowsUpdated(rowIndex, rowIndex); + } + } + + @Override + protected void onRowModifyStateChanged(SpeciesBatchRowModel row, + Boolean oldValue, + Boolean newValue) { + } + //------------------------------------------------------------------------// //-- AbstractTuttiUIHandler methods --// //------------------------------------------------------------------------// @@ -396,9 +469,27 @@ table.getTableHeader().setReorderingAllowed(false); - table.addHighlighter(TuttiUIUtil.newBackgroundColorHighlighter( - HighlightPredicate.READ_ONLY, Color.LIGHT_GRAY)); + Highlighter readOnlyHighlighter = TuttiUIUtil.newBackgroundColorHighlighter( + HighlightPredicate.READ_ONLY, Color.LIGHT_GRAY); + + table.addHighlighter(readOnlyHighlighter); + Highlighter validHighlighter = TuttiUIUtil.newBackgroundColorHighlighter( + new HighlightPredicate.AndHighlightPredicate(HighlightPredicate.EDITABLE, new HighlightPredicate() { + @Override + public boolean isHighlighted(Component renderer, ComponentAdapter adapter) { + + boolean result = false; + if (adapter.isEditable()) { + int rowIndex = adapter.convertRowIndexToModel(adapter.row); + SpeciesBatchRowModel row = getTableModel().getEntry(rowIndex); + result = !row.isValid(); + } + return result; + } + }), Color.RED); + table.addHighlighter(validHighlighter); + // when model datas change let's propagate it table model listenRowsFromModel(); @@ -443,7 +534,9 @@ if (parentContainer == null) { // out of the table can save - saveSelectedRowIfRequired(); + if (getModel().getFishingOperation()!=null) { + saveSelectedRowIfRequired(); + } } } @@ -517,23 +610,7 @@ //-- Internal methods --// //------------------------------------------------------------------------// - protected void moveSamplingNode(SpeciesBatchTreeModel samplingTreeModel, - SpeciesBatchRowModel row, - SpeciesBatchTreeNode oldNode, - SpeciesBatchTreeNode newNode) { - - if (oldNode != null) { - - recomputeSuperSamplingRatio(samplingTreeModel, row, oldNode); - - } - - recomputeSuperSamplingRatio(samplingTreeModel, row, newNode); - - } - protected void recomputeSuperSamplingRatio(SpeciesBatchTreeModel samplingTreeModel, - SpeciesBatchRowModel row, SpeciesBatchTreeNode node) { SpeciesBatchTreeNode superSamplingNode = node.getParent(); @@ -571,8 +648,10 @@ log.info("Sampling ratio: " + samplingRatio); } + samplingContext.applyNewSampleValues(samplingTotalWeight, + samplingRatio); + getTableModel().updateSamplingRatio(samplingContext.getSamplingRows()); - } @@ -613,21 +692,36 @@ TuttiBeanMonitor<SpeciesBatchRowModel> rowMonitor = getRowMonitor(); SpeciesBatchRowModel bean = rowMonitor.getBean(); - if (bean != null && bean.isValid()) { + if (bean != null) { - // there is a valid bean attached to the monitor + if (bean.isValid()) { + // there is a valid bean attached to the monitor - if (rowMonitor.wasModified()) { + if (rowMonitor.wasModified()) { - // monitored bean was modified, save it - if (log.isInfoEnabled()) { - log.info("Row " + bean + " was modified, will save it"); + // monitored bean was modified, save it + if (log.isInfoEnabled()) { + log.info("Row " + bean + " was modified, will save it"); + } + + saveRow(bean); + + // clear modified flag on the monitor + rowMonitor.clearModified(); } + } else { - saveRow(bean); + // row is not valid can not save it - // clear modified flag on the monitor - rowMonitor.clearModified(); + SpeciesBatch catchBean = bean.toBean(); + + FishingOperation fishingOperation = getModel().getFishingOperation(); + + if (!TuttiEntities.isNew(catchBean)) { + + // remove this + persistenceService.deleteSpeciesBatch(catchBean.getId()); + } } } }