Console.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package ui.view;
  2. import java.awt.Color;
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.StringSelection;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import javax.swing.JScrollPane;
  10. import java.awt.BorderLayout;
  11. import javax.swing.JTextPane;
  12. import javax.swing.text.BadLocationException;
  13. import javax.swing.text.Style;
  14. import javax.swing.text.StyleConstants;
  15. import javax.swing.text.StyledDocument;
  16. import javax.swing.JMenuItem;
  17. import javax.swing.JPanel;
  18. import javax.swing.JPopupMenu;
  19. /**
  20. * A Console is a Pane where text can be Printed on.
  21. *
  22. * @author Gruppe14
  23. *
  24. */
  25. public class Console extends JScrollPane {
  26. private static final long serialVersionUID = 1L;
  27. private final JTextPane consoleText = new JTextPane();
  28. private final JPanel panel = new JPanel();
  29. private Style style;
  30. private StyledDocument doc;
  31. // PopUpMenu
  32. private JPopupMenu popmenu = new JPopupMenu();
  33. private JMenuItem itemCopy = new JMenuItem("Copy");
  34. private JMenuItem itemClear = new JMenuItem("Clear Console");
  35. /**
  36. * Constructor.
  37. */
  38. public Console() {
  39. super();
  40. this.setBackground(Color.WHITE);
  41. consoleText.setForeground(Color.BLACK);
  42. consoleText.setEditable(false);
  43. doc = consoleText.getStyledDocument();
  44. style = consoleText.addStyle("I'm a Style", null);
  45. this.setViewportView(panel);
  46. panel.setLayout(new BorderLayout(0, 0));
  47. panel.setBackground(Color.WHITE);
  48. panel.add(consoleText);
  49. setViewportView(panel);
  50. // PopUpMenu
  51. popmenu.add(itemCopy);
  52. popmenu.add(itemClear);
  53. itemCopy.addActionListener(new ActionListener() {
  54. @Override
  55. public void actionPerformed(ActionEvent e) {
  56. Toolkit.getDefaultToolkit().getSystemClipboard()
  57. .setContents(new StringSelection(consoleText.getSelectedText()), null);
  58. }
  59. });
  60. itemClear.addActionListener(new ActionListener() {
  61. @Override
  62. public void actionPerformed(ActionEvent e) {
  63. clearConsole();
  64. }
  65. });
  66. // MouseListener
  67. consoleText.addMouseListener(new MouseAdapter() {
  68. @SuppressWarnings("static-access")
  69. @Override
  70. public void mouseReleased(MouseEvent e) {
  71. if (e.getButton() == e.BUTTON3) {
  72. itemClear.setEnabled(!consoleText.getText().isEmpty());
  73. itemCopy.setEnabled(consoleText.getSelectedText() != null);
  74. popmenu.show(e.getComponent(), e.getX(), e.getY());
  75. }
  76. }
  77. });
  78. }
  79. /**
  80. * Print Text on the console.
  81. *
  82. * @param text
  83. * String the Text
  84. * @param color
  85. * the color of the Text
  86. * @param p
  87. * size of the Text
  88. * @param bold
  89. * bold or not
  90. * @param italic
  91. * italic or not
  92. * @param nl
  93. * new line or not
  94. *
  95. */
  96. public void addText(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  97. StyleConstants.setForeground(style, color);
  98. StyleConstants.setFontSize(style, p);
  99. StyleConstants.setBold(style, bold);
  100. StyleConstants.setItalic(style, italic);
  101. try {
  102. doc.insertString(doc.getLength(), text, style);
  103. if (nl) {
  104. doc.insertString(doc.getLength(), "\n", style);
  105. }
  106. } catch (BadLocationException e) {
  107. }
  108. }
  109. /**
  110. * Print Text on the console in black and font size 12.
  111. *
  112. * @param text
  113. * String the Text
  114. */
  115. public void addText(String text) {
  116. StyleConstants.setForeground(style, Color.BLACK);
  117. StyleConstants.setFontSize(style, 12);
  118. StyleConstants.setBold(style, false);
  119. StyleConstants.setItalic(style, false);
  120. try {
  121. doc.insertString(doc.getLength(), text + "\n", style);
  122. } catch (BadLocationException e) {
  123. }
  124. }
  125. /**
  126. * Clears the console.
  127. */
  128. public void clearConsole() {
  129. consoleText.setText("");
  130. }
  131. }