layerhandler.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,179 行 · 第 1/3 页

JAVA
1,179
字号
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/LayerHandler.java,v $// $RCSfile: LayerHandler.java,v $// $Revision: 1.7.2.9 $// $Date: 2006/08/09 21:01:18 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap;import java.beans.PropertyVetoException;import java.beans.beancontext.BeanContext;import java.io.Serializable;import java.util.Properties;import java.util.Vector;import com.bbn.openmap.event.LayerEvent;import com.bbn.openmap.event.LayerListener;import com.bbn.openmap.event.LayerSupport;import com.bbn.openmap.plugin.PlugIn;import com.bbn.openmap.plugin.PlugInLayer;import com.bbn.openmap.util.ComponentFactory;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.PropUtils;/** * The LayerHandler is a component that keeps track of all Layers for the * MapBean, whether or not they are currently part of the map or not. It is able * to dynamically add and remove layers from the list of available layers. * Whether a layer is added to the MapBean depends on the visibility setting of * the layer. If Layer.isVisible() is true, the layer will be added to the * MapBean. There are methods within the LayerHandler that let you change the * visibility setting of a layer. <p/> <p/> The LayerHandler is able to take a * Properties object, and create layers that are defined within it. The key * property is "layers", which may or may not have a prefix for it. If that * property does have a prefix (prefix.layers, i.e. openmap.layers), then that * prefix has to be known and passed in to the contructor or init method. This * layers property should fit the general openmap marker list paradigm, where * the marker names are listed in a space separated list, and then each marker * name is used as a prefix for the properties for a particular layer. As a * minimum, each layer needs to have the class and prettyName properties * defined. The class property should define the class name to use for the * layer, and the prettyName property needs to be a name for the layer to be * used in the GUI. Any other property that the particular layer can use should * be listed in the Properties, with the applicable marker name as a prefix. * Each layer should have its available properties defined in its documentation. * For example: <p/> <p/> *  * <pre> *           &lt;p/&gt; *             openmap.layers=marker1 marker2 (etc) *             marker1.class=com.bbn.openmap.layer.GraticuleLayer *             marker1.prettyName=Graticule Layer *             # false is default *             marker1.addToBeanContext=false *           &lt;p/&gt; *             marker2.class=com.bbn.openmap.layer.shape.ShapeLayer *             marker2.prettyName=Political Boundaries *             marker2.shapeFile=pathToShapeFile *             marker2.spatialIndex=pathToSpatialIndexFile *             marker2.lineColor=FFFFFFFF *             marker2.fillColor=FFFF0000 *           &lt;p/&gt; * </pre> *  * <p/> <p/> <p/> The LayerHandler is a SoloMapComponent, which means that for a * particular map, there should only be one of them. When a LayerHandler is * added to a BeanContext, it will look for a MapBean to connect to itself as a * LayerListener so that the MapBean will receive LayerEvents - this is the * mechanism that adds and removes layers on the map. If more than one MapBean * is added to the BeanContext, then the last MapBean added will be added as a * LayerListener, with any prior MapBeans added as a LayerListener removed from * the LayerHandler. The MapHandler controls the behavior of multiple * SoloMapComponent addition to the BeanContext. */public class LayerHandler extends OMComponent implements SoloMapComponent,        Serializable {    /**     * Property for space separated layers. If a prefix is needed, just use the     * methods that let you use the prefix - don't worry about the period, it     * will be added automatically.     */    public static final String layersProperty = "layers";    /**     * Property for space separated layers to be displayed at startup. If a     * prefix is needed, just use the methods that let you use the prefix -     * don't worry about the period, it will be added automatically.     */    public static final String startUpLayersProperty = "startUpLayers";    /**     * Flag to set synchronous threading on the LayerHandler, telling it to     * react to layer order changes and layer visibility requests within the     * calling thread. By default, this action is true. Setting it to false may     * eliminate pauses in GUI reactions by offloading work done by layers being     * added to the MapBean, but there have been reports that the asynchronous     * nature of the threading queue may be causing an unexpected state in layer     * order and/or availability under certain intense layer management     * conditions (created by automated processes, for example).     */    public static final String SynchronousThreadingProperty = "synchronousThreading";    /**     * The object holding on to all LayerListeners interested in the layer     * arrangement and availability. Not expected to be null.     */    protected transient LayerSupport listeners = new LayerSupport(this);    /**     * The list of all layers, even the ones that are not part of the map.     */    protected Layer[] allLayers = new Layer[0];    /**     * This handle is only here to keep it appraised of layer prefix names.     */    protected PropertyHandler propertyHandler;    /**     * If you use this constructor, the LayerHandler expects that the layers     * will be created and added later, either by addLayer() or init().     */    public LayerHandler() {}    /**     * Start the LayerHandler, and have it create all the layers as defined in a     * properties file.     *      * @param props properties as defined in an openmap.properties file.     */    public LayerHandler(Properties props) {        init(null, props);    }    /**     * Start the LayerHandler, and have it create all the layers as defined in a     * properties file.     *      * @param prefix the prefix for the layers and startUpLayers properties, as     *        if they are listed as prefix.layers, and prefix.startUpLayers.     * @param props properties as defined in an openmap.propertites file.     */    public LayerHandler(String prefix, Properties props) {        init(prefix, props);    }    /**     * Start the LayerHandler with configured layers.     */    public LayerHandler(Layer[] layers) {        init(layers);    }    /**     * Extension of the OMComponent. If the LayerHandler is created by the     * ComponentFactory (via the PropertyHandler), this method will be called     * automatically. For the OpenMap applications, this method is rigged to     * handle the openmap.layers property by calling init("openmap", props). If     * you are using the LayerHandler in a different setting, then you might     * want to just call init() directly, or extend this class and have     * setProperties do what you want.     */    public void setProperties(String prefix, Properties props) {        super.setProperties(prefix, props);        // Whoa! We used to replace the prefix provided to this method with        // 'openmap',        // AKA Environment.OpenMapPrefix, but that seems rude and hackish. We're        // going to have the getLayers(prefix, props) method use the prefix        // passed in, and if the layerHandler prefix.layers can't be found,        // we'll revert to looking for the openmap.layers property, just to be        // backward compatible.        // init(Environment.OpenMapPrefix, props);        init(prefix, props);    }    /**     * Initialize the LayerHandler by having it construct it's layers from a     * properties object. The properties should be created from an     * openmap.properties file.     *      * @param prefix the prefix to use for the layers and startUpLayers     *        properties.     * @param props properties as defined in an openmap.properties file.     */    public void init(String prefix, Properties props) {        prefix = PropUtils.getScopedPropertyPrefix(prefix);        init(getLayers(prefix, props));        getListeners().setSynchronous(PropUtils.booleanFromProperties(props,                prefix + SynchronousThreadingProperty,                getListeners().isSynchronous()));    }    /**     * Initialize the LayerHandler by having it construct it's layers from a URL     * containing an openmap.properties file.     *      * @param url a url for a properties file.     */    public void init(java.net.URL url) {        init(null, url);    }    /**     * Initialize the LayerHandler by having it construct it's layers from a URL     * containing an openmap.properties file.     *      * @param prefix the prefix to use for the layers and startUpLayers     *        properties.     * @param url a url for a properties file.     */    public void init(String prefix, java.net.URL url) {        try {            java.io.InputStream in = url.openStream();            Properties props = new Properties();            props.load(in);            init(getLayers(prefix, props));        } catch (java.net.MalformedURLException murle) {            Debug.error("LayerHandler.init(URL): " + url                    + " is not a valid URL");        } catch (java.io.IOException e) {            Debug.error("LayerHandler.init(URL): Caught an IOException");        }    }    /**     * Initialize from an array of layers. This will cause the LayerListeners,     * if they exist, to update themselves with the current list of layers. This     * will check to add layers to the MapHandler.     *      * @param layers the initial array of layers.     */    public void init(Layer[] layers) {        // Should get rid of the old layers properly.        removeAll();        // OK, we need to check the allLayers array, because at this point it        // could still be holding non-removable layers. If we just replace them,        // we've broken the contract of nonremoval. Move the nonremovable layers        // to the bottom and put the new layers on top. We also need to check to        // make sure that any duplicate layers on either list are parsed down to        // one layer. We use the Vector.contains() method for that check.        if (allLayers != null && allLayers.length > 0) {            int lLength = (layers != null ? layers.length : 0);            Vector newLayers = new Vector(allLayers.length + lLength);            if (layers != null) {                for (int i = 0; i < lLength; i++) {                    if (!newLayers.contains(layers[i])) {                        newLayers.add(layers[i]);                    }                }            }            for (int i = 0; i < allLayers.length; i++) {                if (!newLayers.contains(allLayers[i])) {                    newLayers.add(allLayers[i]);                }            }            layers = new Layer[newLayers.size()];            layers = (Layer[]) newLayers.toArray(layers);        }        setLayers(layers);        // This should work for layers being reloaded from the PropertyHandler,        // it's better than doing it in the getLayers(...) method below        // (getLayers() is called before init()). For the        // initial LayerHandler construction and Layer creation in an        // application, the BeanContext should be null at this point, so this        // method call will do nothing. But for resetting the layers with new        // ones, they will get dumped into the BeanContext/MapHandler.        addLayersToBeanContext(layers);    }    public void setPropertyHandler(PropertyHandler ph) {        propertyHandler = ph;    }    public PropertyHandler getPropertyHandler() {        return propertyHandler;    }    /**     * This is the method that gets used to parse the layer properties from an     * openmap.properties file, where the layer marker names are listed under a     * layers property, and each layer is then represented by a marker.class     * property, and a maker.prettyName property.     *      * @param p properties containing layers property, the startupLayers     *        property listing the layers to make visible immediately, and the     *        layer properties as well.     * @return Layer[] of layers created from the properties.     */    protected Layer[] getLayers(Properties p) {        return getLayers(null, p);    }    /**     * This is the method that gets used to parse the layer properties from an     * openmap.properties file, where the layer marker names are listed under a     * prefix.layers property, and each layer is then represented by a     * marker.class property, and a maker.prettyName property.     *      * @param prefix the prefix to use to use for the layer list (layers)     *        property and the startUpLayers property. If it is not null, this     *        will cause the method to looke for prefix.layers and     *        prefix.startUpLayers.     * @param p the properties to build the layers from.     * @return Layer[]     */    protected Layer[] getLayers(String prefix, Properties p) {        Debug.message("layerhandler",                "LayerHandler: Getting new layers from properties...");        // First, load the layer marker names into a vector for later        // use        Vector startuplayers;        Vector layersValue;        prefix = PropUtils.getScopedPropertyPrefix(prefix);        String layersValueString = p.getProperty(prefix + layersProperty);        String startupLayersValueString = p.getProperty(prefix                + startUpLayersProperty);        if (layersValueString == null) {            layersValueString = p.getProperty(PropUtils.getScopedPropertyPrefix(Environment.OpenMapPrefix)                    + layersProperty);        }        if (startupLayersValueString == null) {            startupLayersValueString = p.getProperty(PropUtils.getScopedPropertyPrefix(Environment.OpenMapPrefix)                    + startUpLayersProperty);        }        startuplayers = PropUtils.parseSpacedMarkers(startupLayersValueString);        layersValue = PropUtils.parseSpacedMarkers(layersValueString);        if (startuplayers.isEmpty()) {            Debug.message("layerhandler",                    "LayerHandler: No layers on startup list");        }        if (layersValue.isEmpty()) {            Debug.error("LayerHandler.getLayers(): No property \""                    + layersProperty + "\" found in properties.");            return new Layer[0];        } else {            if (Debug.debugging("layerhandler")) {                Debug.output("LayerHandler: Layer markers found = "                        + layersValue);            }        }        Layer[] layers = getLayers(layersValue, startuplayers, p);        // You don't want to call addLayersToBeanContext here, it sets up a        // cycle. The layers are not yet set in the LayerHandler, so the        // LayerHandle won't know to ignore them when they show up in        // findAndInit(). The one thing this call did, however, is get the        // BeanContext to the layers before the startup layers were added to the        // MapBean. It's possible that without this call, layers that build        // their OMGraphicLists once may not have the BeanContext resources they        // need in order to build that list.        // addLayersToBeanContext(layers);        return layers;    }    /**     * A static method that lets you pass in a Properties object, along with two     * Vectors of strings, each Vector representing marker names for layers     * contained in the Properties. <p/> If a PlugIn is listed in the     * properties, the LayerHandler will create a PlugInLayer for it and set the     * PlugIn in that layer.     *      * @param layerList Vector of marker names to use to inspect the properties     *        with.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?