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
9 * Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API
\r
10 *******************************************************************************/
\r
11 package org.mihalis.opal.columns;
\r
13 import java.util.ArrayList;
\r
14 import java.util.List;
\r
16 import org.eclipse.swt.SWT;
\r
17 import org.eclipse.swt.SWTException;
\r
18 import org.mihalis.opal.OpalItem;
\r
21 * Instances of this object are items manipulated by the ColumnBrowser widget.
\r
22 * ColumnItems are part of a tree structure .
\r
26 public class ColumnItem extends OpalItem {
\r
28 private final ColumnBrowserWidget widget;
\r
29 private final ColumnItem parent;
\r
30 private final List<ColumnItem> children;
\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
36 * @param widget the widget that will contain this item (can not be null)
\r
38 * @exception IllegalArgumentException <ul>
\r
39 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
41 * @exception SWTException <ul>
\r
42 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
46 public ColumnItem(final ColumnBrowserWidget widget) {
\r
47 if (widget == null) {
\r
48 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
51 if (widget.isDisposed()) {
\r
52 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
55 this.widget = widget;
\r
57 this.children = new ArrayList<ColumnItem>();
\r
59 if (widget.getRootItem() != null) {
\r
60 widget.getRootItem().children.add(this);
\r
62 widget.updateContent();
\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
69 * @param widget the widget that will contain this item (can not be null)
\r
70 * @param index the position
\r
72 * @exception IllegalArgumentException <ul>
\r
73 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
75 * @exception SWTException <ul>
\r
76 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
80 public ColumnItem(final ColumnBrowserWidget widget, final int index) {
\r
82 if (widget == null) {
\r
83 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
86 if (widget.isDisposed()) {
\r
87 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
90 this.widget = widget;
\r
92 this.children = new ArrayList<ColumnItem>();
\r
93 widget.getRootItem().children.add(index, this);
\r
94 widget.updateContent();
\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
101 * @param widget the widget that will contain this item (can not be null)
\r
103 * @exception IllegalArgumentException <ul>
\r
104 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
106 * @exception SWTException <ul>
\r
107 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
111 public ColumnItem(final ColumnItem parent) {
\r
113 if (parent == null) {
\r
114 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
117 if (parent.widget.isDisposed()) {
\r
118 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\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
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
132 * @param widget the widget that will contain this item (can not be null)
\r
133 * @param index the position
\r
135 * @exception IllegalArgumentException <ul>
\r
136 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
138 * @exception SWTException <ul>
\r
139 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
143 public ColumnItem(final ColumnItem parent, final int index) {
\r
144 if (parent == null) {
\r
145 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
148 if (parent.widget.isDisposed()) {
\r
149 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\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
160 * Remove a given children of this object
\r
162 * @param item the item to remove (can not be null)
\r
164 * @exception IllegalArgumentException <ul>
\r
165 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
167 * @exception SWTException <ul>
\r
168 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
172 public void remove(final ColumnItem item) {
\r
173 if (this.widget == null) {
\r
174 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
177 if (this.widget.isDisposed()) {
\r
178 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
180 this.children.remove(item);
\r
181 this.widget.updateContent();
\r
185 * Remove a children in a given position of this object
\r
187 * @param index position of the children in the items'list
\r
189 * @exception IllegalArgumentException <ul>
\r
190 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
192 * @exception SWTException <ul>
\r
193 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
197 public void remove(final int index) {
\r
198 if (this.widget == null) {
\r
199 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
202 if (this.widget.isDisposed()) {
\r
203 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
205 this.children.remove(index);
\r
206 this.widget.updateContent();
\r
210 * Remove all children of this object
\r
212 * @exception IllegalArgumentException <ul>
\r
213 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
215 * @exception SWTException <ul>
\r
216 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
220 public void removeAll() {
\r
221 if (this.widget == null) {
\r
222 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
225 if (this.widget.isDisposed()) {
\r
226 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
228 this.children.clear();
\r
229 this.widget.updateContent();
\r
233 * Returns an item located at a given position
\r
235 * @param index position
\r
236 * @return the item located at the index position
\r
238 * @exception IllegalArgumentException <ul>
\r
239 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
241 * @exception SWTException <ul>
\r
242 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
246 public ColumnItem getItem(final int index) {
\r
247 if (this.widget == null) {
\r
248 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
251 if (this.widget.isDisposed()) {
\r
252 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
254 return this.children.get(index);
\r
258 * @return the number of children
\r
260 * @exception IllegalArgumentException <ul>
\r
261 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
263 * @exception SWTException <ul>
\r
264 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
268 public int getItemCount() {
\r
269 if (this.widget == null) {
\r
270 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
273 if (this.widget.isDisposed()) {
\r
274 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
276 return this.children.size();
\r
280 * @return all children of this item
\r
282 * @exception IllegalArgumentException <ul>
\r
283 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
285 * @exception SWTException <ul>
\r
286 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
290 public ColumnItem[] getItems() {
\r
291 if (this.widget == null) {
\r
292 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
295 if (this.widget.isDisposed()) {
\r
296 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
298 return this.children.toArray(new ColumnItem[this.children.size()]);
\r
302 * @return the widget that holds this item
\r
304 * @exception IllegalArgumentException <ul>
\r
305 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
307 * @exception SWTException <ul>
\r
308 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
312 public ColumnBrowserWidget getParent() {
\r
313 if (this.widget == null) {
\r
314 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
317 if (this.widget.isDisposed()) {
\r
318 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
320 return this.widget;
\r
324 * @return the parent item, of <code>null</code> if this item is the root
\r
327 * @exception IllegalArgumentException <ul>
\r
328 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
330 * @exception SWTException <ul>
\r
331 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
335 public ColumnItem getParentItem() {
\r
336 if (this.widget == null) {
\r
337 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
340 if (this.widget.isDisposed()) {
\r
341 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
343 return this.parent;
\r
347 * Return the position of a given item in children's list
\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
353 * @exception IllegalArgumentException <ul>
\r
354 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
356 * @exception SWTException <ul>
\r
357 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
\r
361 public int indexOf(final ColumnItem item) {
\r
362 return this.children.indexOf(item);
\r
366 * @see java.lang.Object#hashCode()
\r
369 public int hashCode() {
\r
370 final int prime = 31;
\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
378 * @see java.lang.Object#equals(java.lang.Object)
\r
381 public boolean equals(final Object obj) {
\r
388 if (getClass() != obj.getClass()) {
\r
391 final ColumnItem other = (ColumnItem) obj;
\r
392 if (this.children == null) {
\r
393 if (other.children != null) {
\r
396 } else if (!this.children.equals(other.children)) {
\r
399 if (this.parent == null) {
\r
400 if (other.parent != null) {
\r
403 } else if (!this.parent.equals(other.parent)) {
\r
406 if (this.widget == null) {
\r
407 if (other.widget != null) {
\r
410 } else if (!this.widget.equals(other.widget)) {
\r