[467512] Update the palette after viewpoint selection change

Bug: 467512
Change-Id: I2f91455fe822711e57f961100ab7e45932848ef5
Signed-off-by: Belqassim Djafer <belqassim.djafer@obeo.fr>
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/palette/PaletteManagerImpl.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/palette/PaletteManagerImpl.java
index 59c3721..df4e52e 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/palette/PaletteManagerImpl.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/palette/PaletteManagerImpl.java
@@ -12,6 +12,7 @@
 
 import java.text.MessageFormat;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -46,7 +47,6 @@
 import org.eclipse.sirius.business.api.session.Session;
 import org.eclipse.sirius.business.api.session.SessionManager;
 import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
-import org.eclipse.sirius.common.tools.api.util.EqualityHelper;
 import org.eclipse.sirius.common.tools.api.util.ReflectionHelper;
 import org.eclipse.sirius.common.tools.api.util.StringUtil;
 import org.eclipse.sirius.diagram.DDiagram;
@@ -81,6 +81,7 @@
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.google.common.collect.UnmodifiableIterator;
 
 /**
@@ -317,9 +318,16 @@
      *            The {@session} containing the {@link DDiagram}.
      */
     private void updatePaletteForDiagramWithLayer(DiagramDescription description, Session session, DDiagram dDiagram) {
-        /* Get the list of activated layers */
-        final List<Layer> activatedLayers = dDiagram.getActivatedLayers();
-        final List<Layer> deactivatedLayers = PaletteManagerImpl.getDeactivatedLayers(dDiagram, session, description);
+        // Copy of all layers of selected viewpoints
+        HashSet<Layer> layersInActivatedViewpoints = new HashSet<Layer>(new DiagramComponentizationManager().getAllLayers(session.getSelectedViewpoints(false), description));
+        // Copy of diagram activated layers (in all Viewpoints: activated or
+        // not)
+        HashSet<Layer> activatedLayers = new HashSet<Layer>(dDiagram.getActivatedLayers());
+        // Get the list of activated layers (of selected viewpoints)
+        final List<Layer> activatedLayersOfSelectedViewpoints = Lists.newArrayList(Sets.intersection(layersInActivatedViewpoints, activatedLayers));
+        // Get the list of deactivated layers (deactivated layers of selected
+        // viewpoints and all layers of deselected viewpoints)
+        final List<Layer> deactivatedLayersAndAllLayersOfDeselectedViewpoints = Lists.newArrayList(Sets.symmetricDifference(layersInActivatedViewpoints, activatedLayers));
         // Update the filters
         for (final ToolSection section : new DiagramComponentizationManager().getRootPaletteSections(session.getSelectedViewpoints(false), description)) {
             updateFilters(session, new DiagramComponentizationManager().getAllToolEntries(session.getSelectedViewpoints(false), section));
@@ -334,10 +342,10 @@
                 updateContainer(session, dDiagram, paletteEntry.get(), new DiagramComponentizationManager().getAllToolEntries(session.getSelectedViewpoints(false), section));
             }
         }
-        for (final Layer layer : Lists.newArrayList(deactivatedLayers)) {
+        for (final Layer layer : Lists.newArrayList(deactivatedLayersAndAllLayersOfDeselectedViewpoints)) {
             setLayerVisibility(layer, false);
         }
-        for (final Layer layer : Lists.newArrayList(activatedLayers)) {
+        for (final Layer layer : Lists.newArrayList(activatedLayersOfSelectedViewpoints)) {
             setLayerVisibility(layer, true);
         }
     }
@@ -585,29 +593,6 @@
         return EcoreUtil.getURI(entry).toString();
     }
 
-    // Browse list of all layers and remove from it all layers which are
-    // activated.
-    // all - activated = deactivated
-    private static List<Layer> getDeactivatedLayers(final DDiagram dDiagram, final Session session, final DiagramDescription description) {
-        // Copy of all layers
-        List<Layer> deactivatedLayers = Lists.newArrayList(new DiagramComponentizationManager().getAllLayers(session.getSelectedViewpoints(false), description));
-        // Use a copy of activated layers to avoid a potential
-        // ConcurrentModificationException linked to async execution of this
-        // code during the activation of a Viewpoint.
-        List<Layer> activatedLayers = Lists.newArrayList(dDiagram.getActivatedLayers());
-        for (Layer layer : activatedLayers) {
-            Iterator<Layer> iterator = deactivatedLayers.iterator();
-            while (iterator.hasNext()) {
-                Layer allLayer = iterator.next();
-                // If layer is activated (= in list of activated layer)
-                if (EqualityHelper.areEquals(layer, allLayer)) {
-                    iterator.remove();
-                }
-            }
-        }
-        return deactivatedLayers;
-    }
-
     /**
      * Adds the default tools contributed by the environment in the same group
      * as the default GEF tools.
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/My.basicfamily b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/My.basicfamily
new file mode 100644
index 0000000..204b37d
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/My.basicfamily
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<basicfamily:Family xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basicfamily="http://www.eclipse.org/sirius/sample/basicfamily">
+  <members xsi:type="basicfamily:Man" name="Paul" children="//@members.1 //@members.2"/>
+  <members xsi:type="basicfamily:Man" name="Elias" children="//@members.3 //@members.6 //@members.5 //@members.9" parents="//@members.0 //@members.8"/>
+  <members xsi:type="basicfamily:Woman" name="Lisa" children="//@members.3 //@members.6" parents="//@members.0"/>
+  <members xsi:type="basicfamily:Man" name="Dave" children="//@members.4 //@members.7" parents="//@members.1 //@members.2"/>
+  <members xsi:type="basicfamily:Man" name="Alain" parents="//@members.3 //@members.5"/>
+  <members xsi:type="basicfamily:Woman" name="Anna" children="//@members.4" parents="//@members.1"/>
+  <members xsi:type="basicfamily:Man" name="Bryan" parents="//@members.1 //@members.2"/>
+  <members xsi:type="basicfamily:Woman" name="Fiona" parents="//@members.3"/>
+  <members xsi:type="basicfamily:Man" name="Albert" children="//@members.1"/>
+  <members xsi:type="basicfamily:Woman" name="Clara" parents="//@members.1"/>
+</basicfamily:Family>
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/extended.odesign b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/extended.odesign
new file mode 100644
index 0000000..49eb7ff
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/extended.odesign
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<description:Group xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:description_1="http://www.eclipse.org/sirius/diagram/description/1.1.0" xmlns:style="http://www.eclipse.org/sirius/diagram/description/style/1.1.0" xmlns:tool="http://www.eclipse.org/sirius/diagram/description/tool/1.1.0" xmlns:tool_1="http://www.eclipse.org/sirius/description/tool/1.1.0" name="extended" version="10.0.0.201505222000">
+  <ownedViewpoints name="others">
+    <ownedRepresentationExtensions xsi:type="description_1:DiagramExtensionDescription" name="others" viewpointURI="viewpoint:/DesignerTestProject/persons" representationName="personsDiagram">
+      <layers name="Others_Layer_1">
+        <nodeMappings name="WomanNode" semanticCandidatesExpression="feature:members" domainClass="basicfamily.Woman">
+          <style xsi:type="style:SquareDescription" labelExpression="[name/]" resizeKind="NSEW">
+            <borderColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <labelColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <color xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='purple']"/>
+          </style>
+        </nodeMappings>
+        <toolSections name="OtherTools">
+          <ownedTools xsi:type="tool:NodeCreationDescription" name="createWoman_1" nodeMappings="//@ownedViewpoints[name='others']/@ownedRepresentationExtensions[name='others']/@layers[name='Others_Layer_1']/@nodeMappings[name='WomanNode']">
+            <variable name="container"/>
+            <viewVariable name="containerView"/>
+            <initialOperation>
+              <firstModelOperations xsi:type="tool_1:ChangeContext" browseExpression="var:container"/>
+            </initialOperation>
+          </ownedTools>
+        </toolSections>
+      </layers>
+      <layers name="Others_Layer_2">
+        <nodeMappings name="WomanNode" semanticCandidatesExpression="feature:members" domainClass="basicfamily.Woman">
+          <style xsi:type="style:SquareDescription" labelExpression="[name/]" resizeKind="NSEW">
+            <borderColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <labelColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <color xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='purple']"/>
+          </style>
+        </nodeMappings>
+        <toolSections name="OtherTools">
+          <ownedTools xsi:type="tool:NodeCreationDescription" name="createWoman_2" nodeMappings="//@ownedViewpoints[name='others']/@ownedRepresentationExtensions[name='others']/@layers[name='Others_Layer_2']/@nodeMappings[name='WomanNode']">
+            <variable name="container"/>
+            <viewVariable name="containerView"/>
+            <initialOperation>
+              <firstModelOperations xsi:type="tool_1:ChangeContext" browseExpression="var:container"/>
+            </initialOperation>
+          </ownedTools>
+        </toolSections>
+      </layers>
+    </ownedRepresentationExtensions>
+  </ownedViewpoints>
+</description:Group>
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/family.odesign b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/family.odesign
new file mode 100644
index 0000000..b560920
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/family.odesign
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<description:Group xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:description_1="http://www.eclipse.org/sirius/diagram/description/1.1.0" xmlns:style="http://www.eclipse.org/sirius/diagram/description/style/1.1.0" xmlns:tool="http://www.eclipse.org/sirius/diagram/description/tool/1.1.0" xmlns:tool_1="http://www.eclipse.org/sirius/description/tool/1.1.0" version="10.0.0.201505222000">
+  <ownedViewpoints name="persons">
+    <ownedRepresentations xsi:type="description_1:DiagramDescription" name="personsDiagram" domainClass="basicfamily.Family">
+      <defaultLayer name="Default">
+        <nodeMappings name="Mannode" semanticCandidatesExpression="feature:members" domainClass="basicfamily.Man">
+          <style xsi:type="style:SquareDescription" resizeKind="NSEW">
+            <borderColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <labelColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <color xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='blue']"/>
+          </style>
+        </nodeMappings>
+      </defaultLayer>
+      <additionalLayers name="Person_Layer_1" reusedMappings="//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']" activeByDefault="true">
+        <toolSections name="PersonTools">
+          <ownedTools xsi:type="tool:NodeCreationDescription" name="createMan_1" nodeMappings="//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']">
+            <variable name="container"/>
+            <viewVariable name="containerView"/>
+            <initialOperation>
+              <firstModelOperations xsi:type="tool_1:ChangeContext" browseExpression="var:container">
+                <subModelOperations xsi:type="tool_1:CreateInstance" typeName="Man" referenceName="members">
+                  <subModelOperations xsi:type="tool_1:SetValue" featureName="name" valueExpression="['man'+container.members->filter(Man)->size()/] "/>
+                </subModelOperations>
+              </firstModelOperations>
+            </initialOperation>
+          </ownedTools>
+        </toolSections>
+      </additionalLayers>
+      <additionalLayers name="Person_Layer_2">
+        <toolSections name="PersonTools">
+          <ownedTools xsi:type="tool:NodeCreationDescription" name="createMan_2" nodeMappings="//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']">
+            <variable name="container"/>
+            <viewVariable name="containerView"/>
+            <initialOperation>
+              <firstModelOperations xsi:type="tool_1:ChangeContext" browseExpression="var:container">
+                <subModelOperations xsi:type="tool_1:CreateInstance" typeName="Man" referenceName="members">
+                  <subModelOperations xsi:type="tool_1:SetValue" featureName="name" valueExpression="['man'+container.members->filter(Man)->size()/] "/>
+                </subModelOperations>
+              </firstModelOperations>
+            </initialOperation>
+          </ownedTools>
+        </toolSections>
+      </additionalLayers>
+    </ownedRepresentations>
+  </ownedViewpoints>
+</description:Group>
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/representations.aird b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/representations.aird
new file mode 100644
index 0000000..cb8230f
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/palette/extension/representations.aird
@@ -0,0 +1,144 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<viewpoint:DAnalysis xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basicfamily="http://www.eclipse.org/sirius/sample/basicfamily" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:description_1="http://www.eclipse.org/sirius/diagram/description/1.1.0" xmlns:diagram="http://www.eclipse.org/sirius/diagram/1.1.0" xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.2/notation" xmlns:style="http://www.eclipse.org/sirius/diagram/description/style/1.1.0" xmlns:viewpoint="http://www.eclipse.org/sirius/1.1.0" xsi:schemaLocation="http://www.eclipse.org/sirius/description/1.1.0 http://www.eclipse.org/sirius/1.1.0#//description http://www.eclipse.org/sirius/diagram/description/1.1.0 http://www.eclipse.org/sirius/diagram/1.1.0#//description http://www.eclipse.org/sirius/diagram/description/style/1.1.0 http://www.eclipse.org/sirius/diagram/1.1.0#//description/style" xmi:id="_0AsdwB_WEeWIiat630Qqjg" selectedViews="_m3EnMB_5EeWIiat630Qqjg _m3PmUB_5EeWIiat630Qqjg" version="10.0.0.201505222000">
+  <semanticResources>My.basicfamily</semanticResources>
+  <ownedViews xmi:type="viewpoint:DRepresentationContainer" xmi:id="_3yzdcB_WEeWIiat630Qqjg">
+    <viewpoint xmi:type="description:Viewpoint" href="platform:/resource/DesignerTestProject/family.odesign#//@ownedViewpoints[name='persons']"/>
+  </ownedViews>
+  <ownedViews xmi:type="viewpoint:DRepresentationContainer" xmi:id="_3y5kEB_WEeWIiat630Qqjg">
+    <viewpoint xmi:type="description:Viewpoint" href="platform:/resource/DesignerTestProject/extended.odesign#//@ownedViewpoints[name='others']"/>
+  </ownedViews>
+  <ownedViews xmi:type="viewpoint:DRepresentationContainer" xmi:id="_m3EnMB_5EeWIiat630Qqjg">
+    <ownedRepresentations xmi:type="diagram:DSemanticDiagram" xmi:id="_yzQ3sCCOEeWLIYOuPMQx9w" name="new personsDiagram">
+      <ownedAnnotationEntries xmi:type="description:AnnotationEntry" xmi:id="_yzRewCCOEeWLIYOuPMQx9w" source="DANNOTATION_CUSTOMIZATION_KEY">
+        <data xmi:type="diagram:ComputedStyleDescriptionRegistry" xmi:id="_yzRewSCOEeWLIYOuPMQx9w"/>
+      </ownedAnnotationEntries>
+      <ownedAnnotationEntries xmi:type="description:AnnotationEntry" xmi:id="_yzndACCOEeWLIYOuPMQx9w" source="GMF_DIAGRAMS">
+        <data xmi:type="notation:Diagram" xmi:id="_yzndASCOEeWLIYOuPMQx9w" type="Sirius" element="_yzQ3sCCOEeWLIYOuPMQx9w" measurementUnit="Pixel">
+          <children xmi:type="notation:Node" xmi:id="_yz2GgCCOEeWLIYOuPMQx9w" type="2001" element="_yzRewiCOEeWLIYOuPMQx9w">
+            <children xmi:type="notation:Node" xmi:id="_y0dxkCCOEeWLIYOuPMQx9w" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_y0dxkSCOEeWLIYOuPMQx9w" x="31"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_y0u3UCCOEeWLIYOuPMQx9w" type="3003" element="_yzRewyCOEeWLIYOuPMQx9w">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_y0u3USCOEeWLIYOuPMQx9w" fontName="Segoe UI"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0u3UiCOEeWLIYOuPMQx9w"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_yz2GgSCOEeWLIYOuPMQx9w" fontName="Segoe UI" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_yz2GgiCOEeWLIYOuPMQx9w" width="30" height="30"/>
+          </children>
+          <children xmi:type="notation:Node" xmi:id="_y0rM8CCOEeWLIYOuPMQx9w" type="2001" element="_yzRexCCOEeWLIYOuPMQx9w">
+            <children xmi:type="notation:Node" xmi:id="_y0r0ACCOEeWLIYOuPMQx9w" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_y0r0ASCOEeWLIYOuPMQx9w" x="31"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_y0veYCCOEeWLIYOuPMQx9w" type="3003" element="_yzRexSCOEeWLIYOuPMQx9w">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_y0veYSCOEeWLIYOuPMQx9w" fontName="Segoe UI"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0veYiCOEeWLIYOuPMQx9w"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_y0rM8SCOEeWLIYOuPMQx9w" fontName="Segoe UI" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0rM8iCOEeWLIYOuPMQx9w" x="90" width="30" height="30"/>
+          </children>
+          <children xmi:type="notation:Node" xmi:id="_y0r0AiCOEeWLIYOuPMQx9w" type="2001" element="_yzRexiCOEeWLIYOuPMQx9w">
+            <children xmi:type="notation:Node" xmi:id="_y0sbECCOEeWLIYOuPMQx9w" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_y0sbESCOEeWLIYOuPMQx9w" x="31"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_y0veYyCOEeWLIYOuPMQx9w" type="3003" element="_yzRexyCOEeWLIYOuPMQx9w">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_y0veZCCOEeWLIYOuPMQx9w" fontName="Segoe UI"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0veZSCOEeWLIYOuPMQx9w"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_y0r0AyCOEeWLIYOuPMQx9w" fontName="Segoe UI" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0r0BCCOEeWLIYOuPMQx9w" x="180" width="30" height="30"/>
+          </children>
+          <children xmi:type="notation:Node" xmi:id="_y0sbEiCOEeWLIYOuPMQx9w" type="2001" element="_yzReyCCOEeWLIYOuPMQx9w">
+            <children xmi:type="notation:Node" xmi:id="_y0sbFSCOEeWLIYOuPMQx9w" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_y0sbFiCOEeWLIYOuPMQx9w" x="31"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_y0wFcCCOEeWLIYOuPMQx9w" type="3003" element="_yzReySCOEeWLIYOuPMQx9w">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_y0wFcSCOEeWLIYOuPMQx9w" fontName="Segoe UI"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0wFciCOEeWLIYOuPMQx9w"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_y0sbEyCOEeWLIYOuPMQx9w" fontName="Segoe UI" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0sbFCCOEeWLIYOuPMQx9w" x="270" width="30" height="30"/>
+          </children>
+          <children xmi:type="notation:Node" xmi:id="_y0tCICCOEeWLIYOuPMQx9w" type="2001" element="_yzReyiCOEeWLIYOuPMQx9w">
+            <children xmi:type="notation:Node" xmi:id="_y0tCIyCOEeWLIYOuPMQx9w" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_y0tCJCCOEeWLIYOuPMQx9w" x="31"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_y0wFcyCOEeWLIYOuPMQx9w" type="3003" element="_yzReyyCOEeWLIYOuPMQx9w">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_y0wFdCCOEeWLIYOuPMQx9w" fontName="Segoe UI"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0wFdSCOEeWLIYOuPMQx9w"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_y0tCISCOEeWLIYOuPMQx9w" fontName="Segoe UI" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0tCIiCOEeWLIYOuPMQx9w" x="360" width="30" height="30"/>
+          </children>
+          <children xmi:type="notation:Node" xmi:id="_y0tpMCCOEeWLIYOuPMQx9w" type="2001" element="_yzRezCCOEeWLIYOuPMQx9w">
+            <children xmi:type="notation:Node" xmi:id="_y0tpMyCOEeWLIYOuPMQx9w" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_y0tpNCCOEeWLIYOuPMQx9w" x="31"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_y0wsgCCOEeWLIYOuPMQx9w" type="3003" element="_yzRezSCOEeWLIYOuPMQx9w">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_y0wsgSCOEeWLIYOuPMQx9w" fontName="Segoe UI"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0wsgiCOEeWLIYOuPMQx9w"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_y0tpMSCOEeWLIYOuPMQx9w" fontName="Segoe UI" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_y0tpMiCOEeWLIYOuPMQx9w" x="450" width="30" height="30"/>
+          </children>
+          <styles xmi:type="notation:DiagramStyle" xmi:id="_yzndAiCOEeWLIYOuPMQx9w"/>
+        </data>
+      </ownedAnnotationEntries>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_yzRewiCOEeWLIYOuPMQx9w" name="Paul" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="basicfamily:Man" href="My.basicfamily#//@members.0"/>
+        <semanticElements xmi:type="basicfamily:Man" href="My.basicfamily#//@members.0"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_yzRewyCOEeWLIYOuPMQx9w" color="114,159,207">
+          <description xmi:type="style:SquareDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']/@style"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']"/>
+      </ownedDiagramElements>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_yzRexCCOEeWLIYOuPMQx9w" name="Elias" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="basicfamily:Man" href="My.basicfamily#//@members.1"/>
+        <semanticElements xmi:type="basicfamily:Man" href="My.basicfamily#//@members.1"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_yzRexSCOEeWLIYOuPMQx9w" color="114,159,207">
+          <description xmi:type="style:SquareDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']/@style"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']"/>
+      </ownedDiagramElements>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_yzRexiCOEeWLIYOuPMQx9w" name="Dave" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="basicfamily:Man" href="My.basicfamily#//@members.3"/>
+        <semanticElements xmi:type="basicfamily:Man" href="My.basicfamily#//@members.3"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_yzRexyCOEeWLIYOuPMQx9w" color="114,159,207">
+          <description xmi:type="style:SquareDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']/@style"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']"/>
+      </ownedDiagramElements>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_yzReyCCOEeWLIYOuPMQx9w" name="Alain" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="basicfamily:Man" href="My.basicfamily#//@members.4"/>
+        <semanticElements xmi:type="basicfamily:Man" href="My.basicfamily#//@members.4"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_yzReySCOEeWLIYOuPMQx9w" color="114,159,207">
+          <description xmi:type="style:SquareDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']/@style"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']"/>
+      </ownedDiagramElements>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_yzReyiCOEeWLIYOuPMQx9w" name="Bryan" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="basicfamily:Man" href="My.basicfamily#//@members.6"/>
+        <semanticElements xmi:type="basicfamily:Man" href="My.basicfamily#//@members.6"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_yzReyyCOEeWLIYOuPMQx9w" color="114,159,207">
+          <description xmi:type="style:SquareDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']/@style"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']"/>
+      </ownedDiagramElements>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_yzRezCCOEeWLIYOuPMQx9w" name="Albert" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="basicfamily:Man" href="My.basicfamily#//@members.8"/>
+        <semanticElements xmi:type="basicfamily:Man" href="My.basicfamily#//@members.8"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_yzRezSCOEeWLIYOuPMQx9w" color="114,159,207">
+          <description xmi:type="style:SquareDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']/@style"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer/@nodeMappings[name='Mannode']"/>
+      </ownedDiagramElements>
+      <description xmi:type="description_1:DiagramDescription" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']"/>
+      <filterVariableHistory xmi:type="diagram:FilterVariableHistory" xmi:id="_yzReziCOEeWLIYOuPMQx9w"/>
+      <activatedLayers xmi:type="description_1:Layer" href="family.odesign#//@ownedViewpoints[name='persons']/@ownedRepresentations[name='personsDiagram']/@defaultLayer"/>
+      <target xmi:type="basicfamily:Family" href="My.basicfamily#/"/>
+    </ownedRepresentations>
+    <viewpoint xmi:type="description:Viewpoint" href="family.odesign#//@ownedViewpoints[name='persons']"/>
+  </ownedViews>
+  <ownedViews xmi:type="viewpoint:DRepresentationContainer" xmi:id="_m3PmUB_5EeWIiat630Qqjg">
+    <viewpoint xmi:type="description:Viewpoint" href="extended.odesign#//@ownedViewpoints[name='others']"/>
+  </ownedViews>
+</viewpoint:DAnalysis>
diff --git a/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/editor/SWTBotSiriusDiagramEditor.java b/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/editor/SWTBotSiriusDiagramEditor.java
index abc4ce7..4b460fe 100644
--- a/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/editor/SWTBotSiriusDiagramEditor.java
+++ b/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/editor/SWTBotSiriusDiagramEditor.java
@@ -44,6 +44,7 @@
 import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
 import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
 import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
+import org.eclipse.jface.bindings.keys.KeyStroke;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.sirius.business.api.query.EObjectQuery;
 import org.eclipse.sirius.business.api.session.Session;
@@ -1739,6 +1740,7 @@
             Matcher<MenuItem> withLayerName = WidgetMatcherFactory.withText(layerName);
             SWTBotMenu layerButton = button.menuItem(withLayerName);
             layerButton.click();
+            layerButton.pressShortcut(KeyStroke.getInstance(SWT.ESC));
         } else {
             DesignerViews designerViews = new DesignerViews(designerBot);
             final SiriusOutlineView outlineView = designerViews.getOutlineView().layers();
diff --git a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/PaletteManagerAfterVSMSelectionChange.java b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/PaletteManagerAfterVSMSelectionChange.java
new file mode 100644
index 0000000..b750ed9
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/PaletteManagerAfterVSMSelectionChange.java
@@ -0,0 +1,125 @@
+/*******************************************************************************
+ * Copyright (c) 2015 THALES GLOBAL SERVICES and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *    Obeo - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.sirius.tests.swtbot;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.eclipse.gef.palette.PaletteEntry;
+import org.eclipse.sirius.diagram.DDiagram;
+import org.eclipse.sirius.diagram.ui.tools.internal.palette.SectionPaletteDrawer;
+import org.eclipse.sirius.diagram.ui.tools.internal.palette.SiriusPaletteViewer;
+import org.eclipse.sirius.tests.SiriusTestsPlugin;
+import org.eclipse.sirius.tests.swtbot.support.api.AbstractSiriusSwtBotGefTestCase;
+import org.eclipse.sirius.tests.swtbot.support.api.business.UIResource;
+import org.eclipse.sirius.tests.swtbot.support.api.dialog.ExportAsImageHelper;
+import org.eclipse.sirius.tests.swtbot.support.api.editor.SWTBotSiriusDiagramEditor;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
+
+/**
+ * Tests palette update after viewpoint selection change.
+ * 
+ * @author <a href="mailto:belqassim.djafer@obeo.fr">Belqassim Djafer</a>
+ *
+ */
+public class PaletteManagerAfterVSMSelectionChange extends AbstractSiriusSwtBotGefTestCase {
+
+    private static final String DATA_UNIT_DIR = "/data/unit/tools/palette/extension/";
+
+    private static final String FILE_DIR = "/";
+
+    private static final String MODEL_NAME = "My.basicfamily";
+
+    private static final String MODELER_NAME = "family.odesign";
+
+    private static final String MODELER_EXTENSION_NAME = "extended.odesign";
+
+    private static final String SESSION_NAME = "representations.aird";
+
+    private static final String REPRESENTATION_DESC_NAME = "personsDiagram";
+
+    private static final String REPRESENTATION_INST_DESC_NAME = "new personsDiagram";
+
+    private static final String EXTENSION_VIEWPOINT_NAME = "others";
+
+    private static final String PERSON_LAYER_1 = "Person_Layer_1";
+
+    private static final String OTHERS_LAYER_1 = "Others_Layer_1";
+
+    private static final SortedSet<String> EXPECTED_ENTRIES_LAYER_PERSON1_SHOWN = Sets.newTreeSet(Arrays.asList("createMan_1"));
+
+    private static final SortedSet<String> EXPECTED_ENTRIES_LAYER_PERSON1_OTHERS1_SHOWN = Sets.newTreeSet(Arrays.asList("createMan_1", "createWoman_1"));
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected void onSetUpAfterOpeningDesignerPerspective() throws Exception {
+        copyFileToTestProject(SiriusTestsPlugin.PLUGIN_ID, DATA_UNIT_DIR, MODEL_NAME, SESSION_NAME, MODELER_NAME, MODELER_EXTENSION_NAME);
+        final UIResource sessionAirdResource = new UIResource(designerProject, FILE_DIR, SESSION_NAME);
+        localSession = designerPerspective.openSessionFromFile(sessionAirdResource);
+        localSession.changeViewpointSelection(Sets.newHashSet(EXTENSION_VIEWPOINT_NAME), Sets.<String> newHashSet());
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected void tearDown() throws Exception {
+        localSession = null;
+        super.tearDown();
+        new ExportAsImageHelper().resetDialogSettings();
+    }
+
+    /**
+     * Ensure that only selected viewpoint tools still available from the
+     * palette after a viewpoint selection change.
+     * 
+     * @throws Exception
+     *             Test error.
+     */
+    public void testHideShowLayersAfterDiagramCreation() throws Exception {
+        editor = (SWTBotSiriusDiagramEditor) openRepresentation(localSession.getOpenedSession(), REPRESENTATION_DESC_NAME, REPRESENTATION_INST_DESC_NAME, DDiagram.class);
+        editor.changeLayerActivation(PERSON_LAYER_1);
+        editor.changeLayerActivation(OTHERS_LAYER_1);
+        assertEquals("Palette entries content is wrong", EXPECTED_ENTRIES_LAYER_PERSON1_OTHERS1_SHOWN, getVisiblePaletteEntries());
+        localSession.changeViewpointSelection(Sets.<String> newHashSet(), Collections.singleton(EXTENSION_VIEWPOINT_NAME));
+        assertEquals("Palette entries content is wrong", EXPECTED_ENTRIES_LAYER_PERSON1_SHOWN, getVisiblePaletteEntries());
+    }
+
+    private TreeSet<String> getVisiblePaletteEntries() {
+        Iterable<SectionPaletteDrawer> filtered = Iterables.filter(((SiriusPaletteViewer) editor.getSiriusPaletteGroupEditPartBot().part().getViewer()).getPaletteRoot().getChildren(),
+                SectionPaletteDrawer.class);
+        TreeSet<String> result = Sets.newTreeSet();
+        for (PaletteEntry paletteEntry : filtered) {
+            Iterable<PaletteEntry> visibleEntries = Iterables.filter(((SectionPaletteDrawer) paletteEntry).getChildren(), VISIBLE_ENTRY);
+            for (PaletteEntry visiblePaletteEntry : visibleEntries) {
+                result.add(visiblePaletteEntry.getLabel());
+            }
+        }
+        return result;
+    }
+
+    private static final Predicate<PaletteEntry> VISIBLE_ENTRY = new Predicate<PaletteEntry>() {
+        /**
+         * {@inheritDoc}
+         */
+        public boolean apply(PaletteEntry input) {
+            return input.isVisible();
+        }
+    };
+
+}
diff --git a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/suite/AllTestSuite.java b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/suite/AllTestSuite.java
index bee4f57..6c7cf21 100644
--- a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/suite/AllTestSuite.java
+++ b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/suite/AllTestSuite.java
@@ -353,6 +353,7 @@
         suite.addTestSuite(GroupingContentProviderTest.class);
         suite.addTestSuite(GroupingContentProviderByContainingTest.class);
         suite.addTestSuite(PaletteViewManagementTest.class);
+        suite.addTestSuite(PaletteManagerAfterVSMSelectionChange.class);
         suite.addTestSuite(CreateRepresentationFromSessionTest.class);
         suite.addTestSuite(ToolCreationPositionTest.class);
         suite.addTestSuite(LocalSessionViewTest.class);