Package org.octabyte.fxshell

Source Code of org.octabyte.fxshell.SimpleTerminal

package org.octabyte.fxshell;

import javafx.application.Platform;
import org.octabyte.fxshell.javafx.TerminalScene;
import org.octabyte.fxshell.terminal.command.CommandEventProcessor;
import org.octabyte.fxshell.terminal.command.CommandProcessor;
import org.octabyte.fxshell.terminal.event.ShutdownEvent;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.octabyte.fxshell.terminal.*;
import org.octabyte.fxshell.terminal.processors.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
* @author kobylsa on 14/01/14.
*/
public abstract class SimpleTerminal extends Application {
    private final double width;
    private final double height;
    private static final String userName = System.getProperty("user.name");
    private static final String hostName = getHostName();

    private final TerminalState termState = new TerminalStateSync(new TerminalStateImpl(userName + "@" + hostName + "$ "));
    private final CompositeProcessor compositeProcessor = new CompositeProcessor();
    private final AsyncProcessor terminal = new AsyncProcessor(compositeProcessor);
    private final Map<String, CommandProcessor> cmdProcessors = new HashMap<String, CommandProcessor>();

    private Stage stage = null;

    public SimpleTerminal(double width, double height) {
        super();

        this.width = width;
        this.height = height;

        compositeProcessor.addEventProcessor(new UpDownProcessor(termState));
        compositeProcessor.addEventProcessor(new CommandEventProcessor(termState, cmdProcessors));
        compositeProcessor.addEventProcessor(new TabProcessor());
        compositeProcessor.addEventProcessor(new PasteProcessor(termState));
        compositeProcessor.addEventProcessor(new BasicProcessor(termState));
        compositeProcessor.addEventProcessor(new DelProcessor(termState));
        compositeProcessor.addEventProcessor(new LeftRightProcessor(termState));
        compositeProcessor.addEventProcessor(new HomeEndProcessor(termState));

        addCmdProcessor("exit", new CommandProcessor() {
            @Override
            public void onCommand(String cmd, String... cmdArgs) {
                if ( stage != null ) {
                    Platform.exit();
                }
            }
        });
    }

    public void addCmdProcessor(String cmd, CommandProcessor processor) {
        cmdProcessors.put(cmd, processor);
    }

    public TerminalState getTermState() {
        return termState;
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        terminal.onTerminalEvent(new ShutdownEvent());
    }

    @Override
    public void start(Stage primaryStage) {
        stage = primaryStage;

        primaryStage.setTitle("Hello World!");

        Group root = new Group();
        final Canvas canvas = new Canvas(width, height);
        final GraphicsContext gc = canvas.getGraphicsContext2D();

        VBox vBox = new VBox();
        vBox.getChildren().addAll(canvas);

        root.getChildren().add(vBox);

        Scene scene = new TerminalScene(root, gc, termState, terminal);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }
}
TOP

Related Classes of org.octabyte.fxshell.SimpleTerminal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.