    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/layertree/SetVisibilityTreeNodeListener.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.util.ArrayList;
    040    import java.util.List;
    041    import java.util.Map;
    042
    043    import org.deegree.enterprise.control.ajax.AbstractListener;
    044    import org.deegree.enterprise.control.ajax.ResponseHandler;
    045    import org.deegree.enterprise.control.ajax.WebEvent;
    046    import org.deegree.portal.Constants;
    047    import org.deegree.portal.context.Layer;
    048    import org.deegree.portal.context.LayerGroup;
    049    import org.deegree.portal.context.MapModel;
    050    import org.deegree.portal.context.MapModelEntry;
    051    import org.deegree.portal.context.ViewContext;
    052
    053    /**
    054     * TODO add class documentation here
    055     *
    056     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    057     * @author last edited by: $Author: apoth $
    058     *
    059     * @version $Revision: 20972 $, $Date: 2009-11-23 16:53:52 +0100 (Mo, 23. Nov 2009) $
    060     */
    061    public class SetVisibilityTreeNodeListener extends AbstractListener {
    062
    063        /*
    064         * (non-Javadoc)
    065         *
    066         * @see
    067         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
    068         * , org.deegree.enterprise.control.ajax.ResponseHandler)
    069         */
    070        @SuppressWarnings("unchecked")
    071        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
    072                                throws IOException {
    073            Map<Object, Object> parameter = event.getParameter();
    074            String node = (String) parameter.get( "node" );
    075            boolean hidden = (Boolean) parameter.get( "hidden" );
    076            ViewContext vc = (ViewContext) event.getSession().getAttribute( Constants.CURRENTMAPCONTEXT );
    077            MapModel mapModel = vc.getGeneral().getExtension().getMapModel();
    078            MapModelEntry mme = mapModel.getMapModelEntryByIdentifier( node );
    079            if ( mme != null ) {
    080                mme.setHidden( hidden );
    081                if ( mme instanceof LayerGroup ) {
    082                    recurseDown( hidden, (LayerGroup) mme );
    083                }
    084                recurseUp( mme );
    085            }
    086            List<String> visibleLayers = new ArrayList<String>();
    087            Layer[] layers = vc.getLayerList().getLayers();
    088            for ( Layer layer : layers ) {
    089                if ( !layer.isHidden() ) {
    090                    visibleLayers.add( layer.getName() );
    091                }
    092            }
    093            String[] tmp = visibleLayers.toArray( new String[visibleLayers.size()] );
    094            responseHandler.writeAndClose( false, tmp );
    095        }
    096
    097        /**
    098         * @param mme
    099         */
    100        private void recurseUp( MapModelEntry mme ) {
    101            LayerGroup layerGroup = mme.getParent();
    102            if ( layerGroup != null ) {
    103                List<MapModelEntry> list = layerGroup.getMapModelEntries();
    104                boolean visible = false;
    105                for ( MapModelEntry mapModelEntry : list ) {
    106                    if ( !mapModelEntry.isHidden() ) {
    107                        visible = true;
    108                        break;
    109                    }
    110                }
    111                layerGroup.setHidden( !visible );
    112                recurseUp( layerGroup );
    113            }
    114        }
    115
    116        /**
    117         *
    118         * @param hidden
    119         * @param layerGroup
    120         */
    121        private void recurseDown( boolean hidden, LayerGroup layerGroup ) {
    122            List<MapModelEntry> list = layerGroup.getMapModelEntries();
    123            for ( int i = 0; i < list.size(); i++ ) {
    124                MapModelEntry mapModelEntry = list.get( i );
    125                mapModelEntry.setHidden( hidden );
    126                if ( mapModelEntry instanceof LayerGroup ) {
    127                    recurseDown( hidden, (LayerGroup) mapModelEntry );
    128                }
    129            }
    130        }
    131
    132    }