Ejemplo n.º 1
0
 TextTransferable(JTextComponent c, int start, int end) {
   this.c = c;
   Document doc = c.getDocument();
   try {
     p0 = doc.createPosition(start);
     p1 = doc.createPosition(end);
     plainData = c.getSelectedText();
   } catch (BadLocationException ble) {
   }
 }
  /**
   * Changes a highlight.
   *
   * @param tag the highlight tag
   * @param p0 the beginning of the range >= 0
   * @param p1 the end of the range >= p0
   * @exception BadLocationException if the specified location is invalid
   */
  public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
    if (p0 < 0) {
      throw new BadLocationException("Invalid beginning of the range", p0);
    }

    if (p1 < p0) {
      throw new BadLocationException("Invalid end of the range", p1);
    }

    Document doc = component.getDocument();
    if (tag instanceof LayeredHighlightInfo) {
      LayeredHighlightInfo lhi = (LayeredHighlightInfo) tag;
      if (lhi.width > 0 && lhi.height > 0) {
        component.repaint(lhi.x, lhi.y, lhi.width, lhi.height);
      }
      // Mark the highlights region as invalid, it will reset itself
      // next time asked to paint.
      lhi.width = lhi.height = 0;
      lhi.p0 = doc.createPosition(p0);
      lhi.p1 = doc.createPosition(p1);
      safeDamageRange(Math.min(p0, p1), Math.max(p0, p1));
    } else {
      HighlightInfo info = (HighlightInfo) tag;
      int oldP0 = info.p0.getOffset();
      int oldP1 = info.p1.getOffset();
      if (p0 == oldP0) {
        safeDamageRange(Math.min(oldP1, p1), Math.max(oldP1, p1));
      } else if (p1 == oldP1) {
        safeDamageRange(Math.min(p0, oldP0), Math.max(p0, oldP0));
      } else {
        safeDamageRange(oldP0, oldP1);
        safeDamageRange(p0, p1);
      }
      info.p0 = doc.createPosition(p0);
      info.p1 = doc.createPosition(p1);
    }
  }
 // Create a Transferable implementation that contains the selected text.
 protected Transferable createTransferable(JComponent c) {
   if (c != textComponent) { // ###
     System.out.println("*** createTransferable(): c=" + c);
   }
   source = (JTextComponent) c;
   int start = source.getSelectionStart();
   int end = source.getSelectionEnd();
   Document doc = source.getDocument();
   if (start == end) {
     return null;
   }
   try {
     p0 = doc.createPosition(start);
     p1 = doc.createPosition(end);
     System.out.println(">>> createTransferable(): p0=" + p0 + ", p1=" + p1);
   } catch (BadLocationException e) {
     System.out.println(
         "*** createTransferable(): "
             + "Can't create position - unable to remove text from source.");
   }
   shouldRemove = true;
   String data = source.getSelectedText();
   return new StringSelection(data);
 }
  /**
   * Adds a highlight to the view. Returns a tag that can be used to refer to the highlight.
   *
   * @param p0 the start offset of the range to highlight &gt;= 0
   * @param p1 the end offset of the range to highlight &gt;= p0
   * @param p the painter to use to actually render the highlight
   * @return an object that can be used as a tag to refer to the highlight
   * @exception BadLocationException if the specified location is invalid
   */
  public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p)
      throws BadLocationException {
    if (p0 < 0) {
      throw new BadLocationException("Invalid start offset", p0);
    }

    if (p1 < p0) {
      throw new BadLocationException("Invalid end offset", p1);
    }

    Document doc = component.getDocument();
    HighlightInfo i =
        (getDrawsLayeredHighlights() && (p instanceof LayeredHighlighter.LayerPainter))
            ? new LayeredHighlightInfo()
            : new HighlightInfo();
    i.painter = p;
    i.p0 = doc.createPosition(p0);
    i.p1 = doc.createPosition(p1);
    highlights.addElement(i);
    safeDamageRange(p0, p1);
    return i;
  }
 /**
  * Queues damageRange() call into event dispatch thread to be sure that views are in consistent
  * state.
  */
 private void safeDamageRange(int a0, int a1) throws BadLocationException {
   Document doc = component.getDocument();
   safeDamageRange(doc.createPosition(a0), doc.createPosition(a1));
 }