Index: ClearSelectionExtension.java
===================================================================
--- ClearSelectionExtension.java	(revision 44599)
+++ ClearSelectionExtension.java	(revision 44600)
@@ -22,18 +22,15 @@
  */
 package org.gvsig.app.extension;
 
+import java.util.List;
 import org.gvsig.andami.IconThemeHelper;
-import org.gvsig.andami.messages.NotificationManager;
 import org.gvsig.andami.plugins.Extension;
 import org.gvsig.app.ApplicationLocator;
 import org.gvsig.app.ApplicationManager;
 import org.gvsig.app.project.documents.view.ViewDocument;
 import org.gvsig.app.project.documents.view.gui.IView;
 import org.gvsig.fmap.dal.exception.DataException;
-import org.gvsig.fmap.dal.exception.ReadException;
-import org.gvsig.fmap.dal.feature.FeatureSelection;
 import org.gvsig.fmap.dal.feature.FeatureStore;
-import org.gvsig.fmap.dal.feature.FeatureType;
 import org.gvsig.fmap.mapcontext.MapContext;
 import org.gvsig.fmap.mapcontext.layers.FLayer;
 import org.gvsig.fmap.mapcontext.layers.FLayers;
@@ -47,6 +44,7 @@
 public class ClearSelectionExtension extends Extension {
 
 
+    @Override
     public void execute(String s) {
         ApplicationManager application = ApplicationLocator.getManager();
 
@@ -56,47 +54,47 @@
         }
         ViewDocument document = view.getViewDocument();
         if (s.equalsIgnoreCase("selection-clear-view")) {
-            MapContext mapa = document.getMapContext();
             MapControl mapCtrl = view.getMapControl();
-            FLayers layers = mapa.getLayers();
-            boolean refresh = clearSelectionOfView(layers);
-            if (refresh) {
-                mapCtrl.drawMap(false);
+            FLayers layers = document.getMapContext().getLayers();
+            List<FLayer> activeLayers = layers.getLayers(
+                    FLayers.LAYER_ACTIVE
+                            .and(FLayers.LAYER_AVALAIBLE)
+                            .and(FLayers.LAYER_FLYRVECT)
+            );
+            List<FLayer> layersToDeselect;
+            if (activeLayers.size() >= 1) {
+                layersToDeselect = layers.getLayers(
+                        FLayers.LAYER_ACTIVE
+                                .and(FLayers.LAYER_AVALAIBLE)
+                                .and(FLayers.LAYER_FLYRVECT)
+                                .and(FLayers.LAYER_SELECTION_EMPTY.negate())
+                );
+            } else {
+                layersToDeselect = layers.getLayers(
+                        FLayers.LAYER_AVALAIBLE
+                                .and(FLayers.LAYER_FLYRVECT)
+                                .and(FLayers.LAYER_SELECTION_EMPTY.negate())
+                );
             }
+            clearSelectionOfView(layersToDeselect);
+            mapCtrl.drawMap(false);
             document.setModified(true);
         }
     }
 
-    private boolean clearSelectionOfView(FLayers layers) {
-        boolean refresh = false;
-
-        for (int i = 0; i < layers.getLayersCount(); i++) {
-            FLayer lyr = layers.getLayer(i);
-            if (lyr instanceof FLayers) {
-                refresh = refresh || clearSelectionOfView((FLayers) lyr);
-            } else if (lyr instanceof FLyrVect) {
-                FLyrVect lyrVect = (FLyrVect) lyr;
-                if (lyrVect.isActive()) {
-                    try {
-                        FeatureStore featureStore;
-
-                        featureStore = ((FLyrVect) lyr).getFeatureStore();
-                        if (!featureStore.getFeatureSelection().isEmpty()) {
-                            refresh = true;
-                        }
-                        featureStore.getFeatureSelection().deselectAll();
-                    } catch (ReadException e) {
-                        e.printStackTrace();
-                    } catch (DataException e) {
-                        // TODO Auto-generated catch block
-                        e.printStackTrace();
-                    }
-                }
+    private void clearSelectionOfView(List<FLayer> layers) {
+        for (FLayer layer : layers) {
+            FeatureStore featureStore;
+            featureStore = ((FLyrVect) layer).getFeatureStore();
+            try {
+                featureStore.getFeatureSelection().deselectAll();
+            } catch (DataException ex) {
+                ex.printStackTrace();
             }
         }
-        return refresh;
-    }
+    };
 
+    @Override
     public boolean isVisible() {
         ApplicationManager application = ApplicationLocator.getManager();
         IView view = (IView) application.getActiveComponent(ViewDocument.class);
@@ -108,6 +106,7 @@
         return mapa.getLayers().getLayersCount() > 0;
     }
 
+    @Override
     public boolean isEnabled() {
         ApplicationManager application = ApplicationLocator.getManager();
 
@@ -119,55 +118,43 @@
         if( document == null ) {
             return false;
         }
-        boolean hasActiveVectorLayers = false;
-        boolean hasSelection = false;
-        for (FLayer layer : document.getMapContext().getLayers()) {
-            if( layer.isActive() && layer.isAvailable() && layer instanceof FLyrVect ) {
-                try {
-                    hasActiveVectorLayers = true;
-                    FeatureStore store = ((FLyrVect)layer).getFeatureStore();
-                    FeatureSelection selection = store.getFeatureSelection();
-                    if( !selection.isAvailable() || selection.isEmpty() ) {
-                        return false;
-                    } else {
-                        hasSelection = true;
-                    }
-                } catch (Exception ex) {
-                }
+        
+        FLayers layers = document.getMapContext().getLayers();
+        List<FLayer> activeLayers = layers.getLayers(
+                FLayers.LAYER_ACTIVE
+        );
+        // if has active layers, at least one of them has to have selection
+        if (activeLayers.size()>=1) {
+            List<FLayer> activeLayersWithSelection = layers.getLayers(
+                    FLayers.LAYER_ACTIVE
+                            .and(FLayers.LAYER_AVALAIBLE)
+                            .and(FLayers.LAYER_FLYRVECT)
+                            .and(FLayers.LAYER_SELECTION_EMPTY.negate())
+            );
+            if (activeLayersWithSelection.size()>=1) {
+                return true;
+            } else{
+                return false;
             }
         }
-        return hasActiveVectorLayers && hasSelection;
-    }
 
-    private boolean hasVectorLayersWithSelection(FLayers layers) {
-        for (int i = 0; i < layers.getLayersCount(); i++) {
-            FLayer lyr = layers.getLayer(i);
-            if (lyr instanceof FLayers) {
-                if (hasVectorLayersWithSelection((FLayers) lyr)) {
-                    return true;
-                }
-            } else if (lyr instanceof FLyrVect) {
-                FLyrVect lyrVect = (FLyrVect) lyr;
-                if (lyrVect.isActive()) {
-                    if (lyrVect.isAvailable()) {
-                        try {
-                            if (!lyrVect.getFeatureStore().getFeatureSelection().isEmpty()) {
-                                return true;
-                            }
-                        } catch (DataException e) {
-                            e.printStackTrace();
-                            NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto", e);
-                        }
-                    }
-                }
-            }
+        List<FLayer> selectionLayers = layers.getLayers(
+                        FLayers.LAYER_AVALAIBLE
+                        .and(FLayers.LAYER_FLYRVECT)
+                        .and(FLayers.LAYER_SELECTION_EMPTY.negate())
+        );
+        // if there are not active layers, at least one of all has to have selection
+        if (selectionLayers.size() >= 1) {
+            return true;
         }
+        
         return false;
     }
 
     /**
      * @see org.gvsig.andami.plugins.IExtension#initialize()
      */
+    @Override
     public void initialize() {
         IconThemeHelper.registerIcon("action", "edit-clear", this);
     }
