コード例 #1
0
  /** Import the given stream data into the text component. */
  protected void handleReaderImport(Reader in, JTextComponent c)
      throws BadLocationException, IOException {

    char[] buff = new char[1024];
    int nch;
    boolean lastWasCR = false;
    int last;
    StringBuffer sbuff = null;

    // Read in a block at a time, mapping \r\n to \n, as well as single
    // \r to \n.
    while ((nch = in.read(buff, 0, buff.length)) != -1) {

      if (sbuff == null) {
        sbuff = new StringBuffer(nch);
      }
      last = 0;

      for (int counter = 0; counter < nch; counter++) {

        switch (buff[counter]) {
          case '\r':
            if (lastWasCR) {
              if (counter == 0) sbuff.append('\n');
              else buff[counter - 1] = '\n';
            } else lastWasCR = true;
            break;
          case '\n':
            if (lastWasCR) {
              if (counter > (last + 1)) sbuff.append(buff, last, counter - last - 1);
              // else nothing to do, can skip \r, next write will
              // write \n
              lastWasCR = false;
              last = counter;
            }
            break;
          default:
            if (lastWasCR) {
              if (counter == 0) sbuff.append('\n');
              else buff[counter - 1] = '\n';
              lastWasCR = false;
            }
            break;
        } // End fo switch (buff[counter]).
      } // End of for (int counter = 0; counter < nch; counter++).

      if (last < nch) {
        if (lastWasCR) {
          if (last < (nch - 1)) sbuff.append(buff, last, nch - last - 1);
        } else sbuff.append(buff, last, nch - last);
      }
    } // End of while ((nch = in.read(buff, 0, buff.length)) != -1).

    if (withinSameComponent) {
      ((RTextArea) c).beginAtomicEdit();
    }

    if (lastWasCR) sbuff.append('\n');
    c.replaceSelection(sbuff != null ? sbuff.toString() : "");
  }
コード例 #2
0
 /**
  * This method is called after data has been exported. This method should remove the data that was
  * transfered if the action was MOVE.
  *
  * @param source The component that was the source of the data.
  * @param data The data that was transferred or possibly null if the action is <code>NONE</code>.
  * @param action The actual action that was performed.
  */
 protected void exportDone(JComponent source, Transferable data, int action) {
   // only remove the text if shouldRemove has not been set to
   // false by importData and only if the action is a move
   if (shouldRemove && action == MOVE) {
     TextTransferable t = (TextTransferable) data;
     t.removeText();
     if (withinSameComponent) {
       ((RTextArea) source).endAtomicEdit();
       withinSameComponent = false;
     }
   }
   exportComp = null;
 }