package com.ericsson.tic.vi; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.JTextField; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Color; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; import javax.swing.text.StyleContext; import javax.swing.text.Style; import javax.swing.text.BadLocationException; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * This console panel can be used both for feedback and for issuing commands * directly thru the command line interface. * This functionality is supplied "as is". * * * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class ConsolePanel extends JPanel implements ActionListener { /** A scrollpane. */ private JScrollPane scrollPane; /** A text pane. */ private JTextPane textPane; /** Used for font styles. */ private Style style; /** The textfield to which it is possible to write messages. * Should not be called directly. */ public JTextField textField; /** The window id this console belongs to. */ public int wid; /** * Creates a Console. * * @param wid The window id this console belongs to. */ public ConsolePanel(int wid) { this.wid = wid; this.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(100, 100)); this.setMinimumSize(new Dimension(100, 100)); this.setMaximumSize(new Dimension(100, 100)); textPane = new JTextPane(); scrollPane = new JScrollPane(textPane); textField = new JTextField(); textField.addActionListener(this); textField.setActionCommand("console"); textPane.setEditable(false); textPane.setBackground(Color.black); this.add(scrollPane, BorderLayout.CENTER); this.add(textField, BorderLayout.SOUTH); textField.setFocusable(true); } /** * Print a message on the textfield. * * @param message The message to be written to the console. * @param italic If true the outputt will be with italic text. * @param bold If true the message will be outputted with bold text. * @param foreground Color of the text itself. * @param background Color of the background. */ private void println( String message, boolean italic, boolean bold, Color foreground, Color background) { StyledDocument doc = textPane.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE ); Style style = doc.addStyle("style", def); StyleConstants.setItalic(style, italic); StyleConstants.setBold(style, bold); StyleConstants.setForeground(style, foreground); StyleConstants.setBackground(style, background); StyleConstants.setFontFamily(style, "Monospaced"); try { doc.insertString(doc.getLength(), message + "\n", style); } catch (BadLocationException e) { e.printStackTrace(); } scrollPane.getVerticalScrollBar().setValue( scrollPane.getVerticalScrollBar().getMaximum()); } /* *

Here follows specialized methods for writing with various styles to * the console. The styles are set in the settings. * Note: not fully implemented * *

dbsent, dbrec database queries and responses *

udpsent, udprec mesages sent and recieved over udpsockets *

user accepted user input from the command line *

warn warnings *

err errors *

ex exceptions *

d debug (channel used for development) *

alert change alerts *

chat chat messages from other ynodes :) *

def default or generic output, everything else. */ /** * Print in the dbrec channel. * *

dbsent, dbrec database queries and responses * * @param message The message to be written to the console. */ public void dbrec(String message) { if(true) { println(message, false, true, Color.ORANGE, Color.black); } } /** * Print in the dbrec channel. * *

udpsent, udprec mesages sent and recieved over udpsockets * * @param message The message to be written to the console. */ public void udpsent(String message) { if(true) { println(message, false, true, Color.YELLOW, Color.black); } } /** * Print in the debug channel. * *

d debug (channel used for development) * * @param message The message to be written to the console. */ public void d(String message) { if(true) { println(message, false, true, Color.green, Color.black); } } /** * Print in the chat channel. * *

chat chat messages from other ynodes :) * * @param message The message to be written to the console. */ public void chat(String message) { if(true) { println(message, false, true, Color.pink, Color.black); } } /** * Print in the user channel. * *

user accepted user input from the command line * * @param message The message to be written to the console. */ public void user(String message) { if(true) { println(message, false, false, Color.white, Color.black); } } /** * Print in the default channel. * *

def default or generic output, everything else. * * @param message The message to be written to the console. */ public void def(String message) { if(true) { println(message, false, false, Color.gray, Color.black); } } /** * Print help Title * * @param message The message to be written to the console. */ public void helpTitle(String message) { if(true) { println(message, false, true, Color.pink, Color.black); } } /** * Print help text * * @param message The message to be written to the console. */ public void help(String message) { if(true) { println(message, false, false, Color.white, Color.black); } } /** * Called when the user enters a command and hits enter. * * @param e The ActionEvent which triggered this event. */ public void actionPerformed(ActionEvent e) { if ("console".equals(e.getActionCommand())) { if (textField.getText() == null || textField.getText().equals("")) { def(""); } else { VI.consoleInputBuffer.deposit(textField.getText().toCharArray()); textField.setText(""); } VI.windowList.getFirst().frame.requestFocusInWindow(); VI.focusInView = true; } } }