KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > swt > SWTObservableList


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.databinding.internal.swt;
12
13 import org.eclipse.core.databinding.BindingException;
14 import org.eclipse.core.databinding.observable.Diffs;
15 import org.eclipse.core.databinding.observable.ObservableTracker;
16 import org.eclipse.core.databinding.observable.Realm;
17 import org.eclipse.core.databinding.observable.list.AbstractObservableList;
18
19 /**
20  * Abstract base class of CComboObservableList, ComboObservableList, and
21  * ListObservableList.
22  *
23  * @since 3.2
24  *
25  */

26 public abstract class SWTObservableList extends AbstractObservableList {
27
28     /**
29      *
30      */

31     public SWTObservableList() {
32         super();
33     }
34
35     /**
36      * @param realm
37      */

38     public SWTObservableList(Realm realm) {
39         super(realm);
40     }
41
42     public void add(int index, Object JavaDoc element) {
43         int size = doGetSize();
44         if (index < 0 || index > size)
45             index = size;
46         String JavaDoc[] newItems = new String JavaDoc[size + 1];
47         System.arraycopy(getItems(), 0, newItems, 0, index);
48         newItems[index] = (String JavaDoc) element;
49         System.arraycopy(getItems(), index, newItems, index + 1, size - index);
50         setItems(newItems);
51         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
52                 true, element)));
53     }
54
55     public int doGetSize() {
56         return getItemCount();
57     }
58
59     public Object JavaDoc get(int index) {
60         getterCalled();
61         return getItem(index);
62     }
63
64     public Object JavaDoc getElementType() {
65         return String JavaDoc.class;
66     }
67
68     /**
69      * @param index
70      * @return the item at the given index
71      */

72     protected abstract String JavaDoc getItem(int index);
73
74     /**
75      * @return the item count
76      */

77     protected abstract int getItemCount();
78
79     /**
80      * @return the items
81      */

82     protected abstract String JavaDoc[] getItems();
83
84     private void getterCalled() {
85         ObservableTracker.getterCalled(this);
86     }
87
88     public Object JavaDoc remove(int index) {
89         getterCalled();
90         int size = doGetSize();
91         if (index < 0 || index > size - 1)
92             throw new BindingException(
93                     "Request to remove an element out of the collection bounds"); //$NON-NLS-1$
94

95         String JavaDoc[] newItems = new String JavaDoc[size - 1];
96         String JavaDoc oldElement = getItem(index);
97         if (newItems.length > 0) {
98             System.arraycopy(getItems(), 0, newItems, 0, index);
99             if (size - 1 > index) {
100                 System.arraycopy(getItems(), index + 1, newItems, index, size
101                         - index - 1);
102             }
103         }
104         setItems(newItems);
105         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
106                 false, oldElement)));
107         return oldElement;
108     }
109
110     public Object JavaDoc set(int index, Object JavaDoc element) {
111         String JavaDoc oldElement = getItem(index);
112         setItem(index, (String JavaDoc) element);
113         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
114                 false, oldElement), Diffs.createListDiffEntry(index, true,
115                 element)));
116         return oldElement;
117     }
118
119     /**
120      * @param index
121      * @param string
122      */

123     protected abstract void setItem(int index, String JavaDoc string);
124
125     /**
126      * @param newItems
127      */

128     protected abstract void setItems(String JavaDoc[] newItems);
129
130 }
131
Popular Tags