示例#1
0
  void shiftLeft() {
    javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument();
    try {
      int currentLine = offsetToLine(doc, getSelectionStart());
      int endLine = offsetToLine(doc, getSelectionEnd());

      // The two following cases are to take care of selections that include
      // only the very edge of a line of text, either at the top or bottom
      // of the selection.  Because these lines do not have *any* highlighted
      // text, it does not make sense to modify these lines. ~Forrest (9/22/2006)
      if (endLine > currentLine && getSelectionEnd() == lineToStartOffset(doc, endLine)) {
        endLine--;
      }
      if (endLine > currentLine && getSelectionStart() == (lineToEndOffset(doc, currentLine) - 1)) {
        currentLine++;
      }

      while (currentLine <= endLine) {
        int lineStart = lineToStartOffset(doc, currentLine);
        int lineEnd = lineToEndOffset(doc, currentLine);
        String text = doc.getText(lineStart, lineEnd - lineStart);
        if (text.length() > 0 && text.charAt(0) == ' ') {
          doc.remove(lineStart, 1);
        }
        currentLine++;
      }
    } catch (javax.swing.text.BadLocationException ex) {
      throw new IllegalStateException(ex);
    }
  }
示例#2
0
 String getLineText(int offset) throws javax.swing.text.BadLocationException {
   javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument();
   int currentLine = offsetToLine(doc, offset);
   int lineStart = lineToStartOffset(doc, currentLine);
   int lineEnd = lineToEndOffset(doc, currentLine);
   return doc.getText(lineStart, lineEnd - lineStart);
 }
 public final String getNameText() {
   try {
     return myNameDocument.getText(0, myNameDocument.getLength());
   } catch (BadLocationException e) {
     LOG.error(e);
     return "";
   }
 }
  public void testGetText() throws Exception {
    PlainDocument doc = new PlainDocument();
    CharSequence text = DocumentUtilities.getText(doc);
    assertEquals(1, text.length());
    assertEquals('\n', text.charAt(0));

    text = DocumentUtilities.getText(doc);
    doc.insertString(0, "a\nb", null);
    for (int i = 0; i < doc.getLength() + 1; i++) {
      assertEquals(doc.getText(i, 1).charAt(0), text.charAt(i));
    }
  }
示例#5
0
 String getHelpTarget(int startPosition) {
   // determine the current "word" that the cursor is on
   javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument();
   try {
     int currentLine = offsetToLine(doc, startPosition);
     int startLineOffset = lineToStartOffset(doc, currentLine);
     int lineLength = lineToEndOffset(doc, currentLine) - startLineOffset;
     String lineText = doc.getText(startLineOffset, lineLength);
     int selStartInString = startPosition - startLineOffset;
     return colorizer.getTokenAtPosition(lineText, selStartInString);
   } catch (javax.swing.text.BadLocationException ex) {
     throw new IllegalStateException(ex);
   }
 }
示例#6
0
  void uncomment() {
    javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument();
    try {
      int currentLine = offsetToLine(doc, getSelectionStart());
      int endLine = offsetToLine(doc, getSelectionEnd());

      // The two following cases are to take care of selections that include
      // only the very edge of a line of text, either at the top or bottom
      // of the selection.  Because these lines do not have *any* highlighted
      // text, it does not make sense to modify these lines. ~Forrest (9/22/2006)
      if (endLine > currentLine && getSelectionEnd() == lineToStartOffset(doc, endLine)) {
        endLine--;
      }
      if (endLine > currentLine && getSelectionStart() == (lineToEndOffset(doc, currentLine) - 1)) {
        currentLine++;
      }

      while (currentLine <= endLine) {
        int lineStart = lineToStartOffset(doc, currentLine);
        int lineEnd = lineToEndOffset(doc, currentLine);
        String text = doc.getText(lineStart, lineEnd - lineStart);
        int semicolonPos = text.indexOf(';');
        if (semicolonPos != -1) {
          boolean allSpaces = true;
          for (int i = 0; i < semicolonPos; i++) {
            if (!Character.isWhitespace(text.charAt(i))) {
              allSpaces = false;
              break;
            }
          }
          if (allSpaces) {
            doc.remove(lineStart + semicolonPos, 1);
          }
        }
        currentLine++;
      }
    } catch (javax.swing.text.BadLocationException ex) {
      throw new IllegalStateException(ex);
    }
  }
示例#7
0
 /**
  * Return the lines that span the selection (split as an array of Strings) if there is no
  * selection then current line is returned.
  *
  * <p>Note that the strings returned will not contain the terminating line feeds If the document
  * is empty, then an empty string array is returned. So you can always iterate over the returned
  * array without a null check
  *
  * <p>The text component will then have the full lines set as selection
  *
  * @return String[] of lines spanning selection / or line containing dot
  */
 public static String[] getSelectedLines(JTextComponent target) {
   String[] lines = null;
   try {
     PlainDocument pDoc = (PlainDocument) target.getDocument();
     int start = pDoc.getParagraphElement(target.getSelectionStart()).getStartOffset();
     int end;
     if (target.getSelectionStart() == target.getSelectionEnd()) {
       end = pDoc.getParagraphElement(target.getSelectionEnd()).getEndOffset();
     } else {
       // if more than one line is selected, we need to subtract one from the end
       // so that we do not select the line with the caret and no selection in it
       end = pDoc.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset();
     }
     target.select(start, end);
     lines = pDoc.getText(start, end - start).split("\n");
     target.select(start, end);
   } catch (BadLocationException ex) {
     Logger.getLogger(ActionUtils.class.getName()).log(Level.SEVERE, null, ex);
     lines = EMPTY_STRING_ARRAY;
   }
   return lines;
 }