Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common.externals / src / org / mihalis / opal / columns / ColumnItem.java
1 /*******************************************************************************\r
2  * Copyright (c) 2011 Laurent CARON\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  *     Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API\r
10  *******************************************************************************/\r
11 package org.mihalis.opal.columns;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.List;\r
15 \r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.SWTException;\r
18 import org.mihalis.opal.OpalItem;\r
19 \r
20 /**\r
21  * Instances of this object are items manipulated by the ColumnBrowser widget.\r
22  * ColumnItems are part of a tree structure .\r
23  * \r
24  * @see OpalItem\r
25  */\r
26 public class ColumnItem extends OpalItem {\r
27 \r
28         private final ColumnBrowserWidget widget;\r
29         private final ColumnItem parent;\r
30         private final List<ColumnItem> children;\r
31 \r
32         /**\r
33          * Constructs a new instance of this class given its parent. The item is\r
34          * added to the end of the items maintained by its parent.\r
35          * \r
36          * @param widget the widget that will contain this item (can not be null)\r
37          * \r
38          * @exception IllegalArgumentException <ul>\r
39          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
40          *                </ul>\r
41          * @exception SWTException <ul>\r
42          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
43          *                disposed</li>\r
44          *                </ul>\r
45          */\r
46         public ColumnItem(final ColumnBrowserWidget widget) {\r
47                 if (widget == null) {\r
48                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
49                 }\r
50 \r
51                 if (widget.isDisposed()) {\r
52                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
53                 }\r
54 \r
55                 this.widget = widget;\r
56                 this.parent = null;\r
57                 this.children = new ArrayList<ColumnItem>();\r
58 \r
59                 if (widget.getRootItem() != null) {\r
60                         widget.getRootItem().children.add(this);\r
61                 }\r
62                 widget.updateContent();\r
63         }\r
64 \r
65         /**\r
66          * Constructs a new instance of this class given its parent. The item is\r
67          * added at a given position in the items'list maintained by its parent.\r
68          * \r
69          * @param widget the widget that will contain this item (can not be null)\r
70          * @param index the position\r
71          * \r
72          * @exception IllegalArgumentException <ul>\r
73          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
74          *                </ul>\r
75          * @exception SWTException <ul>\r
76          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
77          *                disposed</li>\r
78          *                </ul>\r
79          */\r
80         public ColumnItem(final ColumnBrowserWidget widget, final int index) {\r
81 \r
82                 if (widget == null) {\r
83                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
84                 }\r
85 \r
86                 if (widget.isDisposed()) {\r
87                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
88                 }\r
89 \r
90                 this.widget = widget;\r
91                 this.parent = null;\r
92                 this.children = new ArrayList<ColumnItem>();\r
93                 widget.getRootItem().children.add(index, this);\r
94                 widget.updateContent();\r
95         }\r
96 \r
97         /**\r
98          * Constructs a new instance of this class given its parent. The item is\r
99          * added to the end of the items maintained by its parent.\r
100          * \r
101          * @param widget the widget that will contain this item (can not be null)\r
102          * \r
103          * @exception IllegalArgumentException <ul>\r
104          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
105          *                </ul>\r
106          * @exception SWTException <ul>\r
107          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
108          *                disposed</li>\r
109          *                </ul>\r
110          */\r
111         public ColumnItem(final ColumnItem parent) {\r
112 \r
113                 if (parent == null) {\r
114                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
115                 }\r
116 \r
117                 if (parent.widget.isDisposed()) {\r
118                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
119                 }\r
120 \r
121                 this.widget = parent.widget;\r
122                 this.parent = parent;\r
123                 this.children = new ArrayList<ColumnItem>();\r
124                 parent.children.add(this);\r
125                 parent.widget.updateContent();\r
126         }\r
127 \r
128         /**\r
129          * Constructs a new instance of this class given its parent. The item is\r
130          * added at a given position in the items'list maintained by its parent.\r
131          * \r
132          * @param widget the widget that will contain this item (can not be null)\r
133          * @param index the position\r
134          * \r
135          * @exception IllegalArgumentException <ul>\r
136          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
137          *                </ul>\r
138          * @exception SWTException <ul>\r
139          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
140          *                disposed</li>\r
141          *                </ul>\r
142          */\r
143         public ColumnItem(final ColumnItem parent, final int index) {\r
144                 if (parent == null) {\r
145                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
146                 }\r
147 \r
148                 if (parent.widget.isDisposed()) {\r
149                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
150                 }\r
151 \r
152                 this.widget = parent.widget;\r
153                 this.parent = parent;\r
154                 this.children = new ArrayList<ColumnItem>();\r
155                 parent.children.add(index, this);\r
156                 parent.widget.updateContent();\r
157         }\r
158 \r
159         /**\r
160          * Remove a given children of this object\r
161          * \r
162          * @param item the item to remove (can not be null)\r
163          * \r
164          * @exception IllegalArgumentException <ul>\r
165          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
166          *                </ul>\r
167          * @exception SWTException <ul>\r
168          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
169          *                disposed</li>\r
170          *                </ul>\r
171          */\r
172         public void remove(final ColumnItem item) {\r
173                 if (this.widget == null) {\r
174                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
175                 }\r
176 \r
177                 if (this.widget.isDisposed()) {\r
178                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
179                 }\r
180                 this.children.remove(item);\r
181                 this.widget.updateContent();\r
182         }\r
183 \r
184         /**\r
185          * Remove a children in a given position of this object\r
186          * \r
187          * @param index position of the children in the items'list\r
188          * \r
189          * @exception IllegalArgumentException <ul>\r
190          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
191          *                </ul>\r
192          * @exception SWTException <ul>\r
193          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
194          *                disposed</li>\r
195          *                </ul>\r
196          */\r
197         public void remove(final int index) {\r
198                 if (this.widget == null) {\r
199                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
200                 }\r
201 \r
202                 if (this.widget.isDisposed()) {\r
203                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
204                 }\r
205                 this.children.remove(index);\r
206                 this.widget.updateContent();\r
207         }\r
208 \r
209         /**\r
210          * Remove all children of this object\r
211          * \r
212          * @exception IllegalArgumentException <ul>\r
213          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
214          *                </ul>\r
215          * @exception SWTException <ul>\r
216          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
217          *                disposed</li>\r
218          *                </ul>\r
219          */\r
220         public void removeAll() {\r
221                 if (this.widget == null) {\r
222                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
223                 }\r
224 \r
225                 if (this.widget.isDisposed()) {\r
226                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
227                 }\r
228                 this.children.clear();\r
229                 this.widget.updateContent();\r
230         }\r
231 \r
232         /**\r
233          * Returns an item located at a given position\r
234          * \r
235          * @param index position\r
236          * @return the item located at the index position\r
237          * \r
238          * @exception IllegalArgumentException <ul>\r
239          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
240          *                </ul>\r
241          * @exception SWTException <ul>\r
242          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
243          *                disposed</li>\r
244          *                </ul>\r
245          */\r
246         public ColumnItem getItem(final int index) {\r
247                 if (this.widget == null) {\r
248                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
249                 }\r
250 \r
251                 if (this.widget.isDisposed()) {\r
252                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
253                 }\r
254                 return this.children.get(index);\r
255         }\r
256 \r
257         /**\r
258          * @return the number of children\r
259          * \r
260          * @exception IllegalArgumentException <ul>\r
261          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
262          *                </ul>\r
263          * @exception SWTException <ul>\r
264          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
265          *                disposed</li>\r
266          *                </ul>\r
267          */\r
268         public int getItemCount() {\r
269                 if (this.widget == null) {\r
270                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
271                 }\r
272 \r
273                 if (this.widget.isDisposed()) {\r
274                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
275                 }\r
276                 return this.children.size();\r
277         }\r
278 \r
279         /**\r
280          * @return all children of this item\r
281          * \r
282          * @exception IllegalArgumentException <ul>\r
283          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
284          *                </ul>\r
285          * @exception SWTException <ul>\r
286          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
287          *                disposed</li>\r
288          *                </ul>\r
289          */\r
290         public ColumnItem[] getItems() {\r
291                 if (this.widget == null) {\r
292                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
293                 }\r
294 \r
295                 if (this.widget.isDisposed()) {\r
296                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
297                 }\r
298                 return this.children.toArray(new ColumnItem[this.children.size()]);\r
299         }\r
300 \r
301         /**\r
302          * @return the widget that holds this item\r
303          * \r
304          * @exception IllegalArgumentException <ul>\r
305          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
306          *                </ul>\r
307          * @exception SWTException <ul>\r
308          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
309          *                disposed</li>\r
310          *                </ul>\r
311          */\r
312         public ColumnBrowserWidget getParent() {\r
313                 if (this.widget == null) {\r
314                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
315                 }\r
316 \r
317                 if (this.widget.isDisposed()) {\r
318                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
319                 }\r
320                 return this.widget;\r
321         }\r
322 \r
323         /**\r
324          * @return the parent item, of <code>null</code> if this item is the root\r
325          *         node\r
326          * \r
327          * @exception IllegalArgumentException <ul>\r
328          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
329          *                </ul>\r
330          * @exception SWTException <ul>\r
331          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
332          *                disposed</li>\r
333          *                </ul>\r
334          */\r
335         public ColumnItem getParentItem() {\r
336                 if (this.widget == null) {\r
337                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
338                 }\r
339 \r
340                 if (this.widget.isDisposed()) {\r
341                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
342                 }\r
343                 return this.parent;\r
344         }\r
345 \r
346         /**\r
347          * Return the position of a given item in children's list\r
348          * \r
349          * @param item item to find\r
350          * @return the position of the children, or -1 if <code>item</code> is a not\r
351          *         a children of this object\r
352          * \r
353          * @exception IllegalArgumentException <ul>\r
354          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
355          *                </ul>\r
356          * @exception SWTException <ul>\r
357          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been\r
358          *                disposed</li>\r
359          *                </ul>\r
360          */\r
361         public int indexOf(final ColumnItem item) {\r
362                 return this.children.indexOf(item);\r
363         }\r
364 \r
365         /**\r
366          * @see java.lang.Object#hashCode()\r
367          */\r
368         @Override\r
369         public int hashCode() {\r
370                 final int prime = 31;\r
371                 int result = 1;\r
372                 result = prime * result + (this.parent == null ? 0 : this.parent.hashCode());\r
373                 result = prime * result + (this.widget == null ? 0 : this.widget.hashCode());\r
374                 return result;\r
375         }\r
376 \r
377         /**\r
378          * @see java.lang.Object#equals(java.lang.Object)\r
379          */\r
380         @Override\r
381         public boolean equals(final Object obj) {\r
382                 if (this == obj) {\r
383                         return true;\r
384                 }\r
385                 if (obj == null) {\r
386                         return false;\r
387                 }\r
388                 if (getClass() != obj.getClass()) {\r
389                         return false;\r
390                 }\r
391                 final ColumnItem other = (ColumnItem) obj;\r
392                 if (this.children == null) {\r
393                         if (other.children != null) {\r
394                                 return false;\r
395                         }\r
396                 } else if (!this.children.equals(other.children)) {\r
397                         return false;\r
398                 }\r
399                 if (this.parent == null) {\r
400                         if (other.parent != null) {\r
401                                 return false;\r
402                         }\r
403                 } else if (!this.parent.equals(other.parent)) {\r
404                         return false;\r
405                 }\r
406                 if (this.widget == null) {\r
407                         if (other.widget != null) {\r
408                                 return false;\r
409                         }\r
410                 } else if (!this.widget.equals(other.widget)) {\r
411                         return false;\r
412                 }\r
413                 return true;\r
414         }\r
415 \r
416 }\r