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.table; 021 022import com.google.gwt.user.client.ui.Widget; 023import gwt.material.design.client.base.MaterialWidget; 024import gwt.material.design.client.base.constants.TableCssName; 025import gwt.material.design.client.constants.IconType; 026import gwt.material.design.client.data.component.CategoryComponent; 027import gwt.material.design.client.js.Js; 028import gwt.material.design.client.ui.MaterialIcon; 029import gwt.material.design.client.ui.html.Text; 030import gwt.material.design.jquery.client.api.JQueryElement; 031 032import java.util.NoSuchElementException; 033 034/** 035 * @author Ben Dol 036 */ 037public class TableSubHeader extends TableRow { 038 039 private TableHeader iconTh; 040 private TableHeader nameTh; 041 042 private MaterialIcon icon; 043 private Text nameLbl; 044 045 private IconType openIcon = IconType.ADD; 046 private IconType closeIcon = IconType.REMOVE; 047 048 public TableSubHeader(TableSubHeader clone) { 049 this(clone.getDataCategory()); 050 copy(clone); 051 } 052 053 public TableSubHeader(CategoryComponent category) { 054 super(category); 055 build(); 056 } 057 058 protected void build() { 059 addStyleName(TableCssName.SUBHEADER); 060 061 iconTh = new TableHeader(); 062 icon = new MaterialIcon(openIcon); 063 iconTh.add(icon); 064 add(iconTh); 065 066 nameTh = new TableHeader(); 067 nameLbl = new Text("Subheader"); 068 nameTh.add(nameLbl); 069 add(nameTh); 070 071 setName(category.getName()); 072 setId(category.getName()); 073 } 074 075 public void add(TableHeader tableHeader) { 076 super.add(tableHeader); 077 } 078 079 public void setName(String name) { 080 nameLbl.setText(name); 081 } 082 083 public String getName() { 084 return nameLbl.getText(); 085 } 086 087 public void hideName() { 088 nameTh.setVisible(false); 089 } 090 091 public void showName() { 092 nameTh.setVisible(true); 093 } 094 095 public TableData getNameTh() { 096 return nameTh; 097 } 098 099 public IconType getOpenIcon() { 100 return openIcon; 101 } 102 103 public void setOpenIcon(IconType openIcon) { 104 this.openIcon = openIcon; 105 106 if(!isOpen()) { 107 icon.setIconType(openIcon); 108 } 109 setDataAttribute("data-open-icon", openIcon.getCssName()); 110 } 111 112 public IconType getCloseIcon() { 113 return closeIcon; 114 } 115 116 public void setCloseIcon(IconType closeIcon) { 117 this.closeIcon = closeIcon; 118 119 if(isOpen()) { 120 icon.setIconType(closeIcon); 121 } 122 setDataAttribute("data-close-icon", closeIcon.getCssName()); 123 } 124 125 public void hideIcon() { 126 iconTh.setVisible(false); 127 } 128 129 public void showIcon() { 130 iconTh.setVisible(true); 131 } 132 133 public TableData getIconTh() { 134 return iconTh; 135 } 136 137 public MaterialIcon getIcon() { 138 return icon; 139 } 140 141 public boolean isOpen() { 142 return $this().hasClass(TableCssName.EXPANDED); 143 } 144 145 public int getScrollPosition() { 146 int pos = Integer.valueOf(String.valueOf($this().data("pos"))); 147 return !Js.isUndefinedOrNull(pos) ? pos : -1; 148 } 149 150 @Override 151 public void setHeight(String height) { 152 if(height != null) { 153 super.setHeight(height); 154 setDataAttribute("data-height", height); 155 156 if (isAttached()) { 157 JQueryElement el = $this().parent(); 158 if(el != null && ((String)el.prop("tagName")).equalsIgnoreCase("div")){ 159 el.height(height); 160 } 161 } 162 } 163 } 164 165 @Override 166 public void setVisible(boolean visible) { 167 super.setVisible(visible); 168 if(visible) { 169 $this().children("*").show(); 170 } else { 171 $this().children("*").hide(); 172 } 173 } 174 175 public void copy(TableSubHeader tableRow) { 176 if(tableRow != null) { 177 super.copy(tableRow); 178 179 icon.setIconType(tableRow.icon.getIconType()); 180 nameLbl.setText(tableRow.nameLbl.getText()); 181 182 setName(tableRow.getName()); 183 setId(tableRow.getId()); 184 185 openIcon = tableRow.openIcon; 186 closeIcon = tableRow.closeIcon; 187 } 188 } 189 190 @Override 191 public void removeFromParent() { 192 Widget parent = getParent(); 193 if(parent != null && parent instanceof MaterialWidget) { 194 try { 195 ((MaterialWidget) parent).getChildren().remove(this); 196 } catch (NoSuchElementException ex) { 197 // This means we have already removed this subheader. 198 } 199 } 200 $this().parent().remove(); 201 } 202 203 @Override 204 public String toString() { 205 return getElement().toString(); 206 } 207}