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