//if selectable layer also add a selection layer
Layer layer = (Layer) event.getNewValue();
List<Layer> layers=new ArrayList<Layer>();
layers.add(layer);
if (layer.hasResource(FeatureSource.class))
layers.add(new SelectionLayer(layer));
synchronized (this.layers){
this.layers.addAll(layers);
}
break;
}
case Notification.ADD_MANY: {
//multiple layer has been added need to add to layers list
//if selectable layer also add a selection layer
List<Layer> layers = new ArrayList<Layer>();
for( Layer layer : (Collection< ? extends Layer>) event.getNewValue() ) {
layers.add(layer);
if (layer.hasResource(FeatureSource.class)
&& findSelectionLayer(layer) == null)
layers.add(new SelectionLayer(layer));
}
synchronized (this.layers){
this.layers.addAll(layers);
}
break;
}
/*
* The collection <code>layers</code> is a sorted TreeMap of <? extends Layer> objects:
* Layer.compareTo() is used to sort and identify items for equality. Comparing is performed
* by z-order. But this collection (<code>layers</code>) contains also
* additional SelectionLayer objects and their z-order is artificial. This leads to
* errors during removing by TreeMap.remove(..) methods.
* The <code>layers</code> collection is re-created safely to fix deleting
* layers from map with synchronization of this cache list of layers and selection layers with
* map's list.
*/
case Notification.REMOVE: {
//remove layer from layers list (both layer and selection layer)
synchronized (layers) {
Layer removedLayer = (Layer) event.getOldValue();
for ( Iterator iter = layers.iterator(); iter.hasNext(); ) {
Layer l = (Layer) iter.next();
if(removedLayer==l){
//remove layer
iter.remove();
}
else if( l instanceof SelectionLayer ){
SelectionLayer sl=(SelectionLayer) l;
if( removedLayer==sl.getWrappedLayer() ){
iter.remove();
}
}
}
}
break;
}
case Notification.REMOVE_MANY: {
//remove layers from layers list (both layer and selection layer)
synchronized (layers) {
Collection<Layer> removedLayers = (Collection<Layer>) event.getOldValue();
for ( Iterator iter = layers.iterator(); iter.hasNext(); ) {
Layer l = (Layer) iter.next();
if( removedLayers.contains(l)){
iter.remove();
}
else if( l instanceof SelectionLayer ){
SelectionLayer sl=(SelectionLayer) l;
if( removedLayers.contains(sl.getWrappedLayer()) ){
iter.remove();
}
}
}
}
break;
}
case Notification.MOVE: {
// moving a layer (getNewValue is expected to return a layer)
// I like type safety better. or at least documentation :(
Layer newV=(Layer) event.getNewValue();
// remove then add the layers to fix ordering of layers (layers are stored in a sorted set that
//is sorted by zorder
synchronized (layers) {
//remove layer and selection layer
SelectionLayer selectionLayer=null;
for( Iterator iter = layers.iterator(); iter.hasNext(); ) {
Layer l = (Layer) iter.next();
if(newV==l)
iter.remove();
else if( l instanceof SelectionLayer ){
SelectionLayer sl=(SelectionLayer) l;
if( newV==sl.getWrappedLayer() ){
iter.remove();
selectionLayer=sl;
}
}
}
//add layers and selection layer
layers.add(newV);
if( selectionLayer!=null ){
layers.add(selectionLayer);
}
}
break;
}case Notification.SET:{
//what is a SET event?
Layer oldV=(Layer) event.getOldValue();
Layer newV=(Layer) event.getNewValue();
SelectionLayer selectionLayer=null;
if( newV.hasResource(FeatureSource.class) )
selectionLayer=new SelectionLayer(newV);
// remove then add the layers to fix ordering of layers.
synchronized (layers) {
for( Iterator iter = layers.iterator(); iter.hasNext(); ) {
Layer l = (Layer) iter.next();
if(oldV==l)
iter.remove();
else if( l instanceof SelectionLayer ){
SelectionLayer sl=(SelectionLayer) l;
if( oldV==sl.getWrappedLayer() ){
iter.remove();
}
}
}
layers.add(newV);