Subversion Repository Public Repository

litesoft

Diff Revisions 282 vs 475 for /trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/ElementMapper.java

Diff revisions: vs.
  @@ -16,141 +16,161 @@
16 16
17 17 package com.google.gwt.gen2.table.client;
18 18
19 - import com.google.gwt.user.client.Element;
20 - import com.google.gwt.user.client.ui.UIObject;
21 - import com.google.gwt.user.client.ui.Widget;
22 -
23 - import java.util.ArrayList;
24 - import java.util.Iterator;
25 - import java.util.NoSuchElementException;
19 + import java.util.*;
20 +
21 + import com.google.gwt.user.client.*;
22 + import com.google.gwt.user.client.ui.*;
26 23
27 24 /**
28 25 * Creates a mapping from elements to their associated ui objects.
29 - *
26 + *
30 27 * @param <MappedType> the type that the element is mapped to
31 28 */
32 - public class ElementMapper<MappedType extends UIObject> {
29 + public class ElementMapper<MappedType extends UIObject>
30 + {
33 31
34 - private static class FreeNode {
35 - int index;
36 - ElementMapper.FreeNode next;
37 -
38 - public FreeNode(int index, ElementMapper.FreeNode next) {
39 - this.index = index;
40 - this.next = next;
41 - }
42 - }
43 -
44 - private static native void clearIndex(Element elem) /*-{
45 - elem["__uiObjectID"] = null;
46 - }-*/;
47 -
48 - private static native int getIndex(Element elem) /*-{
49 - var index = elem["__uiObjectID"];
50 - return (index == null) ? -1 : index;
51 - }-*/;
52 -
53 - private static native void setIndex(Element elem, int index) /*-{
54 - elem["__uiObjectID"] = index;
55 - }-*/;
56 -
57 - private ElementMapper.FreeNode freeList = null;
58 -
59 - private final ArrayList<MappedType> uiObjectList = new ArrayList<MappedType>();
60 -
61 - /**
62 - * Returns the uiObject associated with the given element.
63 - *
64 - * @param elem uiObject's element
65 - * @return the uiObject
66 - */
67 - public MappedType get(Element elem) {
68 - int index = getIndex(elem);
69 - if (index < 0) {
70 - return null;
71 - }
72 - return uiObjectList.get(index);
73 - }
74 -
75 - /**
76 - * Adds the MappedType.
77 - *
78 - * @param uiObject uiObject to add
79 - */
80 - public void put(MappedType uiObject) {
81 - int index;
82 - if (freeList == null) {
83 - index = uiObjectList.size();
84 - uiObjectList.add(uiObject);
85 - } else {
86 - index = freeList.index;
87 - uiObjectList.set(index, uiObject);
88 - freeList = freeList.next;
89 - }
90 - setIndex(uiObject.getElement(), index);
91 - }
92 -
93 - /**
94 - * Remove the uiObject associated with the given element.
95 - *
96 - * @param elem the uiObject's element
97 - */
98 - public void removeByElement(Element elem) {
99 - int index = getIndex(elem);
100 - removeImpl(elem, index);
101 - }
102 -
103 - /**
104 - * Creates an iterator of uiObjects.
105 - *
106 - * @return the iterator
107 - */
108 - public Iterator<MappedType> iterator() {
109 -
110 - return new Iterator() {
111 - int lastIndex = -1;
112 - int nextIndex = -1;
113 - {
114 - findNext();
115 - }
116 -
117 - public boolean hasNext() {
118 - return nextIndex < uiObjectList.size();
119 - }
120 -
121 - public Object next() {
122 - if (!hasNext()) {
123 - throw new NoSuchElementException();
32 + private static class FreeNode
33 + {
34 + int index;
35 + ElementMapper.FreeNode next;
36 +
37 + public FreeNode( int index, ElementMapper.FreeNode next )
38 + {
39 + this.index = index;
40 + this.next = next;
124 41 }
125 - Object result = uiObjectList.get(nextIndex);
126 - lastIndex = nextIndex;
127 - findNext();
128 - return result;
129 - }
130 -
131 - public void remove() {
132 - if (lastIndex < 0) {
133 - throw new IllegalStateException();
42 + }
43 +
44 + private static native void clearIndex( Element elem ) /*-{
45 + elem["__uiObjectID"] = null;
46 + }-*/;
47 +
48 + private static native int getIndex( Element elem ) /*-{
49 + var index = elem["__uiObjectID"];
50 + return (index == null) ? -1 : index;
51 + }-*/;
52 +
53 + private static native void setIndex( Element elem, int index ) /*-{
54 + elem["__uiObjectID"] = index;
55 + }-*/;
56 +
57 + private ElementMapper.FreeNode freeList = null;
58 +
59 + private final ArrayList<MappedType> uiObjectList = new ArrayList<MappedType>();
60 +
61 + /**
62 + * Returns the uiObject associated with the given element.
63 + *
64 + * @param elem uiObject's element
65 + *
66 + * @return the uiObject
67 + */
68 + public MappedType get( Element elem )
69 + {
70 + int index = getIndex( elem );
71 + if ( index < 0 )
72 + {
73 + return null;
134 74 }
135 - Widget w = (Widget) uiObjectList.get(lastIndex);
136 - assert (w.getParent() instanceof HTMLTable);
137 - w.removeFromParent();
138 - lastIndex = -1;
139 - }
140 -
141 - private void findNext() {
142 - while (++nextIndex < uiObjectList.size()) {
143 - if (uiObjectList.get(nextIndex) != null) {
144 - return;
145 - }
75 + return uiObjectList.get( index );
76 + }
77 +
78 + /**
79 + * Adds the MappedType.
80 + *
81 + * @param uiObject uiObject to add
82 + */
83 + public void put( MappedType uiObject )
84 + {
85 + int index;
86 + if ( freeList == null )
87 + {
88 + index = uiObjectList.size();
89 + uiObjectList.add( uiObject );
90 + }
91 + else
92 + {
93 + index = freeList.index;
94 + uiObjectList.set( index, uiObject );
95 + freeList = freeList.next;
146 96 }
147 - }
148 - };
149 - }
150 -
151 - private void removeImpl(Element elem, int index) {
152 - clearIndex(elem);
153 - uiObjectList.set(index, null);
154 - freeList = new FreeNode(index, freeList);
155 - }
97 + setIndex( uiObject.getElement(), index );
98 + }
99 +
100 + /**
101 + * Remove the uiObject associated with the given element.
102 + *
103 + * @param elem the uiObject's element
104 + */
105 + public void removeByElement( Element elem )
106 + {
107 + int index = getIndex( elem );
108 + removeImpl( elem, index );
109 + }
110 +
111 + /**
112 + * Creates an iterator of uiObjects.
113 + *
114 + * @return the iterator
115 + */
116 + public Iterator<MappedType> iterator()
117 + {
118 +
119 + return new Iterator()
120 + {
121 + int lastIndex = -1;
122 + int nextIndex = -1;
123 +
124 + {
125 + findNext();
126 + }
127 +
128 + public boolean hasNext()
129 + {
130 + return nextIndex < uiObjectList.size();
131 + }
132 +
133 + public Object next()
134 + {
135 + if ( !hasNext() )
136 + {
137 + throw new NoSuchElementException();
138 + }
139 + Object result = uiObjectList.get( nextIndex );
140 + lastIndex = nextIndex;
141 + findNext();
142 + return result;
143 + }
144 +
145 + public void remove()
146 + {
147 + if ( lastIndex < 0 )
148 + {
149 + throw new IllegalStateException();
150 + }
151 + Widget w = (Widget) uiObjectList.get( lastIndex );
152 + assert (w.getParent() instanceof HTMLTable);
153 + w.removeFromParent();
154 + lastIndex = -1;
155 + }
156 +
157 + private void findNext()
158 + {
159 + while ( ++nextIndex < uiObjectList.size() )
160 + {
161 + if ( uiObjectList.get( nextIndex ) != null )
162 + {
163 + return;
164 + }
165 + }
166 + }
167 + };
168 + }
169 +
170 + private void removeImpl( Element elem, int index )
171 + {
172 + clearIndex( elem );
173 + uiObjectList.set( index, null );
174 + freeList = new FreeNode( index, freeList );
175 + }
156 176 }