1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.layers;
14 import java.util.Collections;
15 import java.util.Comparator;
17 import java.util.TreeSet;
18 import java.util.concurrent.CopyOnWriteArrayList;
21 * @author Antti Villberg
23 public class SimpleLayers implements ILayersEditor {
25 private static final Comparator<ILayer> LAYER_COMPARATOR = new Comparator<ILayer>() {
27 public int compare(ILayer o1, ILayer o2) {
28 return o1.getName().compareTo(o2.getName());
32 private Set<ILayer> all = new TreeSet<ILayer>(LAYER_COMPARATOR);
33 private volatile Set<ILayer> allSnapshot;
35 private Set<ILayer> visible = new TreeSet<ILayer>(LAYER_COMPARATOR);
36 private volatile Set<ILayer> visibleSnapshot;
38 private CopyOnWriteArrayList<ILayersEditorListener> listeners = new CopyOnWriteArrayList<ILayersEditorListener>();
40 private boolean ignoreFocusSettings = false;
42 private boolean ignoreVisibilitySettings = false;
44 public SimpleLayers() {
47 public SimpleLayers(String... content) {
48 for (String s : content) {
49 ILayer layer = new SimpleLayer(s);
54 visibleSnapshot = null;
58 public Set<ILayer> getLayers() {
59 // Double-checked locking idiom: http://en.wikipedia.org/wiki/Double-checked_locking
60 // Works with acquire/release semantics for volatile
61 // Broken under Java 1.4 and earlier semantics for volatile
62 if (allSnapshot == null) {
64 if (allSnapshot == null) {
65 TreeSet<ILayer> ss = new TreeSet<ILayer>(LAYER_COMPARATOR);
67 allSnapshot = Collections.unmodifiableSet(ss);
75 public Set<ILayer> getVisibleLayers() {
76 // Double-checked locking idiom: http://en.wikipedia.org/wiki/Double-checked_locking
77 // Works with acquire/release semantics for volatile
78 // Broken under Java 1.4 and earlier semantics for volatile
79 if (visibleSnapshot == null) {
81 if (visibleSnapshot == null) {
82 TreeSet<ILayer> ss = new TreeSet<ILayer>(LAYER_COMPARATOR);
84 visibleSnapshot = Collections.unmodifiableSet(ss);
88 return visibleSnapshot;
92 public boolean isActive(ILayer layer) {
94 return visible.contains(layer);
99 public void deactivate(ILayer layer) {
100 boolean deactivated = false;
101 synchronized (this) {
102 deactivated = visible.remove(layer);
105 synchronized (this) {
106 visibleSnapshot = null;
108 for (ILayersEditorListener listener : listeners) {
109 listener.layerDeactivated(layer);
115 public void activate(ILayer layer) {
116 boolean activated = false;
117 synchronized (this) {
118 activated = visible.add(layer);
121 synchronized (this) {
122 visibleSnapshot = null;
124 for (ILayersEditorListener listener : listeners) {
125 listener.layerActivated(layer);
131 public void addLayer(ILayer layer) {
132 boolean added = false;
133 synchronized (this) {
134 added = all.add(layer);
137 synchronized (this) {
140 for (ILayersEditorListener listener : listeners) {
141 listener.layerAdded(layer);
147 public void removeLayer(ILayer layer) {
148 boolean removed = false;
149 synchronized (this) {
150 removed = all.remove(layer);
151 visible.remove(layer);
154 synchronized (this) {
156 visibleSnapshot = null;
158 for (ILayersEditorListener listener : listeners) {
159 listener.layerRemoved(layer);
165 public void addListener(ILayersEditorListener listener) {
166 listeners.add(listener);
170 public void removeListener(ILayersEditorListener listener) {
171 listeners.remove(listener);
175 public boolean getIgnoreFocusSettings() {
176 return ignoreFocusSettings;
180 public void setIgnoreFocusSettings(boolean value) {
181 boolean changed = ignoreFocusSettings != value;
182 ignoreFocusSettings = value;
184 for (ILayersEditorListener listener : listeners) {
185 listener.ignoreFocusChanged(value);
191 public boolean getIgnoreVisibilitySettings() {
192 return ignoreVisibilitySettings;
196 public void setIgnoreVisibilitySettings(boolean value) {
197 boolean changed = ignoreVisibilitySettings != value;
198 ignoreVisibilitySettings = value;
200 for (ILayersEditorListener listener : listeners) {
201 listener.ignoreVisibilityChanged(value);