001package jmri.jmrit.logixng.implementation;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Map;
006import java.util.HashMap;
007import java.util.ServiceLoader;
008
009import javax.annotation.Nonnull;
010
011import jmri.InstanceManager;
012import jmri.InvokeOnGuiThread;
013import jmri.jmrit.logixng.*;
014import jmri.util.*;
015
016/**
017 * Class providing the basic logic of the ActionManager interface.
018 * 
019 * @author Dave Duchamp       Copyright (C) 2007
020 * @author Daniel Bergqvist   Copyright (C) 2018
021 */
022public class DefaultStringActionManager extends AbstractBaseManager<MaleStringActionSocket>
023        implements StringActionManager {
024
025    private final Map<Category, List<Class<? extends Base>>> actionClassList = new HashMap<>();
026    private MaleSocket _lastRegisteredBean;
027
028    
029    public DefaultStringActionManager() {
030        InstanceManager.getDefault(LogixNG_Manager.class).registerManager(this);
031        
032        for (StringActionFactory actionFactory : ServiceLoader.load(StringActionFactory.class)) {
033            actionFactory.init();
034        }
035        
036        for (Category category : Category.values()) {
037            actionClassList.put(category, new ArrayList<>());
038        }
039        
040        for (StringActionFactory actionFactory : ServiceLoader.load(StringActionFactory.class)) {
041            actionFactory.getClasses().forEach((entry) -> {
042//                System.out.format("Add action: %s, %s%n", entry.getKey().name(), entry.getValue().getName());
043                actionClassList.get(entry.getKey()).add(entry.getValue());
044            });
045        }
046        
047        for (MaleStringActionSocketFactory maleSocketFactory : ServiceLoader.load(MaleStringActionSocketFactory.class)) {
048            _maleSocketFactories.add(maleSocketFactory);
049        }
050    }
051
052    /** {@inheritDoc} */
053    @Override
054    public Class<? extends MaleSocket> getMaleSocketClass() {
055        return DefaultMaleStringActionSocket.class;
056    }
057
058    protected MaleStringActionSocket createMaleActionSocket(StringActionBean action) {
059        MaleStringActionSocket socket = new DefaultMaleStringActionSocket(this, action);
060        action.setParent(socket);
061        return socket;
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public MaleSocket getLastRegisteredMaleSocket() {
067        return _lastRegisteredBean;
068    }
069    
070    /** {@inheritDoc} */
071    @Override
072    public MaleStringActionSocket registerBean(MaleStringActionSocket maleSocket) {
073        MaleStringActionSocket bean = super.registerBean(maleSocket);
074        _lastRegisteredBean = maleSocket;
075        return bean;
076    }
077    
078    /**
079     * Remember a NamedBean Object created outside the manager.
080     * This method creates a MaleActionSocket for the action.
081     *
082     * @param action the bean
083     */
084    @Override
085    public MaleStringActionSocket registerAction(@Nonnull StringActionBean action)
086            throws IllegalArgumentException {
087        
088        if (action instanceof MaleStringActionSocket) {
089            throw new IllegalArgumentException("registerAction() cannot register a MaleStringActionSocket. Use the method register() instead.");
090        }
091        
092        // Check if system name is valid
093        if (this.validSystemNameFormat(action.getSystemName()) != NameValidity.VALID) {
094            log.warn("SystemName {} is not in the correct format", action.getSystemName() );
095            throw new IllegalArgumentException(String.format("System name is invalid: %s", action.getSystemName()));
096        }
097        
098        // Keep track of the last created auto system name
099        updateAutoNumber(action.getSystemName());
100        
101        MaleStringActionSocket maleSocket = createMaleActionSocket(action);
102        return registerBean(maleSocket);
103    }
104    
105    @Override
106    public int getXMLOrder() {
107        return LOGIXNG_STRING_ACTIONS;
108    }
109
110    @Override
111    public char typeLetter() {
112        return 'Q';
113    }
114
115    /*.*
116     * Test if parameter is a properly formatted system name.
117     *
118     * @param systemName the system name
119     * @return enum indicating current validity, which might be just as a prefix
120     *./
121    @Override
122    public NameValidity validSystemNameFormat(String systemName) {
123        return LogixNG_Manager.validSystemNameFormat(
124                getSubSystemNamePrefix(), systemName);
125    }
126*/
127    @Override
128    public FemaleStringActionSocket createFemaleSocket(
129            Base parent, FemaleSocketListener listener, String socketName) {
130        return new DefaultFemaleStringActionSocket(parent, listener, socketName);
131    }
132
133    @Override
134    public Map<Category, List<Class<? extends Base>>> getActionClasses() {
135        return actionClassList;
136    }
137
138    /** {@inheritDoc} */
139    @Override
140    public String getBeanTypeHandled(boolean plural) {
141        return Bundle.getMessage(plural ? "BeanNameStringActions" : "BeanNameStringAction");
142    }
143
144    /** {@inheritDoc} */
145    @Override
146    public void deleteStringAction(MaleStringActionSocket x) {
147        // delete the StringAction
148        deregister(x);
149        x.dispose();
150    }
151    
152    static volatile DefaultStringActionManager _instance = null;
153
154    @InvokeOnGuiThread  // this method is not thread safe
155    static public DefaultStringActionManager instance() {
156        if (!ThreadingUtil.isGUIThread()) {
157            LoggingUtil.warnOnce(log, "instance() called on wrong thread");
158        }
159        
160        if (_instance == null) {
161            _instance = new DefaultStringActionManager();
162        }
163        return (_instance);
164    }
165
166    @Override
167    public Class<MaleStringActionSocket> getNamedBeanClass() {
168        return MaleStringActionSocket.class;
169    }
170
171    @Override
172    protected MaleStringActionSocket castBean(MaleSocket maleSocket) {
173        return (MaleStringActionSocket)maleSocket;
174    }
175
176
177    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultStringActionManager.class);
178
179}