All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.tsl2.nano.incubation.terminal.item.selector.Sequence Maven / Gradle / Ivy

Go to download

TSL2 Framework Terminal (Console Application Framework named SIShell, providing Actions, Options, Commands, Inputs, lots of Selectors, PlatformManagement)

There is a newer version: 2.5.2
Show newest version
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Tom, Thomas Schneider
 * created on: 19.08.2015
 * 
 * Copyright: (c) Thomas Schneider 2015, all rights reserved
 */
package de.tsl2.nano.incubation.terminal.item.selector;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.core.Commit;

import de.tsl2.nano.collection.Entry;
import de.tsl2.nano.core.util.ConcurrentUtil;
import de.tsl2.nano.core.util.ListSet;
import de.tsl2.nano.core.util.Util;
import de.tsl2.nano.incubation.terminal.IItem;
import de.tsl2.nano.incubation.terminal.SIShell;
import de.tsl2.nano.incubation.terminal.TextTerminal;
import de.tsl2.nano.incubation.terminal.item.AItem;
import de.tsl2.nano.incubation.terminal.item.Action;

/**
 * Starts an {@link Action} for all items of a {@link Selector} in a daemon thread. implementing the {@link Runnable}
 * interface tells SiShell to do a printscreen every 5 sec.
 * 
 * @author Tom, Thomas Schneider
 * @version $Revision$
 */
public class Sequence extends Selector {
    /** serialVersionUID */
    private static final long serialVersionUID = 1977032828721824363L;

    @Element(required = false)
    Action action;
    @Element(required = false)
    Selector sequence;
    //TODO: use concurrent list
    transient List> result;
    transient Properties context;

    public Sequence() {
    }

    public Sequence(Action action, Selector selector, String description) {
        super(action.getName(), description);
        this.action = action;
        sequence = selector;
        initResult();
    }

    /**
     * starts the defined action on each item in the {@link #sequence} - in a daemon thread
     */ 
    @Override
    public IItem react(IItem caller, String input, final InputStream in, final PrintStream out, final Properties env) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (AItem item : sequence.getNodes(context)) {
                    context.put(sequence.getName(), item.getValue());
                    System.out.print(sequence + " running...");
                    Object r = action.run(context);
                    result.add(new Entry(item, r));
                    SIShell.printScreen(getDescription(env, false), in, out, ask(env),
                        Util.get(SIShell.KEY_WIDTH, TextTerminal.SCREEN_WIDTH),
                        Util.get(SIShell.KEY_HEIGHT, TextTerminal.SCREEN_HEIGHT), TextTerminal.BLOCK_BAR, false, false);
                }
            }
        };
        ConcurrentUtil.startDaemon(getName(), runnable);
        return super.react(caller, input, in, out, env);
    }

    @Override
    public String ask(Properties env) {
        return "Please enter  to stop the sequence!\n";
    }

    /**
     * initResult
     */
    protected void initResult() {
        //prefill the result map
        result = new ListSet>();
        for (AItem item : sequence.getNodes(new Properties())) {
            result.add(new Entry(item, null));
        }
    }

    @Override
    protected List createItems(Map props) {
        return result;
    }

    public void stop() {
        ConcurrentUtil.stopOrInterrupt(getName());
    }

    @Commit
    protected void initDeserialization() {
        initResult();
        context = new Properties();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy