2323import gwt .material .design .client .custom .MaterialWidget ;
2424import gwt .material .design .client .resources .MaterialResources ;
2525
26+ import java .util .Iterator ;
27+
2628import com .google .gwt .core .client .GWT ;
2729import com .google .gwt .uibinder .client .UiBinder ;
2830import com .google .gwt .uibinder .client .UiChild ;
2931import com .google .gwt .uibinder .client .UiField ;
3032import com .google .gwt .user .client .ui .HTMLPanel ;
33+ import com .google .gwt .user .client .ui .HasWidgets ;
34+ import com .google .gwt .user .client .ui .IsWidget ;
3135import com .google .gwt .user .client .ui .Widget ;
3236
3337public class MaterialCollection extends MaterialWidget {
@@ -37,66 +41,195 @@ public class MaterialCollection extends MaterialWidget {
3741 interface MaterialCollectionUiBinder extends UiBinder <Widget , MaterialCollection > {
3842 }
3943
40- @ UiField
44+ @ UiField
4145 UnorderedList collection ;
42-
46+
4347 public MaterialCollection () {
4448 initWidget (uiBinder .createAndBindUi (this ));
4549 }
4650
47-
48-
4951 @ Override
5052 protected void onAttach () {
51- // TODO Auto-generated method stub
5253 super .onAttach ();
5354 this .addStyleName (MaterialResources .INSTANCE .materialcss ().collection ());
5455 }
5556
56-
57-
5857 /**
59- * Add each item on a collection container
60- * @param item the item to be added
58+ * Add new item on the collection container. Implicit as well assigns a
59+ * press indication.
60+ *
61+ * @param item
62+ * The widget to be added to the collection container
63+ * @return The created ListItem, this might be used to add DomHandlers (eg.
64+ * ClickHandlers etc.) and to react therefore on the full widget
65+ * panel
6166 */
6267 @ UiChild (tagname = "item" )
63- public void addCollectionItem (Widget item ){
64- ListItem list = new ListItem (item );
65- list .addStyleName ("collection-item" );
66- collection .add (list );
68+ public ListItem addCollectionItem (Widget item ) {
69+ ListItem listItem = new ListItem (item );
70+ listItem .addStyleName ("collection-item" );
71+ MaterialUiHelper .addMousePressedHandlers (listItem );
72+ collection .add (listItem );
73+ return listItem ;
6774 }
68-
75+
76+ /**
77+ * Insert item on a collection container at the given index. Implicit as
78+ * well assigns a press indication.
79+ *
80+ * @param item
81+ * The widget to be inserted into the collection container
82+ * @param beforeIndex
83+ * The location to insert the widget into the collection
84+ * @return The created ListItem, this might be used to add DomHandlers (eg.
85+ * ClickHandlers etc.) and to react therefore on the full widget
86+ * panel
87+ */
88+ public ListItem insertCollectionItem (Widget item , int beforeIndex ) {
89+ ListItem listItem = new ListItem (item );
90+ listItem .addStyleName ("collection-item" );
91+ MaterialUiHelper .addMousePressedHandlers (listItem );
92+ collection .insert (listItem , beforeIndex );
93+ return listItem ;
94+ }
95+
96+ /**
97+ * Adds an item to the collection styled as header row. No PressedHandler/Indication is
98+ * applied here.
99+ *
100+ * @param item The widget to be added to the put at the top of the collection container
101+ * @return The created ListItem, this might be used to add DomHandlers (eg.
102+ * ClickHandlers etc.) and to react therefore on the full widget
103+ * panel
104+ */
69105 @ UiChild (tagname = "header" )
70- public void addHeader (Widget item ){
71- ListItem list = new ListItem (item );
72- list .addStyleName (MaterialResources .INSTANCE .materialcss ().collectionHeader ());
73- collection .add (list );
106+ public ListItem addHeader (Widget item ) {
107+ ListItem listItem = new ListItem (item );
108+ listItem .addStyleName (MaterialResources .INSTANCE .materialcss ().collectionHeader ());
109+ collection .add (listItem );
110+ return listItem ;
74111 }
75-
112+
113+ /**
114+ * Add new dismissable item on the collection container. Implicit as well
115+ * assigns a press indication.
116+ *
117+ * @param item
118+ * The widget item to be added
119+ * @return The created ListItem, this might be used to add DomHandlers (eg.
120+ * ClickHandlers etc.) and to react therefore on the full widget
121+ * panel
122+ */
76123 @ UiChild (tagname = "dismissable" )
77- public void addDismissableItem (Widget item ){
78- ListItem list = new ListItem (item );
79- list .addStyleName ("collection-item dismissable" );
80- collection .add (list );
124+ public ListItem addDismissableItem (Widget item ) {
125+ ListItem listItem = new ListItem (item );
126+ listItem .addStyleName ("collection-item dismissable" );
127+ MaterialUiHelper .addMousePressedHandlers (listItem );
128+ collection .add (listItem );
129+ return listItem ;
130+ }
131+
132+ /**
133+ * Insert new item on the collection container. Implicit as well assigns a
134+ * press indication.
135+ *
136+ * @param item
137+ * The widget item to be inserted
138+ * @param beforeIndex
139+ * The location to be inserted
140+ * @return The created ListItem, this might be used to add DomHandlers (eg.
141+ * ClickHandlers etc.) and to react therefore on the full widget
142+ * panel
143+ */
144+ public ListItem insertDismissableItem (Widget item , int beforeIndex ) {
145+ ListItem listItem = new ListItem (item );
146+ listItem .addStyleName ("collection-item dismissable" );
147+ MaterialUiHelper .addMousePressedHandlers (listItem );
148+ collection .insert (listItem , beforeIndex );
149+ return listItem ;
81150 }
82-
151+
152+ /**
153+ * Add new Avatar item on the collection container. Implicit as well assigns
154+ * a press indication.
155+ *
156+ * @param item
157+ * The widget item to be added
158+ * @return The created ListItem, this might be used to add DomHandlers (eg.
159+ * ClickHandlers etc.) and to react therefore on the full widget
160+ * panel
161+ */
83162 @ UiChild (tagname = "avatar" )
84- public void addAvatarItem (Widget item ){
85- ListItem list = new ListItem (item );
86- list .addStyleName ("collection-item avatar" );
87- collection .add (list );
88- if (item instanceof MaterialPanel ){
163+ public ListItem addAvatarItem (Widget item ) {
164+ ListItem listItem = new ListItem (item );
165+ listItem .addStyleName ("collection-item avatar" );
166+ MaterialUiHelper .addMousePressedHandlers (listItem );
167+ collection .add (listItem );
168+ if (item instanceof MaterialPanel ) {
89169 HTMLPanel panel = (HTMLPanel ) item ;
90- for (Widget w : panel ){
91- if (w instanceof MaterialIcon ){
170+ for (Widget w : panel ) {
171+ if (w instanceof MaterialIcon ) {
92172 w .addStyleName ("secondary-content" );
93173 }
94174 }
95175 }
176+ return listItem ;
96177 }
97-
98- public void clear (){
178+
179+ /**
180+ * Insert new Avatar item on the collection container. Implicit as well
181+ * assigns a press indication.
182+ *
183+ * @param item
184+ * The item to be added
185+ * @param beforeIndex
186+ * The location to insert the widget item
187+ * @return The created ListItem, this might be used to add DomHandlers (eg.
188+ * ClickHandlers etc.) and to react therefore on the full widget
189+ * panel
190+ */
191+ public ListItem insertAvatarItem (Widget item , int beforeIndex ) {
192+ ListItem listItem = new ListItem (item );
193+ listItem .addStyleName ("collection-item avatar" );
194+ MaterialUiHelper .addMousePressedHandlers (listItem );
195+ collection .insert (listItem , beforeIndex );
196+ if (item instanceof MaterialPanel ) {
197+ HTMLPanel panel = (HTMLPanel ) item ;
198+ for (Widget w : panel ) {
199+ if (w instanceof MaterialIcon ) {
200+ w .addStyleName ("secondary-content" );
201+ }
202+ }
203+ }
204+ return listItem ;
205+ }
206+
207+ public void clear () {
99208 collection .clear ();
100209 }
101210
211+ public Widget getWidget (int index ) {
212+ return getFirstChild (collection .getWidget (index ));
213+ }
214+
215+ public int getWidgetCount () {
216+ return collection .getWidgetCount ();
217+ }
218+
219+ public int getWidgetIndex (Widget child ) {
220+ return collection .getWidgetIndex (child .getParent ());
221+ }
222+
223+ public int getWidgetIndex (IsWidget child ) {
224+ return getWidgetIndex (asWidgetOrNull (child ).getParent ());
225+ }
226+
227+ private Widget getFirstChild (Widget parent ) {
228+ if (parent instanceof HasWidgets ) {
229+ Iterator <Widget > iter = ((HasWidgets ) parent ).iterator ();
230+ return (iter != null && iter .hasNext ()) ? iter .next () : null ;
231+ }
232+ return null ;
233+ }
234+
102235}
0 commit comments