Advertisement
Guest User

Untitled

a guest
Sep 18th, 2012
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1.  
  2. import java.io.*;
  3.  
  4. import javax.swing.JTextArea;
  5.  
  6. import org.eclipse.swt.widgets.Text;
  7.  
  8. /**
  9. * A implementation of the java.io.Writer class which facilitates writing to a
  10. * JTextArea via a stream.
  11. *
  12. * <p>
  13. * <b>Note:</b> There appears to be bug in the Macintosh implementation of the
  14. * JDK 1.1 where a PrintWriter writing to this class will not include the
  15. * correct line feeds for display in a JTextArea. There is a simple test of the
  16. * "java.version" system property which, if it starts with the String "1.1" will
  17. * cause newlines to be written each time the buffer is flushed.
  18. * </p>
  19. *
  20. * @author Anthony Eden
  21. */
  22.  
  23. public class TextAreaWriter extends PrintStream {
  24.  
  25. private boolean closed = false;
  26. private Text textArea;
  27. private StringBuffer buffer;
  28.  
  29. /**
  30. * Constructor.
  31. *
  32. * @param txtfieldProtokoll
  33. * The JTextArea to write to.
  34. */
  35.  
  36. public TextAreaWriter(Text txtfieldProtokoll) {
  37. super(new OutputStream() {
  38. public void write(int b) {
  39. this.write((char) b);
  40. }
  41. });
  42. setTextArea(txtfieldProtokoll);
  43. }
  44.  
  45. /**
  46. * Set the JTextArea to write to.
  47. *
  48. * @param txtfieldProtokoll
  49. * The JTextArea
  50. */
  51.  
  52. public void setTextArea(Text txtfieldProtokoll) {
  53. if (txtfieldProtokoll == null) {
  54. throw new IllegalArgumentException(
  55. "The text area must not be null.");
  56. }
  57. this.textArea = txtfieldProtokoll;
  58. }
  59.  
  60. /** Close the stream. */
  61.  
  62. public void close() {
  63. closed = true;
  64. }
  65.  
  66. /**
  67. * Flush the data that is currently in the buffer.
  68. *
  69. * @throws IOException
  70. */
  71.  
  72. public void flush() { // throws IOException{
  73. // if(closed){
  74. // throw new IOException("The stream is not open.");
  75. // }
  76. // the newline character should not be necessary. The PrintWriter
  77. // should autmatically put the newline, but it doesn't seem to work
  78. textArea.append(getBuffer().toString());
  79. if (System.getProperty("java.version").startsWith("1.1")) {
  80. textArea.append("\n");
  81. }
  82.  
  83. // textArea.setCaretPosition(textArea.getText().length());
  84. // textArea.setca
  85. buffer = null;
  86. }
  87.  
  88. /**
  89. * Write the given character array to the output stream.
  90. *
  91. * @param charArray
  92. * The character array
  93. * @throws IOException
  94. */
  95.  
  96. public void write(char[] charArray) throws IOException {
  97. write(charArray, 0, charArray.length);
  98. }
  99.  
  100. /**
  101. * Write the given character array to the output stream beginning from the
  102. * given offset and proceeding to until the given length is reached.
  103. *
  104. * @param charArray
  105. * The character array
  106. * @param offset
  107. * The start offset
  108. * @param length
  109. * The length to write
  110. * @throws IOException
  111. */
  112.  
  113. public void write(char[] charArray, int offset, int length)
  114. throws IOException {
  115. if (closed) {
  116. throw new IOException("The stream is not open.");
  117. }
  118. getBuffer().append(charArray, offset, length);
  119. }
  120.  
  121. /**
  122. * Write the given character to the output stream.
  123. *
  124. * @param c
  125. * The character
  126. * @throws IOException
  127. */
  128.  
  129. public void write(int c) {
  130. getBuffer().append((char) c);
  131. }
  132.  
  133. /**
  134. * Write the given String to the output stream.
  135. *
  136. * @param string
  137. * The String
  138. * @throws IOException
  139. */
  140. public void write(byte[] buf, int off, int len) {
  141. try {
  142. char[] c = new char[len];
  143. for (int i = 0; i < len; i++)
  144. c[i] = (char) buf[i];
  145. this.write(c, off, len);
  146. } catch (Exception e) {
  147. }
  148. }
  149.  
  150. public void write(String string) throws IOException {
  151. if (closed) {
  152. throw new IOException("The stream is not open.");
  153. }
  154. getBuffer().append(string);
  155. }
  156.  
  157. /**
  158. * Write the given String to the output stream beginning from the given
  159. * offset and proceeding to until the given length is reached.
  160. *
  161. * @param string
  162. * The String
  163. * @param offset
  164. * The start offset
  165. * @param length
  166. * The length to write
  167. * @throws IOException
  168. */
  169.  
  170. public void write(String string, int offset, int length) throws IOException {
  171. if (closed) {
  172. throw new IOException("The stream is not open.");
  173. }
  174. getBuffer().append(string.substring(offset, length));
  175. }
  176.  
  177. // protected methods
  178.  
  179. /**
  180. * Get the StringBuffer which holds the data prior to writing via a call to
  181. * the <code>flush()method. This method should
  182. never return null.
  183. *
  184. * @return A StringBuffer
  185. */
  186.  
  187. protected StringBuffer getBuffer() {
  188. if (buffer == null) {
  189. buffer = new StringBuffer();
  190. }
  191. return buffer;
  192. }
  193.  
  194. public void print(boolean b) {
  195. try {
  196. if (b)
  197. write("true");
  198. else
  199. write("false");
  200. flush();
  201. } catch (Exception e) {
  202. }
  203. }
  204.  
  205. public void print(char c) {
  206. try {
  207. this.write(new char[] { c });
  208. flush();
  209. } catch (Exception e) {
  210. }
  211. }
  212.  
  213. public void print(char[] s) {
  214. try {
  215. this.write(s);
  216. flush();
  217. } catch (Exception e) {
  218. }
  219. }
  220.  
  221. public void print(double d) {
  222. try {
  223. this.write("" + d);
  224. flush();
  225. } catch (Exception e) {
  226. }
  227. }
  228.  
  229. public void print(float f) {
  230. try {
  231. this.write("" + f);
  232. flush();
  233. } catch (Exception e) {
  234. }
  235. }
  236.  
  237. public void print(int i) {
  238. try {
  239. this.write("" + i);
  240. flush();
  241. } catch (Exception e) {
  242. }
  243. }
  244.  
  245. public void print(long l) {
  246. try {
  247. this.write("" + l);
  248. flush();
  249. } catch (Exception e) {
  250. }
  251. }
  252.  
  253. public void print(Object o) {
  254. try {
  255. this.write(o.toString());
  256. flush();
  257. } catch (Exception e) {
  258. }
  259. }
  260.  
  261. public void print(String s) {
  262. try {
  263. this.write(s);
  264. flush();
  265. } catch (Exception e) {
  266. }
  267. }
  268.  
  269. public void println(boolean b) {
  270. print(b);
  271. println();
  272. }
  273.  
  274. public void println(char c) {
  275. print(c);
  276. println();
  277. }
  278.  
  279. public void println(char[] s) {
  280. print(s);
  281. println();
  282. }
  283.  
  284. public void println(double d) {
  285. print(d);
  286. ;
  287. println();
  288. }
  289.  
  290. public void println(float f) {
  291. print(f);
  292. println();
  293. }
  294.  
  295. public void println(int i) {
  296. print(i);
  297. println();
  298. }
  299.  
  300. public void println(long l) {
  301. print(l);
  302. println();
  303. }
  304.  
  305. public void println(Object o) {
  306. print(o);
  307. println();
  308. }
  309.  
  310. public void println(String s) {
  311. print(s);
  312. println();
  313. }
  314.  
  315. public void println() {
  316. try {
  317. this.write(new char[] { '\n' });
  318. flush();
  319. } catch (Exception e) {
  320. }
  321. }
  322.  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement