@Override
 public void run(Result result, SchedulerEvent event) {
   try {
     ALGParserResult sjResult = (ALGParserResult) result;
     List<ParseException> syntaxErrors = sjResult.getJavaParser().syntaxErrors;
     Document document = result.getSnapshot().getSource().getDocument(false);
     List<ErrorDescription> errors = new ArrayList<ErrorDescription>();
     for (ParseException syntaxError : syntaxErrors) {
       Token token = syntaxError.currentToken;
       int start =
           NbDocument.findLineOffset((StyledDocument) document, token.beginLine - 1)
               + token.beginColumn
               - 1;
       int end =
           NbDocument.findLineOffset((StyledDocument) document, token.endLine - 1)
               + token.endColumn;
       ErrorDescription errorDescription =
           ErrorDescriptionFactory.createErrorDescription(
               Severity.ERROR,
               syntaxError.getMessage(),
               document,
               document.createPosition(start),
               document.createPosition(end));
       errors.add(errorDescription);
     }
     HintsController.setErrors(document, "simple-java", errors);
   } catch (BadLocationException ex1) {
     Exceptions.printStackTrace(ex1);
   } catch (org.netbeans.modules.parsing.spi.ParseException ex1) {
     Exceptions.printStackTrace(ex1);
   }
 }
 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) {
   }
 }
Esempio n. 3
0
 /**
  * Attach a {@link Document} to enable line number tracking when editing. The position to track is
  * before the first non-whitespace character on the line. Edits happening before that position
  * will cause the line number to update accordingly. Multiple {@link #startTracking} calls will
  * replace the tracked document. Whoever wants a tracked line should track it and add itself as
  * listener if necessary. ({@link LineHighlight}, {@link LineBreakpoint})
  *
  * @param doc the {@link Document} to use for line number tracking
  */
 public synchronized void startTracking(Document doc) {
   // System.out.println("tracking: " + this);
   if (doc == null) {
     return; // null arg
   }
   if (doc == this.doc) {
     return; // already tracking that doc
   }
   try {
     Element line = doc.getDefaultRootElement().getElement(lineIdx);
     if (line == null) {
       return; // line doesn't exist
     }
     String lineText =
         doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
     // set tracking position at (=before) first non-white space character on line
     pos = doc.createPosition(line.getStartOffset() + nonWhiteSpaceOffset(lineText));
     this.doc = doc;
     doc.addDocumentListener(this);
   } catch (BadLocationException ex) {
     Logger.getLogger(LineID.class.getName()).log(Level.SEVERE, null, ex);
     pos = null;
     this.doc = null;
   }
 }
    @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();
    }
 // 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);
 }