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