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    }
081    
082    public void insertViewItem(ViewItem item, int index)
083    {
084        if (index >= 0 && index <= _children.size())
085        {
086            _children.add(index, item);
087        }
088        else
089        {
090            throw new IllegalArgumentException("Unable to insert an item at index " + index + ". This group contains " + _children.size() + " items.");
091        }
092    }
093    
094    public boolean removeViewItem(ViewItem item)
095    {
096        return _children.remove(item);
097    }
098    
099    public void clear()
100    {
101        _children.clear();
102    }
103    
104    public void copyTo(ViewItem item)
105    {
106        assert item instanceof AbstractViewItemGroup;
107        ((AbstractViewItemGroup) item).setRole(getRole());
108    }
109
110    @Override
111    public int hashCode()
112    {
113        return Objects.hash(_children);
114    }
115
116    @Override
117    public boolean equals(Object obj)
118    {
119        if (this == obj)
120        {
121            return true;
122        }
123        if (obj == null)
124        {
125            return false;
126        }
127        if (getClass() != obj.getClass())
128        {
129            return false;
130        }
131        AbstractViewItemGroup other = (AbstractViewItemGroup) obj;
132        return Objects.equals(_children, other._children);
133    }
134    
135    public boolean equals(Object obj, boolean checkDetails)
136    {
137        if (!equals(obj))
138        {
139            return false;
140        }
141        else if (checkDetails)
142        {
143            AbstractViewItemGroup other = (AbstractViewItemGroup) obj;
144            
145            for (int i = 0; i < _children.size(); i++)
146            {
147                ViewItem child = _children.get(i);
148                ViewItem otherChild = other._children.get(i);
149                
150                if (!child.equals(otherChild, checkDetails))
151                {
152                    return false;
153                }
154            }
155            
156            return Objects.equals(_label, other._label) && Objects.equals(_description, other._description) && Objects.equals(_role, other._role);
157        }
158        else
159        {
160            return true;
161        }
162    }
163}