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

org.bidib.wizard.mvc.stepcontrol.view.CvConsoleView Maven / Gradle / Ivy

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

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.api.service.console.ConsoleColor;
import org.bidib.wizard.client.common.component.ColorPane;
import org.bidib.wizard.client.common.view.BasicPopupMenu;
import org.bidib.wizard.mvc.stepcontrol.model.CvConsoleModel;
import org.bidib.wizard.mvc.stepcontrol.model.CvConsoleModel.ConsoleLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vlsolutions.swing.docking.DockKey;
import com.vlsolutions.swing.docking.Dockable;

public class CvConsoleView implements Dockable {

    private static final Logger LOGGER = LoggerFactory.getLogger(CvConsoleView.class);

    private final DockKey DOCKKEY = new DockKey("CvConsoleView");

    private final JPanel contentPanel;

    private final ColorPane coloredTextPane = new ColorPane();

    private final CvConsoleModel consoleModel;

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");

    private Color colorRed;

    private Color colorBlue;

    private Color colorGreen;

    private Color colorBlack;

    private Color colorGray;

    public CvConsoleView() {
        DOCKKEY.setName(Resources.getString(getClass(), "title"));
        DOCKKEY.setFloatEnabled(true);
        DOCKKEY.setAutoHideEnabled(false);

        LOGGER.info("Create new ConsoleView");
        contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());

        coloredTextPane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(coloredTextPane);
        contentPanel.add(scrollPane);

        this.colorRed = UIManager.getColor("Console.red");
        this.colorBlue = UIManager.getColor("Console.blue");
        this.colorGreen = UIManager.getColor("Console.green");
        this.colorBlack = UIManager.getColor("Console.black");
        this.colorGray = UIManager.getColor("Console.gray");

        consoleModel = CvConsoleModel.getConsoleModel();

        consoleModel.addPropertyChangeListener(CvConsoleModel.PROPERTY_CONSOLE_CONTENT, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                LOGGER.info("Property was changed: {}", evt);
                if (evt instanceof IndexedPropertyChangeEvent) {
                    IndexedPropertyChangeEvent ipce = (IndexedPropertyChangeEvent) evt;

                    ConsoleLine newLine = (ConsoleLine) ipce.getNewValue();

                    if (newLine != null) {
                        String now = dateFormat.format(new Date());
                        coloredTextPane.append(getColor(newLine.getColor()), now + " - " + newLine.getMessage() + "\n");
                    }
                }
            }
        });

        consoleModel
            .addPropertyChangeListener(CvConsoleModel.PROPERTY_CONSOLE_CONTENT_SIZE, new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    LOGGER.info("Property was changed: {}", evt);

                    if (evt.getNewValue() != null) {
                        Integer value = (Integer) evt.getNewValue();
                        if (value == 0) {
                            // clear the console
                            coloredTextPane.clear();
                        }
                    }
                }
            });

        JPopupMenu popupMenu = new BasicPopupMenu();
        JMenuItem clearConsole = new JMenuItem(Resources.getString(getClass(), "clear_console"));
        clearConsole.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireClearConsole();
            }
        });
        popupMenu.add(clearConsole);
        coloredTextPane.setComponentPopupMenu(popupMenu);
    }

    private Color getColor(ConsoleColor consoleColor) {
        Color color = this.colorBlack;
        switch (consoleColor) {
            case red:
                color = this.colorRed;
                break;
            case blue:
                color = this.colorBlue;
                break;
            case green:
                color = this.colorGreen;
                break;
            case gray:
                color = this.colorGray;
                break;
            default:
                break;
        }
        return color;
    }

    @Override
    public Component getComponent() {
        return contentPanel;
    }

    @Override
    public DockKey getDockKey() {
        return DOCKKEY;
    }

    public void close() {
    }

    private void fireClearConsole() {
        LOGGER.info("clear the console.");

        consoleModel.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy