View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions;
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 fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
28  import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.node.StrataNode;
29  import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.node.SubStrataNode;
30  import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorUI;
31  import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import javax.swing.JTree;
36  import javax.swing.tree.TreePath;
37  import java.util.HashSet;
38  import java.util.Set;
39  
40  /**
41   * @author Kevin Morin (Code Lutin)
42   * @since 4.5
43   */
44  public class RemoveStratasAction extends SimpleActionSupport<ZoneEditorUI> {
45  
46      /** Logger. */
47      private static final Log log = LogFactory.getLog(RemoveStratasAction.class);
48  
49      public RemoveStratasAction(ZoneEditorUI zoneEditorUI) {
50          super(zoneEditorUI);
51          setEnabled(false);
52      }
53  
54      @Override
55      protected void onActionPerformed(ZoneEditorUI zoneEditorUI) {
56  
57          JTree zonesTree = zoneEditorUI.getZonesTree();
58          TreePath[] selectedStratas = zonesTree.getSelectionPaths();
59  
60          if (selectedStratas == null) {
61  
62              if (log.isInfoEnabled()) {
63                  log.info("No selected stratas, nor subStratas selected");
64              }
65              return;
66          }
67  
68          // récupération des strates à supprimer
69          Set<StrataNode> stratasNodeToRemove = getStrataNodesToRemove(selectedStratas);
70  
71          // récupération des sous-strates à supprimer
72          Set<SubStrataNode> subStratasNodeToRemove = getSubStrataNodesToRemove(selectedStratas, stratasNodeToRemove);
73  
74          EditProtocolUIModel model = zoneEditorUI.getModel();
75  
76          model.setModifyingZones(true);
77  
78          try {
79  
80              stratasNodeToRemove.forEach(model::unselectStrataNode);
81              subStratasNodeToRemove.forEach(model::unselectSubStrataNode);
82  
83          } finally {
84  
85              model.setModifyingZones(false);
86  
87          }
88  
89          // select zone path
90          zonesTree.setSelectionPath(selectedStratas[0].getParentPath());
91  
92      }
93  
94      protected Set<StrataNode> getStrataNodesToRemove(TreePath[] selectedStratas) {
95          Set<StrataNode> result = new HashSet<>();
96          for (TreePath selectedStrata : selectedStratas) {
97  
98              Object node = selectedStrata.getLastPathComponent();
99              if (node instanceof StrataNode) {
100                 StrataNode strataNode = (StrataNode) node;
101                 if (log.isInfoEnabled()) {
102                     log.info("found strata " + strataNode + " to remove");
103                 }
104                 result.add((StrataNode) node);
105             }
106 
107         }
108         return result;
109     }
110 
111     protected Set<SubStrataNode> getSubStrataNodesToRemove(TreePath[] selectedStratas, Set<StrataNode> stratasNodeToRemove) {
112         Set<SubStrataNode> result = new HashSet<>();
113         for (TreePath selectedStrata : selectedStratas) {
114 
115             Object node = selectedStrata.getLastPathComponent();
116             if (node instanceof SubStrataNode) {
117                 SubStrataNode subStrataNode = (SubStrataNode) node;
118                 if (!stratasNodeToRemove.contains(subStrataNode.getParent())) {
119                     if (log.isInfoEnabled()) {
120                         log.info("found subStrata " + subStrataNode + " to remove");
121                     }
122                     result.add(subStrataNode);
123                 }
124             }
125 
126         }
127         return result;
128     }
129 
130 }