Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. private class RUMAddTextEvent extends AWTEvent {
  2.  
  3. public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
  4. private int index;
  5. private String str;
  6. private AttributeSet as;
  7.  
  8. public RUMAddTextEvent(Component target, int index, String str, AttributeSet as) {
  9. super(target, EVENT_ID);
  10. this.index = index;
  11. this.str = str;
  12. this.as = as;
  13. }
  14.  
  15. public int getIndex() {
  16. return index;
  17. }
  18.  
  19. public String getStr() {
  20. return str;
  21. }
  22.  
  23. public AttributeSet getAs() {
  24. return as;
  25. }
  26. }
  27.  
  28. private class RUMRemoveTextEvent extends AWTEvent {
  29.  
  30. public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
  31. int index;
  32. int size;
  33.  
  34. RUMRemoveTextEvent(Component target, int index, int size) {
  35. super(target, EVENT_ID);
  36. this.index = index;
  37. this.size = size;
  38. }
  39.  
  40. public int getIndex() {
  41. return index;
  42. }
  43.  
  44. public int getSize() {
  45. return size;
  46. }
  47. }
  48.  
  49. /**
  50. * Prints a character at a time to the RUMComm window.
  51. *
  52. * @param c
  53. */
  54. public void simpleOut(Character c) {
  55. cursor.x++;
  56. if (lines.isEmpty()) {
  57. this.lines.add(c.toString());
  58. } else {
  59. this.lines.add(cursor.y, this.lines.get(cursor.y).concat(c.toString()));
  60. this.lines.remove(cursor.y + 1);
  61.  
  62. }
  63.  
  64. try {
  65. //doc.insertString(doc.getLength(), c.toString(), as);
  66.  
  67. eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
  68. eventQueue.postEvent(new RUMAddTextEvent(this, doc.getLength(), c.toString(), as));
  69. getCaret().setDot(doc.getLength());
  70. } catch (Exception ex) {
  71. //Exceptions.printStackTrace(ex);
  72. }
  73. }
  74.  
  75. /**
  76. * Creates a new line
  77. */
  78. public void newLine() {
  79. cursor.y++;
  80. cursor.x = 0;
  81. this.lines.add("");
  82.  
  83. //doc.insertString(doc.getLength(), "n", null);
  84. eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
  85. eventQueue.postEvent(new RUMAddTextEvent(this, doc.getLength(), "n", null));
  86. getCaret().setDot(doc.getLength());
  87.  
  88. }
  89.  
  90. /**
  91. * Backspace implementation.
  92. *
  93. */
  94. public void deleteLast() {
  95. int endPos = doc.getLength();
  96. //doc.remove(endPos - 1, 1);
  97. eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
  98. eventQueue.postEvent(new RUMRemoveTextEvent(this, endPos - 1, 1));
  99. cursor.x--;
  100.  
  101. }
  102.  
  103. @Override
  104. protected void processEvent(AWTEvent awte) {
  105. //super.processEvent(awte);
  106. if (awte instanceof RUMAddTextEvent) {
  107. RUMAddTextEvent ev = (RUMAddTextEvent) awte;
  108. try {
  109. doc.insertString(ev.getIndex(), ev.getStr(), ev.getAs());
  110. } catch (BadLocationException ex) {
  111. Exceptions.printStackTrace(ex);
  112. }
  113. } else if (awte instanceof RUMRemoveTextEvent) {
  114. RUMRemoveTextEvent ev = (RUMRemoveTextEvent) awte;
  115. try {
  116. doc.remove(ev.getIndex(), ev.getSize());
  117. } catch (BadLocationException ex) {
  118. Exceptions.printStackTrace(ex);
  119. }
  120.  
  121. } else {
  122. super.processEvent(awte);
  123. }
  124. }
  125.  
  126. import javax.swing.Timer;
  127. import java.awt.event.ActionEvent;
  128. import java.awt.event.ActionListener;
  129. import java.io.ByteArrayOutputStream;
  130. import java.io.IOException;
  131.  
  132. public class SwingOutputStream extends ByteArrayOutputStream{
  133. private final ISwingLogger fSwingLogger;
  134.  
  135. private Timer fTimer;
  136. private final StringBuilder fBuffer = new StringBuilder( 1000 );
  137.  
  138. public SwingOutputStream( ISwingLogger aSwingLogger ) {
  139. fSwingLogger = aSwingLogger;
  140.  
  141. fTimer = new Timer( 200, new ActionListener() {
  142. public void actionPerformed( ActionEvent aActionEvent ) {
  143. flushBuffer();
  144. }
  145. } );
  146. fTimer.setRepeats( false );
  147. }
  148.  
  149. @Override
  150. public void flush() throws IOException {
  151. synchronized( fBuffer ){
  152. fBuffer.append( toString( "UTF-8") );
  153. }
  154. if ( fTimer.isRunning() ){
  155. fTimer.restart();
  156. } else {
  157. fTimer.start();
  158. }
  159.  
  160. super.flush();
  161. reset();
  162. }
  163.  
  164. private void flushBuffer(){
  165. synchronized ( fBuffer ){
  166. final String output = fBuffer.toString();
  167. fSwingLogger.appendString( output );
  168. fBuffer.setLength( 0 );
  169. }
  170. }
  171. }
  172.  
  173. new RUMAddTextEvent(this, doc.getLength(), ...
  174.  
  175. // in data thread
  176. void receiveData(String newData) {
  177. updateModelText(newData);
  178. invalidateUI();
  179. invokeLater(validateUI);
  180. }
  181.  
  182. // called in UI thread
  183. void validateUI() {
  184. if (!valid) {
  185. ui.text = model.text;
  186. }
  187. valid = true;
  188. }
  189.  
  190. public class EventTest {
  191.  
  192. public static void main(String[] args) {
  193.  
  194. JFrame frame = new JFrame();
  195. frame.setLayout(new BorderLayout());
  196. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  197. frame.setSize(400, 400);
  198. JTextArea area = new JTextArea();
  199. frame.add(new JScrollPane(area));
  200. frame.setLocationRelativeTo(null);
  201. frame.setVisible(true);
  202.  
  203. Thread thread = new Thread(new Dispatcher(area));
  204. thread.setDaemon(true);
  205. thread.start();
  206.  
  207. }
  208.  
  209. public static class Dispatcher implements Runnable {
  210.  
  211. private String message = "Hello from the other sidennCan you read thisnnAll done now";
  212. private JTextArea area;
  213.  
  214. public Dispatcher(JTextArea area) {
  215. this.area = area;
  216. }
  217.  
  218. @Override
  219. public void run() {
  220.  
  221. int index = 0;
  222. while (index < message.length()) {
  223.  
  224. try {
  225. Thread.sleep(250);
  226. } catch (InterruptedException ex) {
  227. }
  228.  
  229. send(message.charAt(index));
  230. index++;
  231.  
  232. }
  233.  
  234. }
  235.  
  236. public void send(final char text) {
  237.  
  238. System.out.println("Hello from out side the EDT - " + EventQueue.isDispatchThread());
  239.  
  240. try {
  241. SwingUtilities.invokeAndWait(new Runnable() {
  242. @Override
  243. public void run() {
  244. System.out.println("Hello from the EDT - " + EventQueue.isDispatchThread());
  245. area.append(new String(new char[]{text}));
  246. }
  247. });
  248. } catch (Exception exp) {
  249. exp.printStackTrace();
  250. }
  251.  
  252. }
  253.  
  254. }
  255.  
  256. }
Add Comment
Please, Sign In to add comment