r409 - in trunk/tutti-ui-swing/src/main: java/fr/ifremer/tutti/ui/swing java/fr/ifremer/tutti/ui/swing/content/operation/catches java/fr/ifremer/tutti/ui/swing/content/operation/catches/species java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split java/fr/ifremer/tutti/ui/swing/util/editor resources/i18n
Author: kmorin Date: 2013-02-13 19:24:23 +0100 (Wed, 13 Feb 2013) New Revision: 409 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/409 Log: continue weight computing Added: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/ComputeWeightsAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/editor/TuttiComputedOrNotDataEditor.java Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUI.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.css trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchRowModel.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchTableModel.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.css trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.jaxx trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/CreateSpeciesBatchUI.css trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/SplitSpeciesBatchUIHandler.java trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUI.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUI.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUI.java 2013-02-13 18:24:23 UTC (rev 409) @@ -117,6 +117,14 @@ * @since 0.1 */ public static final String DECIMAL3_PATTERN = "\\d{0,6}|\\d{1,6}.\\d{0,3}"; + + /** + * Pattern to use for decimal numeric values with 3 decimal digits in + * editors. + * + * @since 0.1 + */ + public static final String DECIMALFORCE3_PATTERN = "\\d{0,6}|\\d{1,6}.\\d{3}"; /** * Pattern to use for decimal numeric values with 2 digits + 3 decimal digits in Added: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/ComputeWeightsAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/ComputeWeightsAction.java (rev 0) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/ComputeWeightsAction.java 2013-02-13 18:24:23 UTC (rev 409) @@ -0,0 +1,190 @@ + +package fr.ifremer.tutti.ui.swing.content.operation.catches; + +import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.ui.swing.AbstractTuttiAction; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SampleCategory; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchRowModel; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchUI; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyRowModel; +import java.awt.event.ActionEvent; +import java.util.List; +import javax.swing.JOptionPane; +import org.apache.commons.collections.CollectionUtils; + +import static org.nuiton.i18n.I18n._; + +/** + * + * @author kmorin <kmorin@codelutin.com> + * @since 1.0 + */ +public class ComputeWeightsAction extends AbstractTuttiAction<EditCatchesUIModel, EditCatchesUI, EditCatchesUIHandler> { + + private static final long serialVersionUID = 1L; + + public ComputeWeightsAction(EditCatchesUIHandler handler) { + super(handler, + "computeWeights", + "generate", + _("tutti.action.computeWeights"), + _("tutti.action.computeWeights.tip"), + true); + } + + @Override + protected void doAction(ActionEvent event) throws Exception { + computeSpeciesBatches(); + } + + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected void computeSpeciesBatches() { + PersistenceService persistenceService = + getService(PersistenceService.class); + + Float totalSortedWeight = 0f; + Float totalUnsortedWeight = 0f; + SpeciesBatchUI ui = getUI().getSpeciesTabContent(); + List<SpeciesBatchRowModel> roots = getUI().getSpeciesTabContent().getModel().getRows(); + for (SpeciesBatchRowModel row : roots) { + if (row.isBatchRoot()) { + Float weight = computeSpeciesBatch(row); + if (weight == null) { + JOptionPane.showMessageDialog( + ui, + _("tutti.dialog.catches.species.computeWeight.error.message"), + _("tutti.dialog.catches.species.computeWeight.error.title"), + JOptionPane.ERROR_MESSAGE); + totalSortedWeight = null; + totalUnsortedWeight = null; + break; + } + if (persistenceService.isSortedQualitativeValue(row.getSortedUnsortedCategory().getCategoryValue())) { + totalSortedWeight += weight; + } else { + totalUnsortedWeight += weight; + } + } + } + Number inertWeight = ui.getSpeciesTotalInertWeightField().getModel(); + if (inertWeight != null) { + totalSortedWeight += inertWeight.floatValue(); + } + Number livingNotItemizedWeight = ui.getSpeciesTotalLivingNotItemizedWeightField().getModel(); + if (livingNotItemizedWeight != null) { + totalSortedWeight += livingNotItemizedWeight.floatValue(); + } + + ui.getSpeciesTotalSampleSortedWeightField() + .setText(totalSortedWeight != null ? totalSortedWeight.toString() : ""); + + if (ui.getSpeciesTotalSortedWeightField().getModel() == null) { + ui.getSpeciesTotalSortedWeightField().setModel(totalSortedWeight); + } + + ui.getSpeciesTotalUnsortedWeightField() + .setText(totalUnsortedWeight != null ? totalUnsortedWeight.toString() : ""); + } + + protected Float computeSpeciesBatch(SpeciesBatchRowModel row) { + SampleCategory finestCategory; + if (row.getAgeCategory().isValid()) { + finestCategory = row.getAgeCategory(); + + } else if (row.getMaturityCategory().isValid()) { + finestCategory = row.getMaturityCategory(); + + } else if (row.getSexCategory().isValid()) { + finestCategory = row.getSexCategory(); + + } else if (row.getSizeCategory().isValid()) { + finestCategory = row.getSizeCategory(); + + } else { + finestCategory = row.getSortedUnsortedCategory(); + } + + Float result = null; + Float categoryWeight = finestCategory.getCategoryWeight(); + Float rowWeight = row.getWeight(); + + List<SpeciesBatchRowModel> children = row.getChildBatch(); + // if the row is not a leaf + if (!row.isBatchLeaf()) { + Float sum = 0f; + // make the sum of the children weights + for (SpeciesBatchRowModel child : children) { + Float weight = computeSpeciesBatch(child); + if (weight == null) { + sum = null; + break; + } + sum += weight; + } + + if (sum != null) { + if (categoryWeight == null) { + finestCategory.setComputedWeight(sum); + } else if (categoryWeight != sum) { + // handle error + } + result = sum; + } + + } else {// the row is a leaf + List<SpeciesFrequencyRowModel> frequency = row.getFrequency(); + + if (CollectionUtils.isNotEmpty(frequency)) { + // if there are frequencies, then compute their weight + Float frequencyWeight = 0f; + for (SpeciesFrequencyRowModel frequencyModel : frequency) { + Float w = frequencyModel.getWeight(); + if (w == null) { + + // can't sum when a null value appears + frequencyWeight = null; + break; + + } else if (frequencyWeight != null) { + + // still can sum weights + frequencyWeight += w; + } + } + + if (categoryWeight == null && rowWeight != null) { + //TODO handle error + + } else if (categoryWeight == null && frequencyWeight != null) { + // if the category weight is null and the frequencies have a weight, + // then this weight is the result + finestCategory.setComputedWeight(frequencyWeight); + result = frequencyWeight; + + } else if (frequencyWeight != null && frequencyWeight != categoryWeight) { + // if the weight of the frequencies is different from the category + // weight, then set the weight of the sample + if (rowWeight == null) { + row.setComputedWeight(frequencyWeight); + + } else if (rowWeight != frequencyWeight) { + //TODO handle error + } + result = categoryWeight; + + + } else { + result = categoryWeight; + } + + } else { + result = categoryWeight; + } + } + + return result; + } +} Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.css =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.css 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.css 2013-02-13 18:24:23 UTC (rev 409) @@ -259,4 +259,8 @@ #cancelButton { _tuttiAction: {CancelEditCatchBatchAction.class}; +} + +#computeSpeciesBatchButton { + _tuttiAction: {ComputeWeightsAction.class}; } \ No newline at end of file Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx 2013-02-13 18:24:23 UTC (rev 409) @@ -350,6 +350,7 @@ constraints='BorderLayout.CENTER'> <JButton id='cancelButton'/> <JButton id='saveButton'/> + <JButton id='computeSpeciesBatchButton'/> </JPanel> </JPanel> Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java 2013-02-13 18:24:23 UTC (rev 409) @@ -92,7 +92,7 @@ if (!selectedBatch.isBatchRoot()) { // remove all sub batches of his parent - SpeciesBatchRowModel parentBatch = selectedBatch.getBatchParent(); + SpeciesBatchRowModel parentBatch = selectedBatch.getParentBatch(); // get parent row index int parentIndex = tableModel.getRowIndex(parentBatch); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java 2013-02-13 18:24:23 UTC (rev 409) @@ -104,7 +104,7 @@ getModel().getRows().removeAll(rowToRemove); // remove childs from parent batch - parentBatch.setBatchChilds(null); + parentBatch.setChildBatch(null); // refresh table from parent batch row index to the end handler.getTableModel().fireTableDataChanged(); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java 2013-02-13 18:24:23 UTC (rev 409) @@ -116,7 +116,7 @@ protected void changeChildrenSpecies(SpeciesBatchRowModel row, Species species) { row.setSpecies(species); - List<SpeciesBatchRowModel> children = row.getBatchChild(); + List<SpeciesBatchRowModel> children = row.getChildBatch(); if (children != null) { for (SpeciesBatchRowModel child : children) { changeChildrenSpecies(child, species); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchRowModel.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchRowModel.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchRowModel.java 2013-02-13 18:24:23 UTC (rev 409) @@ -96,12 +96,14 @@ public static final String PROPERTY_FREQUENCY = "frequency"; public static final String PROPERTY_COMPUTED_NUMBER = "computedOrNotNumber"; + + public static final String PROPERTY_COMPUTED_WEIGHT = "computedOrNotWeight"; public static final String PROPERTY_ATTACHMENTS = "attachments"; - public static final String PROPERTY_BATCH_PARENT = "batchParent"; + public static final String PROPERTY_PARENT_BATCH = "parentBatch"; - public static final String PROPERTY_BATCH_CHILD = "batchChild"; + public static final String PROPERTY_CHILD_BATCH = "childBatch"; public static final String PROPERTY_BATCH_LEAF = "batchLeaf"; @@ -173,7 +175,8 @@ * * @since 0.2 */ - protected Float weight; + protected TuttiComputedOrNotData<Float> computedOrNotWeight = + new TuttiComputedOrNotData<Float>(); /** * Total computed number (from frequencies). @@ -210,7 +213,7 @@ * @see #isBatchRoot() * @since 0.3 */ - protected SpeciesBatchRowModel batchParent; + protected SpeciesBatchRowModel parentBatch; /** * List of child batches (can be null or empty if batch is a leaf). @@ -218,7 +221,7 @@ * @see #isBatchLeaf() * @since 0.3 */ - protected List<SpeciesBatchRowModel> batchChild; + protected List<SpeciesBatchRowModel> childBatch; protected static final Binder<SpeciesBatch, SpeciesBatchRowModel> fromBeanBinder = BinderFactory.newBinder(SpeciesBatch.class, @@ -463,34 +466,34 @@ //-- Navigation properties --// //------------------------------------------------------------------------// - public SpeciesBatchRowModel getBatchParent() { - return batchParent; + public SpeciesBatchRowModel getParentBatch() { + return parentBatch; } - public void setBatchParent(SpeciesBatchRowModel batchParent) { - Object oldValue = getBatchParent(); - this.batchParent = batchParent; - firePropertyChange(PROPERTY_BATCH_PARENT, oldValue, batchParent); + public void setParentBatch(SpeciesBatchRowModel parentBatch) { + Object oldValue = getParentBatch(); + this.parentBatch = parentBatch; + firePropertyChange(PROPERTY_PARENT_BATCH, oldValue, parentBatch); firePropertyChange(PROPERTY_BATCH_ROOT, null, isBatchRoot()); } - public List<SpeciesBatchRowModel> getBatchChild() { - return batchChild; + public List<SpeciesBatchRowModel> getChildBatch() { + return childBatch; } - public void setBatchChilds(List<SpeciesBatchRowModel> batchChild) { - this.batchChild = batchChild; + public void setChildBatch(List<SpeciesBatchRowModel> childBatch) { + this.childBatch = childBatch; // force to propagate child changes - firePropertyChange(PROPERTY_BATCH_CHILD, null, batchChild); + firePropertyChange(PROPERTY_CHILD_BATCH, null, childBatch); firePropertyChange(PROPERTY_BATCH_LEAF, null, isBatchLeaf()); } public boolean isBatchLeaf() { - return CollectionUtils.isEmpty(batchChild); + return CollectionUtils.isEmpty(childBatch); } public boolean isBatchRoot() { - return batchParent == null; + return parentBatch == null; } //------------------------------------------------------------------------// @@ -508,12 +511,12 @@ } public Float getWeight() { - return weight; + return computedOrNotWeight.getData(); } public void setWeight(Float weight) { Object oldValue = getWeight(); - this.weight = weight; + this.computedOrNotWeight.setData(weight); firePropertyChange(PROPERTY_WEIGHT, oldValue, weight); } @@ -567,7 +570,18 @@ firePropertyChange(PROPERTY_COMPUTED_NUMBER, oldValue, computedNumber); } + + public Float getComputedWeight() { + return computedOrNotWeight.getComputedData(); + } + public void setComputedWeight(Float computedWeight) { + Object oldValue = getComputedWeight(); + computedOrNotWeight.setComputedData(computedWeight); + firePropertyChange(PROPERTY_COMPUTED_WEIGHT, oldValue, + computedWeight); + } + public TuttiComputedOrNotData<Integer> getComputedOrNotNumber() { return computedOrNotNumber; } @@ -576,4 +590,12 @@ this.computedOrNotNumber = computedOrNotNumber; } + public TuttiComputedOrNotData<Float> getComputedOrNotWeight() { + return computedOrNotWeight; + } + + public void setComputedOrNotWeight(TuttiComputedOrNotData<Float> computedOrNotWeight) { + this.computedOrNotWeight = computedOrNotWeight; + } + } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchTableModel.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchTableModel.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchTableModel.java 2013-02-13 18:24:23 UTC (rev 409) @@ -33,6 +33,7 @@ import javax.swing.table.TableColumnModel; import java.util.Set; +import org.apache.commons.logging.LogFactory; import static org.nuiton.i18n.I18n.n_; @@ -125,7 +126,7 @@ n_("tutti.table.species.batch.header.ageCategory")); public static final ColumnIdentifier<SpeciesBatchRowModel> WEIGHT = ColumnIdentifier.newId( - SpeciesBatchRowModel.PROPERTY_WEIGHT, + SpeciesBatchRowModel.PROPERTY_COMPUTED_WEIGHT, n_("tutti.table.species.batch.header.weight"), n_("tutti.table.species.batch.header.weight")); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.css =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.css 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.css 2013-02-13 18:24:23 UTC (rev 409) @@ -37,7 +37,8 @@ #speciesTotalWeightField { property: speciesTotalWeight; model: {model.getSpeciesTotalWeight()}; - numberPattern: {INT_6_DIGITS_PATTERN}; + numberPattern: {DECIMALFORCE3_PATTERN}; + useFloat: true; } #speciesTotalSortedWeightLabel { @@ -48,7 +49,8 @@ #speciesTotalSortedWeightField { property: speciesTotalSortedWeight; model: {model.getSpeciesTotalSortedWeight()}; - numberPattern: {INT_6_DIGITS_PATTERN}; + numberPattern: {DECIMALFORCE3_PATTERN}; + useFloat: true; } #speciesTotalSampleSortedWeightLabel { @@ -81,7 +83,8 @@ #speciesTotalInertWeightField { property: speciesTotalInertWeight; model: {model.getSpeciesTotalInertWeight()}; - numberPattern: {INT_6_DIGITS_PATTERN}; + numberPattern: {DECIMALFORCE3_PATTERN}; + useFloat: true; } #speciesTotalLivingNotItemizedWeightLabel { @@ -92,7 +95,8 @@ #speciesTotalLivingNotItemizedWeightField { property: speciesTotalLivingNotItemizedWeight; model: {model.getSpeciesTotalLivingNotItemizedWeight()}; - numberPattern: {INT_6_DIGITS_PATTERN}; + numberPattern: {DECIMALFORCE3_PATTERN}; + useFloat: true; } #tablePopup { @@ -138,12 +142,6 @@ enabled: {model.isCreateSpeciesBatchEnabled()}; } -#computeSpeciesBatchButton { - actionIcon: generate; - mnemonic: E; - text: "tutti.action.computeSpeciesBatch"; -} - #filterTablePane { border: {new TitledBorder(null, "")}; } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.jaxx =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.jaxx 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUI.jaxx 2013-02-13 18:24:23 UTC (rev 409) @@ -156,9 +156,6 @@ <JRadioButton id='filterSpeciesBatchRootButton' onActionPerformed='model.setTableViewMode(TableViewMode.ROOT)'/> </JPanel> - <JButton id='computeSpeciesBatchButton' - constraints='BorderLayout.EAST' - onActionPerformed='handler.computeSpeciesBatch()'/> </JPanel> </cell> </row> Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java 2013-02-13 18:24:23 UTC (rev 409) @@ -41,6 +41,7 @@ import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.service.DecoratorService; +import fr.ifremer.tutti.ui.swing.AbstractTuttiAction; import fr.ifremer.tutti.ui.swing.TuttiUI; import fr.ifremer.tutti.ui.swing.content.operation.AbstractTuttiBatchTableUIHandler; import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI; @@ -57,6 +58,7 @@ import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; import fr.ifremer.tutti.ui.swing.util.editor.AttachmentCellComponent; import fr.ifremer.tutti.ui.swing.util.editor.LongTextCellComponent; +import fr.ifremer.tutti.ui.swing.util.editor.TuttiComputedOrNotDataEditor; import fr.ifremer.tutti.ui.swing.util.table.ColumnIdentifier; import jaxx.runtime.JAXXUtil; import jaxx.runtime.SwingUtil; @@ -87,6 +89,10 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.swing.border.LineBorder; +import jaxx.runtime.swing.JAXXWidgetUtil; +import jaxx.runtime.swing.editor.cell.NumberCellEditor; +import org.apache.commons.lang.reflect.ConstructorUtils; import static org.nuiton.i18n.I18n._; import static org.nuiton.i18n.I18n.n_; @@ -422,7 +428,7 @@ } List<Species> speciesList = persistenceService.getAllSpecies(protocol); - + SPECIES_UNIVERSE_CONTEXT_ENTRY.setContextValue(ui, speciesList); FREQUENCY_LENGTH_CONTEXT_ENTRY.setContextValue(ui, lengthCaracterics); } @@ -586,9 +592,16 @@ { // Weight column - addFloatColumnToModel(columnModel, - SpeciesBatchTableModel.WEIGHT, - TuttiUI.DECIMAL3_PATTERN); + TuttiComputedOrNotDataEditor<Float> editor = + new TuttiComputedOrNotDataEditor(Float.class, false); + editor.getNumberEditor().setSelectAllTextOnError(true); + editor.getNumberEditor().getTextField().setBorder(new LineBorder(Color.GRAY, 2)); + editor.getNumberEditor().setNumberPattern(TuttiUI.DECIMALFORCE3_PATTERN); + + addColumnToModel(columnModel, + editor, + null, + SpeciesBatchTableModel.WEIGHT); } { // Number column (from frequencies) @@ -809,7 +822,7 @@ } // add new batches to his parent - parentBatch.setBatchChilds(newBatches); + parentBatch.setChildBatch(newBatches); //TODO Should only save parentBatch (will persist all his childs) //saveRow(parentBatch); @@ -829,128 +842,20 @@ } public void updateTotalFromFrequencies(SpeciesBatchRowModel row) { - Float totalWeight = null; List<SpeciesFrequencyRowModel> frequency = row.getFrequency(); if (CollectionUtils.isNotEmpty(frequency)) { Integer totalNumber = 0; - totalWeight = 0f; for (SpeciesFrequencyRowModel frequencyModel : frequency) { - totalNumber += frequencyModel.getNumber(); - Float w = frequencyModel.getWeight(); - if (w == null) { - - // can't sum when a null value appears - totalWeight = null; - } else if (totalWeight != null) { - - // still can sum weights - totalWeight += w; + if (frequencyModel.getNumber() != null) { + totalNumber += frequencyModel.getNumber(); } } row.setComputedNumber(totalNumber); } - SampleCategory finestCategory; - if (row.getAgeCategory().isValid()) { - finestCategory = row.getAgeCategory(); - - } else if (row.getMaturityCategory().isValid()) { - finestCategory = row.getMaturityCategory(); - - } else if (row.getSexCategory().isValid()) { - finestCategory = row.getSexCategory(); - - } else if (row.getSizeCategory().isValid()) { - finestCategory = row.getSizeCategory(); - - } else { - finestCategory = row.getSortedUnsortedCategory(); - } - if (finestCategory.getCategoryWeight() == null) { - finestCategory.setComputedWeight(totalWeight); - } - } - public void computeSpeciesBatch() { - Float totalSortedWeight = 0f; - Float totalUnsortedWeight = 0f; - List<SpeciesBatchRowModel> roots = getModel().getRows(); - for (SpeciesBatchRowModel row : roots) { - if (row.isBatchRoot()) { - Float weight = computeSpeciesBatch(row); - if (weight == null) { - JOptionPane.showMessageDialog( - ui, - _("tutti.dialog.catches.species.computeWeight.error.message"), - _("tutti.dialog.catches.species.computeWeight.error.title"), - JOptionPane.ERROR_MESSAGE); - totalSortedWeight = null; - totalUnsortedWeight = null; - break; - } - if (persistenceService.isSortedQualitativeValue(row.getSortedUnsortedCategory().getCategoryValue())) { - totalSortedWeight += weight; - } else { - totalUnsortedWeight += weight; - } - } - } - ui.getSpeciesTotalSampleSortedWeightField() - .setText(totalSortedWeight != null ? totalSortedWeight.toString() : ""); - ui.getSpeciesTotalUnsortedWeightField() - .setText(totalUnsortedWeight != null ? totalUnsortedWeight.toString() : ""); - } - - //------------------------------------------------------------------------// - //-- Internal methods --// - //------------------------------------------------------------------------// - - protected Float computeSpeciesBatch(SpeciesBatchRowModel row) { - Float sum = null; - List<SpeciesBatchRowModel> children = row.getBatchChild(); - if (!row.isBatchLeaf()) { - for (SpeciesBatchRowModel child : children) { - Float weight = computeSpeciesBatch(child); - if (weight == null) { - return null; - } - sum += weight; - } - } - - SampleCategory finestCategory; - if (row.getAgeCategory().isValid()) { - finestCategory = row.getAgeCategory(); - - } else if (row.getMaturityCategory().isValid()) { - finestCategory = row.getMaturityCategory(); - - } else if (row.getSexCategory().isValid()) { - finestCategory = row.getSexCategory(); - - } else if (row.getSizeCategory().isValid()) { - finestCategory = row.getSizeCategory(); - - } else { - finestCategory = row.getSortedUnsortedCategory(); - } - Float weight = finestCategory.getCategoryWeight(); - if (weight == null) { - weight = finestCategory.getComputedWeight(); - } - Float result; - if (sum != null && weight == null) { - finestCategory.setComputedWeight(sum); - result = sum; - - } else { - result = weight; - } - return result; - } - protected void saveRows(Iterable<SpeciesBatchRowModel> rows) { for (SpeciesBatchRowModel row : rows) { saveRow(row); @@ -970,6 +875,11 @@ SpeciesBatch catchBean = row.toBean(); catchBean.setFishingOperation(fishingOperation); + + SpeciesBatchRowModel parent = row.getParentBatch(); + if (parent != null) { + catchBean.setParentBatch(parent.toBean()); + } // apply sample category catchBean.setSampleCategoryType(sampleCategory.getCategoryType().getType()); @@ -978,7 +888,7 @@ if (TuttiEntities.isNew(catchBean)) { - SpeciesBatchRowModel batchParent = row.getBatchParent(); + SpeciesBatchRowModel batchParent = row.getParentBatch(); String parentBatchId = null; if (batchParent != null) { @@ -994,7 +904,7 @@ row.setId(catchBean.getId()); } else { if (log.isInfoEnabled()) { - log.info("Persist existing species batch: " + catchBean.getId()); + log.info("Persist existing species batch: " + catchBean.getId() + " (parent : " + catchBean.getParentBatch() + ")"); } persistenceService.saveSpeciesBatch(catchBean); } @@ -1122,7 +1032,7 @@ if (!row.isBatchLeaf()) { - for (SpeciesBatchRowModel batchChild : row.getBatchChild()) { + for (SpeciesBatchRowModel batchChild : row.getChildBatch()) { collectedRows.add(batchChild); collectChilds(batchChild, collectedRows); } @@ -1165,7 +1075,7 @@ SpeciesBatchRowModel childRow = loadSpeciesBatch(childBatch, newRow, rows); batchChilds.add(childRow); } - newRow.setBatchChilds(batchChilds); + newRow.setChildBatch(batchChilds); } return newRow; @@ -1194,7 +1104,7 @@ newRow.setSpecies(parentRow.getSpecies()); newRow.setSpeciesToConfirm(parentRow.getSpeciesToConfirm()); - newRow.setBatchParent(parentRow); + newRow.setParentBatch(parentRow); newRow.setSpecies(parentRow.getSpecies()); if (sampleCategoryEnum != SampleCategoryEnum.sortedUnsorted) { Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java 2013-02-13 18:24:23 UTC (rev 409) @@ -173,11 +173,9 @@ for (SpeciesProtocol sp : protocol.getSpecies()) { speciesProtocol.put(sp.getSpeciesReferenceTaxonId(), sp); } -// speciesProtocol = TuttiEntities.splitById(protocol.getSpecies()); } - - //TODO Use protocol to have lengthStepCaracteristic to use (if any protocol) + initBeanComboBox(ui.getLengthStepCaracteristicComboBox(), lengthStepCaracterics, model.getLengthStepCaracteristic()); @@ -239,7 +237,7 @@ addFloatColumnToModel(columnModel, SpeciesFrequencyTableModel.WEIGHT, - TuttiUI.DECIMAL3_PATTERN); + TuttiUI.DECIMALFORCE3_PATTERN); } // create table model @@ -251,10 +249,10 @@ table.setModel(tableModel); table.setColumnModel(columnModel); - installTableKeyListener(columnModel, table); - initTable(table); + installTableKeyListener(columnModel, table); + listenValidatorValid(ui.getValidator(), model); } @@ -413,32 +411,29 @@ lengthStep); } } - } else { + } + + if (lengthStepCaracteristic == null && protocol != null) { - // not frequency, guess from protocol (if any) + Species species = speciesBatch.getSpecies(); - if (protocol != null) { + SpeciesProtocol sProtocol = + speciesProtocol.get(species.getReferenceTaxonId()); - Species species = speciesBatch.getSpecies(); + if (sProtocol != null) { - SpeciesProtocol sProtocol = - speciesProtocol.get(species.getId()); - if (sProtocol != null) { + String lengthStepPmfmId = sProtocol.getLengthStepPmfmId(); + lengthStepCaracteristic = + this.lengthStepCaracteristics.get(lengthStepPmfmId); + lengthStep = sProtocol.getLengthStep(); - String lengthStepPmfmId = sProtocol.getLengthStepPmfmId(); - - lengthStepCaracteristic = - this.lengthStepCaracteristics.get(lengthStepPmfmId); - lengthStep = sProtocol.getLengthStep(); - - if (log.isInfoEnabled()) { - log.info("Use existing from protocol lengthStep " + - "caracteristic / step " + - decorate(lengthStepCaracteristic) + " / " + - lengthStep); - } + if (log.isInfoEnabled()) { + log.info("Use existing from protocol lengthStep " + + "caracteristic / step " + + decorate(lengthStepCaracteristic) + " / " + + lengthStep); } } } @@ -451,12 +446,6 @@ SpeciesFrequencyUIModel model = getModel(); - // connect model to validator - ui.getValidator().setBean(model); - - model.setRows(editFrequency); - model.setLengthStepCaracteristic(lengthStepCaracteristic); - SpeciesFrequencyUIModel.ConfigurationMode mode = SpeciesFrequencyUIModel.ConfigurationMode.SIMPLE; if (protocol != null) { Integer taxonId = speciesBatch.getSpecies().getReferenceTaxonId(); @@ -465,12 +454,19 @@ mode = SpeciesFrequencyUIModel.ConfigurationMode.SIMPLE_COUNTING; } } - if (speciesBatch.getNumber() != null && editFrequency.size() != 1) { + + if (speciesBatch.getNumber() != null && editFrequency.isEmpty()) { mode = SpeciesFrequencyUIModel.ConfigurationMode.SIMPLE_COUNTING; + model.setSimpleCount(speciesBatch.getNumber()); } model.setConfigurationMode(mode); - model.setSimpleCount(speciesBatch.getNumber()); + // connect model to validator + ui.getValidator().setBean(model); + + model.setRows(editFrequency); + model.setLengthStepCaracteristic(lengthStepCaracteristic); + // keep batch (will be used to push back editing entry) model.setBatch(speciesBatch); } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/CreateSpeciesBatchUI.css =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/CreateSpeciesBatchUI.css 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/CreateSpeciesBatchUI.css 2013-02-13 18:24:23 UTC (rev 409) @@ -75,7 +75,7 @@ property: batchWeight; model: {model.getBatchWeight()}; useFloat: true; - numberPattern: {DECIMAL1_PATTERN}; + numberPattern: {DECIMALFORCE3_PATTERN}; bean: {model}; } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/SplitSpeciesBatchUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/SplitSpeciesBatchUIHandler.java 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/split/SplitSpeciesBatchUIHandler.java 2013-02-13 18:24:23 UTC (rev 409) @@ -382,7 +382,7 @@ addFloatColumnToModel(columnModel, SplitSpeciesBatchTableModel.WEIGHT, - TuttiUI.DECIMAL3_PATTERN); + TuttiUI.DECIMALFORCE3_PATTERN); } } Added: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/editor/TuttiComputedOrNotDataEditor.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/editor/TuttiComputedOrNotDataEditor.java (rev 0) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/editor/TuttiComputedOrNotDataEditor.java 2013-02-13 18:24:23 UTC (rev 409) @@ -0,0 +1,117 @@ + +package fr.ifremer.tutti.ui.swing.util.editor; + +import fr.ifremer.tutti.ui.swing.util.TuttiComputedOrNotData; +import java.awt.Component; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import javax.swing.AbstractCellEditor; +import javax.swing.JTable; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; +import javax.swing.event.AncestorEvent; +import javax.swing.event.AncestorListener; +import javax.swing.table.TableCellEditor; +import jaxx.runtime.swing.editor.NumberEditor; + +/** + * Editor for TuttiComputedOrNotData + * + * @author kmorin <kmorin@codelutin.com> + * @since 1.0 + */ +public class TuttiComputedOrNotDataEditor<E extends Number> extends AbstractCellEditor + implements TableCellEditor, FocusListener, AncestorListener { + + private static final long serialVersionUID = 1L; + + protected final NumberEditor numberEditor; + + protected TuttiComputedOrNotData<E> data; + + /** constructor */ + public TuttiComputedOrNotDataEditor(Class<E> type, boolean useSign) { + numberEditor = new NumberEditor(); + numberEditor.getTextField().setHorizontalAlignment(SwingConstants.RIGHT); + numberEditor.getTextField().setBorder(null); + numberEditor.getTextField().addFocusListener(this); + numberEditor.getTextField().addAncestorListener(this); + + numberEditor.setModelType(type); + + numberEditor.setUseSign(useSign); + numberEditor.init(); + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, int column) { + + data = (TuttiComputedOrNotData<E>) value; + numberEditor.setModel(data.getData()); + + // Check nullity and set the text that will be selected with the current value + if (data.getData() != null) { + numberEditor.setModelText(String.valueOf(data.getData())); + } + return numberEditor; + } + + public NumberEditor getNumberEditor() { + return numberEditor; + } + + @Override + public TuttiComputedOrNotData<E> getCellEditorValue() { + return data; + } + + @Override + public void focusGained(FocusEvent e) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + numberEditor.getTextField().requestFocus(); + numberEditor.getTextField().selectAll(); + } + }); + } + + @Override + public void focusLost(FocusEvent e) { + } + + @Override + public void ancestorAdded(AncestorEvent event) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + numberEditor.getTextField().requestFocus(); + numberEditor.getTextField().selectAll(); + } + }); + } + + @Override + public void ancestorRemoved(AncestorEvent event) { + } + + @Override + public void ancestorMoved(AncestorEvent event) { + } + + @Override + public boolean stopCellEditing() { + boolean result = super.stopCellEditing(); + // Reset previous data to avoid keeping it on other cell edition + if (result) { + data.setData((E) numberEditor.getModel()); + + numberEditor.setModel(null); + // Use empty string, otherwise there is a NPE in NumberEditorHandler + numberEditor.setModelText(""); + + data = null; + } + return result; + } + +} Modified: trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties =================================================================== --- trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties 2013-02-12 18:44:24 UTC (rev 408) +++ trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties 2013-02-13 18:24:23 UTC (rev 409) @@ -27,7 +27,8 @@ tutti.action.cloneProtocol=Cloner tutti.action.cloneProtocol.tip=Dupliquer le protocole sélectionné tutti.action.close=Fermer -tutti.action.computeSpeciesBatch=Elever les poids +tutti.action.computeWeights=Elever les poids +tutti.action.computeWeights.tip=Elever les poids tutti.action.configuration=Configuration tutti.action.configuration.tip=Configurer Tutti tutti.action.createSpeciesBatch=Créer un lot pour une espèce
participants (1)
-
kmorin@users.forge.codelutin.com