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.ArrayList; 019import java.util.List; 020import java.util.Objects; 021 022import org.ametys.runtime.model.exception.BadItemTypeException; 023 024/** 025 * View element that can access to other view items 026 */ 027public class ViewElementAccessor extends ViewElement implements ViewItemAccessor 028{ 029 private List<ViewItem> _viewItems = new ArrayList<>(); 030 031 public List<ViewItem> getViewItems() 032 { 033 return _viewItems; 034 } 035 036 public void addViewItem(ViewItem item) 037 { 038 _viewItems.add(item); 039 item.setParent(this); 040 } 041 042 public void insertViewItem(ViewItem item, int index) 043 { 044 if (index >= 0 && index <= _viewItems.size()) 045 { 046 _viewItems.add(index, item); 047 } 048 else 049 { 050 throw new IllegalArgumentException("Unable to insert an item at index " + index + ". This group contains " + _viewItems.size() + " items."); 051 } 052 } 053 054 public boolean removeViewItem(ViewItem item) 055 { 056 return _viewItems.remove(item); 057 } 058 059 public void clear() 060 { 061 _viewItems.clear(); 062 } 063 064 /** 065 * Creates a {@link ViewElementAccessor} with the items of the given {@link ModelItemAccessor} 066 * @param modelItemAccessor the model item accessor 067 * @return the created {@link ViewElementAccessor} 068 * @throws IllegalArgumentException if the model item accessor is <code>null</code> 069 */ 070 public static ViewElementAccessor of(ModelItemAccessor modelItemAccessor) throws IllegalArgumentException 071 { 072 if (modelItemAccessor == null) 073 { 074 throw new IllegalArgumentException("Unable to create the view from a null model"); 075 } 076 else 077 { 078 return ViewHelper.createViewItemAccessor(List.of(modelItemAccessor)); 079 } 080 } 081 082 /** 083 * Creates a {@link ViewElementAccessor} with the given items 084 * @param modelItemAccessor the model item accessing the items definitions 085 * @param itemPaths the paths of the items to put in the view item 086 * @return the created {@link ViewElementAccessor} 087 * @throws IllegalArgumentException if the model item accessor is <code>null</code> or if an item path is <code>null</code>, empty, or is not defined in the given model items 088 * @throws BadItemTypeException if a segment in a path (but not the last) does not represent a group item 089 */ 090 public static ViewElementAccessor of(ModelItemGroup modelItemAccessor, String... itemPaths) throws IllegalArgumentException, BadItemTypeException 091 { 092 if (modelItemAccessor == null) 093 { 094 throw new IllegalArgumentException("Unable to create the view from a null model"); 095 } 096 else 097 { 098 return ViewHelper.createViewItemAccessor(List.of(modelItemAccessor), itemPaths); 099 } 100 } 101 102 @Override 103 public void copyTo(ViewItem item, View referenceView, String itemPath) 104 { 105 super.copyTo(item, referenceView, itemPath); 106 107 assert item instanceof ViewElementAccessor; 108 ViewHelper.addViewAccessorItems((ViewElementAccessor) item, this, referenceView, itemPath); 109 } 110 111 @Override 112 public ViewElementAccessor createInstance() 113 { 114 return new ViewElementAccessor(); 115 } 116 117 @Override 118 public int hashCode() 119 { 120 final int prime = 31; 121 int result = super.hashCode(); 122 result = prime * result + Objects.hash(_viewItems); 123 return result; 124 } 125 126 @Override 127 public boolean equals(Object obj) 128 { 129 if (this == obj) 130 { 131 return true; 132 } 133 if (!super.equals(obj)) 134 { 135 return false; 136 } 137 if (getClass() != obj.getClass()) 138 { 139 return false; 140 } 141 ViewElementAccessor other = (ViewElementAccessor) obj; 142 return Objects.equals(_viewItems, other._viewItems); 143 } 144 145 @Override 146 public String toString() 147 { 148 return super.toString() + ": " + _viewItems.toString(); 149 } 150}