001/* 002 * Copyright 2020 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 objects that can access to some view items 024 */ 025public interface ViewItemAccessor 026{ 027 /** 028 * Retrieves all the view items of this accessor 029 * @return the view items 030 */ 031 public List<ViewItem> getViewItems(); 032 033 /** 034 * Add a view item in the accessor 035 * @param item the item to add 036 */ 037 public void addViewItem(ViewItem item); 038 039 /** 040 * Insert a view item in the accessor 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 * Removes the given items of this accessor 048 * @param item the item to remove 049 * @return <code>true</code> if this accessor contained the given item, <code>false</code> otherwise 050 */ 051 public boolean removeViewItem(ViewItem item); 052 053 /** 054 * Removes all items of this accessor 055 */ 056 public void clear(); 057 058 /** 059 * Add view items in the accessor 060 * @param items the items to add 061 */ 062 public default void addViewItems(List<ViewItem> items) 063 { 064 for (ViewItem item : items) 065 { 066 addViewItem(item); 067 } 068 } 069 070 /** 071 * Checks if the current {@link ViewItemAccessor} contains the given model view item 072 * Consider that the accessor contains the given item if it contains an item representing the same definition AND with the same children if it is a group 073 * @param item the item 074 * @return <code>true</code> if the accessor contains the given item, <code>false</code> otherwise 075 */ 076 public default boolean hasModelViewItem(ModelViewItem item) 077 { 078 for (ViewItem viewItem : getViewItems()) 079 { 080 if (viewItem instanceof ModelViewItem && viewItem.equals(item) 081 || viewItem instanceof ViewItemAccessor && ((ViewItemAccessor) viewItem).hasModelViewItem(item)) 082 { 083 return true; 084 } 085 } 086 087 // No item found 088 return false; 089 } 090 091 /** 092 * Checks if there is a model view item with the given name 093 * @param itemName the name of the item 094 * @return <code>true</code> if the accessor can acces to an item with the given name, <code>false</code> otherwise 095 */ 096 public default boolean hasModelViewItem(String itemName) 097 { 098 return getModelViewItem(itemName) != null; 099 } 100 101 /** 102 * Retrieves the model view item with the given name 103 * If there are more than one corresponding items, the first one is retrieved 104 * @param itemName the name of the item 105 * @return the model view item, or <code>null</code> if none was found 106 */ 107 public default ModelViewItem getModelViewItem(String itemName) 108 { 109 if (StringUtils.isEmpty(itemName)) 110 { 111 return null; 112 } 113 else 114 { 115 for (ViewItem item : getViewItems()) 116 { 117 // The item is a reference to the model 118 if (item instanceof ModelViewItem) 119 { 120 if (itemName.equals(item.getName())) 121 { 122 return (ModelViewItem) item; 123 } 124 } 125 // The item is not a reference to the model and is a group => check its children 126 else if (item instanceof ViewItemAccessor) 127 { 128 ModelViewItem modelViewItem = ((ViewItemAccessor) item).getModelViewItem(itemName); 129 if (modelViewItem != null) 130 { 131 return modelViewItem; 132 } 133 } 134 } 135 136 // No item found 137 return null; 138 } 139 } 140}