/** * Wraps lines at and below the caret that exceed the number of characters per line. * * <p>Lines are wrapped at spaces between words when possible. Lines that don't exceed the number * of characters per line are not changed. * * <p>Currently this cannot be undone. */ public void rewrapFromCaret() { StringBuilder stringBuilder = new StringBuilder(charsPerLine * 3); for (int i = content.getLineAtOffset(currentText.getCaretOffset()); i < content.getLineCount(); i++) { String line = content.getLine(i); if (line.length() == 0) continue; // line too long if (line.length() > charsPerLine) { int wordWrap, wordEnd; // find beginning of word being wrapped if (line.charAt(charsPerLine) != ' ') { for (wordWrap = charsPerLine; wordWrap > charsPerLine / 2; wordWrap--) if (line.charAt(wordWrap) == ' ') break; if (wordWrap == charsPerLine / 2) continue; wordWrap++; } else { for (wordWrap = charsPerLine; wordWrap < line.length(); wordWrap++) if (line.charAt(wordWrap) != ' ') break; if (wordWrap == line.length()) continue; } // find end of word before word being wrapped for (wordEnd = wordWrap - 1; wordEnd > charsPerLine / 4; wordEnd--) if (line.charAt(wordEnd) != ' ') break; if (wordEnd == charsPerLine / 4) continue; wordEnd++; // build replacement text int length = line.length(); stringBuilder.setLength(0); stringBuilder .append(line.substring(0, wordEnd)) .append(eol) .append(line.substring(wordWrap, length)); if (length > 0 && line.charAt(length - 1) != PARAGRAPH_END) if (i < content.getLineCount() - 1) { String next = content.getLine(i + 1); stringBuilder.append(' ').append(next); length += eol.length() + next.length(); } content.replaceTextRange(content.getOffsetAtLine(i), length, stringBuilder.toString()); } else if (line.length() > 0 && line.charAt(line.length() - 1) == PARAGRAPH_END) break; } clearChanges(); }
/** * Convert mouse screen coordinates to a <code>StyledText</code> offset. * * @param x screen X-coordinate * @param y screen Y-coordinate * @param absolute if <code>true</code>, coordinates are expected to be absolute screen * coordinates * @return text offset * @see StyledText#getOffsetAtLocation() */ private int getOffsetAtLocation(int x, int y, boolean absolute) { StyledText textWidget = fViewer.getTextWidget(); StyledTextContent content = textWidget.getContent(); Point location; if (absolute) { location = textWidget.toControl(x, y); } else { location = new Point(x, y); } int line = (textWidget.getTopPixel() + location.y) / textWidget.getLineHeight(); if (line >= content.getLineCount()) { return content.getCharCount(); } int lineOffset = content.getOffsetAtLine(line); String lineText = content.getLine(line); Point endOfLine = textWidget.getLocationAtOffset(lineOffset + lineText.length()); if (location.x >= endOfLine.x) { return lineOffset + lineText.length(); } try { return textWidget.getOffsetAtLocation(location); } catch (IllegalArgumentException iae) { // we are expecting this return -1; } }
/** * Writes data in BRF format to <code>Writer</code>. * * @param writer the writer stream to write the data. * @exception IOException * @see #readBRF(Reader) */ public void writeBRF(Writer writer) throws IOException { // write first line String line = content.getLine(0); if (line.length() > 0 && line.charAt(line.length() - 1) == PARAGRAPH_END) writer.write(line.substring(0, line.length() - 1)); else writer.write(line); // write remaining lines for (int i = 1; i < content.getLineCount(); i++) { writer.write(eol); if (isFirstLineOnPage(i)) writer.write(0xc); line = content.getLine(i); if (line.length() > 0 && line.charAt(line.length() - 1) == PARAGRAPH_END) writer.write(line.substring(0, line.length() - 1)); else writer.write(line); } writer.flush(); resetChanges(); }
/** * Writes data in BrailleZephyr file format to <code>Writer</code>. * * @param writer the writer stream to write the data. * @exception IOException * @see #readBZY(Reader) */ public void writeBZY(Writer writer) throws IOException { // write configuration lines writer.write("Version " + versionMajor + ' ' + versionMinor + ' ' + versionPatch + eol); writer.write("CharsPerLine " + charsPerLine + eol); writer.write("LinesPerPage " + linesPerPage + eol); writer.write("CaretOffset " + currentText.getCaretOffset() + eol); writer.write("ViewFocus "); if (currentText == brailleText) writer.write("braille" + eol); else writer.write("ascii" + eol); if (content.getCharCount() > 0) writer.write( "ReturnAtEnd " + (content.getLine(content.getLineCount() - 1).length() == 0) + eol); else writer.write("ReturnAtEnd false" + eol); writer.write("HeaderEnd" + eol); // write first line String line = content.getLine(0); if (line.length() > 0 && line.charAt(line.length() - 1) == PARAGRAPH_END) writer.write(line.substring(0, line.length() - 1) + (char) 0xb6); else writer.write(line); // write text for (int i = 1; i < content.getLineCount(); i++) { writer.write(eol); line = content.getLine(i); if (line.length() > 0 && line.charAt(line.length() - 1) == PARAGRAPH_END) writer.write(line.substring(0, line.length() - 1) + (char) 0xb6); else writer.write(line); } writer.flush(); resetChanges(); }
/* * @see org.eclipse.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(org.eclipse.swt.graphics.GC, org.eclipse.swt.custom.StyledText, int, int, org.eclipse.swt.graphics.Color) */ public void draw( Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { if (annotation instanceof ProjectionAnnotation) { ProjectionAnnotation projectionAnnotation = (ProjectionAnnotation) annotation; if (projectionAnnotation.isCollapsed()) { if (gc != null) { StyledTextContent content = textWidget.getContent(); int line = content.getLineAtOffset(offset); int lineStart = content.getOffsetAtLine(line); String text = content.getLine(line); int lineLength = text == null ? 0 : text.length(); int lineEnd = lineStart + lineLength; Point p = textWidget.getLocationAtOffset(lineEnd); Color c = gc.getForeground(); gc.setForeground(color); FontMetrics metrics = gc.getFontMetrics(); // baseline: where the dots are drawn int baseline = textWidget.getBaseline(offset); // descent: number of pixels that the box extends over baseline int descent = Math.min(2, textWidget.getLineHeight(offset) - baseline); // ascent: so much does the box stand up from baseline int ascent = metrics.getAscent(); // leading: free space from line top to box upper line int leading = baseline - ascent; // height: height of the box int height = ascent + descent; int width = metrics.getAverageCharWidth(); gc.drawRectangle(p.x, p.y + leading, width, height); int third = width / 3; int dotsVertical = p.y + baseline - 1; gc.drawPoint(p.x + third, dotsVertical); gc.drawPoint(p.x + width - third, dotsVertical); gc.setForeground(c); } else { textWidget.redrawRange(offset, length, true); } } } }