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.List; 019 020import org.apache.commons.lang3.StringUtils; 021 022/** 023 * Interface for view item containers 024 */ 025public interface ViewItemContainer 026{ 027 /** 028 * Retrieves all the view items of this container 029 * @return the view items 030 */ 031 public List<ViewItem> getViewItems(); 032 033 /** 034 * Add a view item in the container 035 * @param item the item to add 036 */ 037 public void addViewItem(ViewItem item); 038 039 /** 040 * Insert a view item in the container at the given index 041 * @param item the item to insert 042 * @param index index at which the item has to be inserted 043 */ 044 public void insertViewItem(ViewItem item, int index); 045 046 /** 047 * Checks if there is a model view item with the given name 048 * Usually, a container can't have more than one references to the same model item. 049 * But it can have view items with the same name if they are not references to the model (model view items). 050 * @param itemName the name of the item 051 * @return <code>true</code> if the container contains an item with the given name, <code>false</code> otherwise 052 */ 053 public default boolean hasModelViewItem(String itemName) 054 { 055 return getModelViewItem(itemName) != null; 056 } 057 058 /** 059 * Retrieves the model view item with the given name 060 * Usually, a container can't have more than one references to the same model item. 061 * But it can have view items with the same name if they are not references to the model (model view items). 062 * @param itemName the name of the item 063 * @return the model view item, or <code>null</code> if none was found 064 */ 065 public default ModelViewItem getModelViewItem(String itemName) 066 { 067 if (StringUtils.isEmpty(itemName)) 068 { 069 return null; 070 } 071 else 072 { 073 for (ViewItem item : getViewItems()) 074 { 075 // The item is a reference to the model 076 if (item instanceof ModelViewItem) 077 { 078 if (itemName.equals(item.getName())) 079 { 080 return (ModelViewItem) item; 081 } 082 } 083 // The item is not a reference to the model => check its children 084 else if (item instanceof ViewItemContainer) 085 { 086 ModelViewItem modelViewItem = ((ViewItemContainer) item).getModelViewItem(itemName); 087 if (modelViewItem != null) 088 { 089 return modelViewItem; 090 } 091 } 092 } 093 094 // No item found 095 return null; 096 } 097 } 098}