]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/common/StructuredResourceSelection.java
latest release (0.41), third attempt
[simantics/3d.git] / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / common / StructuredResourceSelection.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.common;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.Arrays;\r
15 import java.util.Iterator;\r
16 import java.util.List;\r
17 \r
18 import org.eclipse.jface.viewers.IStructuredSelection;\r
19 import org.simantics.db.Resource;\r
20 \r
21 \r
22 /**\r
23  * StructuredSelection that contains ResourceSelections\r
24  * \r
25  * @author Marko Luukkainen\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public class StructuredResourceSelection implements IStructuredSelection {\r
29     \r
30     public static final StructuredResourceSelection EMPTY = new StructuredResourceSelection() {\r
31         // Create an empty fixed size List to ensure the list is not modified.\r
32         private List<Resource> empty = Arrays.asList(); \r
33         \r
34         public void add(Resource rs) {\r
35             throw new UnsupportedOperationException("BUG: attempted to modify StructuredResourceSelection.EMPTY");\r
36         }\r
37         public List<Resource> getSelectionList() {\r
38             return empty;\r
39         }\r
40     };\r
41 \r
42     private List<Resource> selections;\r
43     \r
44     /**\r
45      * Creates a new selection that doesn't contain any items\r
46      */\r
47     public StructuredResourceSelection() {\r
48     }\r
49 \r
50     public StructuredResourceSelection(Resource rs) {\r
51         getSelectionList().add(rs);\r
52     }\r
53 \r
54     public StructuredResourceSelection(Resource... rss) {\r
55         List<Resource> s = getSelectionList();\r
56         for (Resource rs : rss)\r
57             s.add(rs);\r
58     }\r
59 \r
60     public void add(Resource rs) {\r
61         getSelectionList().add(rs);\r
62     }\r
63 \r
64     public List<Resource> getSelectionList() {\r
65         if (selections == null) {\r
66             selections = new ArrayList<Resource>();\r
67         }\r
68         return selections;\r
69     }\r
70 \r
71     /*\r
72      * (non-Javadoc)\r
73      * \r
74      * @see org.eclipse.jface.viewers.ISelection#isEmpty()\r
75      */\r
76     public boolean isEmpty() {\r
77         return selections == null || selections.isEmpty();\r
78     }\r
79 \r
80     /*\r
81      * (non-Javadoc)\r
82      * \r
83      * @see org.eclipse.jface.viewers.IStructuredSelection#getFirstElement()\r
84      */\r
85     public Object getFirstElement() {\r
86         if (!isEmpty())\r
87             return selections.get(0);\r
88         return null;\r
89     }\r
90 \r
91     /*\r
92      * (non-Javadoc)\r
93      * \r
94      * @see org.eclipse.jface.viewers.IStructuredSelection#iterator()\r
95      */\r
96     public Iterator<Resource> iterator() {\r
97         return getSelectionList().iterator();\r
98     }\r
99 \r
100     /*\r
101      * (non-Javadoc)\r
102      * \r
103      * @see org.eclipse.jface.viewers.IStructuredSelection#size()\r
104      */\r
105     public int size() {\r
106         return selections == null ? 0 : selections.size();\r
107     }\r
108 \r
109     /*\r
110      * (non-Javadoc)\r
111      * \r
112      * @see org.eclipse.jface.viewers.IStructuredSelection#toArray()\r
113      */\r
114     public Object[] toArray() {\r
115         return selections == null ? new Object[0] : selections.toArray();\r
116     }\r
117 \r
118     /*\r
119      * (non-Javadoc)\r
120      * \r
121      * @see org.eclipse.jface.viewers.IStructuredSelection#toList()\r
122      */\r
123     public List<Resource> toList() {\r
124         return selections == null ? Arrays.asList(new Resource[0]) : selections;\r
125     }\r
126 \r
127     /**\r
128      * Returns whether this structured selection is equal to the given object.\r
129      * Two structured selections are equal iff they contain the same elements\r
130      * in the same order.\r
131      *\r
132      * @param o the other object\r
133      * @return <code>true</code> if they are equal, and <code>false</code> otherwise\r
134      */\r
135     public boolean equals(Object o) {\r
136         if (this == o) {\r
137             return true;\r
138         }\r
139         // null and other classes\r
140         if (!(o instanceof StructuredResourceSelection)) {\r
141             return false;\r
142         }\r
143         StructuredResourceSelection other = (StructuredResourceSelection) o;\r
144 \r
145         // either or both empty?\r
146         if (isEmpty()) {\r
147             return other.isEmpty();\r
148         }\r
149         if (other.isEmpty()) {\r
150             return false;\r
151         }\r
152 \r
153         // check size\r
154         if (size() != other.size())\r
155             return false;\r
156         \r
157         // element comparison\r
158         Iterator<Resource> it = iterator();\r
159         Iterator<Resource> otherIt = other.iterator();\r
160         while (it.hasNext()) {\r
161             if (!it.next().equals(otherIt.next()))\r
162                 return false;\r
163         }\r
164         \r
165         return true;\r
166     }\r
167     \r
168     @Override\r
169     public String toString() {\r
170         return Arrays.toString(getSelectionList().toArray());\r
171     }\r
172     \r
173 }\r