layerhandler.java
来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,179 行 · 第 1/3 页
JAVA
1,179 行
* @param visibleLayerList Vector of marker names representing the layers * that should initially be set to visible when created, so that * those layers are initially added to the map. * @param p Properties object containing the layers properties. * @return Layer[] */ public static Layer[] getLayers(Vector layerList, Vector visibleLayerList, Properties p) { int nLayerNames = layerList.size(); Vector layers = new Vector(nLayerNames); for (int i = 0; i < nLayerNames; i++) { String layerName = (String) layerList.elementAt(i); String classProperty = layerName + ".class"; String className = p.getProperty(classProperty); if (className == null) { Debug.error("LayerHandler.getLayers(): Failed to locate property \"" + classProperty + "\"\n Skipping layer \"" + layerName + "\""); continue; } Object obj = ComponentFactory.create(className, layerName, p); Layer l; if (obj instanceof Layer) { l = (Layer) obj; } else if (obj instanceof PlugIn) { PlugInLayer pl = new PlugInLayer(); pl.setProperties(layerName, p); pl.setPlugIn((PlugIn) obj); l = pl; } else { Debug.error("LayerHandler: Skipped \"" + layerName + "\" " + (obj == null ? " - unable to create " : ", type " + obj.getClass().getName() + " is not a layer or plugin")); continue; } // Figure out of the layer is on the startup list, // and make it visible if it is... l.setVisible(visibleLayerList.contains(layerName)); // The ComponentFactory does this now // l.setProperties(layerName, p); layers.addElement(l); if (Debug.debugging("layerhandler")) { Debug.output("LayerHandler: layer " + l.getName() + (l.isVisible() ? " is visible" : " is not visible")); } } int nLayers = layers.size(); if (nLayers == 0) { return new Layer[0]; } else { Layer[] value = new Layer[nLayers]; layers.copyInto(value); return value; } } /** * Add a LayerListener to the LayerHandler, in order to be told about layers * that need to be added to the map. The new LayerListener will receive two * events, one telling it all the layers available, and one telling it which * layers are active (visible). * * @param ll LayerListener, usually the MapBean or other GUI components * interested in providing layer controls. */ public void addLayerListener(LayerListener ll) { Debug.message("layerhandler", "LayerHandler: adding layer listener"); listeners.addLayerListener(ll); // Usually, the listeners are interested in one type of event // or the other. So fire both, and let the listener hash it // out. ll.setLayers(new LayerEvent(this, LayerEvent.ALL, allLayers)); ll.setLayers(new LayerEvent(this, LayerEvent.ADD, getMapLayers())); } /** * Add a LayerListener to the LayerHandler, in order to be told about layers * that need to be added to the map. * * @param ll LayerListener, usually the MapBean or other GUI components * interested in providing layer controls. */ public void removeLayerListener(LayerListener ll) { if (listeners != null) { listeners.removeLayerListener(ll); } } /** * Set all the layers held by the LayerHandler. The visible layers will be * sent to listeners interested in visible layers (LayerEvent.REPLACE), and * the list of all layers will be sent to listeners interested in * LayerEvent.ALL events. <p/> <p/> This method will not add the layers to * the MapHandler, so you can call this if you know the layers are already * in the MapHandler or don't need to be. If you want layers to be added to * the MapHandler (if the LayerHandler knows about it), call init(Layer[]) * instead. <p/> <p/> Also, this method will disregard layer non-removable * status for any layers currently held, and will simply replace all layers * with the ones provided. If you want the non-removable flag to be adhered * to, call init(Layers[]). * * @param layers Layer array of all the layers to be held by the * LayerHandler. */ public synchronized void setLayers(Layer[] layers) { allLayers = organizeBackgroundLayers(layers); if (Debug.debugging("layerhandler")) { Debug.output("LayerHandler.setLayers: " + layers); } // The setLayers needs to push a new runnable into a fifo // stack. A copy of the layers should be made, and then passed // to the runnable. When the runnable finishes, it should kick // off any other threads waiting to get started. Layer[] layersCopy = new Layer[allLayers.length]; System.arraycopy(allLayers, 0, layersCopy, 0, allLayers.length); getListeners().pushLayerEvent(LayerEvent.ALL, layersCopy); getListeners().pushLayerEvent(LayerEvent.REPLACE, getMapLayers()); } /** * Checks to see if there are background layers on top of foreground layers. * * @param layers * @return true if background layers need to be pushed down */ protected boolean isForegroundUnderBackgroundLayer(Layer[] layers) { boolean ret = false; boolean foundBackgroundLayer = false; for (int i = 0; i < layers.length; i++) { boolean isBackgroundLayer = layers[i].getAddAsBackground(); if (isBackgroundLayer) { foundBackgroundLayer = true; } else if (foundBackgroundLayer) { ret = true; break; } } return ret; } /** * Does the check to see of foreground layers are below background layers, * and then iterates through the Layer[] switching layers around until they * are in the approproate order. * * @param layers * @return */ protected Layer[] organizeBackgroundLayers(Layer[] layers) { while (isForegroundUnderBackgroundLayer(layers)) { for (int i = layers.length - 1; i > 0; i--) { Layer layer1 = layers[i - 1]; Layer layer2 = layers[i]; if (!layer2.getAddAsBackground() && layer1.getAddAsBackground()) { layers[i - 1] = layer2; layers[i] = layer1; } } } return layers; } /** * Returns the object responsible for holding on to objects listening to * layer changes. * * @return LayerSupport containing pointers to all objects interested in the * status (order, visibility) of the available layers. */ protected LayerSupport getListeners() { return listeners; } /** * If you are futzing with the layer visibility outside the perview of the * LayerHandler (not using the turnLayerOn() methods) then you can call this * to get all the listeners using the current set of visible layers. */ public void setLayers() { setLayers(allLayers); } /** * Tell anyone interested in the layers to update the layer pretty names. * Same as setLayers(). * * @deprecated Replaced by setLayers(). */ public void updateLayerLabels() { setLayers(allLayers); } /** * Get a layer array, of potential layers that CAN be added to the map, not * the ones that are active on the map. A new array is returned, containing * the current layers. * * @return new Layer[] containing new layers. */ public synchronized Layer[] getLayers() { if (allLayers == null) { return new Layer[0]; } else { Layer[] layers = new Layer[allLayers.length]; System.arraycopy(allLayers, 0, layers, 0, allLayers.length); return layers; } } /** * Get the layers that are currently part of the Map - the ones that are * visible. * * @return an Layer[] of visible Layers. */ public Layer[] getMapLayers() { Debug.message("layerhandler", "LayerHandler.getMapLayers()"); int numEnabled = 0; int cakeIndex = 0; Layer[] cake = null; Layer[] layers = getLayers(); // First loop finds out how many visible layers there are, // Second loop creates the layer cake of visible layers. for (int j = 0; j < 2; j++) { for (int i = 0; i < layers.length; i++) { if (layers[i] != null && layers[i].isVisible()) { if (j == 0) { numEnabled++; } else { cake[cakeIndex++] = layers[i]; } } } if (j == 0) { cake = new Layer[numEnabled]; } } return cake; } /** * Move a layer to a certain position. Returns true if the layer exists in * the LayerHandler, false if is doesn't. No action is taken if the layer * isn't already added to the LayerHandler stack. If the position is 0 or * less the layer is moved on top. If the position is greater or equal to * the number of layers, the layer is moved to the bottom of the pile. * * @param layer the layer to move. * @param toPosition the array index to place it, shifting the other layers * up or down, depending on where the layer is originally. * @return true if the layer is already contained in the LayerHandler, false * if not. */ public boolean moveLayer(Layer layer, int toPosition) { boolean found = false; if (allLayers == null) { return false; } int i = 0; for (i = 0; i < allLayers.length; i++) { if (layer == allLayers[i]) { found = true; break; } } if (found) { // i should be set to the index of the layer. int pos = toPosition; if (pos < 0) { pos = 0; } else if (pos >= allLayers.length) { pos = allLayers.length - 1; } if (pos == i) { return true; } Layer movedLayer = allLayers[i]; int direction; if (i > pos) { direction = -1; } else { direction = 1; } while (i != pos) { allLayers[i] = allLayers[i + direction]; i += direction; } allLayers[pos] = movedLayer; setLayers(allLayers); } return found; } /** * Add a layer to the bottom of the layer stack. If the layer is already * part of the layer stack, nothing is done. * * @param layer the layer to add. */ public void addLayer(Layer layer) { if (allLayers == null) { addLayer(layer, 0); return; } int i = 0; for (i = 0; i < allLayers.length; i++) { if (layer == allLayers[i]) { return; } } addLayer(layer, allLayers.length + 1); } /** * 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 * than the length of the current array, the layer is put at the end, (on * the bottom). The layer is placed on the map if it's visiblity is true. A * Layer can only be added once. If you add a layer that is already added to * the LayerHandler, it will be moved to the requested postition. * * @param layer the layer to add. * @param position the array index to place it. */ public void addLayer(Layer layer, int position) { if (moveLayer(layer, position)) { return; } if (allLayers == null) { allLayers = new Layer[0]; } Layer[] newLayers = new Layer[allLayers.length + 1]; if (position >= allLayers.length) { // Put the new layer on the bottom System.arraycopy(allLayers, 0, newLayers, 0, allLayers.length); newLayers[allLayers.length] = layer; } else if (position <= 0) { // Put the new layer on top System.arraycopy(allLayers, 0, newLayers, 1, allLayers.length); newLayers[0] = layer; } else { newLayers[position] = layer; System.arraycopy(allLayers, 0, newLayers, 0, position); System.arraycopy(allLayers, position, newLayers, position + 1, allLayers.length - position); } if (propertyHandler != null) { String pre = layer.getPropertyPrefix(); if (pre != null && pre != "") { propertyHandler.addUsedPrefix(layer.getPropertyPrefix()); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?