    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/layertree/RemoveTreeNodeListener.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005     - Department of Geography, University of Bonn -
    006     and
    007     - lat/lon GmbH -
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035     ----------------------------------------------------------------------------*/
    036    package org.deegree.portal.standard.wms.control.layertree;
    037
    038    import java.io.IOException;
    039    import java.net.URISyntaxException;
    040    import java.util.ArrayList;
    041    import java.util.List;
    042    import java.util.Map;
    043
    044    import org.deegree.enterprise.control.ajax.AbstractListener;
    045    import org.deegree.enterprise.control.ajax.ResponseHandler;
    046    import org.deegree.enterprise.control.ajax.WebEvent;
    047    import org.deegree.framework.log.ILogger;
    048    import org.deegree.framework.log.LoggerFactory;
    049    import org.deegree.portal.Constants;
    050    import org.deegree.portal.context.Layer;
    051    import org.deegree.portal.context.LayerGroup;
    052    import org.deegree.portal.context.MMLayer;
    053    import org.deegree.portal.context.MapModel;
    054    import org.deegree.portal.context.MapModelEntry;
    055    import org.deegree.portal.context.ViewContext;
    056
    057    /**
    058     * TODO add class documentation here
    059     *
    060     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    061     * @author last edited by: $Author: apoth $
    062     *
    063     * @version $Revision: 21677 $, $Date: 2009-12-29 13:44:30 +0100 (Di, 29. Dez 2009) $
    064     */
    065    public class RemoveTreeNodeListener extends AbstractListener {
    066
    067        private static final ILogger LOG = LoggerFactory.getLogger( RemoveTreeNodeListener.class );
    068
    069        /*
    070         * (non-Javadoc)
    071         *
    072         * @see
    073         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
    074         * , org.deegree.enterprise.control.ajax.ResponseHandler)
    075         */
    076        @SuppressWarnings("unchecked")
    077        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
    078                                throws IOException {
    079            Map<Object, Object> parameter = event.getParameter();
    080            String node = (String) parameter.get( "node" );
    081            ViewContext vc = (ViewContext) event.getSession().getAttribute( Constants.CURRENTMAPCONTEXT );
    082            MapModel mapModel = vc.getGeneral().getExtension().getMapModel();
    083            MapModelEntry mme = mapModel.getMapModelEntryByIdentifier( node );
    084            mapModel.remove( mme );
    085            try {
    086                if ( mme instanceof MMLayer ) {
    087                    Layer layer = ( (MMLayer) mme ).getLayer();
    088
    089                    vc.getLayerList().removeLayer( layer.getName(),
    090                                                   layer.getServer().getOnlineResource().toURI().toASCIIString() );
    091
    092
    093                    responseHandler.writeAndClose( false, new String[] {( (MMLayer) mme ).getLayer().getName()} );
    094                } else {
    095                    LayerGroup layerGroup = (LayerGroup) mme;
    096                    List<Layer> layers = new ArrayList<Layer>();
    097                    collectLayers( layers, layerGroup );
    098                    String[] s = new String[layers.size()];
    099                    int i = 0;
    100                    for ( Layer layer : layers ) {
    101                        s[i++] = layer.getName();
    102                        vc.getLayerList().removeLayer( layer.getName(),
    103                                                       layer.getServer().getOnlineResource().toURI().toASCIIString() );
    104                    }
    105
    106                    responseHandler.writeAndClose( false, s );
    107                }
    108            } catch ( URISyntaxException e ) {
    109                LOG.logError( e );
    110                responseHandler.writeAndClose( "ERROR: " + e.getMessage() );
    111            }
    112
    113        }
    114
    115        private void collectLayers( List<Layer> layers, LayerGroup layerGroup ) {
    116            List<MMLayer> list;
    117            list = layerGroup.getLayers();
    118            for ( MMLayer mmLayer : list ) {
    119                layers.add( mmLayer.getLayer() );
    120            }
    121            List<LayerGroup> lgList = layerGroup.getLayerGroups();
    122            for ( LayerGroup lg : lgList ) {
    123                collectLayers( layers, lg );
    124            }
    125        }
    126
    127    }