View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.base.Predicate;
28  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
29  import jaxx.runtime.swing.editor.bean.BeanDoubleList;
30  import org.apache.commons.lang3.mutable.MutableInt;
31  
32  import javax.swing.JList;
33  import java.util.Collection;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Objects;
37  import java.util.TreeMap;
38  
39  /**
40   * Un compteur de caractéristiques.
41   *
42   * Created on 27/04/16.
43   *
44   * @author Tony Chemit - chemit@codelutin.com
45   * @since 4.5
46   */
47  public class CaracteristicsCount {
48  
49      public void attachPredicateToUi(BeanDoubleList<Caracteristic> ui) {
50          Predicate<List<Caracteristic>> listPredicate = input -> {
51              JList<Caracteristic> selectedList = ui.getSelectedList();
52              List<Caracteristic> selectedValuesList = selectedList.getSelectedValuesList();
53              for (Caracteristic caracteristic : selectedValuesList) {
54                  if (isCaracteristicUsed(caracteristic)) {
55                      return false;
56                  }
57              }
58              return true;
59          };
60          ui.getModel().addCanRemoveItemsPredicate(listPredicate);
61  
62      }
63  
64      private final MutableInt ZERO = new MutableInt();
65  
66      private final Map<Integer, MutableInt> counts = new TreeMap<>();
67  
68      public boolean isCaracteristicUsed(Caracteristic caracteristic) {
69          Objects.requireNonNull(caracteristic);
70          MutableInt result = counts.getOrDefault(caracteristic.getIdAsInt(), ZERO);
71          return result.intValue() > 0;
72      }
73  
74      public void addCaracteristic(Caracteristic caracteristic) {
75          Objects.requireNonNull(caracteristic);
76          counts.compute(caracteristic.getIdAsInt(), (k, v) -> {
77              if (v == null) {
78                  return new MutableInt(1);
79              } else {
80                  v.increment();
81                  return v;
82              }
83          });
84  
85      }
86  
87      public void removeCaracteristic(Caracteristic caracteristic) {
88          Objects.requireNonNull(caracteristic);
89          counts.compute(caracteristic.getIdAsInt(), (k, v) -> {
90              if (v == null || v.intValue() == 1) {
91                  return null;
92              } else {
93                  v.decrement();
94                  return v;
95              }
96          });
97  
98      }
99  
100     public void removeCaracteristics(Collection<Caracteristic> caracteristics) {
101         Objects.requireNonNull(caracteristics);
102         caracteristics.forEach(this::removeCaracteristic);
103     }
104 
105 }