// 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;
 }
    @Override
    public void runWithEvent(Event event) {
      if (mCanFix) {
        ITextOperationTarget operation =
            (ITextOperationTarget) mTextEditor.getAdapter(ITextOperationTarget.class);
        final int opCode = ISourceViewer.QUICK_ASSIST;
        if (operation != null && operation.canDoOperation(opCode)) {
          mTextEditor.selectAndReveal(mPosition.getOffset(), mPosition.getLength());
          operation.doOperation(opCode);
        }
        return;
      }

      super.run();
    }
 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);
 }
    @Override
    public ICompletionProposal[] computeQuickAssistProposals(
        IQuickAssistInvocationContext invocationContext) {
      IAnnotationModel amodel = invocationContext.getSourceViewer().getAnnotationModel();
      IDocument doc = invocationContext.getSourceViewer().getDocument();

      int offset = invocationContext.getOffset();
      Iterator<?> it = amodel.getAnnotationIterator();
      TreeSet<ICompletionProposal> proposalSet =
          new TreeSet<>(
              new Comparator<Object>() {

                @Override
                public int compare(Object o1, Object o2) {
                  if (o1 instanceof ICompletionProposal && o2 instanceof ICompletionProposal) {
                    ICompletionProposal proposal1 = (ICompletionProposal) o1;
                    ICompletionProposal proposal2 = (ICompletionProposal) o2;
                    return proposal1
                        .getDisplayString()
                        .compareToIgnoreCase(proposal2.getDisplayString());
                  }
                  return 0;
                }
              });
      while (it.hasNext()) {
        Object key = it.next();
        if (!(key instanceof SimpleMarkerAnnotation)) {
          if (key instanceof SpellingAnnotation) {
            SpellingAnnotation annotation = (SpellingAnnotation) key;
            if (amodel.getPosition(annotation).overlapsWith(offset, 1)) {
              ICompletionProposal[] proposals = annotation.getSpellingProblem().getProposals();
              for (ICompletionProposal proposal : proposals) {
                proposalSet.add(proposal);
              }
            }
          }
          continue;
        }

        SimpleMarkerAnnotation annotation = (SimpleMarkerAnnotation) key;
        populateDataModelForAnnotation(annotation);
        IMarker marker = annotation.getMarker();

        IMarkerResolution[] mapping = fResMap.get(marker);
        if (mapping != null) {
          Position pos = amodel.getPosition(annotation);
          try {
            int line = doc.getLineOfOffset(pos.getOffset());
            int start = pos.getOffset();
            String delim = doc.getLineDelimiter(line);
            int delimLength = delim != null ? delim.length() : 0;
            int end = doc.getLineLength(line) + start - delimLength;
            if (offset >= start && offset <= end) {
              for (IMarkerResolution markerResolution : mapping) {
                PDECompletionProposal proposal =
                    new PDECompletionProposal(markerResolution, pos, marker);
                if (!proposalSet.contains(proposal)) {
                  proposalSet.add(proposal);
                }
              }
            }
          } catch (BadLocationException e) {
          }
        }
      }

      return proposalSet.toArray(new ICompletionProposal[proposalSet.size()]);
    }