/plugins/LogViewer/tags/rel-0-7/src/java/ghm/follow/JTextAreaDestination.java

# · Java · 254 lines · 140 code · 31 blank · 83 comment · 26 complexity · d8d87153fea4a6b9fd1c2276c2a69811 MD5 · raw file

  1. /*
  2. Copyright (C) 2000, 2001 Greg Merrill (greghmerrill@yahoo.com)
  3. This file is part of Follow (http://follow.sf.net).
  4. Follow is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public
  6. License as published by the Free Software Foundation.
  7. Follow is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Follow; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. package ghm.follow;
  16. import java.util.*;
  17. import java.util.regex.*;
  18. import javax.swing.JTextArea;
  19. import javax.swing.text.BadLocationException;
  20. import javax.swing.text.Document;
  21. import logviewer.LogType;
  22. /**
  23. * Implementation of {@link OutputDestination} which appends Strings to a {@link
  24. * JTextArea}.
  25. *
  26. * @author <a href="mailto:greghmerrill@yahoo.com">Greg Merrill</a>
  27. * @version $Revision: 1156 $
  28. * @see OutputDestination
  29. * @see JTextArea
  30. */
  31. public class JTextAreaDestination extends OutputDestinationComponent {
  32. private boolean wrapFind = false;
  33. private LogType logType = null;
  34. /**
  35. * Construct a new JTextAreaDestination.
  36. *
  37. * @param jTextArea text will be appended to this text area
  38. * @param autoPositionCaret if true, caret will be automatically moved to
  39. * the bottom of the text area when text is appended
  40. */
  41. public JTextAreaDestination(JTextArea jTextArea, boolean autoPositionCaret) {
  42. this(jTextArea, autoPositionCaret, null);
  43. }
  44. public JTextAreaDestination(JTextArea jTextArea, boolean autoPositionCaret, LogType logType) {
  45. jTextArea_ = jTextArea;
  46. autoPositionCaret_ = autoPositionCaret;
  47. this.logType = logType;
  48. }
  49. /**
  50. * Gets the jTextArea attribute of the JTextAreaDestination object
  51. *
  52. * @return The jTextArea value
  53. */
  54. public JTextArea getJTextArea() {
  55. return jTextArea_;
  56. }
  57. /**
  58. * Sets the jTextArea attribute of the JTextAreaDestination object
  59. *
  60. * @param jTextArea The new jTextArea value
  61. */
  62. public void setJTextArea(JTextArea jTextArea) {
  63. jTextArea_ = jTextArea;
  64. }
  65. /**
  66. * @return whether caret will be automatically moved to the bottom of the
  67. * text area when text is appended
  68. */
  69. public boolean autoPositionCaret() {
  70. return autoPositionCaret_;
  71. }
  72. /**
  73. * @param autoPositionCaret if true, caret will be automatically moved to
  74. * the bottom of the text area when text is appended
  75. */
  76. public void setAutoPositionCaret(boolean autoPositionCaret) {
  77. autoPositionCaret_ = autoPositionCaret;
  78. }
  79. public void toggleAutoPositionCaret() {
  80. autoPositionCaret_ = !autoPositionCaret_;
  81. }
  82. /** Description of the Method */
  83. public void toggleWordWrap() {
  84. jTextArea_.setWrapStyleWord(!jTextArea_.getWrapStyleWord());
  85. jTextArea_.setLineWrap(!jTextArea_.getLineWrap());
  86. }
  87. /**
  88. * Gets the wordWrap attribute of the JTextAreaDestination object
  89. *
  90. * @return The wordWrap value
  91. */
  92. public boolean getWordWrap() {
  93. return jTextArea_.getLineWrap();
  94. }
  95. /**
  96. * Sets the wordWrap attribute of the JTextAreaDestination object
  97. *
  98. * @param b The new wordWrap value
  99. */
  100. public void setWordWrap(boolean b) {
  101. jTextArea_.setWrapStyleWord(b);
  102. jTextArea_.setLineWrap(b);
  103. }
  104. /**
  105. * Description of the Method
  106. *
  107. * @param toFind
  108. */
  109. public void find(String toFind) {
  110. if (toFind == null || toFind.length() == 0) {
  111. return;
  112. }
  113. try {
  114. String doc = jTextArea_.getDocument().getText(0, jTextArea_.getDocument().getLength());
  115. Pattern pattern = Pattern.compile(toFind, Pattern.DOTALL);
  116. Matcher matcher = pattern.matcher(doc);
  117. if (matcher.find()) {
  118. int start = matcher.start();
  119. int end = matcher.end();
  120. jTextArea_.setCaretPosition(start);
  121. jTextArea_.select(start, end);
  122. }
  123. }
  124. catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. /**
  129. * Description of the Method
  130. *
  131. * @param toFind
  132. */
  133. public void findNext(String toFind) {
  134. if (toFind == null || toFind.length() == 0) {
  135. return;
  136. }
  137. try {
  138. int initial_caret = jTextArea_.getCaretPosition();
  139. int caret = initial_caret;
  140. String doc = jTextArea_.getDocument().getText(0, jTextArea_.getDocument().getLength());
  141. Pattern pattern = Pattern.compile(toFind, Pattern.DOTALL);
  142. Matcher matcher = pattern.matcher(doc);
  143. if (matcher.find()) {
  144. boolean done = false;
  145. while (!done) {
  146. matcher = pattern.matcher(doc);
  147. while (matcher.find()) {
  148. int start = matcher.start();
  149. int end = matcher.end();
  150. if (start < caret) {
  151. continue;
  152. }
  153. caret = end;
  154. jTextArea_.setCaretPosition(start);
  155. jTextArea_.select(start, end);
  156. done = true;
  157. break;
  158. }
  159. if (wrapFind) {
  160. caret = 0;
  161. }
  162. else {
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. catch (Exception e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. /**
  173. * Sets the wrapFind attribute of the JTextAreaDestination object
  174. *
  175. * @param wrap The new wrapFind value
  176. */
  177. public void setWrapFind(boolean wrap) {
  178. wrapFind = wrap;
  179. }
  180. /** Description of the Method */
  181. public void clear() {
  182. jTextArea_.setText("");
  183. }
  184. public void print( String[] s ) {
  185. for ( int i = 0; i < s.length; i++ ) {
  186. print( s[i] );
  187. }
  188. }
  189. /**
  190. * Description of the Method
  191. *
  192. * @param s
  193. */
  194. public void print(String s) {
  195. jTextArea_.append(s);
  196. if (autoPositionCaret_) {
  197. jTextArea_.setCaretPosition(jTextArea_.getDocument().getLength());
  198. }
  199. }
  200. /**
  201. * @deprecated
  202. */
  203. private String removeRowsByRegex(String s) {
  204. if (logType == null)
  205. return s;
  206. String regex = logType.getRowRegex();
  207. if (regex == null)
  208. return s;
  209. boolean include = logType.getRowInclude();
  210. int flags = logType.getRowFlags();
  211. Pattern p = Pattern.compile(regex, flags);
  212. Matcher m = p.matcher(s);
  213. if (m.matches() && include) {
  214. return s;
  215. }
  216. else if (!m.matches() && !include) {
  217. return s;
  218. }
  219. return "";
  220. }
  221. protected JTextArea jTextArea_;
  222. protected boolean autoPositionCaret_;
  223. }