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 java.util.ArrayList; 023import java.util.Collection; 024 025/** 026 * @author Ben Dol 027 */ 028public class Components<T extends Component<?>> extends ArrayList<T> { 029 030 public interface Clone<T> { 031 T clone(T clone); 032 } 033 034 private int maxSize = -1; 035 036 public Components() { 037 } 038 039 public Components(int maxSize) { 040 this.maxSize = maxSize; 041 } 042 043 public Components(Collection<? extends T> components) { 044 this(components, null); 045 } 046 047 public Components(Collection<? extends T> components, Clone<T> clone) { 048 super(components); 049 050 if(clone != null) { 051 Components<T> cloned = new Components<>(); 052 for(T component : this) { 053 cloned.add(clone.clone(component)); 054 } 055 super.clear(); 056 addAll(cloned); 057 } 058 } 059 060 /** 061 * Clear both the Widget and Component. 062 */ 063 @Override 064 public void clear() { 065 clearWidgets(); 066 super.clear(); 067 } 068 069 /** 070 * Clear the components Widgets, without dumping the component. 071 */ 072 public void clearWidgets() { 073 // clear dom rows 074 for(T component : this) { 075 if(component != null) { 076 component.clearWidget(); 077 } 078 } 079 } 080 081 /** 082 * Clear the component data without touching the DOM. 083 */ 084 public void clearComponents() { 085 super.clear(); 086 } 087 088 public boolean isFull() { 089 return maxSize != -1 && size() == maxSize; 090 } 091 092 @Override 093 public boolean add(T t) { 094 return !isFull() && super.add(t); 095 } 096 097 @Override 098 public boolean addAll(Collection<? extends T> c) { 099 return canAdd(c) && super.addAll(c); 100 } 101 102 @Override 103 public boolean addAll(int index, Collection<? extends T> c) { 104 return canAdd(c) && super.addAll(index, c); 105 } 106 107 @Override 108 public T remove(int index) { 109 T component = super.get(index); 110 if(component != null) { 111 component.clearWidget(); 112 } 113 return softRemove(index); 114 } 115 116 public T softRemove(int index) { 117 return super.remove(index); 118 } 119 120 public boolean softRemove(T item) { 121 int i = indexOf(item); 122 if (i == -1) { 123 return false; 124 } 125 super.remove(i); 126 return true; 127 } 128 129 public boolean canAdd(Collection<? extends T> c) { 130 return !isFull() && (maxSize == -1 || (c.size() + size()) <= maxSize); 131 } 132}