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.data.component; 021 022import com.google.gwt.user.client.ui.Widget; 023 024import java.util.List; 025 026/** 027 * @author Ben Dol 028 */ 029public class Component<E extends Widget> { 030 031 private E widget; 032 private boolean redraw; 033 034 private Components<Component<?>> children; 035 036 public Component() {} 037 038 public Component(E widget, boolean redraw) { 039 this.widget = widget; 040 this.redraw = redraw; 041 } 042 043 public E getWidget() { 044 return widget; 045 } 046 047 public void setWidget(E widget) { 048 this.widget = widget; 049 } 050 051 public boolean isRendered() { 052 return widget != null && widget.getParent() != null; 053 } 054 055 public boolean isRedraw() { 056 return redraw; 057 } 058 059 public void setRedraw(boolean redraw) { 060 this.redraw = redraw; 061 } 062 063 public boolean isDrawable() { 064 return widget != null; 065 } 066 067 public Components<Component<?>> getChildren() { 068 return children != null ? children : new Components<>(); 069 } 070 071 public void add(Component<?> child) { 072 if(children == null) { 073 children = new Components<>(); 074 } 075 076 assert !child.equals(this) : "Attempting to add Component to itself."; 077 children.add(child); 078 } 079 080 public void addAll(List<Component<?>> children) { 081 if(children != null) { 082 if (this.children == null) { 083 this.children = new Components<>(); 084 } 085 086 for (Component<?> child : children) { 087 add(child); 088 } 089 } 090 } 091 092 public void remove(Component<?> child) { 093 if(children != null) { 094 children.remove(child); 095 } 096 } 097 098 public void destroyChildren() { 099 if(children != null) { 100 children.clear(); 101 } 102 } 103 104 public void removeFromParent() { 105 if(widget != null && widget.isAttached()) { 106 widget.removeFromParent(); 107 } 108 109 // clear children 110 destroyChildren(); 111 } 112 113 protected void clearWidget() { 114 if(widget != null) { 115 widget.removeFromParent(); 116 widget = null; 117 } 118 119 // clear children 120 destroyChildren(); 121 } 122}