001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.client.ui; 021 022import com.google.gwt.dom.client.Document; 023import com.google.gwt.event.shared.HandlerRegistration; 024import com.google.gwt.safehtml.shared.SafeHtmlUtils; 025import com.google.gwt.user.client.ui.Widget; 026import gwt.material.design.client.base.HasActiveParent; 027import gwt.material.design.client.base.HasClearActiveHandler; 028import gwt.material.design.client.base.MaterialWidget; 029import gwt.material.design.client.base.helper.UiHelper; 030import gwt.material.design.client.constants.CssName; 031import gwt.material.design.client.constants.HeadingSize; 032import gwt.material.design.client.events.ClearActiveEvent; 033import gwt.material.design.client.ui.html.Heading; 034import gwt.material.design.client.ui.html.ListItem; 035 036//@formatter:off 037 038/** 039 * Collections allow you to group list objects together. 040 * <p> 041 * <p> 042 * <h3>UiBinder Usage:</h3> 043 * <pre> 044 * {@code 045 * Simple 046 * <m:MaterialCollection > 047 * <m:MaterialCollectionItem><m:MaterialLabel text="Collecton 1"/></m:MaterialCollectionItem> 048 * <m:MaterialCollectionItem><m:MaterialLabel text="Collecton 2"/></m:MaterialCollectionItem> 049 * <m:MaterialCollectionItem><m:MaterialLabel text="Collecton 3"/></m:MaterialCollectionItem> 050 * <m:MaterialCollectionItem><m:MaterialLabel text="Collecton 4"/></m:MaterialCollectionItem> 051 * </m:MaterialCollection> 052 * 053 * Links 054 * <m:MaterialCollection > 055 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 1"/></m:MaterialCollectionItem> 056 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 2"/></m:MaterialCollectionItem> 057 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 3"/></m:MaterialCollectionItem> 058 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 4"/></m:MaterialCollectionItem> 059 * </m:MaterialCollection> 060 * 061 * Header 062 * <m:MaterialCollection header="Header Title"> 063 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 1"/></m:MaterialCollectionItem> 064 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 2"/></m:MaterialCollectionItem> 065 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 3"/></m:MaterialCollectionItem> 066 * <m:MaterialCollectionItem><m:MaterialLink text="Collecton 4"/></m:MaterialCollectionItem> 067 * </m:MaterialCollection> 068 * 069 * Secondary Content 070 * <m:MaterialCollection header="Header Title"> 071 * <m:MaterialCollectionItem> 072 * <m:MaterialLink text="Collecton 1"/> 073 * <m:MaterialSecondaryContent><m:MaterialIcon iconType="POLYMER" iconPosition="RIGHT" waves="DEFAULT"/></m:MaterialSecondaryContent> 074 * </m:MaterialCollectionItem> 075 * <m:MaterialCollectionItem> 076 * <m:MaterialLink text="Collecton 2"/> 077 * <m:MaterialSecondaryContent><m:MaterialIcon iconType="POLYMER" iconPosition="RIGHT" waves="DEFAULT"/></m:MaterialSecondaryContent> 078 * </m:MaterialCollectionItem> 079 * <m:MaterialCollectionItem> 080 * <m:MaterialLink text="Collecton 3"/> 081 * <m:MaterialSecondaryContent><m:MaterialIcon iconType="POLYMER" iconPosition="RIGHT" waves="DEFAULT"/></m:MaterialSecondaryContent> 082 * </m:MaterialCollectionItem> 083 * <m:MaterialCollectionItem> 084 * <m:MaterialLink text="Collecton 4"/> 085 * <m:MaterialSecondaryContent><m:MaterialIcon iconType="POLYMER" iconPosition="RIGHT" waves="DEFAULT"/></m:MaterialSecondaryContent> 086 * </m:MaterialCollectionItem> 087 * </m:MaterialCollection> 088 * } 089 * </pre> 090 * </p> 091 * 092 * @author kevzlou7979 093 * @author Ben Dol 094 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#collections">Material Collections</a> 095 * @see <a href="https://material.io/guidelines/components/lists-controls.html#lists-controls-types-of-menu-controls">Material Design Specification</a> 096 */ 097//@formatter:on 098public class MaterialCollection extends MaterialWidget implements HasActiveParent, HasClearActiveHandler { 099 100 private int index; 101 private Heading headerLabel = new Heading(HeadingSize.H4); 102 103 /** 104 * Creates an empty collection component. 105 */ 106 public MaterialCollection() { 107 super(Document.get().createULElement(), CssName.COLLECTION); 108 } 109 110 @Override 111 public void clearActive() { 112 clearActiveClass(this); 113 } 114 115 /** 116 * Sets the header of the collection component. 117 */ 118 public void setHeader(String header) { 119 headerLabel.getElement().setInnerSafeHtml(SafeHtmlUtils.fromString(header)); 120 addStyleName(CssName.WITH_HEADER); 121 ListItem item = new ListItem(headerLabel); 122 UiHelper.addMousePressedHandlers(item); 123 item.setStyleName(CssName.COLLECTION_HEADER); 124 insert(item, 0); 125 } 126 127 @Override 128 public void setActive(int index) { 129 setActive(index, true); 130 } 131 132 @Override 133 public void setActive(int index, boolean value) { 134 this.index = index; 135 Widget activeWidget = getActive(); 136 if (activeWidget != null) { 137 if (index <= getWidgetCount()) { 138 if (index != 0) { 139 clearActiveClass(this); 140 if (activeWidget instanceof MaterialCollectionItem) { 141 ((MaterialCollectionItem) activeWidget).setActive(value); 142 } 143 } else { 144 throw new IllegalArgumentException("The active index must be a one-base index to mark as active."); 145 } 146 } 147 } 148 } 149 150 @Override 151 public Widget getActive() { 152 try { 153 return getWidget(index - 1); 154 } catch (IndexOutOfBoundsException ex) { 155 return null; 156 } 157 } 158 159 public Heading getHeaderLabel() { 160 return headerLabel; 161 } 162 163 @Override 164 public HandlerRegistration addClearActiveHandler(final ClearActiveEvent.ClearActiveHandler handler) { 165 return addHandler(handler, ClearActiveEvent.TYPE); 166 } 167}