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

org.bidib.wizard.mvc.stepcontrol.model.CvConsoleModel Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.mvc.stepcontrol.model;

import java.util.LinkedList;
import java.util.List;

import org.bidib.wizard.api.service.console.ConsoleColor;
import org.bidib.wizard.common.context.DefaultApplicationContext;

import com.jgoodies.binding.beans.Model;

public class CvConsoleModel extends Model {

    private static final long serialVersionUID = 1L;

    public static final String PROPERTY_CONSOLE_CONTENT = "consoleContent";

    public static final String PROPERTY_CONSOLE_CONTENT_SIZE = "consoleContentSize";

    private List consoleMessages = new LinkedList<>();

    public static final class ConsoleLine {
        private final ConsoleColor color;

        private final String message;

        public ConsoleLine(ConsoleColor color, String message) {
            this.color = color;
            this.message = message;
        }

        /**
         * @return the color
         */
        public ConsoleColor getColor() {
            return color;
        }

        /**
         * @return the message
         */
        public String getMessage() {
            return message;
        }
    }

    public void addConsoleLine(ConsoleColor color, String line) {
        ConsoleLine consoleLine = new ConsoleLine(color, line);

        // only keep 250 in memory
        if (consoleMessages.size() > 250) {
            consoleMessages.remove(0);
        }

        consoleMessages.add(consoleLine);

        int index = consoleMessages.size() - 1;

        fireIndexedPropertyChange(PROPERTY_CONSOLE_CONTENT, index, null, consoleLine);
    }

    public void clear() {
        int oldSize = consoleMessages.size();

        consoleMessages.clear();

        firePropertyChange(PROPERTY_CONSOLE_CONTENT_SIZE, oldSize, consoleMessages.size());
    }

    public static synchronized CvConsoleModel getConsoleModel() {
        CvConsoleModel consoleModel =
            DefaultApplicationContext
                .getInstance().get(DefaultApplicationContext.KEY_CV_CONSOLE_MODEL, CvConsoleModel.class);
        if (consoleModel == null) {
            consoleModel = new CvConsoleModel();
            DefaultApplicationContext
                .getInstance().register(DefaultApplicationContext.KEY_CV_CONSOLE_MODEL, consoleModel);
        }

        return consoleModel;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy