layerhandler.java

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

JAVA
1,179
字号
        // Need to make this call before thinking about adding the Layer to the        // BeanContext, so when the Layer shows up in the findAndInit() method,        // it's already a part of the Layer list. One potential problem that may        // occur is that the Layer might not be ready to be added to the map and        // to other application components that get LayerEvents from the        // LayerHandler, and they will all know about the layer being in the        // stack after the setLayers() call.        setLayers(newLayers);        // Add the layer to the BeanContext, if it wants to be and it's not        // already in a BeanContext. Thought about making the BC check look for        // the same BC as the LayerHandler is a part of, but it's probably        // better just to do a null check in case the Layer is a member of a        // more restricted BeanContext with limited access.        BeanContext bc = getBeanContext();        if (bc != null && layer.getAddToBeanContext()                && layer.getBeanContext() == null) {            bc.add(layer);        }    }    /**     * Add a layer to a certain position in the layer array. If the position is     * 0 or less, the layer is put up front (on top). If the position is greater     * thatn the length of the current array, the layer is put at the end, (on     * the bottom).     *      * @param layer the layer to add.     * @param position the array index to place it.     * @param addedLayerTurnedOn turn the layer on.     * @deprecated the layer will be turned on if its visibility is true.     */    public void addLayer(Layer layer, int position, boolean addedLayerTurnedOn) {        layer.setVisible(addedLayerTurnedOn);        addLayer(layer, position);    }    /**     * Remove a layer from the list of potentials.     *      * @param layer to remove.     */    public void removeLayer(Layer layer) {        if (layer != null && layer.isRemovable()) {            int index = -1;            for (int i = 0; i < allLayers.length; i++) {                if (layer == allLayers[i]) {                    index = i;                    break;                }            }            // If the layer is actually there...            if (index != -1) {                removeLayer(allLayers, index);            }        } else {            if (layer != null) {                Debug.error("LayerHandler: received command to remove "                        + layer.getName()                        + ", which has been designated as *NOT* removeable");                throw new com.bbn.openmap.util.HandleError("LayerHandler commanded to delete a layer ("                        + layer.getName() + ") that is not removeable");            }        }    }    /**     * Remove a layer from the list of potentials.     *      * @param index of layer in the layer array. Top-most is first.     */    public void removeLayer(int index) {        if (index >= 0 && index < allLayers.length) {            removeLayer(allLayers, index);        }    }    public boolean hasLayer(Layer l) {        Layer[] layers = allLayers;        for (int i = 0; i < layers.length; i++) {            if (layers[i] == l) {                return true;            }        }        return false;    }    /**     * Remove all the layers (that are marked as removeable).     */    public void removeAll() {        if (allLayers == null || allLayers.length == 0) {            return;        }        BeanContext bc = getBeanContext();        Layer[] oldLayers = allLayers;        Vector nonRemoveableLayers = null;        for (int i = 0; i < oldLayers.length; i++) {            Layer layer = oldLayers[i];            if (layer.isRemovable()) {                turnLayerOn(false, layer);                layer.clearListeners();                if (bc != null) {                    // Remove the layer from the BeanContext                    bc.remove(layer);                }                oldLayers[i] = null;            } else {                if (nonRemoveableLayers == null) {                    nonRemoveableLayers = new Vector(oldLayers.length);                }                nonRemoveableLayers.add(layer);            }        }        if (nonRemoveableLayers != null) {            allLayers = new Layer[nonRemoveableLayers.size()];            allLayers = (Layer[]) nonRemoveableLayers.toArray(allLayers);        } else {            allLayers = new Layer[0];        }        setLayers(allLayers);        // I know this is bad but it seems to work, forcing the        // memory from old, deleted layers to be freed. With such a        // drastic method call as removeAll, this should be OK.        System.gc();    }    /**     * The version that does the work. The other two functions do sanity checks.     * Calls setLayers(), and removes the layer from the BeanContext.     *      * @param currentLayers the current layers handled in the LayersMenu.     * @param index the validated index of the layer to remove.     */    protected void removeLayer(Layer[] currentLayers, int index) {        Layer rLayer = currentLayers[index];        if (!rLayer.isRemovable()) {            Debug.error("LayerHandler: received command to remove "                    + rLayer.getName()                    + ", which has been designated as *NOT* removeable");            return;        }        rLayer.setVisible(false);        Layer[] newLayers = new Layer[currentLayers.length - 1];        System.arraycopy(currentLayers, 0, newLayers, 0, index);        System.arraycopy(currentLayers,                index + 1,                newLayers,                index,                currentLayers.length - index - 1);        // Remove the layer to the BeanContext, if it wants to be.        BeanContext bc = getBeanContext();        if (bc != null) {            bc.remove(rLayer);        }        turnLayerOn(false, rLayer);        rLayer.clearListeners();        rLayer = null;        // Shouldn't call this, but it's the only thing that seems to        // make it work...        if (Debug.debugging("helpgc")) {            System.gc();        }        setLayers(newLayers);    }    /**     * Take a layer that the LayersMenu knows about, that may or may not be a     * part of the map, and change its visibility by adding/removing it from the     * MapBean.     *      * @param setting true to add layer to the map.     * @param index the index of the layer to turn on/off.     * @return true of index represented a layer, false if not or if something     *         went wrong.     */    public boolean turnLayerOn(boolean setting, int index) {        try {            turnLayerOn(setting, allLayers[index]);            return true;        } catch (ArrayIndexOutOfBoundsException aoobe) {            // Do nothing...        } catch (NullPointerException npe) {            // Do nothing...        }        return false;    }    /**     * Take a layer that the LayersMenu knows about, that may or may not be a     * part of the map, and change its visibility by adding/removing it from the     * MapBean. If the layer is not found, it's added and the visibility depends     * on the setting parameter.     *      * @param setting true to add layer to the map.     * @param layer the layer to turn on.     * @return true if the layer was found, false if not or if something went     *         wrong.     */    public boolean turnLayerOn(boolean setting, Layer layer) {        if ((setting && !layer.isVisible()) || (!setting && layer.isVisible())) {            if (Debug.debugging("layerhandler")) {                Debug.output("LayerHandler: turning " + layer.getName()                        + (setting ? " on" : " off"));            }            layer.setVisible(setting);            getListeners().pushLayerEvent(LayerEvent.REPLACE, getMapLayers());            return true;        }        return false;    }    /**     * Called from childrenAdded(), when a new component is added to the     * BeanContext, and from setBeanContext() when the LayerHandler is initially     * added to the BeanContext. This method takes the iterator provided when     * those methods are called, and looks for the objects that the LayerHandler     * is interested in, namely, the MapBean, the PropertyHandler, or any other     * LayerListeners. The LayerHandler handles multiple LayerListeners, and if     * one is found, it is added to the LayerListener list. If a PropertyHandler     * is found, then init() is called, effectively resetting the layers held by     * the LayerHandler.     *      * @param someObj an Object being added to the MapHandler/BeanContext.     */    public void findAndInit(Object someObj) {        if (someObj instanceof com.bbn.openmap.event.LayerListener) {            Debug.message("layerhandler", "LayerHandler found a LayerListener.");            addLayerListener((LayerListener) someObj);        }        if (someObj instanceof Layer) {            if (Debug.debugging("layerhandler")) {                Debug.output("LayerHandler found a Layer |"                        + ((Layer) someObj).getName() + "|");            }            if (!hasLayer((Layer) someObj)) {                addLayer((Layer) someObj, 0);            }        }        if (someObj instanceof PropertyHandler) {            // Used to notify the PropertyHandler of used property            // prefix names.            setPropertyHandler((PropertyHandler) someObj);        }    }    /**     * A BeanContextMembershipListener interface method, which is called when     * new objects are removed from the BeanContext. If a LayerListener or Layer     * is found on this list, it is removed from the list of LayerListeners.     *      * @param someObj an Object being removed from the MapHandler/BeanContext.     */    public void findAndUndo(Object someObj) {        if (someObj instanceof com.bbn.openmap.event.LayerListener) {            Debug.message("layerhandler",                    "LayerListener object is being removed");            removeLayerListener((LayerListener) someObj);        }        if (someObj instanceof Layer) {            removeLayer((Layer) someObj);        }        if (someObj instanceof PropertyHandler                && someObj == getPropertyHandler()) {            setPropertyHandler(null);        }    }    /**     * Add layers to the BeanContext, if they want to be. Since the BeanContext     * is a Collection, it doesn't matter if a layer is already there because     * duplicates aren't allowed.     *      * @param layers layers to add, if they want to be.     */    public void addLayersToBeanContext(Layer[] layers) {        BeanContext bc = getBeanContext();        if (bc == null || layers == null) {            return;        }        for (int i = 0; i < layers.length; i++) {            if (layers[i].getAddToBeanContext()                    && layers[i].getBeanContext() == null) {                bc.add(layers[i]);            }        }    }    /**     * Add layers to the BeanContext, if they want to be. Since the BeanContext     * is a Collection, it doesn't matter if a layer is already there because     * duplicates aren't allowed.     *      * @param layers layers to add, if they want to be.     */    public void removeLayersFromBeanContext(Layer[] layers) {        BeanContext bc = getBeanContext();        if (bc == null || layers == null) {            return;        }        for (int i = 0; i < layers.length; i++) {            bc.remove(layers[i]);        }    }    /**     * Called when the LayerHandler is added to a BeanContext. This method calls     * findAndInit() to hook up with any objects that may already be added to     * the BeanContext. A BeanContextChild method.     *      * @param in_bc BeanContext.     */    public void setBeanContext(BeanContext in_bc) throws PropertyVetoException {        if (in_bc != null) {            if (Debug.debugging("layerhandler")) {                Debug.output("LayerHandler.setBeanContext()");            }            in_bc.addBeanContextMembershipListener(this);            beanContextChildSupport.setBeanContext(in_bc);            // This will cause findAndInit to be called on the layers and            // plugins after they are added to the MapHandler, so they can find            // the components they need before they get added to the            // map (if they are to be added at startup).            addLayersToBeanContext(getLayers());            // Calling this here may (will) cause the MapBean to get            // loaded with its initial layers, since it is a            // LayerListener.            findAndInit(in_bc.iterator());        }    }    public void setSynchronousThreading(boolean s) {        getListeners().setSynchronous(s);    }    public boolean isSynchronousThreading() {        return getListeners().isSynchronous();    }    public Properties getProperties(Properties props) {        props = super.getProperties(props);        props.put(PropUtils.getScopedPropertyPrefix(this)                + SynchronousThreadingProperty,                Boolean.toString(getListeners().isSynchronous()));        return props;    }    public Properties getPropertyInfo(Properties props) {        props = super.getPropertyInfo(props);        String internString = i18n.get(LayerHandler.class,                SynchronousThreadingProperty,                I18n.TOOLTIP,                "Launch new threads to do work.");        props.put(SynchronousThreadingProperty, internString);        internString = i18n.get(LayerHandler.class,                SynchronousThreadingProperty,                "Synchronous Threading");        props.put(SynchronousThreadingProperty + LabelEditorProperty,                internString);        props.put(SynchronousThreadingProperty + EditorProperty,                "com.bbn.openmap.util.propertyEditor.YesNoPropertyEditor");        return props;    }}

⌨️ 快捷键说明

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