You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextPane.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.textpane;
  23. import java.awt.BorderLayout;
  24. import java.awt.Color;
  25. import java.awt.Dimension;
  26. import java.awt.event.AdjustmentEvent;
  27. import java.awt.event.AdjustmentListener;
  28. import java.awt.event.MouseWheelEvent;
  29. import java.awt.event.MouseWheelListener;
  30. import java.awt.font.TextAttribute;
  31. import java.text.AttributedString;
  32. import javax.swing.JComponent;
  33. import javax.swing.JFrame;
  34. import javax.swing.JScrollBar;
  35. /**
  36. * Styled, scrollable text pane.
  37. */
  38. public final class TextPane extends JComponent implements AdjustmentListener,
  39. MouseWheelListener {
  40. /**
  41. * A version number for this class. It should be changed whenever the class
  42. * structure is changed (or anything else that would prevent serialized
  43. * objects being unserialized with the new class).
  44. */
  45. private static final long serialVersionUID = 2;
  46. /** Scrollbar for the component. */
  47. private JScrollBar scrollBar;
  48. /** Canvas object, used to draw text. */
  49. private TextPaneCanvas textPane;
  50. /** IRCDocument. */
  51. private IRCDocument document;
  52. /**
  53. * Creates a new instance of TextPane.
  54. */
  55. public TextPane() {
  56. document = new IRCDocument();
  57. this.setLayout(new BorderLayout());
  58. textPane = new TextPaneCanvas(this, document);
  59. this.add(textPane, BorderLayout.CENTER);
  60. scrollBar = new JScrollBar(JScrollBar.VERTICAL);
  61. this.add(scrollBar, BorderLayout.LINE_END);
  62. this.setAutoscrolls(true);
  63. scrollBar.setMaximum(document.getNumLines());
  64. scrollBar.setBlockIncrement(10);
  65. scrollBar.setUnitIncrement(1);
  66. scrollBar.addAdjustmentListener(this);
  67. this.addMouseWheelListener(this);
  68. }
  69. /**
  70. * Adds text to the textpane.
  71. * @param text text to add
  72. */
  73. public void addText(final String text) {
  74. addText(new AttributedString(text));
  75. }
  76. /**
  77. * Adds styled text to the textpane.
  78. * @param text styled text to add
  79. */
  80. public void addText(final AttributedString text) {
  81. document.addText(text);
  82. scrollBar.setMaximum(document.getNumLines());
  83. if (!scrollBar.getValueIsAdjusting()
  84. && (scrollBar.getValue() == scrollBar.getMaximum() - 1)) {
  85. setScrollBarPosition(document.getNumLines());
  86. }
  87. }
  88. /**
  89. * Sets the new position for the scrollbar and the associated position
  90. * to render the text from.
  91. * @param position new position of the scrollbar
  92. */
  93. private void setScrollBarPosition(final int position) {
  94. scrollBar.setValue(position);
  95. textPane.setScrollBarPosition(position);
  96. }
  97. /** {@inheritDoc}. */
  98. public void adjustmentValueChanged(final AdjustmentEvent e) {
  99. if (e.getValue() < document.getNumLines()) {
  100. scrollBar.setValue(e.getValue());
  101. textPane.setScrollBarPosition(e.getValue());
  102. }
  103. }
  104. /** {@inheritDoc}. */
  105. public void mouseWheelMoved(final MouseWheelEvent e) {
  106. if (scrollBar.isEnabled()) {
  107. if (e.getWheelRotation() > 0) {
  108. setScrollBarPosition(scrollBar.getValue() + e.getScrollAmount());
  109. } else {
  110. setScrollBarPosition(scrollBar.getValue() - e.getScrollAmount());
  111. }
  112. }
  113. }
  114. /** temporary method to add some text to the text pane. */
  115. public void addTestText() {
  116. for (int i = 0; i <= 20; i++) {
  117. AttributedString attributedString =
  118. new AttributedString("this is a line");
  119. document.addText(attributedString);
  120. attributedString = new AttributedString("this is a line");
  121. attributedString.addAttribute(TextAttribute.UNDERLINE,
  122. TextAttribute.UNDERLINE_ON,
  123. 0, attributedString.getIterator().getEndIndex());
  124. document.addText(attributedString);
  125. attributedString = new AttributedString("this is a line");
  126. attributedString.addAttribute(TextAttribute.WEIGHT,
  127. TextAttribute.WEIGHT_ULTRABOLD,
  128. 0, attributedString.getIterator().getEndIndex());
  129. document.addText(attributedString);
  130. attributedString = new AttributedString("this is a line");
  131. attributedString.addAttribute(TextAttribute.UNDERLINE,
  132. TextAttribute.UNDERLINE_ON,
  133. 5, attributedString.getIterator().getEndIndex());
  134. attributedString.addAttribute(TextAttribute.FOREGROUND, Color.GREEN,
  135. 5, 10);
  136. document.addText(attributedString);
  137. attributedString = new AttributedString("this is a long, long, long, "
  138. + "long, long, long, long, long, long, long, long, long, "
  139. + "long, long, long, long, long, long, long, long, long, "
  140. + "long, long, long line");
  141. document.addText(attributedString);
  142. }
  143. scrollBar.setMaximum(document.getNumLines());
  144. setScrollBarPosition(document.getNumLines());
  145. }
  146. /**
  147. * temporary method to text the textpane.
  148. * @param args command line arguments
  149. */
  150. public static void main(final String[] args) {
  151. final TextPane tpc = new TextPane();
  152. final JFrame frame = new JFrame("Test textpane");
  153. tpc.setDoubleBuffered(true);
  154. tpc.setBackground(Color.WHITE);
  155. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  156. frame.add(tpc);
  157. frame.setSize(new Dimension(400, 400));
  158. frame.setVisible(true);
  159. tpc.addTestText();
  160. }
  161. }