001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.runtime.model; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021import java.util.Objects; 022 023import org.ametys.runtime.i18n.I18nizableText; 024 025/** 026 * Abstract class for group of view items 027 */ 028public abstract class AbstractViewItemGroup implements ViewItemGroup 029{ 030 /** The role of the view item group */ 031 protected String _role; 032 033 /** The label of the view item group */ 034 protected I18nizableText _label; 035 036 /** The description of the view item group */ 037 protected I18nizableText _description; 038 039 /** The view item group's children */ 040 protected List<ViewItem> _children = new ArrayList<>(); 041 042 public I18nizableText getLabel() 043 { 044 return _label; 045 } 046 047 public void setLabel(I18nizableText label) 048 { 049 _label = label; 050 } 051 052 public I18nizableText getDescription() 053 { 054 return _description; 055 } 056 057 public void setDescription(I18nizableText description) 058 { 059 _description = description; 060 } 061 062 public String getRole() 063 { 064 return _role; 065 } 066 067 public void setRole(String role) 068 { 069 _role = role; 070 } 071 072 public List<ViewItem> getViewItems() 073 { 074 return Collections.unmodifiableList(_children); 075 } 076 077 public void addViewItem(ViewItem item) 078 { 079 _children.add(item); 080 item.setParent(this); 081 } 082 083 public void insertViewItem(ViewItem item, int index) 084 { 085 if (index >= 0 && index <= _children.size()) 086 { 087 _children.add(index, item); 088 } 089 else 090 { 091 throw new IllegalArgumentException("Unable to insert an item at index " + index + ". This group contains " + _children.size() + " items."); 092 } 093 } 094 095 public boolean removeViewItem(ViewItem item) 096 { 097 return _children.remove(item); 098 } 099 100 public void clear() 101 { 102 _children.clear(); 103 } 104 105 public void copyTo(ViewItem item) 106 { 107 assert item instanceof AbstractViewItemGroup; 108 ((AbstractViewItemGroup) item).setRole(getRole()); 109 } 110 111 @Override 112 public int hashCode() 113 { 114 return Objects.hash(_children); 115 } 116 117 @Override 118 public boolean equals(Object obj) 119 { 120 if (this == obj) 121 { 122 return true; 123 } 124 if (obj == null) 125 { 126 return false; 127 } 128 if (getClass() != obj.getClass()) 129 { 130 return false; 131 } 132 AbstractViewItemGroup other = (AbstractViewItemGroup) obj; 133 return Objects.equals(_children, other._children); 134 } 135 136 public boolean equals(Object obj, boolean checkDetails) 137 { 138 if (!equals(obj)) 139 { 140 return false; 141 } 142 else if (checkDetails) 143 { 144 AbstractViewItemGroup other = (AbstractViewItemGroup) obj; 145 146 for (int i = 0; i < _children.size(); i++) 147 { 148 ViewItem child = _children.get(i); 149 ViewItem otherChild = other._children.get(i); 150 151 if (!child.equals(otherChild, checkDetails)) 152 { 153 return false; 154 } 155 } 156 157 return Objects.equals(_label, other._label) && Objects.equals(_description, other._description) && Objects.equals(_role, other._role); 158 } 159 else 160 { 161 return true; 162 } 163 } 164}