]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/layers/SimpleLayers.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / layers / SimpleLayers.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.layers;
13
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.Set;
17 import java.util.TreeSet;
18 import java.util.concurrent.CopyOnWriteArrayList;
19
20 /**
21  * @author Antti Villberg
22  */
23 public class SimpleLayers implements ILayersEditor {
24
25     private static final Comparator<ILayer> LAYER_COMPARATOR = new Comparator<ILayer>() {
26         @Override
27         public int compare(ILayer o1, ILayer o2) {
28             return o1.getName().compareTo(o2.getName());
29         }
30     };
31
32     private Set<ILayer>                                 all                      = new TreeSet<ILayer>(LAYER_COMPARATOR);
33     private volatile Set<ILayer>                        allSnapshot;
34
35     private Set<ILayer>                                 visible                  = new TreeSet<ILayer>(LAYER_COMPARATOR);
36     private volatile Set<ILayer>                        visibleSnapshot;
37
38     private CopyOnWriteArrayList<ILayersEditorListener> listeners                = new CopyOnWriteArrayList<ILayersEditorListener>();
39
40     private boolean                                     ignoreFocusSettings      = false;
41
42     private boolean                                     ignoreVisibilitySettings = false;
43
44     public SimpleLayers() {
45     }
46
47     public SimpleLayers(String... content) {
48         for (String s : content) {
49             ILayer layer = new SimpleLayer(s);
50             all.add(layer);
51             visible.add(layer);
52         }
53         allSnapshot = null;
54         visibleSnapshot = null;
55     }
56
57     @Override
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) {
63             synchronized (this) {
64                 if (allSnapshot == null) {
65                     TreeSet<ILayer> ss = new TreeSet<ILayer>(LAYER_COMPARATOR);
66                     ss.addAll(all);
67                     allSnapshot = Collections.unmodifiableSet(ss);
68                 }
69             }
70         }
71         return allSnapshot;
72     }
73
74     @Override
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) {
80             synchronized (this) {
81                 if (visibleSnapshot == null) {
82                     TreeSet<ILayer> ss = new TreeSet<ILayer>(LAYER_COMPARATOR);
83                     ss.addAll(visible);
84                     visibleSnapshot = Collections.unmodifiableSet(ss);
85                 }
86             }
87         }
88         return visibleSnapshot;
89     }
90
91     @Override
92     public boolean isActive(ILayer layer) {
93         synchronized (this) {
94             return visible.contains(layer);
95         }
96     }
97
98     @Override
99     public void deactivate(ILayer layer) {
100         boolean deactivated = false;
101         synchronized (this) {
102             deactivated = visible.remove(layer);
103         }
104         if (deactivated) {
105             synchronized (this) {
106                 visibleSnapshot = null;
107             }
108             for (ILayersEditorListener listener : listeners) {
109                 listener.layerDeactivated(layer);
110             }
111         }
112     }
113
114     @Override
115     public void activate(ILayer layer) {
116         boolean activated = false;
117         synchronized (this) {
118             activated = visible.add(layer);
119         }
120         if (activated) {
121             synchronized (this) {
122                 visibleSnapshot = null;
123             }
124             for (ILayersEditorListener listener : listeners) {
125                 listener.layerActivated(layer);
126             }
127         }
128     }
129
130     @Override
131     public void addLayer(ILayer layer) {
132         boolean added = false;
133         synchronized (this) {
134             added = all.add(layer);
135         }
136         if (added) {
137             synchronized (this) {
138                 allSnapshot = null;
139             }
140             for (ILayersEditorListener listener : listeners) {
141                 listener.layerAdded(layer);
142             }
143         }
144     }
145
146     @Override
147     public void removeLayer(ILayer layer) {
148         boolean removed = false;
149         synchronized (this) {
150             removed = all.remove(layer);
151             visible.remove(layer);
152         }
153         if (removed) {
154             synchronized (this) {
155                 allSnapshot = null;
156                 visibleSnapshot = null;
157             }
158             for (ILayersEditorListener listener : listeners) {
159                 listener.layerRemoved(layer);
160             }
161         }
162     }
163
164     @Override
165     public void addListener(ILayersEditorListener listener) {
166         listeners.add(listener);
167     }
168
169     @Override
170     public void removeListener(ILayersEditorListener listener) {
171         listeners.remove(listener);
172     }
173
174     @Override
175     public boolean getIgnoreFocusSettings() {
176         return ignoreFocusSettings;
177     }
178
179     @Override
180     public void setIgnoreFocusSettings(boolean value) {
181         boolean changed = ignoreFocusSettings != value;
182         ignoreFocusSettings = value;
183         if (changed) {
184             for (ILayersEditorListener listener : listeners) {
185                 listener.ignoreFocusChanged(value);
186             }
187         }
188     }
189
190     @Override
191     public boolean getIgnoreVisibilitySettings() {
192         return ignoreVisibilitySettings;
193     }
194
195     @Override
196     public void setIgnoreVisibilitySettings(boolean value) {
197         boolean changed = ignoreVisibilitySettings != value;
198         ignoreVisibilitySettings = value;
199         if (changed) {
200             for (ILayersEditorListener listener : listeners) {
201                 listener.ignoreVisibilityChanged(value);
202             }
203         }
204     }
205
206 }