001package com.ganteater.ae.desktop.view;
002
003import java.awt.BorderLayout;
004import java.awt.Font;
005import java.util.Properties;
006
007import javax.swing.JScrollBar;
008import javax.swing.JScrollPane;
009import javax.swing.JTextArea;
010
011import com.ganteater.ae.AEManager;
012import com.ganteater.ae.processor.annotation.CommandDescription;
013
014@CommandDescription("`Text` View Type example:\r\n"
015                + "\r\n"
016                + "```xml \r\n"
017                + "<View name=\"textView\" reuse=\"true\" type=\"Text\" font='enum:SERIF|SANSSERIF|MONOSPACED|DIALOG|DIALOGINPUT' /> \r\n"
018                + "<Out view=\"textView\" value=\"text\" /> \r\n"
019                + "```"
020                + "The `font` attribute can be in one of the following formats:\r\n"
021                + "\r\n"
022                + "    fontname-style-pointsize\r\n"
023                + "    fontname-style\r\n"
024                + "    fontname-pointsize\r\n"
025                + "    fontname\r\n\r\n"
026                + "For more information see https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html#decode-java.lang.String-")
027public class Text extends View {
028        private static final long serialVersionUID = -6679978094828718987L;
029        private JTextArea fTextArrea = new JTextArea();
030        JScrollPane scroll = new JScrollPane(fTextArrea);
031
032        public void init(Properties params, AEManager manager) {
033                super.init(params, null);
034                fTextArrea.setEditable(true);
035                String font = params.getProperty("font");
036                if(font != null) {
037                        Font decode = Font.decode(font);
038                        fTextArrea.setFont(decode);
039                }
040                add(scroll, BorderLayout.CENTER);
041        }
042
043        @Override
044        public void out(Object value, Properties properties) {
045                int length = fTextArrea.getText().length();
046                boolean scrollOn = fTextArrea.getCaretPosition() == length || length == 0;
047                fTextArrea.append(value + "\n");
048                if (scrollOn) {
049                        length = fTextArrea.getText().length();
050                        fTextArrea.setCaretPosition(length);
051                        JScrollBar verticalScrollBar = scroll.getVerticalScrollBar();
052                        verticalScrollBar.setValue(verticalScrollBar.getMaximum());
053                }
054        }
055
056}