001package jmri.jmrit.logixng.implementation;
002
003import java.io.PrintWriter;
004import java.util.*;
005
006import jmri.jmrit.logixng.LogixNG;
007import jmri.jmrit.logixng.LogixNG_InitializationManager;
008
009/**
010 * Class providing the basic logic of the LogixNG_InitializationManager interface.
011 * 
012 * @author Daniel Bergqvist   Copyright (C) 2021
013 */
014public class DefaultLogixNGInitializationManager implements LogixNG_InitializationManager {
015    
016    private final List<LogixNG> initLogixNGs = new ArrayList<>();
017    
018    /** {@inheritDoc} */
019    @Override
020    public void add(LogixNG logixNG) {
021        initLogixNGs.add(logixNG);
022    }
023    
024    /** {@inheritDoc} */
025    @Override
026    public void delete(LogixNG logixNG) {
027        initLogixNGs.remove(logixNG);
028    }
029    
030    /** {@inheritDoc} */
031    @Override
032    public void delete(int index) {
033        initLogixNGs.remove(index);
034    }
035    
036    /** {@inheritDoc} */
037    @Override
038    public void moveUp(int index) {
039        if ((index == 0) || (index >= initLogixNGs.size())) return;
040        LogixNG logixNG = initLogixNGs.remove(index);
041        initLogixNGs.add(index-1, logixNG);
042    }
043    
044    /** {@inheritDoc} */
045    @Override
046    public void moveDown(int index) {
047        if (index+1 >= initLogixNGs.size()) return;
048        LogixNG logixNG = initLogixNGs.remove(index);
049        initLogixNGs.add(index+1, logixNG);
050    }
051    
052    /** {@inheritDoc} */
053    @Override
054    public List<LogixNG> getList() {
055        return Collections.unmodifiableList(initLogixNGs);
056    }
057    
058    /** {@inheritDoc} */
059    @Override
060    public void printTree(Locale locale, PrintWriter writer, String indent) {
061        writer.println("LogixNG initialization table:");
062        for (LogixNG logixNG : initLogixNGs) {
063            writer.append(indent);
064            writer.append(logixNG.getSystemName());
065            writer.append(", ");
066            writer.append(logixNG.getUserName());
067            writer.println();
068        }
069        writer.println();
070    }
071    
072}