    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/layertree/MoveTreeNodeListener.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.framework.log.ILogger;
    047    import org.deegree.framework.log.LoggerFactory;
    048    import org.deegree.portal.Constants;
    049    import org.deegree.portal.context.Layer;
    050    import org.deegree.portal.context.LayerGroup;
    051    import org.deegree.portal.context.LayerList;
    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.MapModelVisitor;
    056    import org.deegree.portal.context.ViewContext;
    057
    058    /**
    059     * TODO add class documentation here
    060     *
    061     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    062     * @author last edited by: $Author: apoth $
    063     *
    064     * @version $Revision: 21879 $, $Date: 2010-01-12 09:36:59 +0100 (Di, 12. Jan 2010) $
    065     */
    066    public class MoveTreeNodeListener extends AbstractListener {
    067
    068        private static final ILogger LOG = LoggerFactory.getLogger( MoveTreeNodeListener.class );
    069
    070        /*
    071         * (non-Javadoc)
    072         *
    073         * @see
    074         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
    075         * , org.deegree.enterprise.control.ajax.ResponseHandler)
    076         */
    077        @SuppressWarnings("unchecked")
    078        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
    079                                throws IOException {
    080
    081            Map<Object, Object> parameter = event.getParameter();
    082            String node = (String) parameter.get( "node" );
    083            String parentNode = (String) parameter.get( "parentNode" );
    084            String beforeNode = (String) parameter.get( "beforeNode" );
    085            ViewContext vc = (ViewContext) event.getSession().getAttribute( Constants.CURRENTMAPCONTEXT );
    086            MapModel mapModel = vc.getGeneral().getExtension().getMapModel();
    087            MapModelEntry mme = mapModel.getMapModelEntryByIdentifier( node );
    088            LayerGroup parent = (LayerGroup) mapModel.getMapModelEntryByIdentifier( parentNode );
    089
    090            if ( mme != null ) {
    091                // update tree/map model
    092                MapModelEntry antecessor = null;
    093                if ( beforeNode != null && beforeNode.length() > 0) {
    094                    antecessor = mapModel.getMapModelEntryByIdentifier( beforeNode );
    095                    List<MapModelEntry> list = parent.getMapModelEntries();
    096
    097                    for ( int i = 0; i < list.size(); i++ ) {
    098                        if ( list.get( i ).equals( antecessor ) ) {
    099                            if ( i > 0 ) {
    100                                antecessor = list.get( i - 1 );
    101                            } else {
    102                                antecessor = null;
    103                            }
    104                            break;
    105                        }
    106                    }
    107                }
    108                mapModel.move( mme, parent, antecessor, (beforeNode != null && beforeNode.length() > 0) );
    109
    110                // update WMC layer list
    111                LayerList layerList = vc.getLayerList();
    112                final List<Layer> layers = new ArrayList<Layer>( layerList.getLayers().length );
    113                try {
    114                    mapModel.walkLayerTree( new MapModelVisitor() {
    115
    116                        public void visit( LayerGroup layerGroup )
    117                                                throws Exception {
    118                            // ignore
    119                        }
    120
    121                        public void visit( MMLayer layer )
    122                                                throws Exception {
    123                            layers.add( layer.getLayer() );
    124                        }
    125                    } );
    126                    layerList.setLayers( layers.toArray( new Layer[layers.size()] ) );
    127                    LayerBean[] beans = new LayerBean[layers.size()];
    128                    for ( int i = 0; i < beans.length; i++ ) {
    129                        beans[i] = new LayerBean( layers.get( i ).getServer().getTitle(), layers.get( i ).getName(),
    130                                                  layers.get( i ).getServer().getService() + " "
    131                                                                          + layers.get( i ).getServer().getVersion(),
    132                                                  layers.get( i ).getServer().getOnlineResource().toURI().toASCIIString(),
    133                                                  layers.get( i ).getFormatList().getCurrentFormat().getName() );
    134                    }
    135                    responseHandler.writeAndClose( false, beans );
    136                } catch ( Exception e ) {
    137                    LOG.logError( e );
    138                    responseHandler.writeAndClose( "ERROR: " + e.getMessage() );
    139                    return;
    140                }
    141
    142            }
    143
    144        }
    145
    146    }