/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2012 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.gwt.client.action.toolbar;

import java.util.List;

import org.geomajas.global.GeomajasConstant;
import org.geomajas.gwt.client.action.ToolbarAction;
import org.geomajas.gwt.client.i18n.I18nProvider;
import org.geomajas.gwt.client.map.feature.Feature;
import org.geomajas.gwt.client.map.feature.LazyLoadCallback;
import org.geomajas.gwt.client.map.layer.Layer;
import org.geomajas.gwt.client.map.layer.VectorLayer;
import org.geomajas.gwt.client.map.store.VectorLayerStore;
import org.geomajas.gwt.client.spatial.Bbox;
import org.geomajas.gwt.client.util.WidgetLayout;
import org.geomajas.gwt.client.widget.MapWidget;

import com.smartgwt.client.widgets.events.ClickEvent;

/**
 * Pan so that the selected items are in the center of the screen.
 * 
 * @author Frank Wynants
 */
public class PanToSelectionAction extends ToolbarAction {

	private MapWidget mapWidget;

	public PanToSelectionAction(MapWidget mapWidget) {
		super(WidgetLayout.iconPanToSelection, I18nProvider.getToolbar().panToSelectionTitle(),
				I18nProvider.getToolbar().panToSelectionTooltip());
		this.mapWidget = mapWidget;
	}

	/**
	 * Pan to the selected features.
	 */
	public void onClick(ClickEvent clickEvent) {
		// get the selected VectorLayer
		Layer<?> selectedLayer = mapWidget.getMapModel().getSelectedLayer();
		if (selectedLayer instanceof VectorLayer) {
			// iterate all features of the selected layer
			VectorLayerStore featureStore = ((VectorLayer) selectedLayer).getFeatureStore();
			featureStore.getFeatures(GeomajasConstant.FEATURE_INCLUDE_GEOMETRY, new LazyLoadCallback() {

				public void execute(List<Feature> response) {
					boolean success = false;
					Bbox selectionBounds = new Bbox(0, 0, 0, 0);
					for (Feature feature : response) {
						// if the feature is selected union the bounding box
						if (feature.isSelected()) {
							selectionBounds = selectionBounds.union(feature.getGeometry().getBounds());
							success = true;
						}
					}

					// only pan when their where really some items selected
					if (success) {
						mapWidget.getMapModel().getMapView().setCenterPosition(selectionBounds.getCenterPoint());
					}
				}
			});
		}
	}
}

@@ -483,60 +483,43 @@
 til.
+Array
 List;%0A
-%0A
 import 
-org.geomajas.global.GeomajasConstan
+java.util.Lis
 t;%0A
+%0A
 impo
@@ -708,238 +708,25 @@
 map.
-feature.LazyLoadCallback;%0Aimport org.geomajas.gwt.client.map.layer.Layer;%0Aimport org.geomajas.gwt.client.map.layer.VectorLayer;%0Aimport org.geomajas.gwt.client.map.store.VectorLayerStore;%0Aimport org.geomajas.gwt.client.spatial.Bbox
+layer.VectorLayer
 ;%0Aim
@@ -1400,775 +1400,190 @@
 %7B%0A%09%09
-// get the selected VectorLayer%0A%09%09Layer%3C?%3E selectedLayer = mapWidget.getMapModel().getSelectedLayer();%0A%09%09if (selectedLayer instanceof VectorLayer) %7B%0A%09%09%09// iterate all features of the selected layer%0A%09%09%09VectorLayerStore featureStore = ((VectorLayer) selectedLayer).getFeatureStore();%0A%09%09%09featureStore.getFeatures(GeomajasConstant.FEATURE_INCLUDE_GEOMETRY, new LazyLoadCallback() %7B%0A%0A%09%09%09%09public void execute(List%3CFeature%3E response) %7B%0A%09%09%09%09%09boolean success = false;%0A%09%09%09%09%09Bbox selectionBounds = new Bbox(0, 0, 0, 0);%0A%09%09%09%09%09for (Feature feature : response) %7B%0A%09%09%09%09%09%09// if the feature is selected union the bounding box%0A%09%09%09%09%09%09if (feature.isSelected()) %7B%0A%09%09%09%09%09%09%09selectionBounds = selectionBounds.union(feature.getGeometry().getBounds());%0A%09%09%09%09%09%09%09success = true;%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%0A%09%09%09
+List%3CFeature%3E features = new ArrayList%3CFeature%3E();%0A %09%09for (VectorLayer layer : mapWidget.getMapModel().getVectorLayers()) %7B%0A%09%09%09features.addAll(layer.getSelectedFeatureValues());%0A%09%09%7D%0A
 %09%09//
@@ -1588,19 +1588,20 @@
 // only 
-pan
+zoom
  when th
@@ -1605,10 +1605,10 @@
  the
-i
 r
+e
  whe
@@ -1641,31 +1641,37 @@
 ted%0A
-%09%09%09
 %09%09if (
-success
+features.size() %3E 0
 ) %7B%0A
-%09%09%09
 %09%09%09m
@@ -1697,91 +1697,30 @@
 l().
-getMapView().setCenterPosition(selectionBounds.getCenterPoint());%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D
+panToFeatures(features
 );%0A%09

use panToFeatures() of mapModel + apply on all features in analogy to zoomToFeatures()