View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.genericformat.tree;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 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.collect.Iterators;
28  import fr.ifremer.tutti.persistence.model.CruiseDataModel;
29  import fr.ifremer.tutti.persistence.model.OperationDataModel;
30  
31  import java.util.Iterator;
32  import java.util.LinkedHashSet;
33  import java.util.Set;
34  import javax.swing.tree.TreeNode;
35  
36  /**
37   * Created on 3/29/15.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 3.14.3
41   */
42  public class CruiseSelectTreeNode extends DataSelectTreeNodeSupport<CruiseDataModel> implements Iterable<OperationSelectTreeNode> {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private boolean selected;
47  
48      private final int nbChilds;
49  
50      private int nbChildSelected;
51  
52      private boolean objectValueIsAdjusting;
53  
54      public CruiseSelectTreeNode(CruiseDataModel userObject) {
55          super(userObject);
56  
57          for (OperationDataModel operation : userObject) {
58  
59              OperationSelectTreeNode operationNode = new OperationSelectTreeNode(operation);
60              add(operationNode);
61  
62          }
63  
64          nbChilds = userObject.size();
65  
66      }
67  
68      public boolean isPartialSelected() {
69          return !selected && nbChildSelected > 0;
70      }
71  
72      public int getNbChilds() {
73          return nbChilds;
74      }
75  
76      public int getNbChildSelected() {
77          return nbChildSelected;
78      }
79  
80      @Override
81      public boolean isSelected() {
82          return selected;
83      }
84  
85      @Override
86      public void setSelected(boolean selected) {
87  
88          objectValueIsAdjusting = true;
89          this.selected = selected;
90          try {
91  
92              for (OperationSelectTreeNode o : this) {
93                  o.setSelected(selected);
94              }
95  
96          } finally {
97  
98              objectValueIsAdjusting = false;
99  
100         }
101 
102         updateSelectedSate();
103 
104     }
105 
106     @Override
107     public CruiseDataModel getSelectedDataModel() {
108 
109         CruiseDataModel result;
110 
111         if (isSelected() || isPartialSelected()) {
112 
113             Set<OperationDataModel> operations = new LinkedHashSet<>();
114 
115             for (OperationSelectTreeNode o : this) {
116                 OperationDataModel operation = o.getSelectedDataModel();
117                 if (operation != null) {
118                     operations.add(operation);
119                 }
120             }
121             result = new CruiseDataModel(getId(), getLabel(), operations);
122 
123         } else {
124             result = null;
125         }
126 
127         return result;
128 
129     }
130 
131     @Override
132     public Iterator<OperationSelectTreeNode> iterator() {
133         return Iterators.forEnumeration(children());
134     }
135 
136     public void updateSelectedSate() {
137 
138         if (!objectValueIsAdjusting && nbChilds > 0) {
139 
140             nbChildSelected = 0;
141             for (OperationSelectTreeNode o : this) {
142                 if (o.isSelected()) {
143                     nbChildSelected++;
144                 }
145             }
146 
147             if (selected) {
148 
149                 if (nbChildSelected < nbChilds) {
150                     selected = false;
151                 }
152 
153             } else if (nbChildSelected == nbChilds) {
154                 selected = true;
155             }
156 
157         }
158 
159     }
160 
161     public boolean isSelectedDataExists() {
162 
163         boolean result = false;
164         if (isSelected() || isPartialSelected()) {
165             result = getOptionalId() != null;
166         }
167         return result;
168 
169     }
170 
171     public boolean isChildSelectedDataExists() {
172         boolean result = false;
173 
174         for (int i = 0, max = this.getChildCount(); i < max; i++) {
175             TreeNode child = this.getChildAt(i);
176             if (child instanceof OperationSelectTreeNode) {
177                 OperationSelectTreeNode o = (OperationSelectTreeNode)child;
178                 result = o.isSelectedDataExists();
179                 if (result) {
180                     break;
181                 }
182             }
183         }
184 
185         return result;
186     }
187 
188 }