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) item).setRole(getRole());
110    }
111
112    @Override
113    public int hashCode()
114    {
115        return Objects.hash(_children);
116    }
117
118    @Override
119    public boolean equals(Object obj)
120    {
121        if (this == obj)
122        {
123            return true;
124        }
125        if (obj == null)
126        {
127            return false;
128        }
129        if (getClass() != obj.getClass())
130        {
131            return false;
132        }
133        AbstractViewItemGroup other = (AbstractViewItemGroup) obj;
134        return Objects.equals(_children, other._children);
135    }
136    
137    public boolean equals(Object obj, boolean checkDetails)
138    {
139        if (!equals(obj))
140        {
141            return false;
142        }
143        else if (checkDetails)
144        {
145            AbstractViewItemGroup other = (AbstractViewItemGroup) obj;
146            
147            for (int i = 0; i < _children.size(); i++)
148            {
149                ViewItem child = _children.get(i);
150                ViewItem otherChild = other._children.get(i);
151                
152                if (!child.equals(otherChild, checkDetails))
153                {
154                    return false;
155                }
156            }
157            
158            return Objects.equals(_label, other._label) && Objects.equals(_description, other._description) && Objects.equals(_role, other._role);
159        }
160        else
161        {
162            return true;
163        }
164    }
165}