public void insertUpdate(final DocumentEvent e) {
   EventQueue.invokeLater(
       new Runnable() {
         public void run() {
           try {
             String inserted = e.getDocument().getText(e.getOffset(), e.getLength());
             int i = inserted.indexOf('\n');
             if (i >= 0) {
               int offset = e.getOffset() + i;
               if (offset + 1 == e.getDocument().getLength()) {
                 returnResponse();
               } else {
                 // remove the '\n' and put it at the end
                 e.getDocument().remove(offset, 1);
                 e.getDocument().insertString(e.getDocument().getLength(), "\n", null);
                 // insertUpdate will be called again, since we have inserted the '\n' at
                 // the end
               }
             } else if (maxLen >= 0
                 && e.getDocument().getLength() - initialPos >= maxLen) {
               returnResponse();
             }
           } catch (BadLocationException ex) {
             returnResponse();
           }
         }
       });
 }
 String response() {
   EventQueue.invokeLater(this);
   try {
     return resultQueue.take();
   } catch (InterruptedException ex) {
     return null;
   } finally {
     cleanup();
   }
 }
 void cleanup() { // not required to be called from the GUI thread
   EventQueue.invokeLater(
       new Runnable() {
         public void run() {
           run.getDocument().removeDocumentListener(listener);
           run.setEditable(false);
           run.setNavigationFilter(null);
           run.setCaretPosition(run.getDocument().getLength());
           Simulator.getInstance().removeStopListener(stopListener);
         }
       });
 }
 public void removeUpdate(final DocumentEvent e) {
   EventQueue.invokeLater(
       new Runnable() {
         public void run() {
           if ((e.getDocument().getLength() < initialPos || e.getOffset() < initialPos)
               && e instanceof UndoableEdit) {
             ((UndoableEdit) e).undo();
             run.setCaretPosition(e.getOffset() + e.getLength());
           }
         }
       });
 }