This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git commit e0bb2d6e3f2fc3685dd06ef39c7dd26e24b5fedd Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Apr 27 18:57:23 2016 +0200 Revue de code et suppression de code statique et pas très jolie à regarder :( (See #8238) --- .../content/protocol/CaracteristicsCount.java | 81 +++++++++++ .../protocol/EditProtocolSpeciesTableModel.java | 125 +---------------- .../content/protocol/EditProtocolUIHandler.java | 151 ++++++++------------- .../content/protocol/EditProtocolUIModel.java | 67 ++++++++- .../actions/RemoveBenthosProtocolAction.java | 21 +-- .../actions/RemoveSpeciesProtocolAction.java | 20 +-- 6 files changed, 236 insertions(+), 229 deletions(-) diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/CaracteristicsCount.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/CaracteristicsCount.java new file mode 100644 index 0000000..ab643d9 --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/CaracteristicsCount.java @@ -0,0 +1,81 @@ +package fr.ifremer.tutti.ui.swing.content.protocol; + +import com.google.common.base.Predicate; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import jaxx.runtime.swing.editor.bean.BeanDoubleList; +import org.apache.commons.lang3.mutable.MutableInt; + +import javax.swing.JList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; + +/** + * Un compteur de caractéristiques. + * + * Created on 27/04/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 4.5 + */ +public class CaracteristicsCount { + + public void attachPredicateToUi(BeanDoubleList<Caracteristic> ui) { + Predicate<List<Caracteristic>> listPredicate = input -> { + JList<Caracteristic> selectedList = ui.getSelectedList(); + List<Caracteristic> selectedValuesList = selectedList.getSelectedValuesList(); + for (Caracteristic caracteristic : selectedValuesList) { + if (isCaracteristicUsed(caracteristic)) { + return false; + } + } + return true; + }; + ui.getModel().addCanRemoveItemsPredicate(listPredicate); + + } + + private final MutableInt ZERO = new MutableInt(); + + private final Map<Integer, MutableInt> counts = new TreeMap<>(); + + public boolean isCaracteristicUsed(Caracteristic caracteristic) { + Objects.requireNonNull(caracteristic); + MutableInt result = counts.getOrDefault(caracteristic.getIdAsInt(), ZERO); + return result.intValue() > 0; + } + + public void addCaracteristic(Caracteristic caracteristic) { + Objects.requireNonNull(caracteristic); + counts.compute(caracteristic.getIdAsInt(), (k, v) -> { + if (v == null) { + return new MutableInt(1); + } else { + v.increment(); + return v; + } + }); + + } + + public void removeCaracteristic(Caracteristic caracteristic) { + Objects.requireNonNull(caracteristic); + counts.compute(caracteristic.getIdAsInt(), (k, v) -> { + if (v == null || v.intValue() == 1) { + return null; + } else { + v.decrement(); + return v; + } + }); + + } + + public void removeCaracteristics(Collection<Caracteristic> caracteristics) { + Objects.requireNonNull(caracteristics); + caracteristics.forEach(this::removeCaracteristic); + } + +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolSpeciesTableModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolSpeciesTableModel.java index 356e7ba..2e909d9 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolSpeciesTableModel.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolSpeciesTableModel.java @@ -23,16 +23,13 @@ package fr.ifremer.tutti.ui.swing.content.protocol; */ import com.google.common.collect.Lists; -import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; -import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.Species; import org.jdesktop.swingx.table.TableColumnModelExt; import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel; import org.nuiton.jaxx.application.swing.table.ColumnIdentifier; -import java.util.ArrayList; -import java.util.Collection; import java.util.List; +import java.util.function.Supplier; import static org.nuiton.i18n.I18n.n; @@ -82,70 +79,21 @@ public class EditProtocolSpeciesTableModel extends AbstractApplicationTableModel n("tutti.editProtocol.table.header.useRtp"), n("tutti.editProtocol.table.header.useRtp.tip")); - protected final SampleCategoryModel sampleCategoryModel; - - // FIXME check if ok to add benthos AND species together - private static final List<Caracteristic> lengthStepPmfmUsed = new ArrayList<>(); - private static final List<Caracteristic> maturitiesPmfmUsed = new ArrayList<>(); + protected final Supplier<EditProtocolSpeciesRowModel> rowSupplier; private static final long serialVersionUID = 1L; - public EditProtocolSpeciesTableModel(SampleCategoryModel sampleCategoryModel, + public EditProtocolSpeciesTableModel(Supplier<EditProtocolSpeciesRowModel> rowSupplier, TableColumnModelExt columnModel) { super(columnModel, false, false); - this.sampleCategoryModel = sampleCategoryModel; + this.rowSupplier = rowSupplier; setNoneEditableCols(SPECIES_ID); } - public static EditProtocolSpeciesRowModel newRow(SampleCategoryModel sampleCategoryModel) { - EditProtocolSpeciesRowModel result = new EditProtocolSpeciesRowModel(); - - List<Integer> mandatoryIds = - Lists.newArrayList(sampleCategoryModel.getSamplingOrder()); - // always remove the first category (V/HV) - mandatoryIds.remove(0); - result.setMandatorySampleCategoryId(mandatoryIds); - - result.setWeightEnabled(true); - result.setValid(true); - - result.addPropertyChangeListener(EditProtocolSpeciesRowModel.PROPERTY_LENGTH_STEP_PMFM, - evt -> { - // update the used lengthstep caracteristic - if (evt.getOldValue() != null) { - removeUsedLengthStepPmfm((Caracteristic) evt.getOldValue()); - } - if (evt.getNewValue() == null) { - - EditProtocolSpeciesRowModel rowModel = (EditProtocolSpeciesRowModel) evt.getSource(); - rowModel.setRtpMale(null); - rowModel.setRtpFemale(null); - rowModel.setRtpUndefined(null); - - } else { - addUsedLengthStepPmfm((Caracteristic) evt.getNewValue()); - } - - }); - - result.addPropertyChangeListener(EditProtocolSpeciesRowModel.PROPERTY_MATURITY_PMFM, - evt -> { - // update the used maturities - if (evt.getOldValue() != null) { - removeUsedMaturityPmfm((Caracteristic) evt.getOldValue()); - } - if (evt.getNewValue() != null) { - addUsedMaturityPmfm((Caracteristic) evt.getNewValue()); - } - }); - - return result; - } - @Override public EditProtocolSpeciesRowModel createNewRow() { - return newRow(sampleCategoryModel); + return rowSupplier.get(); } @Override @@ -194,67 +142,4 @@ public class EditProtocolSpeciesTableModel extends AbstractApplicationTableModel return result; } - // operations on the used lengthstep pmfms - - public static void addUsedLengthStepPmfm(Caracteristic maturity) { - lengthStepPmfmUsed.add(maturity); - } - - public static void removeUsedLengthStepPmfm(Caracteristic maturity) { - lengthStepPmfmUsed.remove(maturity); - } - - public static void removeUsedLengthStepPmfms(Collection<Caracteristic> maturities) { - lengthStepPmfmUsed.removeAll(maturities); - } - - /** - * Check if the caracteristics are used in the maturity caracteristics of the species or benthos - * @param caracteristics the list of caracteristics to check - * @return true if none of the maturities are used - */ - public static boolean isNoneOfTheLengthStepPmfmsUsed(Collection<Caracteristic> caracteristics) { - return lengthStepPmfmUsed.stream().noneMatch(caracteristics::contains); - } - - /** - * Check if the caracteristic is used in the maturity caracteristics of the species or benthos - * @param caracteristic the caracteristic to check - * @return true if the maturity is used - */ - public static boolean isLengthStepPmfmUsed(Caracteristic caracteristic) { - return lengthStepPmfmUsed.contains(caracteristic); - } - - // operations on the used maturity pfmfs - - public static void addUsedMaturityPmfm(Caracteristic maturity) { - maturitiesPmfmUsed.add(maturity); - } - - public static void removeUsedMaturityPmfm(Caracteristic maturity) { - maturitiesPmfmUsed.remove(maturity); - } - - public static void removeUsedMaturityPmfms(Collection<Caracteristic> maturities) { - maturitiesPmfmUsed.removeAll(maturities); - } - - /** - * Check if the caracteristics are used in the maturity caracteristics of the species or benthos - * @param caracteristics the list of caracteristics to check - * @return true if none of the maturities are used - */ - public static boolean isNoneOfTheMaturityPmfmsUsed(Collection<Caracteristic> caracteristics) { - return maturitiesPmfmUsed.stream().noneMatch(caracteristics::contains); - } - - /** - * Check if the caracteristic is used in the maturity caracteristics of the species or benthos - * @param caracteristic the caracteristic to check - * @return true if the maturity is used - */ - public static boolean isMaturityPmfmUsed(Caracteristic caracteristic) { - return maturitiesPmfmUsed.contains(caracteristic); - } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java index b00f796..d622ee3 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java @@ -209,7 +209,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI getDataContext().resetValidationDataContext(); - EditProtocolUIModel model = new EditProtocolUIModel(); + EditProtocolUIModel model = new EditProtocolUIModel(sampleCategoryModel); // load cache data @@ -246,7 +246,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI locationLabelCacheBuilder.put(strata.getId(), strata.getLabel()); programStratasAndSubstratas.get(strata).stream().filter(Objects::nonNull) - .forEach(subStrata -> locationLabelCacheBuilder.put(subStrata.getId(), subStrata.getLabel())); + .forEach(subStrata -> locationLabelCacheBuilder.put(subStrata.getId(), subStrata.getLabel())); }); @@ -340,7 +340,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI } if (model.getLengthClassesPmfmId() == null) { - model.setLengthClassesPmfmId(Lists.<String>newArrayList()); + model.setLengthClassesPmfmId(new ArrayList<>()); } model.setProgramId(getDataContext().getProgramId()); @@ -352,8 +352,8 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI Collection<Species> referents = model.getAllReferentSpeciesByTaxonId().values(); - initBeanFilterableComboBox(this.ui.getSpeciesComboBox(), Lists.newArrayList(referents), null); - initBeanFilterableComboBox(this.ui.getBenthosComboBox(), Lists.newArrayList(referents), null); + initBeanFilterableComboBox(this.ui.getSpeciesComboBox(), new ArrayList<>(referents), null); + initBeanFilterableComboBox(this.ui.getBenthosComboBox(), new ArrayList<>(referents), null); initBeanFilterableComboBox(ui.getCaracteristicMappingComboBox(), new ArrayList<>(model.getCaracteristics()), null); List<EditProtocolCaracteristicsRowModel> caracteristicMappingRows; @@ -369,10 +369,10 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI // build protocolSpecies and benthos rows if (protocol == null) { - caracteristicMappingRows = Lists.newArrayList(); - operationFieldMappingRows = Lists.newArrayList(); - speciesRows = Lists.newArrayList(); - benthosRows = Lists.newArrayList(); + caracteristicMappingRows = new ArrayList<>(); + operationFieldMappingRows = new ArrayList<>(); + speciesRows = new ArrayList<>(); + benthosRows = new ArrayList<>(); } else { @@ -427,7 +427,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI addLengthClassesColumnToModel(columnModel, model.getLengthClassesPmfmId()); addMaturityColumnToModel(columnModel, model.getMaturityPmfmId()); - addCpsPmfmColumnToModel(columnModel, EditProtocolSpeciesTableModel.CALCIFIED_PIECES_SAMPLING_TYPE_PMFM_ID); + addCpsPmfmColumnToModel(columnModel); addBooleanColumnToModel(columnModel, EditProtocolSpeciesTableModel.WEIGHT_ENABLED, table); @@ -490,7 +490,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI addLengthClassesColumnToModel(columnModel, model.getLengthClassesPmfmId()); addMaturityColumnToModel(columnModel, model.getMaturityPmfmId()); - addCpsPmfmColumnToModel(columnModel, EditProtocolSpeciesTableModel.CALCIFIED_PIECES_SAMPLING_TYPE_PMFM_ID); + addCpsPmfmColumnToModel(columnModel); addBooleanColumnToModel(columnModel, EditProtocolSpeciesTableModel.WEIGHT_ENABLED, table); @@ -530,25 +530,24 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI } BeanDoubleList<Caracteristic> maturityList = this.ui.getMaturityList(); + model.getMaturityPmfmUsed().attachPredicateToUi(maturityList); + BeanDoubleList<Caracteristic> lengthClassesList = this.ui.getLengthClassesList(); + model.getLengthStepPmfmUsed().attachPredicateToUi(lengthClassesList); - allDoubleLists = Lists.newArrayList( - lengthClassesList, - this.ui.getIndividualObservationList(), - maturityList - ); + allDoubleLists = new ArrayList<>(); + allDoubleLists.add(lengthClassesList); + allDoubleLists.add(ui.getIndividualObservationList()); + allDoubleLists.add(maturityList); initDoubleList(EditProtocolUIModel.PROPERTY_LENGTH_CLASSES_PMFM_ID, lengthClassesList, Lists.newArrayList(model.getCaracteristics()), model.getLengthClassesPmfmId()); - JList lengthClassesSelectedList = lengthClassesList.getSelectedList(); - lengthClassesList.getModel().addCanRemoveItemsPredicate( - input -> EditProtocolSpeciesTableModel.isNoneOfTheLengthStepPmfmsUsed(lengthClassesSelectedList.getSelectedValuesList())); - - ListCellRenderer lengthClassesDefaultRenderer = lengthClassesSelectedList.getCellRenderer(); - lengthClassesSelectedList.setCellRenderer(new LengthStepCaracteristicCellRenderer(lengthClassesDefaultRenderer)); + JList<Caracteristic> lengthClassesSelectedList = lengthClassesList.getSelectedList(); + ListCellRenderer<? super Caracteristic> lengthClassesDefaultRenderer = lengthClassesSelectedList.getCellRenderer(); + lengthClassesSelectedList.setCellRenderer(new LengthStepCaracteristicCellRenderer(getConfig().getColorWarningRow(), model.getLengthStepPmfmUsed(), lengthClassesDefaultRenderer)); initDoubleList(EditProtocolUIModel.PROPERTY_INDIVIDUAL_OBSERVATION_PMFM_ID, this.ui.getIndividualObservationList(), @@ -560,10 +559,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI model.getCaracteristics().stream().filter(caracteristic -> !caracteristic.isQualitativeValueEmpty()).collect(Collectors.toList()), model.getMaturityPmfmId()); - JList maturitySelectedList = maturityList.getSelectedList(); - maturityList.getModel().addCanRemoveItemsPredicate( - input -> EditProtocolSpeciesTableModel.isNoneOfTheMaturityPmfmsUsed(maturityList.getSelectedList().getSelectedValuesList())); - + JList<Caracteristic> maturitySelectedList = maturityList.getSelectedList(); JMenuItem editMaturity = ui.getEditMaturityCaracteristicAction(); maturityList.getSelectedListPopup().add(editMaturity); @@ -573,8 +569,8 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI editMaturity.setEnabled(editMaturityEnabled); }); - ListCellRenderer maturityDefaultRenderer = maturitySelectedList.getCellRenderer(); - maturitySelectedList.setCellRenderer(new MaturityCaracteristicCellRenderer(maturityDefaultRenderer)); + ListCellRenderer<? super Caracteristic> maturityDefaultRenderer = maturitySelectedList.getCellRenderer(); + maturitySelectedList.setCellRenderer(new MaturityCaracteristicCellRenderer(model.getMaturityPmfmUsed(), maturityDefaultRenderer)); // update lengthstep and maturity list button states when the user goes back to the caracteristic panel @@ -1012,7 +1008,8 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI } public EditProtocolSpeciesRowModel createNewProtocolSpeciesRow() { - EditProtocolSpeciesRowModel result = EditProtocolSpeciesTableModel.newRow(sampleCategoryModel); + + EditProtocolSpeciesRowModel result = getModel().newRow(); result.addPropertyChangeListener(EditProtocolSpeciesRowModel.PROPERTY_LENGTH_STEP_PMFM, evt -> { @@ -1254,8 +1251,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI List<Caracteristic> availableCaracteristics, List<String> selectedCaracteristics) { - initBeanList(widget, availableCaracteristics, - Lists.<Caracteristic>newArrayList()); + initBeanList(widget, availableCaracteristics, new ArrayList<>()); UpdateSelectedList listener = new UpdateSelectedList(widget, getModel().getAllCaracteristic()); widget.putClientProperty("_updateListener", listener); @@ -1263,7 +1259,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI listener.select(selectedCaracteristics); // add a filter to keep only in universe everything except what is selected by other double lists - List<BeanDoubleList<Caracteristic>> list = Lists.newArrayList(allDoubleLists); + List<BeanDoubleList<Caracteristic>> list = new ArrayList<>(allDoubleLists); list.remove(widget); widget.getHandler().addFilter(new SelectValuePredicate(list)); widget.getHandler().addFilter(caracteristic -> !getModel().isCaracteristicUsedInMapping(caracteristic)); @@ -1274,14 +1270,14 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI protected void selectCaracteristics(List<String> ids, JComboBox comboBox) { Map<String, Caracteristic> allCaracteristic = getModel().getAllCaracteristic(); - List<Caracteristic> selection = Lists.newArrayList(); + List<Caracteristic> selection = new ArrayList<>(); if (CollectionUtils.isNotEmpty(ids)) { for (String id : ids) { selection.add(allCaracteristic.get(id)); } } - List<Caracteristic> dataToList = Lists.newArrayList(selection); + List<Caracteristic> dataToList = new ArrayList<>(selection); // add a null value at first position if (!dataToList.isEmpty() && dataToList.get(0) != null) { @@ -1290,13 +1286,11 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI SwingUtil.fillComboBox(comboBox, dataToList, null); } - protected void addLengthClassesColumnToModel(TableColumnModel model, - List<String> selectedIds) { + protected void addLengthClassesColumnToModel(TableColumnModel model, List<String> selectedIds) { - Decorator<Caracteristic> decorator = - getDecorator(Caracteristic.class, null); + Decorator<Caracteristic> decorator = getDecorator(Caracteristic.class, null); - final JComboBox comboBox = new JComboBox(); + JComboBox<Caracteristic> comboBox = new JComboBox<>(); getModel().addPropertyChangeListener(EditProtocolUIModel.PROPERTY_LENGTH_CLASSES_PMFM_ID, evt -> { List<String> ids = (List<String>) evt.getNewValue(); @@ -1315,13 +1309,11 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI EditProtocolSpeciesTableModel.LENGTH_STEP_PMFM_ID); } - protected void addMaturityColumnToModel(TableColumnModel model, - List<String> selectedIds) { + protected void addMaturityColumnToModel(TableColumnModel model, List<String> selectedIds) { - Decorator<Caracteristic> decorator = - getDecorator(Caracteristic.class, null); + Decorator<Caracteristic> decorator = getDecorator(Caracteristic.class, null); - final JComboBox comboBox = new JComboBox(); + JComboBox<Caracteristic> comboBox = new JComboBox(); getModel().addPropertyChangeListener(EditProtocolUIModel.PROPERTY_MATURITY_PMFM_ID, evt -> { List<String> ids = (List<String>) evt.getNewValue(); @@ -1341,8 +1333,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI EditProtocolSpeciesTableModel.MATURITY_PMFM_ID); } - protected void addCpsPmfmColumnToModel(TableColumnModel model, - ColumnIdentifier<EditProtocolSpeciesRowModel> column) { + protected void addCpsPmfmColumnToModel(TableColumnModel model) { Decorator<CaracteristicQualitativeValue> caracteristicQualitativeValueDecorator = getDecorator(CaracteristicQualitativeValue.class, null); @@ -1352,29 +1343,6 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI caracteristicQualitativeValueDecorator, getDataContext().getCpsTypeValues()); -// Decorator<Caracteristic> decorator = -// getDecorator(Caracteristic.class, null); -// -// final JComboBox comboBox = new JComboBox(); -// -// comboBox.setRenderer(newListCellRender(decorator)); -// -// List<Caracteristic> dataToList = Lists.newArrayList(getModel().getCaracteristics()); -// -// // add a null value at first position -// if (!dataToList.isEmpty() && dataToList.get(0) != null) { -// dataToList.add(0, null); -// } -// SwingUtil.fillComboBox(comboBox, dataToList, null); -// -// ObjectToStringConverter converter = BeanUIUtil.newDecoratedObjectToStringConverter(decorator); -// BeanUIUtil.decorate(comboBox, converter); -// ComboBoxCellEditor editor = new ComboBoxCellEditor(comboBox); -// -// addColumnToModel(model, -// editor, -// newTableCellRender(decorator), -// column); } protected void initTable(final JXTable table, @@ -1384,8 +1352,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI List<EditProtocolSpeciesRowModel> rows, ListSelectionListener selectionListener) { - final EditProtocolSpeciesTableModel tableModel = - new EditProtocolSpeciesTableModel(sampleCategoryModel, columnModel); + EditProtocolSpeciesTableModel tableModel = new EditProtocolSpeciesTableModel(getModel()::newRow, columnModel); table.setModel(tableModel); table.setColumnModel(columnModel); @@ -1415,8 +1382,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI e.getLastRow() == Integer.MAX_VALUE) { // get species column - TableColumnExt tableColumn = - (TableColumnExt) table.getColumns().get(0); + TableColumnExt tableColumn = (TableColumnExt) table.getColumns().get(0); // get column comparator TuttiDecorator.TuttiDecoratorComparator<Species> comparator = @@ -1667,33 +1633,36 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI } - private class LengthStepCaracteristicCellRenderer implements ListCellRenderer { + private static class LengthStepCaracteristicCellRenderer implements ListCellRenderer<Caracteristic> { - private final ListCellRenderer defaultRenderer; + private final CaracteristicsCount caracteristicsCount; + private final Color notRemovableColor; - private Color notRemovableColor = getConfig().getColorWarningRow(); + private final ListCellRenderer<? super Caracteristic> defaultRenderer; - LengthStepCaracteristicCellRenderer(ListCellRenderer defaultRenderer) { + LengthStepCaracteristicCellRenderer(Color notRemovableColor, CaracteristicsCount caracteristicsCount, ListCellRenderer<? super Caracteristic> defaultRenderer) { + this.notRemovableColor = notRemovableColor; + this.caracteristicsCount = caracteristicsCount; this.defaultRenderer = defaultRenderer; } @Override - public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + public Component getListCellRendererComponent(JList<? extends Caracteristic> list, Caracteristic value, int index, boolean isSelected, boolean cellHasFocus) { Component result = defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - Caracteristic caracteristic = (Caracteristic) value; - if (isSelected && EditProtocolSpeciesTableModel.isLengthStepPmfmUsed(caracteristic)) { + if (isSelected && caracteristicsCount.isCaracteristicUsed(value)) { result.setBackground(notRemovableColor.darker()); } return result; } } - private class MaturityCaracteristicCellRenderer implements ListCellRenderer { + private class MaturityCaracteristicCellRenderer implements ListCellRenderer<Caracteristic> { public static final String TEXT_PATTERN = "<html><body><strong>%s</strong> :<ul><li>%s</li></ul><strong>%s</strong> :<ul><li>%s</li></ul></body></html>"; - private final ListCellRenderer defaultRenderer; + private final CaracteristicsCount caracteristicsCount; + private final ListCellRenderer<? super Caracteristic> defaultRenderer; private Color validColor = getConfig().getColorCellWithValue(); @@ -1701,21 +1670,21 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI private Color notRemovableColor = getConfig().getColorWarningRow(); - MaturityCaracteristicCellRenderer(ListCellRenderer defaultRenderer) { + MaturityCaracteristicCellRenderer(CaracteristicsCount caracteristicsCount, ListCellRenderer<? super Caracteristic> defaultRenderer) { + this.caracteristicsCount = caracteristicsCount; this.defaultRenderer = defaultRenderer; } @Override - public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + public Component getListCellRendererComponent(JList<? extends Caracteristic> list, Caracteristic value, int index, boolean isSelected, boolean cellHasFocus) { Component result = defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - Caracteristic maturityCaracteristic = (Caracteristic) value; Color backgroud; - if (isSelected && EditProtocolSpeciesTableModel.isMaturityPmfmUsed(maturityCaracteristic)) { - backgroud= notRemovableColor; + if (isSelected && caracteristicsCount.isCaracteristicUsed(value)) { + backgroud = notRemovableColor; - } else if (!EditProtocolUIHandler.this.getModel().isMaturityValid(maturityCaracteristic)) { - backgroud= invalidColor; + } else if (!EditProtocolUIHandler.this.getModel().isMaturityValid(value)) { + backgroud = invalidColor; } else { backgroud = validColor; } @@ -1726,7 +1695,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI if (result instanceof JComponent) { - ((JComponent) result).setToolTipText(createToolTipText(maturityCaracteristic)); + ((JComponent) result).setToolTipText(createToolTipText(value)); } return result; } @@ -1749,7 +1718,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI }); tooltip = String.format(TEXT_PATTERN, t("tutti.editProtocol.field.maturity.immature.tip"), StringUtils.join(immatureStates, "</li><li>"), - t("tutti.editProtocol.field.maturity.mature.tip"), StringUtils.join(matureStates, "</li><li>")); + t("tutti.editProtocol.field.maturity.mature.tip"), StringUtils.join(matureStates, "</li><li>")); } return tooltip; diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java index 58b5b02..622a3a3 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java @@ -29,6 +29,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; import fr.ifremer.tutti.persistence.entities.protocol.CaracteristicMappingRow; import fr.ifremer.tutti.persistence.entities.protocol.MaturityCaracteristic; import fr.ifremer.tutti.persistence.entities.protocol.MaturityCaracteristics; @@ -110,6 +111,7 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, * @since 1.3 */ protected final TuttiProtocol editObject = TuttiProtocols.newTuttiProtocol(); + private final SampleCategoryModel sampleCategoryModel; /** * Flag when a incoming protocol is imported. @@ -211,8 +213,19 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, private boolean modifyingZones; - public EditProtocolUIModel() { + /** + * Le compteur de méthode de mensuration utilisées par les espèces et benthos. + */ + private final CaracteristicsCount lengthStepPmfmUsed = new CaracteristicsCount(); + + /** + * Le compteur de méthode de maturité utilisées par les espèces et benthos. + */ + private final CaracteristicsCount maturityPmfmUsed = new CaracteristicsCount(); + + public EditProtocolUIModel(SampleCategoryModel sampleCategoryModel) { super(fromBeanBinder, toBeanBinder); + this.sampleCategoryModel = sampleCategoryModel; // reset the list of maturity pmfm id when the list of maturity caracteristic changes addPropertyChangeListener(PROPERTY_MATURITY_CARACTERISTICS, new PropertyChangeListener() { @@ -244,6 +257,58 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, }); } + public CaracteristicsCount getLengthStepPmfmUsed() { + return lengthStepPmfmUsed; + } + + public CaracteristicsCount getMaturityPmfmUsed() { + return maturityPmfmUsed; + } + + public EditProtocolSpeciesRowModel newRow() { + EditProtocolSpeciesRowModel result = new EditProtocolSpeciesRowModel(); + + List<Integer> mandatoryIds = Lists.newArrayList(sampleCategoryModel.getSamplingOrder()); + // always remove the first category (V/HV) + mandatoryIds.remove(0); + result.setMandatorySampleCategoryId(mandatoryIds); + + result.setWeightEnabled(true); + result.setValid(true); + + result.addPropertyChangeListener(EditProtocolSpeciesRowModel.PROPERTY_LENGTH_STEP_PMFM, + evt -> { + // update the used lengthstep caracteristic + if (evt.getOldValue() != null) { + lengthStepPmfmUsed.removeCaracteristic((Caracteristic) evt.getOldValue()); + } + if (evt.getNewValue() == null) { + + EditProtocolSpeciesRowModel rowModel = (EditProtocolSpeciesRowModel) evt.getSource(); + rowModel.setRtpMale(null); + rowModel.setRtpFemale(null); + rowModel.setRtpUndefined(null); + + } else { + lengthStepPmfmUsed.addCaracteristic((Caracteristic) evt.getNewValue()); + } + + }); + + result.addPropertyChangeListener(EditProtocolSpeciesRowModel.PROPERTY_MATURITY_PMFM, + evt -> { + // update the used maturities + if (evt.getOldValue() != null) { + maturityPmfmUsed.removeCaracteristic((Caracteristic) evt.getOldValue()); + } + if (evt.getNewValue() != null) { + maturityPmfmUsed.addCaracteristic((Caracteristic) evt.getNewValue()); + } + }); + + return result; + } + @Override protected TuttiProtocol newEntity() { return TuttiProtocols.newTuttiProtocol(); diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveBenthosProtocolAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveBenthosProtocolAction.java index 171f7d4..c3e210c 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveBenthosProtocolAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveBenthosProtocolAction.java @@ -40,6 +40,7 @@ import javax.swing.JTable; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -70,6 +71,7 @@ public class RemoveBenthosProtocolAction extends LongActionSupport<EditProtocolU /** * calcified pieces sampling rows to delete + * * @since 4.5 */ protected Collection<CalcifiedPiecesSamplingEditorRowModel> cpsRowsToDelete; @@ -119,8 +121,8 @@ public class RemoveBenthosProtocolAction extends LongActionSupport<EditProtocolU List<CalcifiedPiecesSamplingEditorRowModel> cpsRows = getModel().getCpsRows(); cpsRowsToDelete = cpsRows.stream() - .filter(r -> removedSpecies.contains(r.getProtocolSpecies().getSpecies())) - .collect(Collectors.toList()); + .filter(r -> removedSpecies.contains(r.getProtocolSpecies().getSpecies())) + .collect(Collectors.toList()); if (!cpsRowsToDelete.isEmpty()) { @@ -153,16 +155,17 @@ public class RemoveBenthosProtocolAction extends LongActionSupport<EditProtocolU // remove all rows from model getModel().getBenthosRow().removeAll(removedRows); - // remove the lengthstep pmfm of the rows from the used lengthstep pmfm - EditProtocolSpeciesTableModel.removeUsedLengthStepPmfms(removedRows.stream() - .map(EditProtocolSpeciesRowModel::getLengthStepPmfm) - .collect(Collectors.toList())); + getModel().getLengthStepPmfmUsed().removeCaracteristics(removedRows.stream() + .map(EditProtocolSpeciesRowModel::getLengthStepPmfm) + .filter(Objects::nonNull) + .collect(Collectors.toList())); // remove the maturities of the rows from the used maturities - EditProtocolSpeciesTableModel.removeUsedMaturityPmfms(removedRows.stream() - .map(EditProtocolSpeciesRowModel::getMaturityPmfm) - .collect(Collectors.toList())); + getModel().getMaturityPmfmUsed().removeCaracteristics(removedRows.stream() + .map(EditProtocolSpeciesRowModel::getMaturityPmfm) + .filter(Objects::nonNull) + .collect(Collectors.toList())); // remove the protocolSpecies from the cps table or combobox if (!cpsRowsToDelete.isEmpty()) { diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveSpeciesProtocolAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveSpeciesProtocolAction.java index 7bd7624..f218cfe 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveSpeciesProtocolAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/actions/RemoveSpeciesProtocolAction.java @@ -40,6 +40,7 @@ import javax.swing.JTable; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -69,6 +70,7 @@ public class RemoveSpeciesProtocolAction extends LongActionSupport<EditProtocolU /** * calcified pieces sampling rows to delete + * * @since 4.5 */ protected Collection<CalcifiedPiecesSamplingEditorRowModel> cpsRowsToDelete; @@ -117,8 +119,8 @@ public class RemoveSpeciesProtocolAction extends LongActionSupport<EditProtocolU List<CalcifiedPiecesSamplingEditorRowModel> cpsRows = getModel().getCpsRows(); cpsRowsToDelete = cpsRows.stream() - .filter(r -> removedSpecies.contains(r.getProtocolSpecies().getSpecies())) - .collect(Collectors.toList()); + .filter(r -> removedSpecies.contains(r.getProtocolSpecies().getSpecies())) + .collect(Collectors.toList()); if (!cpsRowsToDelete.isEmpty()) { @@ -151,14 +153,16 @@ public class RemoveSpeciesProtocolAction extends LongActionSupport<EditProtocolU getModel().getSpeciesRow().removeAll(removedRows); // remove the lengthstep pmfm of the rows from the used lengthstep pmfm - EditProtocolSpeciesTableModel.removeUsedLengthStepPmfms(removedRows.stream() - .map(EditProtocolSpeciesRowModel::getLengthStepPmfm) - .collect(Collectors.toList())); + getModel().getLengthStepPmfmUsed().removeCaracteristics(removedRows.stream() + .map(EditProtocolSpeciesRowModel::getLengthStepPmfm) + .filter(Objects::nonNull) + .collect(Collectors.toList())); // remove the maturities of the rows from the used maturities - EditProtocolSpeciesTableModel.removeUsedMaturityPmfms(removedRows.stream() - .map(EditProtocolSpeciesRowModel::getMaturityPmfm) - .collect(Collectors.toList())); + getModel().getMaturityPmfmUsed().removeCaracteristics(removedRows.stream() + .map(EditProtocolSpeciesRowModel::getMaturityPmfm) + .filter(Objects::nonNull) + .collect(Collectors.toList())); // remove the protocolSpecies from the cps table or combobox if (!cpsRowsToDelete.isEmpty()) { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.