Exemple #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();
 }
Exemple #2
0
  private void onActiveLayerChanged(final Layer old) {
    fireActiveLayerChanged(old, activeLayer);

    /* This only makes the buttons look disabled. Disabling the actions as well requires
     * the user to re-select the tool after i.e. moving a layer. While testing I found
     * that I switch layers and actions at the same time and it was annoying to mind the
     * order. This way it works as visual clue for new users */
    for (final AbstractButton b : Main.map.allMapModeButtons) {
      MapMode mode = (MapMode) b.getAction();
      final boolean activeLayerSupported = mode.layerIsSupported(activeLayer);
      if (activeLayerSupported) {
        Main.registerActionShortcut(mode, mode.getShortcut()); // fix #6876
      } else {
        Main.unregisterShortcut(mode.getShortcut());
      }
      GuiHelper.runInEDTAndWait(
          new Runnable() {
            @Override
            public void run() {
              b.setEnabled(activeLayerSupported);
            }
          });
    }
    AudioPlayer.reset();
    repaint();
  }
Exemple #3
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();
    }
  }
Exemple #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();
  }