123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package ui.view;
- import java.awt.Color;
- import java.awt.Toolkit;
- import java.awt.datatransfer.StringSelection;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JScrollPane;
- import java.awt.BorderLayout;
- import javax.swing.JTextPane;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Style;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.StyledDocument;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JPopupMenu;
- /**
- * A Console is a Pane where text can be Printed on.
- *
- * @author Gruppe14
- *
- */
- public class Console extends JScrollPane {
- private static final long serialVersionUID = 1L;
- private final JTextPane consoleText = new JTextPane();
- private final JPanel panel = new JPanel();
- private Style style;
- private StyledDocument doc;
- // PopUpMenu
- private JPopupMenu popmenu = new JPopupMenu();
- private JMenuItem itemCopy = new JMenuItem("Copy");
- private JMenuItem itemClear = new JMenuItem("Clear Console");
- /**
- * Constructor.
- */
- public Console() {
- super();
- this.setBackground(Color.WHITE);
- consoleText.setForeground(Color.BLACK);
- consoleText.setEditable(false);
- doc = consoleText.getStyledDocument();
- style = consoleText.addStyle("I'm a Style", null);
- this.setViewportView(panel);
- panel.setLayout(new BorderLayout(0, 0));
- panel.setBackground(Color.WHITE);
- panel.add(consoleText);
- setViewportView(panel);
- // PopUpMenu
- popmenu.add(itemCopy);
- popmenu.add(itemClear);
- itemCopy.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Toolkit.getDefaultToolkit().getSystemClipboard()
- .setContents(new StringSelection(consoleText.getSelectedText()), null);
- }
- });
- itemClear.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- clearConsole();
- }
- });
- // MouseListener
- consoleText.addMouseListener(new MouseAdapter() {
- @SuppressWarnings("static-access")
- @Override
- public void mouseReleased(MouseEvent e) {
- if (e.getButton() == e.BUTTON3) {
- itemClear.setEnabled(!consoleText.getText().isEmpty());
- itemCopy.setEnabled(consoleText.getSelectedText() != null);
- popmenu.show(e.getComponent(), e.getX(), e.getY());
- }
- }
- });
- }
- /**
- * Print Text on the console.
- *
- * @param text
- * String the Text
- * @param color
- * the color of the Text
- * @param p
- * size of the Text
- * @param bold
- * bold or not
- * @param italic
- * italic or not
- * @param nl
- * new line or not
- *
- */
- public void addText(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
- StyleConstants.setForeground(style, color);
- StyleConstants.setFontSize(style, p);
- StyleConstants.setBold(style, bold);
- StyleConstants.setItalic(style, italic);
- try {
- doc.insertString(doc.getLength(), text, style);
- if (nl) {
- doc.insertString(doc.getLength(), "\n", style);
- }
- } catch (BadLocationException e) {
- }
- }
- /**
- * Print Text on the console in black and font size 12.
- *
- * @param text
- * String the Text
- */
- public void addText(String text) {
- StyleConstants.setForeground(style, Color.BLACK);
- StyleConstants.setFontSize(style, 12);
- StyleConstants.setBold(style, false);
- StyleConstants.setItalic(style, false);
- try {
- doc.insertString(doc.getLength(), text + "\n", style);
- } catch (BadLocationException e) {
- }
- }
- /**
- * Clears the console.
- */
- public void clearConsole() {
- consoleText.setText("");
- }
- }
|