void removeText() {
   if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
     try {
       Document doc = c.getDocument();
       doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
     } catch (BadLocationException e) {
     }
   }
 }
Exemple #2
0
 /**
  * Called when the {@link Document} registered using {@link #startTracking} is edited. This
  * happens when text is inserted or removed.
  *
  * @param de
  */
 protected void editEvent(DocumentEvent de) {
   // System.out.println("document edit @ " + de.getOffset());
   if (de.getOffset() <= pos.getOffset()) {
     updatePosition();
     // System.out.println("updating, new line no: " + lineNo);
   }
 }
 /**
  * Two folds are considered equal if they start at the same offset.
  *
  * @param otherFold Another fold to compare this one to.
  * @return How this fold compares to the other.
  */
 @Override
 public int compareTo(Fold otherFold) {
   int result = -1;
   if (otherFold != null) {
     result = startOffs.getOffset() - otherFold.startOffs.getOffset();
     // result = getStartLine() - otherFold.getStartLine();
   }
   return result;
 }
 // Remove the old text if the action is a MOVE.
 // However, we do not allow dropping on top of the selected text, so in that case do nothing.
 protected void exportDone(JComponent c, Transferable data, int action) {
   if (c != textComponent) { // ###
     System.out.println("*** exportDone(): c=" + c);
   }
   System.out.println(
       ">>> exportDone(): action="
           + action
           + ", MOVE="
           + MOVE
           + ", shouldRemove="
           + shouldRemove);
   if (shouldRemove && (action == MOVE)) {
     if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
       try {
         textComponent.getDocument().remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
       } catch (BadLocationException e) {
         System.out.println("*** exportDone(): Can't remove text from source.");
       }
     }
   }
   source = null;
 }
Exemple #5
0
 /** Update the tracked position. Will notify listeners if line number has changed. */
 protected synchronized void updatePosition() {
   if (doc != null && pos != null) {
     // track position
     int offset = pos.getOffset();
     int oldLineIdx = lineIdx;
     lineIdx = doc.getDefaultRootElement().getElementIndex(offset); // offset to lineNo
     if (lineIdx != oldLineIdx) {
       for (LineListener l : listeners) {
         if (l != null) {
           l.lineChanged(this, oldLineIdx, lineIdx);
         } else {
           listeners.remove(l); // remove null listener
         }
       }
     }
   }
 }
    @Override
    public String getSelectedText() {
      Document doc = getDocument();
      int start = getSelectionStart();
      int end = getSelectionEnd();

      try {
        Position p0 = doc.createPosition(start);
        Position p1 = doc.createPosition(end);
        StringWriter sw = new StringWriter(p1.getOffset() - p0.getOffset());
        getEditorKit().write(sw, doc, p0.getOffset(), p1.getOffset() - p0.getOffset());

        return StringUtil.removeHtmlTags(sw.toString());
      } catch (BadLocationException e) {
        LOG.warn(e);
      } catch (IOException e) {
        LOG.warn(e);
      }
      return super.getSelectedText();
    }
 public boolean importData(JComponent c, Transferable t) {
   if (c != textComponent) { // ### never happens, we dont share transfer handlers
     System.out.println("*** importData(): c=" + c);
   }
   try {
     DataFlavor[] flavors = t.getTransferDataFlavors();
     boolean hasStringFlavor = t.isDataFlavorSupported(DataFlavor.stringFlavor);
     boolean hasImageFlavor = t.isDataFlavorSupported(DataFlavor.imageFlavor);
     boolean hasFilelistFlavor = t.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
     //
     System.out.println(">>> import data to text panel (" + flavors.length + " flavors)");
     // ### for (int i = 0; i < flavors.length; i++) {
     // ###	System.out.println(flavors[i]);
     // ###}
     System.out.println("  >   string flavor supported: " + hasStringFlavor);
     System.out.println("  >    image flavor supported: " + hasImageFlavor);
     System.out.println("  > filelist flavor supported: " + hasFilelistFlavor);
     //
     // We do not allow dropping on top of the selected text
     if ((source == textComponent)
         && (textComponent.getCaretPosition() >= p0.getOffset())
         && (textComponent.getCaretPosition() <= p1.getOffset())) {
       shouldRemove = false;
       System.out.println(
           ">>> dropping on top of the selected text is not allowed -- import canceled");
       return true;
     }
     //
     if (hasStringFlavor) {
       String data = (String) t.getTransferData(DataFlavor.stringFlavor);
       int pos = textComponent.getCaretPosition();
       if (DeepaMehtaUtils.isImage(data)) {
         HTMLEditorKit kit = (HTMLEditorKit) ((JEditorPane) textComponent).getEditorKit();
         HTMLDocument doc = (HTMLDocument) textComponent.getDocument();
         String html = "<img src=\"" + data + "\"></img>";
         kit.insertHTML(doc, pos, html, 0, 0, HTML.Tag.IMG); // ### <img> not XML conform
         // ### doc.insertBeforeStart(doc.getParagraphElement(pos), html);
         System.out.println(">>> IMG tag inserted: \"" + html + "\"");
       } else {
         textComponent.getDocument().insertString(pos, data, null);
         System.out.println(">>> regular text inserted: \"" + data + "\"");
       }
     } else if (hasFilelistFlavor) {
       java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
       System.out.println("    " + files.size() + " files:");
       for (int i = 0; i < files.size(); i++) {
         File file = (File) files.get(i);
         String filename = file.getName();
         System.out.println("    " + file);
         if (DeepaMehtaUtils.isHTML(filename)) {
           String html = DeepaMehtaUtils.readFile(file);
           textComponent.setText(html); // ### replace instead insert
           textComponent.setCaretPosition(0);
           // ### ((JEditorPane) textComponent).setPage("file://" + file);	// ### replace instead
           // insert
           // ### setDirty("dropping HTML file");
           System.out.println(">>> HTML inserted (read from file)");
           break; // ### max one file is inserted
         } else if (DeepaMehtaUtils.isImage(filename)) {
           HTMLEditorKit kit = (HTMLEditorKit) ((JEditorPane) textComponent).getEditorKit();
           HTMLDocument doc = (HTMLDocument) textComponent.getDocument();
           int pos = textComponent.getCaretPosition();
           String imagefile = file.getPath().replace('\\', '/'); // ###
           String html = "<img src=\"" + imagefile + "\"></img>";
           kit.insertHTML(doc, pos, html, 0, 0, HTML.Tag.IMG); // ### <img> not XML conform
           // ### doc.insertBeforeStart(doc.getParagraphElement(pos), html);
           System.out.println(">>> IMG tag inserted: \"" + html + "\"");
         } else {
           System.out.println(
               "### importData(): only implemented for HTML files -- import canceled");
         }
       }
     } else {
       System.out.println("*** importData(): no supported flavor " + c);
     }
     return true;
   } catch (UnsupportedFlavorException ufe) {
     System.out.println("*** while dropping to text panel: " + ufe);
   } catch (BadLocationException ble) {
     System.out.println("*** while dropping to text panel: " + ble);
   } catch (IOException ioe) {
     System.out.println("*** while dropping to text panel: " + ioe);
   }
   //
   return super.importData(c, t);
 }
 /**
  * Returns the starting offset of this fold region. For example, for languages such as C and Java,
  * this would be the offset of the opening curly brace of a code block.
  *
  * <p>The value returned by this method will automatically update as the text area's contents are
  * modified, to track the starting offset of the code block.
  *
  * @return The start offset of this fold.
  * @see #getStartLine()
  * @see #getEndOffset()
  */
 public int getStartOffset() {
   return startOffs.getOffset();
 }
 /**
  * Returns the end offset of this fold. For example, in languages such as C and Java, this might
  * be the offset of the closing curly brace of a code block.
  *
  * <p>The value returned by this method will automatically update as the text area's contents are
  * modified, to track the ending offset of the code block.
  *
  * @return The end offset of this code block, or {@link Integer#MAX_VALUE} if this fold region
  *     isn't closed properly. The latter causes this fold to collapsed all lines through the end
  *     of the file.
  * @see #getEndLine()
  * @see #getStartOffset()
  */
 public int getEndOffset() {
   return endOffs != null ? endOffs.getOffset() : Integer.MAX_VALUE;
 }