View Javadoc
1   package fr.ifremer.dali.ui.swing.content.home.map;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.dali.ui.swing.util.map.layer.DataFeatureLayer;
27  import fr.ifremer.dali.ui.swing.util.map.layer.DataLayerCollection;
28  import org.geotools.map.Layer;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Set;
33  import java.util.stream.Collectors;
34  
35  /**
36   * A layer collection used to group layers from a data (location, survey and its operations)
37   *
38   * @author peck7 on 12/06/2017.
39   */
40  public class SurveyLayerCollection implements DataLayerCollection {
41  
42      private DataFeatureLayer locationLayer;
43      private DataCompositeLayer surveyLayer;
44      private List<DataCompositeLayer> operationLayers;
45  
46      public SurveyLayerCollection() {
47          operationLayers = new ArrayList<>();
48      }
49  
50      public void setLocationLayer(DataFeatureLayer locationLayer) {
51          this.locationLayer = locationLayer;
52      }
53  
54      public void setSurveyLayer(DataFeatureLayer surveyLineLayer, DataFeatureLayer surveyPointLayer) {
55          this.surveyLayer = new DataCompositeLayer(surveyLineLayer, surveyPointLayer);
56      }
57  
58      public void addOperationLayer(DataFeatureLayer operationLineLayer, DataFeatureLayer operationPointLayer) {
59          this.operationLayers.add(new DataCompositeLayer(operationLineLayer, operationPointLayer));
60      }
61  
62      public Integer getSurveyId() {
63          return surveyLayer != null ? surveyLayer.getId() : null;
64      }
65  
66      public Integer getLocationId() {
67          return locationLayer != null ? locationLayer.getId() : null;
68      }
69  
70      public Set<Integer> getOperationIds() {
71          return operationLayers.stream().map(DataCompositeLayer::getId).collect(Collectors.toSet());
72      }
73  
74      /**
75       * Get all existing layers
76       * @return all existing layers
77       */
78      @Override
79      public List<Layer> getLayers() {
80          List<Layer> layers = new ArrayList<>();
81          if (locationLayer != null) layers.add(locationLayer);
82          if (surveyLayer != null) layers.addAll(surveyLayer.getLayers());
83          for (DataCompositeLayer operationLayer : operationLayers) {
84              if (operationLayer != null) layers.addAll(operationLayer.getLayers());
85          }
86          return layers;
87      }
88  
89      /**
90       * Inner class describing a layer of line or point
91       */
92      private class DataCompositeLayer {
93  
94          private DataFeatureLayer lineLayer;
95          private DataFeatureLayer pointLayer;
96  
97          DataCompositeLayer(DataFeatureLayer lineLayer, DataFeatureLayer pointLayer) {
98  
99              // Affect line data layer
100             this.lineLayer = lineLayer;
101 
102             // Affect point data layer
103             this.pointLayer = pointLayer;
104         }
105 
106         List<Layer> getLayers() {
107             List<Layer> layers = new ArrayList<>();
108             if (lineLayer != null) layers.add(lineLayer);
109             if (pointLayer != null) layers.add(pointLayer);
110             return layers;
111         }
112 
113         public Integer getId() {
114             return lineLayer != null && lineLayer.getId() != null ? lineLayer.getId() : (pointLayer != null && pointLayer.getId() != null ? pointLayer.getId() : null);
115         }
116     }
117 }