    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/layertree/LayerTreeListener.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.URL;
    040    import java.nio.charset.Charset;
    041    import java.util.List;
    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.framework.util.MapUtils;
    047    import org.deegree.model.spatialschema.Envelope;
    048    import org.deegree.model.spatialschema.GeometryFactory;
    049    import org.deegree.model.spatialschema.Point;
    050    import org.deegree.portal.Constants;
    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: 25492 $, $Date: 2010-07-26 13:07:01 +0200 (Mo, 26. Jul 2010) $
    064     */
    065    public class LayerTreeListener extends AbstractListener {
    066
    067        private double scale = 0;
    068
    069        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
    070                                throws IOException {
    071            ViewContext vc = (ViewContext) event.getSession().getAttribute( Constants.CURRENTMAPCONTEXT );
    072            Point[] points = vc.getGeneral().getBoundingBox();
    073            Envelope bbox = GeometryFactory.createEnvelope( points[0].getPosition(), points[1].getPosition(),
    074                                                            points[0].getCoordinateSystem() );
    075            scale = MapUtils.calcScaleWMS111( vc.getGeneral().getWindow().width, vc.getGeneral().getWindow().height, bbox,
    076                                              bbox.getCoordinateSystem() );
    077
    078            StringBuilder sb = new StringBuilder( 10000 );
    079            sb.append( '[' );
    080            // return all nodes for root
    081            MapModel mapModel = vc.getGeneral().getExtension().getMapModel();
    082            List<LayerGroup> layerGroups = mapModel.getLayerGroups();
    083            for ( LayerGroup lg : layerGroups ) {
    084                sb.append( "{'text' : " );
    085                sb.append( "'" ).append( lg.getTitle() ).append( "'," );
    086                sb.append( "'id' : '" ).append( lg.getIdentifier() ).append( "'," );
    087                sb.append( "'checked': true," );
    088                sb.append( "'expanded': " ).append( lg.isExpanded() ).append( "," );
    089                sb.append( "'leaf' : false, 'cls' : 'folder' " );
    090                appendChildren( sb, lg );
    091                sb.append( "}" );
    092
    093            }
    094            sb.append( ']' );
    095            String charEnc = Charset.defaultCharset().displayName();
    096            responseHandler.setContentType( "application/json; " + charEnc );
    097            responseHandler.writeAndClose( sb.toString() );
    098        }
    099
    100        /**
    101         * @param sb
    102         * @param n
    103         * @param vc
    104         */
    105        private void appendChildren( StringBuilder sb, LayerGroup layerGroup ) {
    106
    107            List<MapModelEntry> mme = layerGroup.getMapModelEntries();
    108
    109            if ( mme.size() > 0 ) {
    110
    111                sb.append( ", 'children' : [" );
    112                for ( int i = 0; i < mme.size(); i++ ) {
    113                    if ( mme.get( i ) instanceof MMLayer ) {
    114                        MMLayer layer = (MMLayer) mme.get( i );
    115                        URL s = null;
    116                        if ( layer.getLayer().getStyleList().getCurrentStyle().getLegendURL() != null ) {
    117                            s = layer.getLayer().getStyleList().getCurrentStyle().getLegendURL().getOnlineResource();
    118                        }
    119                        sb.append( "{'text' : " );
    120                        sb.append( "'" ).append( layer.getTitle() ).append( "'," );
    121                        sb.append( "'id' : '" ).append( layer.getLayer().getExtension().getIdentifier() ).append( "'," );
    122                        if ( s != null ) {
    123                            sb.append( "'img' : '" ).append( s.toExternalForm() ).append( "'," );
    124                        }
    125                        if ( layer.getLayer().getAbstract() != null ) {
    126                            sb.append( "'qtip': '" ).append( layer.getLayer().getAbstract() ).append( "'," );
    127                        } else {
    128                            sb.append( "'qtip': '" ).append( layer.getTitle() ).append( "'," );
    129                        }
    130                        if ( scale < layer.getLayer().getExtension().getMinScaleHint()
    131                             || scale > layer.getLayer().getExtension().getMaxScaleHint() ) {
    132                            sb.append( "'disabled': true," );
    133                        }
    134                        sb.append( "'checked': " ).append( layer.isHidden() ? "false," : "true," );
    135                        sb.append( "'leaf' : true, 'cls' : 'file'}" );
    136                        if ( i < mme.size() - 1 ) {
    137                            sb.append( ',' );
    138                        }
    139                    } else {
    140                        LayerGroup lg = (LayerGroup) mme.get( i );
    141                        sb.append( "{'text' : " );
    142                        sb.append( "'" ).append( lg.getTitle() ).append( "'," );
    143                        sb.append( "'id' : '" ).append( lg.getIdentifier() ).append( "'," );
    144                        sb.append( "'checked': " ).append( !lg.isHidden() ).append( "," );
    145                        sb.append( "'expanded': " ).append( lg.isExpanded() ).append( "," );
    146                        sb.append( "'leaf' : false, 'cls' : 'folder' " );
    147                        appendChildren( sb, lg );
    148                        sb.append( "}" );
    149                        if ( i < mme.size() - 1 ) {
    150                            sb.append( ',' );
    151                        }
    152                    }
    153                }
    154                sb.append( "]" );
    155            }
    156
    157        }
    158
    159    }