001package gwt.material.design.client.ui; 002 003/* 004 * #%L 005 * GwtMaterial 006 * %% 007 * Copyright (C) 2015 GwtMaterialDesign 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import com.google.gwt.dom.client.Document; 024import com.google.gwt.event.dom.client.ClickEvent; 025import com.google.gwt.event.dom.client.ClickHandler; 026import com.google.gwt.event.dom.client.HasClickHandlers; 027import com.google.gwt.event.shared.HandlerRegistration; 028import com.google.gwt.user.client.ui.HasValue; 029import com.google.gwt.user.client.ui.Widget; 030 031import gwt.material.design.client.base.MaterialWidget; 032import gwt.material.design.client.base.HasAvatar; 033import gwt.material.design.client.base.HasDismissable; 034import gwt.material.design.client.base.helper.UiHelper; 035import gwt.material.design.client.base.mixin.ToggleStyleMixin; 036import gwt.material.design.client.constants.CollectionType; 037 038//@formatter:off 039 040/** 041 * Collection element to define every items 042 * @author kevzlou7979 043 * @author Ben Dol 044 * @see <a href="http://gwt-material-demo.herokuapp.com/#collections">Material Collections</a> 045 */ 046//@formatter:on 047public class MaterialCollectionItem extends MaterialWidget implements HasClickHandlers, HasDismissable, HasAvatar { 048 049 private final ToggleStyleMixin<MaterialCollectionItem> avatarMixin = new ToggleStyleMixin<>(this, "avatar"); 050 private final ToggleStyleMixin<MaterialCollectionItem> dismissableMixin = new ToggleStyleMixin<>(this, "dismissable"); 051 052 private HandlerRegistration handlerReg; 053 054 public MaterialCollectionItem() { 055 super(Document.get().createLIElement(), "collection-item"); 056 UiHelper.addMousePressedHandlers(this); 057 } 058 059 @Override 060 protected void onLoad() { 061 super.onLoad(); 062 initDismissableCollection(); 063 } 064 065 protected native void initDismissableCollection() /*-{ 066 $wnd.initDismissableCollection(); 067 }-*/; 068 069 public void setType(CollectionType type) { 070 switch (type) { 071 case AVATAR: 072 addStyleName(type.getCssName()); 073 break; 074 case CHECKBOX: 075 if(getWidgetCount() > 0) { 076 getWidget(0).getElement().getStyle().setProperty("display" , "inline"); 077 } 078 if(handlerReg != null) { 079 handlerReg.removeHandler(); 080 } 081 handlerReg = addClickHandler(new ClickHandler() { 082 @Override 083 public void onClick(ClickEvent event) { 084 for(Widget w : MaterialCollectionItem.this) { 085 if(w instanceof MaterialCollectionSecondary) { 086 for(Widget a : (MaterialCollectionSecondary)w) { 087 if(a instanceof HasValue) { 088 try { 089 @SuppressWarnings("unchecked") 090 HasValue<Boolean> cb = (HasValue<Boolean>) a; 091 if (cb.getValue()) { 092 cb.setValue(false); 093 } else { 094 cb.setValue(true); 095 } 096 } catch (ClassCastException ex) { 097 // Ignore non-boolean has value handlers. 098 } 099 } 100 } 101 } 102 } 103 } 104 }); 105 break; 106 default: 107 break; 108 } 109 } 110 111 @Override 112 public void setDismissable(boolean dismissable) { 113 dismissableMixin.setOn(dismissable); 114 } 115 116 @Override 117 public boolean isDismissable() { 118 return dismissableMixin.isOn(); 119 } 120 121 @Override 122 public void setAvatar(boolean avatar) { 123 avatarMixin.setOn(avatar); 124 } 125 126 @Override 127 public boolean isAvatar() { 128 return avatarMixin.isOn(); 129 } 130 131 @Override 132 public HandlerRegistration addClickHandler(final ClickHandler handler) { 133 return addDomHandler(new ClickHandler() { 134 @Override 135 public void onClick(ClickEvent event) { 136 if(isEnabled()){ 137 handler.onClick(event); 138 } 139 } 140 }, ClickEvent.getType()); 141 } 142}