]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/layers/SimpleLayer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / layers / SimpleLayer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.layers;\r
13 \r
14 import java.util.concurrent.CopyOnWriteArrayList;\r
15 \r
16 /**\r
17  * @author Antti Villberg\r
18  */\r
19 public class SimpleLayer implements IEditableLayer {\r
20 \r
21     private CopyOnWriteArrayList<ILayerListener> listeners = new CopyOnWriteArrayList<ILayerListener>();\r
22 \r
23     private String name;\r
24 \r
25     public SimpleLayer(String name) {\r
26         if (name == null)\r
27             throw new IllegalArgumentException("null name");\r
28         this.name = name;\r
29     }\r
30 \r
31     @Override\r
32     public String getName() {\r
33         return name;\r
34     }\r
35 \r
36     @Override\r
37     public void setName(String name) {\r
38         if (name == null)\r
39             throw new IllegalArgumentException("null name");\r
40         String oldName = this.name;\r
41         this.name = name;\r
42         if (!name.equals(oldName)) {\r
43             fireLayerChanged(new LayerChangeEvent(this, PROP_NAME, oldName, name));\r
44         }\r
45     }\r
46 \r
47     @Override\r
48     public int hashCode() {\r
49         return name.hashCode();\r
50     }\r
51 \r
52     @Override\r
53     public boolean equals(Object obj) {\r
54         if (this == obj)\r
55             return true;\r
56         if (obj == null)\r
57             return false;\r
58         if (getClass() != obj.getClass())\r
59             return false;\r
60         SimpleLayer other = (SimpleLayer) obj;\r
61         return name.equals(other.name);\r
62     }\r
63 \r
64     @Override\r
65     public void addLayerListener(ILayerListener l) {\r
66         listeners.add(l);\r
67     }\r
68 \r
69     @Override\r
70     public void removeLayerListener(ILayerListener l) {\r
71         listeners.remove(l);\r
72     }\r
73 \r
74     protected void fireLayerChanged(LayerChangeEvent event) {\r
75         for (ILayerListener l : listeners) {\r
76             l.layerChanged(event);\r
77         }\r
78     }\r
79 \r
80     @Override\r
81     public String toString() {\r
82         return getClass().getSimpleName() + "[" + name + "]";\r
83     }\r
84 \r
85 }\r