001/*
002 *  Copyright 2010 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.cms.form;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import org.ametys.plugins.repository.metadata.CompositeMetadata;
023
024/**
025 * A repeater field for managing a ordered collection of
026 * {@link CompositeMetadata} metadata.
027 */
028public class RepeaterField extends AbstractField
029{
030    private final List<RepeaterEntry> _entries = new ArrayList<>();
031
032    /**
033     * Add an entry.
034     * @param entry the entry
035     */
036    public void addEntry(RepeaterEntry entry)
037    {
038        _entries.add(entry);
039    }
040    
041    /**
042     * Retrieves all the entries.
043     * @return the entries.
044     */
045    public List<RepeaterEntry> getEntries()
046    {
047        return Collections.unmodifiableList(_entries);
048    }
049    
050    @Override
051    public String toString()
052    {
053        StringBuilder repeater = new StringBuilder();
054        
055        repeater.append("REPEATER [");
056        repeater.append(_entries.size());
057        repeater.append("]");
058        repeater.append(System.getProperty("line.separator"));
059        
060        for (RepeaterEntry entry : _entries)
061        {
062            repeater.append(entry);
063        }
064        
065        return repeater.toString();
066    }
067    
068    /**
069     * A repeater entry is a {@link Form} with new position and
070     * old position.
071     */
072    public static class RepeaterEntry extends Form
073    {
074        private int _position;
075        private int _previousPosition = -1;
076        
077        /**
078         * Set the new position.
079         * @param position the new position.
080         */
081        public void setPosition(int position)
082        {
083            _position = position;
084        }
085        
086        /**
087         * Retrieves the new position.
088         * @return the new position.
089         */
090        public int getPosition()
091        {
092            return _position;
093        }
094
095        /**
096         * Set the previous position.
097         * @param position the previous position.
098         */
099        public void setPreviousPosition(int position)
100        {
101            _previousPosition = position;
102        }
103        
104        /**
105         * Retrieves the previous position.
106         * @return the previous position or <code>-1</code>
107         *         if this is a new entry.
108         */
109        public int getPreviousPosition()
110        {
111            return _previousPosition;
112        }
113    }
114}