Example #1
0
 /**
  * Moves the layer to the given new position. No event is fired, but repaints according to the new
  * Z-Order of the layers.
  *
  * @param layer The layer to move
  * @param pos The new position of the layer
  */
 public void moveLayer(Layer layer, int pos) {
   layerLock.writeLock().lock();
   layerLock.readLock().lock();
   EnumSet<LayerListenerType> listenersToFire;
   Layer oldActiveLayer = activeLayer;
   OsmDataLayer oldEditLayer = editLayer;
   try {
     try {
       int curLayerPos = layers.indexOf(layer);
       if (curLayerPos == -1) throw new IllegalArgumentException(tr("Layer not in list."));
       if (pos == curLayerPos) return; // already in place.
       layers.remove(curLayerPos);
       if (pos >= layers.size()) {
         layers.add(layer);
       } else {
         layers.add(pos, layer);
       }
       listenersToFire = setEditLayer(layers);
     } finally {
       layerLock.writeLock().unlock();
     }
     onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire);
     AudioPlayer.reset();
   } finally {
     layerLock.readLock().unlock();
   }
   repaint();
 }
Example #2
0
  /**
   * Add a layer to the current MapView. The layer will be added at topmost position.
   *
   * @param layer The layer to add
   */
  public void addLayer(Layer layer) {
    boolean isOsmDataLayer = layer instanceof OsmDataLayer;
    layerLock.writeLock().lock();
    layerLock.readLock().lock();
    EnumSet<LayerListenerType> listenersToFire = EnumSet.noneOf(LayerListenerType.class);
    Layer oldActiveLayer = activeLayer;
    OsmDataLayer oldEditLayer = editLayer;
    try {
      try {
        if (layer instanceof MarkerLayer && playHeadMarker == null) {
          playHeadMarker = PlayHeadMarker.create();
        }

        if (layer instanceof GpxLayer) {
          addGpxLayer((GpxLayer) layer);
        } else if (layers.isEmpty()) {
          layers.add(layer);
        } else if (layer.isBackgroundLayer()) {
          int i = 0;
          for (; i < layers.size(); i++) {
            if (layers.get(i).isBackgroundLayer()) {
              break;
            }
          }
          layers.add(i, layer);
        } else {
          layers.add(0, layer);
        }

        if (isOsmDataLayer || oldActiveLayer == null) {
          // autoselect the new layer
          listenersToFire.addAll(setActiveLayer(layer, true));
        }
      } finally {
        layerLock.writeLock().unlock();
      }

      fireLayerAdded(layer);
      if (isOsmDataLayer) {
        ((OsmDataLayer) layer).addLayerStateChangeListener(this);
      }
      onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire);
      layer.addPropertyChangeListener(this);
      Main.addProjectionChangeListener(layer);
      AudioPlayer.reset();
    } finally {
      layerLock.readLock().unlock();
    }
    if (!listenersToFire.isEmpty()) {
      repaint();
    }
  }
Example #3
0
 /**
  * Sets the active layer to <code>layer</code>. If <code>layer</code> is an instance of {@link
  * OsmDataLayer} also sets {@link #editLayer} to <code>layer</code>.
  *
  * @param layer the layer to be activate; must be one of the layers in the list of layers
  * @throws IllegalArgumentException if layer is not in the lis of layers
  */
 public void setActiveLayer(Layer layer) {
   layerLock.writeLock().lock();
   layerLock.readLock().lock();
   EnumSet<LayerListenerType> listenersToFire;
   Layer oldActiveLayer = activeLayer;
   OsmDataLayer oldEditLayer = editLayer;
   try {
     try {
       listenersToFire = setActiveLayer(layer, true);
     } finally {
       layerLock.writeLock().unlock();
     }
     onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire);
   } finally {
     layerLock.readLock().unlock();
   }
   repaint();
 }
Example #4
0
  /**
   * Remove the layer from the mapview. If the layer was in the list before, an LayerChange event is
   * fired.
   *
   * @param layer The layer to remove
   */
  public void removeLayer(Layer layer) {
    layerLock.writeLock().lock();
    layerLock.readLock().lock();

    EnumSet<LayerListenerType> listenersToFire = EnumSet.noneOf(LayerListenerType.class);
    Layer oldActiveLayer = activeLayer;
    OsmDataLayer oldEditLayer = editLayer;
    try {
      try {
        List<Layer> layersList = new ArrayList<>(layers);

        if (!layersList.remove(layer)) return;

        listenersToFire = setEditLayer(layersList);

        if (layer == activeLayer) {
          listenersToFire.addAll(setActiveLayer(determineNextActiveLayer(layersList), false));
        }

        if (layer instanceof OsmDataLayer) {
          ((OsmDataLayer) layer).removeLayerPropertyChangeListener(this);
        }

        layers.remove(layer);
        Main.removeProjectionChangeListener(layer);

      } finally {
        layerLock.writeLock().unlock();
      }
      onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire);
      fireLayerRemoved(layer);
      layer.removePropertyChangeListener(this);
      layer.destroy();
      AudioPlayer.reset();
    } finally {
      layerLock.readLock().unlock();
    }
    repaint();
  }