1 package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.content.protocol.zones.tree.node.ZoneNode;
32 import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import javax.swing.JTree;
37 import javax.swing.tree.TreePath;
38 import java.util.HashSet;
39 import java.util.LinkedHashSet;
40 import java.util.Optional;
41 import java.util.Set;
42
43
44
45
46
47 public class AddStratasAction extends SimpleActionSupport<ZoneEditorUI> {
48
49
50 private static final Log log = LogFactory.getLog(AddStratasAction.class);
51
52 public AddStratasAction(ZoneEditorUI zoneEditorUI) {
53 super(zoneEditorUI);
54 setEnabled(false);
55 }
56
57 @Override
58 protected void onActionPerformed(ZoneEditorUI zoneEditorUI) {
59
60 Optional<ZoneNode> optionalSelectedZoneNode = getSelectedZone(zoneEditorUI);
61 if (!optionalSelectedZoneNode.isPresent()) {
62 if (log.isInfoEnabled()) {
63 log.info("No zone selected");
64 }
65 return;
66 }
67
68 JTree availableStratasTree = zoneEditorUI.getAvailableStratasTree();
69 TreePath[] selectedAvailableStrataPaths = availableStratasTree.getSelectionPaths();
70
71 if (selectedAvailableStrataPaths == null) {
72
73 if (log.isInfoEnabled()) {
74 log.info("No available stratas, nor subStratas selected");
75 }
76 return;
77 }
78
79 Set<StrataNode> strataNodesToAdd = getStrataNodesToAdd(selectedAvailableStrataPaths);
80 Set<SubStrataNode> subStrataNodesToAdd = getSubStrataNodesToAdd(selectedAvailableStrataPaths, strataNodesToAdd);
81
82 ZoneNode selectedZoneNode = optionalSelectedZoneNode.get();
83
84 EditProtocolUIModel model = zoneEditorUI.getModel();
85
86 model.setModifyingZones(true);
87
88 try {
89
90 strataNodesToAdd.forEach(strataNode -> model.selectStrataNodes(selectedZoneNode, strataNode));
91 subStrataNodesToAdd.forEach(subStrataNode -> model.selectedSubStraNodes(selectedZoneNode, subStrataNode));
92
93 } finally {
94
95 model.setModifyingZones(false);
96
97 }
98
99 }
100
101 protected Optional<ZoneNode> getSelectedZone(ZoneEditorUI zoneEditorUI) {
102
103 JTree zonesTree = zoneEditorUI.getZonesTree();
104 TreePath selectedZonePath = zonesTree.getSelectionPath();
105 return Optional.ofNullable(selectedZonePath == null ? null : (ZoneNode) selectedZonePath.getPathComponent(1));
106 }
107
108 protected Set<StrataNode> getStrataNodesToAdd(TreePath[] selectedStratas) {
109 Set<StrataNode> result = new HashSet<>();
110 for (TreePath selectedStrata : selectedStratas) {
111
112 Object node = selectedStrata.getLastPathComponent();
113 if (node instanceof StrataNode) {
114 StrataNode strataNode = (StrataNode) node;
115 if (log.isInfoEnabled()) {
116 log.info("found strata " + strataNode + " to add");
117 }
118 result.add(strataNode);
119 }
120
121 }
122 return result;
123 }
124
125 protected Set<SubStrataNode> getSubStrataNodesToAdd(TreePath[] selectedStratas, Set<StrataNode> stratasNodeToAdd) {
126 Set<SubStrataNode> result = new LinkedHashSet<>();
127 for (TreePath selectedStrata : selectedStratas) {
128
129 Object node = selectedStrata.getLastPathComponent();
130 if (node instanceof SubStrataNode) {
131 SubStrataNode subStrataNode = (SubStrataNode) node;
132 StrataNode strataNode = subStrataNode.getParent();
133 if (!stratasNodeToAdd.contains(strataNode)) {
134 if (log.isInfoEnabled()) {
135 log.info("found subStrata " + subStrataNode + " to add");
136 }
137 result.add(subStrataNode);
138 }
139 }
140
141 }
142 return result;
143 }
144 }